UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

163 lines (162 loc) 8.41 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["text", "group", "debug", "debug_counter", "reset", "skip_correction", "increase", "decrease", "up", "down", "inherit", "level", "size", "skeleton", "element", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/web.dom-collections.iterator.js"; import React from 'react'; import classnames from 'classnames'; import { isTrue, validateDOMAttributes } from '../../shared/component-helper'; import '../../shared/helpers'; import { createSpacingClasses } from '../space/SpacingHelper'; import HeadingContext from './HeadingContext'; import HeadingProvider from './HeadingProvider'; import { createSkeletonClass } from '../skeleton/SkeletonHelper'; import { correctInternalHeadingLevel, resetLevels, resetAllLevels, setNextLevel, globalSyncCounter, globalHeadingCounter, windupHeadings, teardownHeadings, debugCounter, getHeadingSize, getHeadingElement } from './HeadingHelpers'; import { initCounter } from './HeadingCounter'; import { useTheme, Context } from '../../shared'; 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 } = props, rest = _objectWithoutProperties(props, _excluded); const [state, setState] = React.useState(() => { var _headingContext$headi, _state$headingContext, _state$headingContext2, _state$headingContext3, _state$headingContext4; 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 ? void 0 : (_state$headingContext2 = _state$headingContext.heading) === null || _state$headingContext2 === void 0 ? void 0 : _state$headingContext2.skip_correction), source: props.text || props.children, debug: props.debug || ((_state$headingContext3 = state.headingContext) === null || _state$headingContext3 === void 0 ? void 0 : (_state$headingContext4 = _state$headingContext3.heading) === null || _state$headingContext4 === void 0 ? void 0 : _state$headingContext4.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$headingContext5, _state$headingContext6, _state$headingContext7, _state$headingContext8; const { level: newLevel } = correctInternalHeadingLevel({ counter: state.counter, isRerender: true, level, bypassChecks: isTrue(props.skip_correction) || isTrue((_state$headingContext5 = state.headingContext) === null || _state$headingContext5 === void 0 ? void 0 : (_state$headingContext6 = _state$headingContext5.heading) === null || _state$headingContext6 === void 0 ? void 0 : _state$headingContext6.skip_correction), source: props.text || props.children, debug: props.debug || ((_state$headingContext7 = state.headingContext) === null || _state$headingContext7 === void 0 ? void 0 : (_state$headingContext8 = _state$headingContext7.heading) === null || _state$headingContext8 === void 0 ? void 0 : _state$headingContext8.debug) }); state.level = state.prevLevel = newLevel; setState(_objectSpread({}, state)); } }, [props.level]); const theme = useTheme(); let { size, element, skeleton } = props; const { level } = state; const debug = _debug || (headingContext === null || headingContext === void 0 ? 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 ? void 0 : (_headingContext$headi3 = headingContext.heading) === null || _headingContext$headi3 === void 0 ? void 0 : _headingContext$headi3.debug_counter); const attributes = _objectSpread({}, 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 ? 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