@atlaskit/primitives
Version:
Primitives are token-backed low-level building blocks.
120 lines (115 loc) • 5.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _react2 = require("@emotion/react");
var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
var _styleMaps = require("../xcss/style-maps.partial");
var _surfaceProvider = require("./internal/surface-provider");
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) { (0, _defineProperty2.default)(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 */
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 = (0, _react2.css)({
margin: 0
});
var variantStyles = _objectSpread(_objectSpread({}, _styleMaps.bodyTextStylesMap), _styleMaps.uiTextStylesMap);
var strongStyles = (0, _react2.css)({
fontWeight: "var(--ds-font-weight-bold, bold)"
});
var emStyles = (0, _react2.css)({
fontStyle: 'italic'
});
var textAlignMap = {
center: (0, _react2.css)({
textAlign: 'center'
}),
end: (0, _react2.css)({
textAlign: 'end'
}),
start: (0, _react2.css)({
textAlign: 'start'
})
};
var truncateStyles = (0, _react2.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 = (0, _surfaceProvider.useSurface)();
var inverseTextColor = _styleMaps.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__*/(0, _react.createContext)(false);
var useHasTextAncestor = function useHasTextAncestor() {
return (0, _react.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 = (0, _objectWithoutProperties2.default)(_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';
}
}
(0, _tinyInvariant.default)(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 (0, _react2.jsx)(_react.Fragment, null, children);
}
var component = (0, _react2.jsx)(Component, {
css: [resetStyles, variant && variantStyles[variant], color && _styleMaps.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 : (0, _react2.jsx)(HasTextAncestorContext.Provider, {
value: true
}, component);
};
var _default = exports.default = Text;