@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
95 lines (94 loc) • 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = HeadingProvider;
var _react = _interopRequireDefault(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 = _react.default.useContext(_HeadingContext.default);
const [state, setState] = _react.default.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;
});
_react.default.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]);
_react.default.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