@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
100 lines (99 loc) • 3.21 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.TypographyContext = exports.Provider = void 0;
exports.getHeadingLineHeightSize = getHeadingLineHeightSize;
exports.useTypography = void 0;
var _react = require("react");
var _clsx = require("clsx");
var _Element = _interopRequireDefault(require("../Element.js"));
var _withComponentMarkers = _interopRequireDefault(require("../../shared/helpers/withComponentMarkers.js"));
var _Context = _interopRequireDefault(require("../../shared/Context.js"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const TypographyContext = exports.TypographyContext = (0, _react.createContext)({
proseMaxWidth: undefined,
responsive: undefined
});
const Typography = props => {
const {
element = 'p',
className,
size,
lineHeight,
align,
family,
weight,
decoration,
slant,
...rest
} = useTypography(props);
const context = (0, _react.useContext)(_Context.default);
return (0, _jsxRuntime.jsx)(_Element.default, {
as: element,
...rest,
className: (0, _clsx.clsx)(className, size && `dnb-t__size--${size}`, align && `dnb-t__align--${align}`, family && `dnb-t__family--${family}`, weight && `dnb-t__weight--${weight}`, decoration && `dnb-t__decoration--${decoration}`, slant && `dnb-t__slant--${slant}`, context?.theme?.surface === 'dark' && 'dnb-t--surface-dark', (lineHeight || size) && `dnb-t__line-height--${lineHeight || size}`)
});
};
const Provider = ({
children,
...rest
}) => {
const parentContext = (0, _react.useContext)(TypographyContext);
const newContext = {
...parentContext,
...rest
};
return (0, _jsxRuntime.jsx)(TypographyContext, {
value: newContext,
children: children
});
};
exports.Provider = Provider;
(0, _withComponentMarkers.default)(Typography, {
_supportsSpacingProps: true
});
Typography.Provider = Provider;
Typography.Context = Provider;
var _default = exports.default = Typography;
const HEADING_LINE_HEIGHT_MAP = {
'x-small': 'x-small',
small: 'small',
basis: 'basis',
medium: 'medium',
large: 'large',
'x-large': 'x-large',
'xx-large': 'xx-large'
};
function getHeadingLineHeightSize(fontSize) {
return HEADING_LINE_HEIGHT_MAP[fontSize] || 'basis';
}
const useTypography = ({
proseMaxWidth: proseMaxWidthProp,
...rest
}) => {
const {
proseMaxWidth: proseMaxWidthContext,
responsive
} = (0, _react.useContext)(TypographyContext);
const proseMaxWidth = proseMaxWidthProp !== null && proseMaxWidthProp !== void 0 ? proseMaxWidthProp : proseMaxWidthContext;
const style = proseMaxWidth ? {
maxWidth: `${proseMaxWidth === true ? 60 : proseMaxWidth}ch`
} : undefined;
return {
...rest,
...(style !== undefined && {
style: {
...style,
...rest.style
}
}),
...(responsive !== undefined && {
className: (0, _clsx.clsx)(rest.className, responsive && 'dnb-t__responsive-on', responsive === false && 'dnb-t__responsive-off')
})
};
};
exports.useTypography = useTypography;
//# sourceMappingURL=Typography.js.map