UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

157 lines (156 loc) 5.44 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import React from 'react'; import classnames from 'classnames'; import { isTrue, validateDOMAttributes } from "../../shared/component-helper.js"; import "../../shared/helpers.js"; import { createSpacingClasses } from "../space/SpacingHelper.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, getHeadingSize, getHeadingElement } from "./HeadingHelpers.js"; import { initCounter } from "./HeadingCounter.js"; import { useTheme, Context } from "../../shared/index.js"; export default function Heading(props) { const context = React.useContext(Context); const headingContext = React.useContext(HeadingContext); const _ref = React.useRef(); const { text, group: _group, debug: _debug, debug_counter: _debug_counter, reset: _reset, skip_correction: _skip_correction, 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: isTrue(props.inherit), reset: props.reset, increase: isTrue(props.increase) || isTrue(props.up), decrease: isTrue(props.decrease) || isTrue(props.down), bypassChecks: isTrue(props.skip_correction) || isTrue(state.headingContext?.heading?.skip_correction), 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: isTrue(props.skip_correction) || isTrue(state.headingContext?.heading?.skip_correction), 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 debug_counter = _debug_counter || headingContext?.heading?.debug_counter; 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; return React.createElement(Element, _extends({}, attributes, { ref: _ref, className: classnames(`dnb-heading dnb-h--${size}`, createSkeletonClass('font', skeleton, headingContext), className, createSpacingClasses(props)) }), debug && React.createElement("span", { className: "dnb-heading__debug" }, `[h${level || '6'}] `, debug_counter && React.createElement(React.Fragment, null, ' ', React.createElement("span", { className: "dnb-code" }, debugCounter(state.counter)))), text || children); } Heading.Level = HeadingProvider; Heading.Increase = props => React.createElement(HeadingProvider, _extends({ increase: true }, props)); Heading.Decrease = props => React.createElement(HeadingProvider, _extends({ decrease: true }, props)); Heading.Up = props => React.createElement(HeadingProvider, _extends({ increase: true }, props)); Heading.Down = props => React.createElement(HeadingProvider, _extends({ decrease: true }, props)); Heading.Reset = props => { globalHeadingCounter.current?.reset(); return React.createElement(HeadingProvider, props); }; Heading.resetLevels = resetLevels; Heading.setNextLevel = setNextLevel; Heading._isHeadingElement = true; Heading._supportsSpacingProps = true; export { resetAllLevels, resetLevels, setNextLevel }; //# sourceMappingURL=Heading.js.map