UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

141 lines (140 loc) 3.98 kB
"use client"; import _Object$hasOwn from "core-js-pure/stable/object/has-own.js"; import React from 'react'; import clsx from 'clsx'; import { warn, validateDOMAttributes, processChildren, getClosestParent } from "../../shared/component-helper.js"; import { useMediaQuery } from "../../shared/index.js"; import AccordionContext from "./AccordionContext.js"; import { applySpacing } from "../space/SpacingUtils.js"; import HeightAnimation from "../height-animation/HeightAnimation.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx } from "react/jsx-runtime"; export default function AccordionContent(props) { const context = React.useContext(AccordionContext); const { id, expanded, keepInDOM, preventRerender, singleContainer, disabled, noAnimation, contentRef } = context; const { className, children, instance, ...rest } = props; let elementRef = React.useRef(null); const cacheRef = React.useRef(null); if (contentRef) { elementRef = contentRef; } const setContainerHeight = () => { const { singleContainer } = context; if (singleContainer) { const contentElem = elementRef.current; if (contentElem) { try { contentElem.style.height = ''; const containerElement = 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) { warn(e); } } } }; const renderContent = () => { const children = processChildren(props); const { expanded, keepInDOM, preventRerender, preventRerenderConditional } = context; let content = children; if (typeof content === 'string') { content = _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; }; React.useEffect(() => { if (expanded && singleContainer) { setContainerHeight(); } }, [children, expanded, singleContainer, setContainerHeight]); React.useState(() => { if (instance && _Object$hasOwn(instance, 'current')) { const mutableInstance = instance; mutableInstance.current = { setContainerHeight }; } }); const isSmallScreen = useMediaQuery({ when: { max: 'small' } }); const content = renderContent(); const wrapperParams = { className: clsx('dnb-accordion__content', className), ...rest }; const keepInDOMContent = keepInDOM || preventRerender; const innerParams = applySpacing(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; } validateDOMAttributes(props, wrapperParams); validateDOMAttributes(null, innerParams); const animate = !noAnimation && (singleContainer ? isSmallScreen : true); return _jsx(HeightAnimation, { ...wrapperParams, open: expanded, animate: animate, keepInDOM: keepInDOMContent, ref: elementRef, children: _jsx("section", { ...innerParams, children: content }) }); } withComponentMarkers(AccordionContent, { _supportsSpacingProps: true }); //# sourceMappingURL=AccordionContent.js.map