UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

131 lines (130 loc) 3.89 kB
"use client"; import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _Object$hasOwn from "core-js-pure/stable/object/has-own.js"; import React from 'react'; import classnames from 'classnames'; import { warn, validateDOMAttributes, processChildren, getClosestParent } from "../../shared/component-helper.js"; import { useMediaQuery } from "../../shared/index.js"; import AccordionContext from "./AccordionContext.js"; import { createSpacingClasses } from "../space/SpacingHelper.js"; import HeightAnimation from "../height-animation/HeightAnimation.js"; export default function AccordionContent(props) { const context = React.useContext(AccordionContext); const { id, expanded, prerender, prevent_rerender, single_container, disabled, no_animation, 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 { single_container } = context; if (single_container) { const contentElem = elementRef.current; if (contentElem) { try { contentElem.style.height = ''; const containerElement = getClosestParent('dnb-accordion-group--single-container', contentElem); if (no_animation) { 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, prerender, prevent_rerender, prevent_rerender_conditional } = context; let content = children; if (typeof content === 'string') { content = React.createElement("p", { className: "dnb-p" }, content); } if (prevent_rerender) { if (!(expanded || prerender)) { content = null; } if (prevent_rerender_conditional && cacheRef.current !== content) { cacheRef.current = content; } if (cacheRef.current) { content = cacheRef.current; } else { cacheRef.current = content; } } return content; }; React.useEffect(() => { if (expanded && single_container) { setContainerHeight(); } }, [children, expanded, single_container, setContainerHeight]); React.useState(() => { if (instance && _Object$hasOwn(instance, 'current')) { instance.current = { setContainerHeight }; } }); const isSmallScreen = useMediaQuery({ when: { max: 'small' } }); const content = renderContent(); const wrapperParams = { className: classnames('dnb-accordion__content', className), ...rest }; const keepInDOM = prerender || prevent_rerender; const innerParams = { id: `${id}-content`, 'aria-labelledby': `${id}-header`, className: classnames('dnb-accordion__content__inner', createSpacingClasses(rest)) }; if (expanded) { innerParams['aria-expanded'] = true; } if (!expanded || disabled) { innerParams.disabled = true; innerParams['aria-hidden'] = true; } validateDOMAttributes(props, wrapperParams); validateDOMAttributes(null, innerParams); const animate = !no_animation && (single_container ? isSmallScreen : true); return React.createElement(HeightAnimation, _extends({}, wrapperParams, { open: expanded, animate: animate, keepInDOM: keepInDOM, innerRef: elementRef }), React.createElement("section", innerParams, content)); } AccordionContent._supportsSpacingProps = true; //# sourceMappingURL=AccordionContent.js.map