UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

161 lines (160 loc) 7.1 kB
"use client"; import _extends from "@babel/runtime-corejs3/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) { var _headingContext$headi2, _headingContext$headi3; 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(() => { 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: 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 = state.headingContext) === null || _state$headingContext === void 0 || (_state$headingContext = _state$headingContext.heading) === null || _state$headingContext === void 0 ? void 0 : _state$headingContext.skip_correction), 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: isTrue(props.skip_correction) || isTrue((_state$headingContext3 = state.headingContext) === null || _state$headingContext3 === void 0 || (_state$headingContext3 = _state$headingContext3.heading) === null || _state$headingContext3 === void 0 ? void 0 : _state$headingContext3.skip_correction), 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 debug_counter = _debug_counter || (headingContext === null || headingContext === void 0 || (_headingContext$headi3 = headingContext.heading) === null || _headingContext$headi3 === void 0 ? void 0 : _headingContext$headi3.debug_counter); 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; 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 => { var _globalHeadingCounter; (_globalHeadingCounter = globalHeadingCounter.current) === null || _globalHeadingCounter === void 0 || _globalHeadingCounter.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