UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

91 lines (90 loc) 2.6 kB
"use client"; import { createContext, useContext } from 'react'; import { clsx } from 'clsx'; import E from "../Element.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import Context from "../../shared/Context.js"; import { jsx as _jsx } from "react/jsx-runtime"; export const TypographyContext = createContext({ proseMaxWidth: undefined, responsive: undefined }); const Typography = props => { const { element = 'p', className, size, lineHeight, align, family, weight, decoration, slant, ...rest } = useTypography(props); const context = useContext(Context); return _jsx(E, { as: element, ...rest, className: 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 = useContext(TypographyContext); const newContext = { ...parentContext, ...rest }; return _jsx(TypographyContext, { value: newContext, children: children }); }; withComponentMarkers(Typography, { _supportsSpacingProps: true }); Typography.Provider = Provider; Typography.Context = Provider; export default Typography; export { Provider }; 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' }; export function getHeadingLineHeightSize(fontSize) { return HEADING_LINE_HEIGHT_MAP[fontSize] || 'basis'; } export const useTypography = ({ proseMaxWidth: proseMaxWidthProp, ...rest }) => { const { proseMaxWidth: proseMaxWidthContext, responsive } = 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: clsx(rest.className, responsive && 'dnb-t__responsive-on', responsive === false && 'dnb-t__responsive-off') }) }; }; //# sourceMappingURL=Typography.js.map