@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
206 lines (205 loc) • 6.66 kB
JavaScript
"use strict";
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = AccordionContent;
var _hasOwn = _interopRequireDefault(require("core-js-pure/stable/object/has-own.js"));
var _react = require("react");
var _clsx = _interopRequireDefault(require("clsx"));
var _componentHelper = require("../../shared/component-helper.js");
var _index = require("../../shared/index.js");
var _AccordionContext = _interopRequireDefault(require("./AccordionContext.js"));
var _SpacingUtils = require("../space/SpacingUtils.js");
var _HeightAnimation = _interopRequireDefault(require("../height-animation/HeightAnimation.js"));
var _withComponentMarkers = _interopRequireDefault(require("../../shared/helpers/withComponentMarkers.js"));
var _useSharedState = require("../../shared/helpers/useSharedState.js");
var _AccordionTertiaryContent = _interopRequireDefault(require("./AccordionTertiaryContent.js"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function AccordionContent(props) {
const context = (0, _react.useContext)(_AccordionContext.default);
const isStandalone = !context.id && props.id;
if (isStandalone) {
return (0, _jsxRuntime.jsx)(AccordionContentStandalone, {
...props
});
}
return (0, _jsxRuntime.jsx)(AccordionContentInner, {
...props
});
}
function AccordionContentStandalone({
ref: _ref,
...props
}) {
var _sharedData$expanded, _sharedData$shouldFoc;
const {
id,
className,
children,
title,
keepInDOM: keepInDOMProp = false,
noAnimation = false,
...rest
} = props;
const contentRef = (0, _react.useRef)(null);
const {
data: sharedData,
set
} = (0, _useSharedState.useSharedState)(id);
const expanded = (_sharedData$expanded = sharedData === null || sharedData === void 0 ? void 0 : sharedData.expanded) !== null && _sharedData$expanded !== void 0 ? _sharedData$expanded : false;
const shouldFocusContent = (_sharedData$shouldFoc = sharedData === null || sharedData === void 0 ? void 0 : sharedData.shouldFocusContent) !== null && _sharedData$shouldFoc !== void 0 ? _sharedData$shouldFoc : false;
const contentId = `${id}-content`;
const content = (0, _componentHelper.processChildren)(props);
const wrapperParams = (0, _SpacingUtils.useSpacing)(props, {
className: (0, _clsx.default)('dnb-accordion__content', className),
...rest
});
(0, _componentHelper.validateDOMAttributes)(props, wrapperParams);
return (0, _jsxRuntime.jsx)(_AccordionTertiaryContent.default, {
...wrapperParams,
contentRef: contentRef,
contentId: contentId,
expanded: expanded,
noAnimation: noAnimation,
shouldFocusContent: shouldFocusContent,
onFocusHandled: () => set({
...sharedData,
shouldFocusContent: false
}),
keepInDOM: keepInDOMProp,
title: title,
"aria-label": title,
children: content
});
}
function AccordionContentInner(props) {
const context = (0, _react.useContext)(_AccordionContext.default);
const {
id,
expanded,
keepInDOM,
preventRerender,
singleContainer,
disabled,
noAnimation,
contentRef
} = context;
const {
className,
children,
instance,
...rest
} = props;
let elementRef = (0, _react.useRef)(null);
const cacheRef = (0, _react.useRef)(null);
if (contentRef) {
elementRef = contentRef;
}
const setContainerHeight = (0, _react.useCallback)(() => {
const {
singleContainer
} = context;
if (singleContainer) {
const contentElem = elementRef.current;
if (contentElem) {
try {
contentElem.style.height = '';
const containerElement = (0, _componentHelper.getClosestParent)('dnb-accordion-group--single-container', contentElem);
if (noAnimation) {
containerElement.style.transitionDuration = '1ms';
}
const minHeight = (contentElem.offsetHeight + contentElem.offsetTop) / 16;
containerElement.style.minHeight = `${minHeight}rem`;
} catch (e) {
(0, _componentHelper.warn)(e);
}
}
}
}, [context, noAnimation]);
const renderContent = () => {
const children = (0, _componentHelper.processChildren)(props);
const {
expanded,
keepInDOM,
preventRerender,
preventRerenderConditional
} = context;
let content = children;
if (typeof content === 'string') {
content = (0, _jsxRuntime.jsx)("p", {
className: "dnb-p",
children: content
});
}
if (preventRerender) {
if (!(expanded || keepInDOM)) {
content = null;
}
if (preventRerenderConditional && cacheRef.current !== content) {
cacheRef.current = content;
}
if (cacheRef.current) {
content = cacheRef.current;
} else {
cacheRef.current = content;
}
}
return content;
};
(0, _react.useEffect)(() => {
if (expanded && singleContainer) {
setContainerHeight();
}
}, [children, expanded, singleContainer, setContainerHeight]);
(0, _react.useState)(() => {
if (instance && (0, _hasOwn.default)(instance, 'current')) {
const mutableInstance = instance;
mutableInstance.current = {
setContainerHeight
};
}
});
const isSmallScreen = (0, _index.useMediaQuery)({
when: {
max: 'small'
}
});
const content = renderContent();
const wrapperParams = {
className: (0, _clsx.default)('dnb-accordion__content', className),
...rest
};
const keepInDOMContent = keepInDOM || preventRerender;
const innerParams = (0, _SpacingUtils.useSpacing)(rest, {
id: `${id}-content`,
'aria-labelledby': `${id}-header`,
className: 'dnb-accordion__content__inner'
});
if (expanded) {
innerParams['aria-expanded'] = true;
}
if (!expanded || disabled) {
innerParams.disabled = true;
innerParams['aria-hidden'] = true;
}
(0, _componentHelper.validateDOMAttributes)(props, wrapperParams);
(0, _componentHelper.validateDOMAttributes)(null, innerParams);
const animate = !noAnimation && (singleContainer ? isSmallScreen : true);
return (0, _jsxRuntime.jsx)(_HeightAnimation.default, {
...wrapperParams,
open: expanded,
animate: animate,
keepInDOM: keepInDOMContent,
ref: elementRef,
children: (0, _jsxRuntime.jsx)("section", {
...innerParams,
children: content
})
});
}
(0, _withComponentMarkers.default)(AccordionContent, {
_supportsSpacingProps: true
});
//# sourceMappingURL=AccordionContent.js.map