UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

96 lines (95 loc) 3.51 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = HeadingProvider; var _react = require("react"); var _HeadingContext = _interopRequireDefault(require("./HeadingContext.js")); var _HeadingHelpers = require("./HeadingHelpers.js"); var _HeadingCounter = require("./HeadingCounter.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function HeadingProvider(props) { const context = (0, _react.useContext)(_HeadingContext.default); const [state, setState] = (0, _react.useState)(() => { const state = { context, _listenForPropChanges: true }; const existingContext = context.heading; state.counter = (0, _HeadingCounter.initCounter)(props); state.newProps = props; if (existingContext) { state.newProps = { ...existingContext, ...props }; } if (existingContext) { state.counter.setContextCounter(existingContext.counter); } else { state.counter.setContextCounter(_HeadingHelpers.globalHeadingCounter.current); } state.counter = (0, _HeadingHelpers.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: state.newProps.skipCorrection, source: props.text || props.children, debug: state.newProps.debug }); _HeadingHelpers.globalSyncCounter.current = state.counter; state.level = state.counter.level; state.prevLevel = parseFloat(String(state.newProps.level)) || state.counter.level; return state; }); (0, _react.useEffect)(() => { const level = parseFloat(String(props.level)); if (state.prevLevel !== props.level && level > 0 && level !== state.level) { var _state$context$headin, _state$context$headin2; const { level: newLevel } = (0, _HeadingHelpers.correctInternalHeadingLevel)({ counter: state.counter, level, bypassChecks: props.skipCorrection || ((_state$context$headin = state.context.heading) === null || _state$context$headin === void 0 ? void 0 : _state$context$headin.skipCorrection), source: props.text || props.children, debug: props.debug || ((_state$context$headin2 = state.context.heading) === null || _state$context$headin2 === void 0 ? void 0 : _state$context$headin2.debug) }); state.level = state.prevLevel = newLevel; setState({ ...state }); } }, [props.level]); (0, _react.useEffect)(() => { (0, _HeadingHelpers.windupHeadings)(); return () => { (0, _HeadingHelpers.teardownHeadings)(); }; }, []); return (0, _jsxRuntime.jsx)(_HeadingContext.default, { value: { heading: { ...state.newProps, ...state } }, children: state.newProps.debugCounter && (0, _jsxRuntime.jsxs)("span", { className: "dnb-heading__context", children: [(0, _jsxRuntime.jsxs)("span", { className: "dnb-heading__debug", children: ["Context:", ' ', (0, _jsxRuntime.jsx)("span", { className: "dnb-code", children: (0, _HeadingHelpers.debugCounter)(state.counter) })] }), props.children] }) || props.children }); } //# sourceMappingURL=HeadingProvider.js.map