UNPKG

@base-ui/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

81 lines (79 loc) 2.62 kB
'use client'; import * as React from 'react'; import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect'; import { useRenderElement } from "../../utils/useRenderElement.js"; import { useDrawerProviderContext } from "../provider/DrawerProviderContext.js"; import { DrawerBackdropCssVars } from "../backdrop/DrawerBackdropCssVars.js"; import { DrawerPopupCssVars } from "../popup/DrawerPopupCssVars.js"; const stateAttributesMapping = { active(value) { if (value) { return { 'data-active': '' }; } return { 'data-inactive': '' }; } }; /** * A wrapper element intended to contain your app's main UI. * Applies `data-active` when any drawer within the nearest <Drawer.Provider> is open. * * Documentation: [Base UI Drawer](https://base-ui.com/react/components/drawer) */ export const DrawerIndent = /*#__PURE__*/React.forwardRef(function DrawerIndent(componentProps, forwardedRef) { const { render, className, ...elementProps } = componentProps; const providerContext = useDrawerProviderContext(true); const active = providerContext?.active ?? false; const visualStateStore = providerContext?.visualStateStore; const indentRef = React.useRef(null); useIsoLayoutEffect(() => { const element = indentRef.current; if (!element || !visualStateStore) { return undefined; } const syncVisualState = () => { const { swipeProgress, frontmostHeight } = visualStateStore.getSnapshot(); if (swipeProgress <= 0) { element.style.setProperty(DrawerBackdropCssVars.swipeProgress, '0'); } else { element.style.setProperty(DrawerBackdropCssVars.swipeProgress, `${swipeProgress}`); } if (frontmostHeight <= 0) { element.style.removeProperty(DrawerPopupCssVars.height); } else { element.style.setProperty(DrawerPopupCssVars.height, `${frontmostHeight}px`); } }; syncVisualState(); const unsubscribe = visualStateStore.subscribe(syncVisualState); return () => { unsubscribe(); element.style.setProperty(DrawerBackdropCssVars.swipeProgress, '0'); element.style.removeProperty(DrawerPopupCssVars.height); }; }, [visualStateStore]); const state = { active }; return useRenderElement('div', componentProps, { ref: [forwardedRef, indentRef], state, props: [{ style: { [DrawerBackdropCssVars.swipeProgress]: '0' } }, elementProps], stateAttributesMapping }); }); if (process.env.NODE_ENV !== "production") DrawerIndent.displayName = "DrawerIndent";