UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

179 lines (178 loc) 7.12 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) { var _headingContext$headi2, _headingContext$headi3; 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(() => { var _headingContext$headi, _state$headingContext, _state$headingContext2; const state = { level: null, counter: null, context, headingContext }; state.counter = initCounter(props); const counter = (_headingContext$headi = headingContext.heading) !== null && _headingContext$headi !== void 0 && _headingContext$headi.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 = state.headingContext) === null || _state$headingContext === void 0 || (_state$headingContext = _state$headingContext.heading) === null || _state$headingContext === void 0 ? void 0 : _state$headingContext.skipCorrection), source: props.text || props.children, debug: props.debug || ((_state$headingContext2 = state.headingContext) === null || _state$headingContext2 === void 0 || (_state$headingContext2 = _state$headingContext2.heading) === null || _state$headingContext2 === void 0 ? void 0 : _state$headingContext2.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) { var _state$headingContext3, _state$headingContext4; const { level: newLevel } = correctInternalHeadingLevel({ counter: state.counter, isRerender: true, level, bypassChecks: props.skipCorrection || ((_state$headingContext3 = state.headingContext) === null || _state$headingContext3 === void 0 || (_state$headingContext3 = _state$headingContext3.heading) === null || _state$headingContext3 === void 0 ? void 0 : _state$headingContext3.skipCorrection), source: props.text || props.children, debug: props.debug || ((_state$headingContext4 = state.headingContext) === null || _state$headingContext4 === void 0 || (_state$headingContext4 = _state$headingContext4.heading) === null || _state$headingContext4 === void 0 ? void 0 : _state$headingContext4.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 === null || headingContext === void 0 || (_headingContext$headi2 = headingContext.heading) === null || _headingContext$headi2 === void 0 ? void 0 : _headingContext$headi2.debug); const debugCounter = _debugCounter || (headingContext === null || headingContext === void 0 || (_headingContext$headi3 = headingContext.heading) === null || _headingContext$headi3 === void 0 ? void 0 : _headingContext$headi3.debugCounter); const attributes = { ...rest }; if (element == null) { element = getHeadingElement(level); if (size == null) { size = getHeadingSize(theme === null || theme === void 0 ? void 0 : 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 === null || context === void 0 ? void 0 : 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 => { var _globalHeadingCounter; (_globalHeadingCounter = globalHeadingCounter.current) === null || _globalHeadingCounter === void 0 || _globalHeadingCounter.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