@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
129 lines (128 loc) • 3.26 kB
JavaScript
"use client";
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";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
function Visibility(props) {
const {
id,
visible,
pathDefined,
pathUndefined,
pathTruthy,
pathFalsy,
pathTrue,
pathFalse,
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,
visibleWhen,
visibleWhenNot,
inferData,
filterData
});
const open = check();
const content = _jsx(VisibilityContext, {
value: {
isVisible: open,
keepInDOM,
props
},
children: 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 _jsx(FieldProvider, {
...providerProps,
children: content
});
}
if (animate) {
return _jsx(HeightAnimation, {
id: id,
open: open,
onAnimationEnd: onAnimationEnd,
onOpen: onOpen,
keepInDOM: Boolean(keepInDOM),
className: "dnb-forms-visibility",
compensateForGap: compensateForGap,
...rest,
children: _jsx(FieldProvider, {
...providerProps,
children: content
})
});
}
if (mountedRef.current) {
onVisible?.(open);
}
if (keepInDOM) {
return _jsx("span", {
id: id,
className: "dnb-forms-visibility",
hidden: !open,
children: _jsx(FieldProvider, {
...providerProps,
children: content
})
});
}
if (id) {
return _jsx(_Fragment, {
children: open ? _jsx("span", {
id: id,
className: "dnb-forms-visibility",
children: content
}) : null
});
}
return _jsx(_Fragment, {
children: open ? content : null
});
}
withComponentMarkers(Visibility, {
_supportsSpacingProps: 'children'
});
export default Visibility;
//# sourceMappingURL=Visibility.js.map