UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

103 lines (102 loc) 2.91 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import React, { useCallback, useContext } from 'react'; import { warn } from "../../../../shared/helpers.js"; import useMountEffect from "../../../../shared/helpers/useMountEffect.js"; import useMounted from "../../../../shared/helpers/useMounted.js"; import HeightAnimation from "../../../../components/HeightAnimation.js"; import FieldProvider from "../../Field/Provider/index.js"; import useVisibility from "./useVisibility.js"; import VisibilityContext from "./VisibilityContext.js"; import SummaryListContext from "../../Value/SummaryList/SummaryListContext.js"; function Visibility(props) { const { visible, pathDefined, pathUndefined, pathTruthy, pathFalsy, pathTrue, pathFalse, pathValue, whenValue, visibleWhen, visibleWhenNot, inferData, filterData, onVisible, onAnimationEnd, animate, keepInDOM, compensateForGap, fieldPropsWhenHidden, withinIterate, children, ...rest } = props; useMountEffect(() => { if (fieldPropsWhenHidden && !keepInDOM) { warn('Using "fieldPropsWhenHidden" requires "keepInDOM" to be true.'); } }); const { check } = useVisibility({ visible, withinIterate, pathDefined, pathUndefined, pathTruthy, pathFalsy, pathTrue, pathFalse, pathValue, whenValue, visibleWhen, visibleWhenNot, inferData, filterData }); const open = check(); const content = React.createElement(VisibilityContext.Provider, { value: { isVisible: open, keepInDOM, props } }, children); const mountedRef = useMounted(); const onOpen = useCallback(state => { if (mountedRef.current) { onVisible?.(state); } }, [mountedRef, onVisible]); const summaryListContext = useContext(SummaryListContext); const providerProps = !open ? fieldPropsWhenHidden : null; if ((animate || keepInDOM) && summaryListContext && !summaryListContext.isNested) { return React.createElement(FieldProvider, providerProps, content); } if (animate) { return React.createElement(HeightAnimation, _extends({ open: open, onAnimationEnd: onAnimationEnd, onOpen: onOpen, keepInDOM: Boolean(keepInDOM), className: "dnb-forms-visibility", compensateForGap: compensateForGap }, rest), React.createElement(FieldProvider, providerProps, content)); } if (mountedRef.current) { onVisible?.(open); } if (keepInDOM) { return React.createElement("span", { className: "dnb-forms-visibility", hidden: !open }, React.createElement(FieldProvider, providerProps, content)); } return React.createElement(React.Fragment, null, open ? content : null); } Visibility._supportsSpacingProps = 'children'; export default Visibility; //# sourceMappingURL=Visibility.js.map