@atlaskit/primitives
Version:
Primitives are token-backed low-level building blocks.
114 lines (110 loc) • 4.72 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/** @jsx jsx */
import { createContext, Fragment, useContext } from 'react';
import { css, jsx } from '@emotion/react';
import invariant from 'tiny-invariant';
import { bodyTextStylesMap, inverseColorMap, textColorStylesMap, uiTextStylesMap } from '../xcss/style-maps.partial';
import { useSurface } from './internal/surface-provider';
var asAllowlist = ['span', 'p', 'strong', 'em'];
// We're doing this because our CSS reset can add top margins to elements such as `p` which is totally insane.
// Long term we should remove those instances from the reset - it should be a reset to 0.
// For now, at least we know <Text> will be unaffected by this.
var resetStyles = css({
margin: 0
});
var variantStyles = _objectSpread(_objectSpread({}, bodyTextStylesMap), uiTextStylesMap);
var strongStyles = css({
fontWeight: "var(--ds-font-weight-bold, bold)"
});
var emStyles = css({
fontStyle: 'italic'
});
var textAlignMap = {
center: css({
textAlign: 'center'
}),
end: css({
textAlign: 'end'
}),
start: css({
textAlign: 'start'
})
};
var truncateStyles = css({
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap'
});
/**
* Custom hook designed to abstract the parsing of the color props and make it clearer in the future how color is reconciled between themes and tokens.
*/
var useColor = function useColor(colorProp) {
var surface = useSurface();
var inverseTextColor = inverseColorMap[surface];
/**
* Where the color of the surface is inverted we override the user choice
* as there is no valid choice that is not covered by the override.
*/
var color = inverseTextColor !== null && inverseTextColor !== void 0 ? inverseTextColor : colorProp;
return color;
};
var HasTextAncestorContext = /*#__PURE__*/createContext(false);
var useHasTextAncestor = function useHasTextAncestor() {
return useContext(HasTextAncestorContext);
};
/**
* __Text__
*
* Text is a primitive component that has the Atlassian Design System's design guidelines baked in.
* This includes considerations for text attributes such as color, font size, font weight, and line height.
* It renders a `span` by default.
*
* @internal
*/
var Text = function Text(_ref) {
var children = _ref.children,
props = _objectWithoutProperties(_ref, _excluded);
var asElement = props.as,
colorProp = props.color,
_props$shouldTruncate = props.shouldTruncate,
shouldTruncate = _props$shouldTruncate === void 0 ? false : _props$shouldTruncate,
textAlign = props.textAlign,
testId = props.testId,
id = props.id,
_props$variant = props.variant,
variant = _props$variant === void 0 ? 'body' : _props$variant;
var Component = asElement;
if (!Component) {
if (variant.includes('body')) {
Component = 'p';
} else {
// ui text and default => span
Component = 'span';
}
}
invariant(asAllowlist.includes(Component), "@atlaskit/primitives: Text received an invalid \"as\" value of \"".concat(Component, "\""));
var color = useColor(colorProp);
var isWrapped = useHasTextAncestor();
/**
* If the text is already wrapped and applies no props we can just
* render the children directly as a fragment.
*/
if (isWrapped && Object.keys(props).length === 0) {
return jsx(Fragment, null, children);
}
var component = jsx(Component, {
css: [resetStyles, variant && variantStyles[variant], color && textColorStylesMap[color], shouldTruncate && truncateStyles, textAlign && textAlignMap[textAlign], asElement === 'em' && emStyles, asElement === 'strong' && strongStyles],
"data-testid": testId,
id: id
}, children);
return isWrapped ?
// no need to re-apply context if the text is already wrapped
component : jsx(HasTextAncestorContext.Provider, {
value: true
}, component);
};
export default Text;