UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

175 lines (174 loc) 5.47 kB
"use client"; import React from 'react'; import clsx from 'clsx'; import { validateDOMAttributes } from "../../shared/component-helper.js"; import "../../shared/helpers.js"; import { applySpacing } from "../space/SpacingUtils.js"; import HeadingContext from "./HeadingContext.js"; import HeadingProvider from "./HeadingProvider.js"; import { createSkeletonClass } from "../skeleton/SkeletonHelper.js"; import { correctInternalHeadingLevel, resetLevels, resetAllLevels, setNextLevel, globalSyncCounter, globalHeadingCounter, windupHeadings, teardownHeadings, debugCounter as debugCounterFn, getHeadingSize, getHeadingElement } from "./HeadingHelpers.js"; import { initCounter } from "./HeadingCounter.js"; import { useTheme, Context } from "../../shared/index.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; export default function Heading(props) { const context = React.useContext(Context); const headingContext = React.useContext(HeadingContext); const _ref = React.useRef(undefined); const { text, group: _group, debug: _debug, debugCounter: _debugCounter, reset: _reset, skipCorrection: _skipCorrection, increase: _increase, decrease: _decrease, up: _up, down: _down, inherit: _inherit, level: _level, size: _size, skeleton: _skeleton, element: _element, className, children, ...rest } = props; const [state, setState] = React.useState(() => { const state = { level: null, counter: null, context, headingContext }; state.counter = initCounter(props); const counter = headingContext.heading?.counter ? headingContext.heading.counter : globalHeadingCounter.current; state.counter.setContextCounter(counter); state.counter.isHeading = true; state.counter = correctInternalHeadingLevel({ counter: state.counter, ref: props, level: parseFloat(String(props.level)), inherit: props.inherit, reset: props.reset, increase: props.increase || props.up, decrease: props.decrease || props.down, bypassChecks: props.skipCorrection || state.headingContext?.heading?.skipCorrection, source: props.text || props.children, debug: props.debug || state.headingContext?.heading?.debug }); globalSyncCounter.current = state.counter; state.level = state.counter.level; state.prevLevel = parseFloat(String(props.level)); return state; }); React.useEffect(() => { windupHeadings(); state.counter.windup(); return () => { teardownHeadings(); state.counter.teardown(); }; }, []); React.useEffect(() => { const level = parseFloat(String(props.level)); if (state.prevLevel !== props.level && level > 0 && level !== state.level) { const { level: newLevel } = correctInternalHeadingLevel({ counter: state.counter, isRerender: true, level, bypassChecks: props.skipCorrection || state.headingContext?.heading?.skipCorrection, source: props.text || props.children, debug: props.debug || state.headingContext?.heading?.debug }); state.level = state.prevLevel = newLevel; setState({ ...state }); } }, [props.level]); const theme = useTheme(); let { size, element, skeleton } = props; const { level } = state; const debug = _debug || headingContext?.heading?.debug; const debugCounter = _debugCounter || headingContext?.heading?.debugCounter; const attributes = { ...rest }; if (element == null) { element = getHeadingElement(level); if (size == null) { size = getHeadingSize(theme?.name)[level]; } } else { if (!attributes.role) { attributes.role = 'heading'; } if (!attributes['aria-level']) { attributes['aria-level'] = String(level); } } validateDOMAttributes(props, attributes); if (typeof context?.skeleton !== 'undefined') { skeleton = context.skeleton; } const Element = element; const elementProps = applySpacing(props, { ...attributes, ref: _ref, className: clsx(`dnb-heading dnb-h--${size}`, createSkeletonClass('font', skeleton, headingContext), className) }); return _jsxs(Element, { ...elementProps, children: [debug && _jsxs("span", { className: "dnb-heading__debug", children: [`[h${level || '6'}] `, debugCounter && _jsxs(_Fragment, { children: [' ', _jsx("span", { className: "dnb-code", children: debugCounterFn(state.counter) })] })] }), text || children] }); } Heading.Level = HeadingProvider; Heading.Increase = props => _jsx(HeadingProvider, { increase: true, ...props }); Heading.Decrease = props => _jsx(HeadingProvider, { decrease: true, ...props }); Heading.Up = props => _jsx(HeadingProvider, { increase: true, ...props }); Heading.Down = props => _jsx(HeadingProvider, { decrease: true, ...props }); Heading.Reset = props => { globalHeadingCounter.current?.reset(); return _jsx(HeadingProvider, { ...props }); }; Heading.resetLevels = resetLevels; Heading.setNextLevel = setNextLevel; withComponentMarkers(Heading, { _isHeadingElement: true, _supportsSpacingProps: true }); export { resetAllLevels, resetLevels, setNextLevel }; //# sourceMappingURL=Heading.js.map