@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
105 lines (104 loc) • 3.49 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["visible", "pathDefined", "pathUndefined", "pathTruthy", "pathFalsy", "pathTrue", "pathFalse", "pathValue", "whenValue", "visibleWhen", "visibleWhenNot", "inferData", "filterData", "onVisible", "onAnimationEnd", "animate", "keepInDOM", "compensateForGap", "fieldPropsWhenHidden", "withinIterate", "children"];
import React, { useCallback, useContext } from 'react';
import { warn } from '../../../../shared/helpers';
import useMountEffect from '../../../../shared/helpers/useMountEffect';
import useMounted from '../../../../shared/helpers/useMounted';
import HeightAnimation from '../../../../components/HeightAnimation';
import FieldProvider from '../../Field/Provider';
import useVisibility from './useVisibility';
import VisibilityContext from './VisibilityContext';
import SummaryListContext from '../../Value/SummaryList/SummaryListContext';
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
} = props,
rest = _objectWithoutProperties(props, _excluded);
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 === null || onVisible === void 0 ? void 0 : 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 === null || onVisible === void 0 ? void 0 : 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