@loke/ui
Version:
To install dependencies:
267 lines (266 loc) • 11.8 kB
JavaScript
// src/components/popover/popover.tsx
import { composeEventHandlers } from "@loke/ui/compose-events";
import { useComposedRefs } from "@loke/ui/compose-refs";
import { createContextScope } from "@loke/ui/context";
import { DismissableLayer } from "@loke/ui/dismissable-layer";
import { useFocusGuards } from "@loke/ui/focus-guards";
import {
FocusScope,
FocusScopeBranchProvider,
useFocusScopeBranch,
useFocusScopeBranchRegistry
} from "@loke/ui/focus-scope";
import * as PopperPrimitive from "@loke/ui/popper";
import { createPopperScope } from "@loke/ui/popper";
import { Portal as PortalPrimitive } from "@loke/ui/portal";
import { Presence } from "@loke/ui/presence";
import { Primitive } from "@loke/ui/primitive";
import { createSlot } from "@loke/ui/slot";
import { useControllableState } from "@loke/ui/use-controllable-state";
import { useId } from "@loke/ui/use-id";
import { hideOthers } from "aria-hidden";
import {
forwardRef,
useCallback,
useEffect,
useMemo,
useRef,
useState
} from "react";
import { RemoveScroll } from "react-remove-scroll";
import { jsx } from "react/jsx-runtime";
var POPOVER_NAME = "Popover", [createPopoverContext, createPopoverScope] = createContextScope(POPOVER_NAME, [createPopperScope]), usePopperScope = createPopperScope(), [PopoverProvider, usePopoverContext] = createPopoverContext(POPOVER_NAME), Popover = /* @__PURE__ */ Object.assign((props) => {
let {
__scopePopover,
children,
open: openProp,
defaultOpen,
onOpenChange,
modal = !1
} = props, popperScope = usePopperScope(__scopePopover), triggerRef = useRef(null), [hasCustomAnchor, setHasCustomAnchor] = useState(!1), [open, setOpen] = useControllableState({
caller: POPOVER_NAME,
defaultProp: defaultOpen ?? !1,
onChange: onOpenChange,
prop: openProp
});
return /* @__PURE__ */ jsx(PopperPrimitive.Root, {
...popperScope,
children: /* @__PURE__ */ jsx(PopoverProvider, {
contentId: useId(),
hasCustomAnchor,
modal,
onCustomAnchorAdd: useCallback(() => setHasCustomAnchor(!0), []),
onCustomAnchorRemove: useCallback(() => setHasCustomAnchor(!1), []),
onOpenChange: setOpen,
onOpenToggle: useCallback(() => setOpen((prevOpen) => !prevOpen), [setOpen]),
open,
scope: __scopePopover,
triggerRef,
children
})
});
}, { displayName: POPOVER_NAME }), ANCHOR_NAME = "PopoverAnchor", PopoverAnchor = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopePopover, ...anchorProps } = props, context = usePopoverContext(ANCHOR_NAME, __scopePopover), popperScope = usePopperScope(__scopePopover), { onCustomAnchorAdd, onCustomAnchorRemove } = context;
return useEffect(() => (onCustomAnchorAdd(), () => onCustomAnchorRemove()), [onCustomAnchorAdd, onCustomAnchorRemove]), /* @__PURE__ */ jsx(PopperPrimitive.Anchor, {
...popperScope,
...anchorProps,
ref: forwardedRef
});
}), { displayName: ANCHOR_NAME }), TRIGGER_NAME = "PopoverTrigger", PopoverTrigger = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopePopover, ...triggerProps } = props, context = usePopoverContext(TRIGGER_NAME, __scopePopover), popperScope = usePopperScope(__scopePopover), composedTriggerRef = useComposedRefs(forwardedRef, context.triggerRef), trigger = /* @__PURE__ */ jsx(Primitive.button, {
"aria-controls": context.open ? context.contentId : void 0,
"aria-expanded": context.open,
"aria-haspopup": "dialog",
"data-state": getState(context.open),
type: "button",
...triggerProps,
onClick: composeEventHandlers(props.onClick, context.onOpenToggle),
ref: composedTriggerRef
});
return context.hasCustomAnchor ? trigger : /* @__PURE__ */ jsx(PopperPrimitive.Anchor, {
asChild: !0,
...popperScope,
children: trigger
});
}), { displayName: TRIGGER_NAME }), PORTAL_NAME = "PopoverPortal", [PortalProvider, usePortalContext] = createPopoverContext(PORTAL_NAME, {
forceMount: void 0
}), PopoverPortal = /* @__PURE__ */ Object.assign((props) => {
let { __scopePopover, forceMount, children, container } = props, context = usePopoverContext(PORTAL_NAME, __scopePopover);
return /* @__PURE__ */ jsx(PortalProvider, {
forceMount,
scope: __scopePopover,
children: /* @__PURE__ */ jsx(Presence, {
present: forceMount || context.open,
children: /* @__PURE__ */ jsx(PortalPrimitive, {
asChild: !0,
container,
children
})
})
});
}, { displayName: PORTAL_NAME }), CONTENT_NAME = "PopoverContent", PopoverContent = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let portalContext = usePortalContext(CONTENT_NAME, props.__scopePopover), { forceMount = portalContext.forceMount, ...contentProps } = props, context = usePopoverContext(CONTENT_NAME, props.__scopePopover);
return /* @__PURE__ */ jsx(Presence, {
present: forceMount || context.open,
children: context.modal ? /* @__PURE__ */ jsx(PopoverContentModal, {
...contentProps,
ref: forwardedRef
}) : /* @__PURE__ */ jsx(PopoverContentNonModal, {
...contentProps,
ref: forwardedRef
})
});
}), { displayName: CONTENT_NAME }), Slot = createSlot("PopoverContent.RemoveScroll"), PopoverContentModal = /* @__PURE__ */ forwardRef((props, forwardedRef) => {
let context = usePopoverContext(CONTENT_NAME, props.__scopePopover), contentRef = useRef(null), composedRefs = useComposedRefs(forwardedRef, contentRef), isRightClickOutsideRef = useRef(!1), { nodes: branchNodes, registry: branchRegistry } = useFocusScopeBranchRegistry(), shards = useMemo(() => [contentRef, ...branchNodes.map((node) => ({ current: node }))], [branchNodes]);
return useEffect(() => {
let content = contentRef.current;
if (content)
return hideOthers([content, ...branchNodes]);
}, [branchNodes]), /* @__PURE__ */ jsx(RemoveScroll, {
allowPinchZoom: !0,
as: Slot,
shards,
children: /* @__PURE__ */ jsx(PopoverContentImpl, {
...props,
branchNodes,
branchRegistry,
disableOutsidePointerEvents: !0,
onCloseAutoFocus: composeEventHandlers(props.onCloseAutoFocus, (event) => {
if (isRightClickOutsideRef.current)
event.preventDefault();
else {
let trigger = context.triggerRef.current;
if (trigger)
event.preventDefault(), trigger.focus();
}
}),
onFocusOutside: composeEventHandlers(props.onFocusOutside, (event) => event.preventDefault(), { checkForDefaultPrevented: !1 }),
onPointerDownOutside: composeEventHandlers(props.onPointerDownOutside, (event) => {
let originalEvent = event.detail.originalEvent, ctrlLeftClick = originalEvent.button === 0 && originalEvent.ctrlKey === !0, isRightClick = originalEvent.button === 2 || ctrlLeftClick;
isRightClickOutsideRef.current = isRightClick;
}, { checkForDefaultPrevented: !1 }),
ref: composedRefs,
trapFocus: context.open
})
});
}), PopoverContentNonModal = /* @__PURE__ */ forwardRef((props, forwardedRef) => {
let context = usePopoverContext(CONTENT_NAME, props.__scopePopover), hasInteractedOutsideRef = useRef(!1), hasPointerDownOutsideRef = useRef(!1);
return /* @__PURE__ */ jsx(PopoverContentImpl, {
...props,
disableOutsidePointerEvents: !1,
onCloseAutoFocus: (event) => {
if (props.onCloseAutoFocus?.(event), !event.defaultPrevented)
if (hasInteractedOutsideRef.current)
event.preventDefault();
else {
let trigger = context.triggerRef.current;
if (trigger)
event.preventDefault(), trigger.focus();
}
hasInteractedOutsideRef.current = !1, hasPointerDownOutsideRef.current = !1;
},
onInteractOutside: (event) => {
if (props.onInteractOutside?.(event), !event.defaultPrevented) {
if (hasInteractedOutsideRef.current = !0, event.detail.originalEvent.type === "pointerdown")
hasPointerDownOutsideRef.current = !0;
}
let target = event.target;
if (context.triggerRef.current?.contains(target))
event.preventDefault();
if (event.detail.originalEvent.type === "focusin" && hasPointerDownOutsideRef.current)
event.preventDefault();
},
ref: forwardedRef,
trapFocus: !1
});
}), PopoverContentImpl = /* @__PURE__ */ forwardRef((props, forwardedRef) => {
let {
__scopePopover,
trapFocus,
onOpenAutoFocus,
onCloseAutoFocus,
disableOutsidePointerEvents,
onEscapeKeyDown,
onPointerDownOutside,
onFocusOutside,
onInteractOutside,
branchNodes,
branchRegistry,
...contentProps
} = props, context = usePopoverContext(CONTENT_NAME, __scopePopover), popperScope = usePopperScope(__scopePopover), [contentNode, setContentNode] = useState(null), composedRefs = useComposedRefs(forwardedRef, setContentNode);
return useFocusScopeBranch(contentNode), useFocusGuards(), /* @__PURE__ */ jsx(FocusScopeBranchProvider, {
registry: branchRegistry,
children: /* @__PURE__ */ jsx(FocusScope, {
asChild: !0,
branches: branchNodes,
loop: !0,
onMountAutoFocus: onOpenAutoFocus,
onUnmountAutoFocus: onCloseAutoFocus,
trapped: trapFocus,
children: /* @__PURE__ */ jsx(DismissableLayer, {
asChild: !0,
deferPointerDownOutside: !0,
disableOutsidePointerEvents,
onDismiss: () => context.onOpenChange(!1),
onEscapeKeyDown,
onFocusOutside,
onInteractOutside,
onPointerDownOutside,
children: /* @__PURE__ */ jsx(PopperPrimitive.Content, {
"data-state": getState(context.open),
id: context.contentId,
role: "dialog",
...popperScope,
...contentProps,
ref: composedRefs,
style: {
...contentProps.style,
...{
"--loke-popover-content-available-height": "var(--loke-popper-available-height)",
"--loke-popover-content-available-width": "var(--loke-popper-available-width)",
"--loke-popover-content-transform-origin": "var(--loke-popper-transform-origin)",
"--loke-popover-trigger-height": "var(--loke-popper-anchor-height)",
"--loke-popover-trigger-width": "var(--loke-popper-anchor-width)"
}
}
})
})
})
});
}), CLOSE_NAME = "PopoverClose", PopoverClose = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopePopover, ...closeProps } = props, context = usePopoverContext(CLOSE_NAME, __scopePopover);
return /* @__PURE__ */ jsx(Primitive.button, {
type: "button",
...closeProps,
onClick: composeEventHandlers(props.onClick, () => context.onOpenChange(!1)),
ref: forwardedRef
});
}), { displayName: CLOSE_NAME }), ARROW_NAME = "PopoverArrow", PopoverArrow = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopePopover, ...arrowProps } = props, popperScope = usePopperScope(__scopePopover);
return /* @__PURE__ */ jsx(PopperPrimitive.Arrow, {
...popperScope,
...arrowProps,
ref: forwardedRef
});
}), { displayName: ARROW_NAME });
function getState(open) {
return open ? "open" : "closed";
}
var Root2 = Popover, Anchor2 = PopoverAnchor, Trigger = PopoverTrigger, Portal = PopoverPortal, Content2 = PopoverContent, Close = PopoverClose, Arrow2 = PopoverArrow;
export {
Anchor2 as Anchor,
Arrow2 as Arrow,
Close,
Content2 as Content,
Popover,
PopoverAnchor,
PopoverArrow,
PopoverClose,
PopoverContent,
PopoverPortal,
PopoverTrigger,
Portal,
Root2 as Root,
Trigger,
createPopoverScope
};