@loke/ui
Version:
To install dependencies:
303 lines (300 loc) • 15.1 kB
JavaScript
var __create = Object.create;
var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
return this[key];
}
var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => {
var canCache = mod != null && typeof mod === "object";
if (canCache) {
var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod);
if (cached)
return cached;
}
target = mod != null ? __create(__getProtoOf(mod)) : {};
let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target;
if (mod && typeof mod === "object" || typeof mod === "function") {
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: __accessProp.bind(mod, key),
enumerable: !0
});
}
if (canCache)
cache.set(mod, to);
return to;
}, __toCommonJS = (from) => {
var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc;
if (entry)
return entry;
if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") {
for (var key of __getOwnPropNames(from))
if (!__hasOwnProp.call(entry, key))
__defProp(entry, key, {
get: __accessProp.bind(from, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return __moduleCache.set(from, entry), entry;
}, __moduleCache;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: !0,
configurable: !0,
set: __exportSetter.bind(all, name)
});
};
// src/lib/dismissable-layer/index.ts
var exports_dismissable_layer = {};
__export(exports_dismissable_layer, {
Branch: () => Branch,
DismissableLayer: () => DismissableLayer,
DismissableLayerBranch: () => DismissableLayerBranch,
Root: () => Root,
useDismissableLayerSurface: () => useDismissableLayerSurface
});
module.exports = __toCommonJS(exports_dismissable_layer);
// src/lib/dismissable-layer/dismissable-layer.tsx
var import_compose_events = require("@loke/ui/compose-events"), import_compose_refs = require("@loke/ui/compose-refs"), import_primitive = require("@loke/ui/primitive"), import_use_callback_ref = require("@loke/ui/use-callback-ref"), import_react = require("react"), jsx_runtime = require("react/jsx-runtime"), DISMISSABLE_LAYER_NAME = "DismissableLayer", CONTEXT_UPDATE = "dismissableLayer.update", POINTER_DOWN_OUTSIDE = "dismissableLayer.pointerDownOutside", FOCUS_OUTSIDE = "dismissableLayer.focusOutside", originalBodyPointerEvents, DismissableLayerContext = import_react.createContext({
branches: /* @__PURE__ */ new Set,
dismissableSurfaces: /* @__PURE__ */ new Set,
layers: /* @__PURE__ */ new Set,
layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set
}), DismissableLayer = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => {
let {
disableOutsidePointerEvents = !1,
deferPointerDownOutside = !1,
onEscapeKeyDown,
onPointerDownOutside,
onFocusOutside,
onInteractOutside,
onDismiss,
...layerProps
} = props, context = import_react.useContext(DismissableLayerContext), [node, setNode] = import_react.useState(null), ownerDocument = node?.ownerDocument ?? globalThis?.document, [, force] = import_react.useState({}), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, setNode), layers = Array.from(context.layers), [highestLayerWithOutsidePointerEventsDisabled] = [
...context.layersWithOutsidePointerEventsDisabled
].slice(-1), highestLayerWithOutsidePointerEventsDisabledIndex = highestLayerWithOutsidePointerEventsDisabled ? layers.indexOf(highestLayerWithOutsidePointerEventsDisabled) : -1, index = node ? layers.indexOf(node) : -1, isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0, isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex, isDeferredPointerDownOutsideRef = import_react.useRef(!1), pointerDownOutside = usePointerDownOutside((event) => {
if (onPointerDownOutside?.(event), onInteractOutside?.(event), !event.defaultPrevented)
onDismiss?.();
}, {
deferPointerDownOutside,
dismissableSurfaces: context.dismissableSurfaces,
isDeferredPointerDownOutsideRef,
ownerDocument,
shouldHandlePointerDownOutside: import_react.useCallback((target) => {
if (!(target instanceof Node))
return !1;
let isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target));
return isPointerEventsEnabled && !isPointerDownOnBranch;
}, [context.branches, isPointerEventsEnabled])
}), focusOutside = useFocusOutside((event) => {
if (deferPointerDownOutside && isDeferredPointerDownOutsideRef.current)
return;
let target = event.target;
if ([...context.branches].some((branch) => branch.contains(target)))
return;
if (onFocusOutside?.(event), onInteractOutside?.(event), !event.defaultPrevented)
onDismiss?.();
}, ownerDocument), isHighestLayer = node ? index === layers.length - 1 : !1, handleKeyDown = import_use_callback_ref.useCallbackRef((event) => {
if (event.key !== "Escape")
return;
if (onEscapeKeyDown?.(event), !event.defaultPrevented && onDismiss)
event.preventDefault(), onDismiss();
});
import_react.useEffect(() => {
if (!isHighestLayer)
return;
return ownerDocument.addEventListener("keydown", handleKeyDown, {
capture: !0
}), () => ownerDocument.removeEventListener("keydown", handleKeyDown, {
capture: !0
});
}, [handleKeyDown, isHighestLayer, ownerDocument]), import_react.useEffect(() => {
if (!node)
return;
if (disableOutsidePointerEvents) {
if (context.layersWithOutsidePointerEventsDisabled.size === 0)
originalBodyPointerEvents = ownerDocument.body.style.pointerEvents, ownerDocument.body.style.pointerEvents = "none";
context.layersWithOutsidePointerEventsDisabled.add(node);
}
return context.layers.add(node), dispatchUpdate(), () => {
if (disableOutsidePointerEvents) {
if (context.layersWithOutsidePointerEventsDisabled.delete(node), context.layersWithOutsidePointerEventsDisabled.size === 0)
ownerDocument.body.style.pointerEvents = originalBodyPointerEvents;
}
};
}, [node, ownerDocument, disableOutsidePointerEvents, context]), import_react.useEffect(() => () => {
if (!node)
return;
context.layers.delete(node), context.layersWithOutsidePointerEventsDisabled.delete(node), dispatchUpdate();
}, [node, context]), import_react.useEffect(() => {
let handleUpdate = () => force({});
return document.addEventListener(CONTEXT_UPDATE, handleUpdate), () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
}, []);
let pointerEvents;
if (isBodyPointerEventsDisabled)
pointerEvents = isPointerEventsEnabled ? "auto" : "none";
return /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.div, {
...layerProps,
onBlurCapture: import_compose_events.composeEventHandlers(props.onBlurCapture, focusOutside.onBlurCapture),
onFocusCapture: import_compose_events.composeEventHandlers(props.onFocusCapture, focusOutside.onFocusCapture),
onPointerDownCapture: import_compose_events.composeEventHandlers(props.onPointerDownCapture, pointerDownOutside.onPointerDownCapture),
ref: composedRefs,
style: {
pointerEvents,
...props.style
}
});
}), { displayName: DISMISSABLE_LAYER_NAME }), BRANCH_NAME = "DismissableLayerBranch", DismissableLayerBranch = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => {
let context = import_react.useContext(DismissableLayerContext), ref = import_react.useRef(null), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, ref);
return import_react.useEffect(() => {
let node = ref.current;
if (node)
return context.branches.add(node), () => {
context.branches.delete(node);
};
}, [context.branches]), /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.div, {
...props,
ref: composedRefs
});
}), { displayName: BRANCH_NAME });
function useDismissableLayerSurface() {
let context = import_react.useContext(DismissableLayerContext), [node, setNode] = import_react.useState(null);
return import_react.useEffect(() => {
if (!node)
return;
return context.dismissableSurfaces.add(node), () => {
context.dismissableSurfaces.delete(node);
};
}, [context.dismissableSurfaces, node]), setNode;
}
var alwaysHandlePointerDownOutside = () => !0;
function usePointerDownOutside(onPointerDownOutside, args) {
let {
deferPointerDownOutside = !1,
dismissableSurfaces,
isDeferredPointerDownOutsideRef,
ownerDocument = globalThis?.document,
shouldHandlePointerDownOutside = alwaysHandlePointerDownOutside
} = args, handlePointerDownOutside = import_use_callback_ref.useCallbackRef(onPointerDownOutside), isPointerInsideReactTreeRef = import_react.useRef(!1), isPointerDownOutsideRef = import_react.useRef(!1), interceptedOutsideInteractionEventsRef = import_react.useRef(/* @__PURE__ */ new Map), handleClickRef = import_react.useRef(() => {});
return import_react.useEffect(() => {
let clickTimerId;
function resetOutsideInteraction() {
isPointerDownOutsideRef.current = !1, isDeferredPointerDownOutsideRef.current = !1, interceptedOutsideInteractionEventsRef.current.clear();
}
function isOutsideInteractionIntercepted() {
return Array.from(interceptedOutsideInteractionEventsRef.current.values()).some(Boolean);
}
function handleInteractionCapture(event) {
if (!isPointerDownOutsideRef.current)
return;
let target = event.target;
if (!(target instanceof Node && [...dismissableSurfaces].some((surface) => surface.contains(target))))
interceptedOutsideInteractionEventsRef.current.set(event.type, !0);
if (event.type === "click")
clickTimerId = window.setTimeout(() => {
if (isPointerDownOutsideRef.current)
handleClickRef.current();
}, 0);
}
function handleInteractionBubble(event) {
if (isPointerDownOutsideRef.current)
interceptedOutsideInteractionEventsRef.current.set(event.type, !1);
}
let handlePointerDown = (event) => {
if (event.target && !isPointerInsideReactTreeRef.current) {
let handleAndDispatchPointerDownOutsideEvent = function() {
ownerDocument.removeEventListener("click", handleClickRef.current);
let wasOutsideInteractionIntercepted = isOutsideInteractionIntercepted();
if (resetOutsideInteraction(), !wasOutsideInteractionIntercepted)
handleAndDispatchCustomEvent(POINTER_DOWN_OUTSIDE, handlePointerDownOutside, eventDetail, { discrete: !0 });
};
if (!shouldHandlePointerDownOutside(event.target)) {
ownerDocument.removeEventListener("click", handleClickRef.current), resetOutsideInteraction(), isPointerInsideReactTreeRef.current = !1;
return;
}
let eventDetail = { originalEvent: event };
if (isPointerDownOutsideRef.current = !0, isDeferredPointerDownOutsideRef.current = deferPointerDownOutside && event.button === 0, interceptedOutsideInteractionEventsRef.current.clear(), !deferPointerDownOutside || event.button !== 0)
handleAndDispatchPointerDownOutsideEvent();
else
ownerDocument.removeEventListener("click", handleClickRef.current), handleClickRef.current = handleAndDispatchPointerDownOutsideEvent, ownerDocument.addEventListener("click", handleClickRef.current, {
once: !0
});
} else
ownerDocument.removeEventListener("click", handleClickRef.current), resetOutsideInteraction();
isPointerInsideReactTreeRef.current = !1;
}, outsideInteractionEvents = [
"pointerup",
"mousedown",
"mouseup",
"touchstart",
"touchend",
"click"
];
for (let eventName of outsideInteractionEvents)
ownerDocument.addEventListener(eventName, handleInteractionCapture, !0), ownerDocument.addEventListener(eventName, handleInteractionBubble);
let timerId = window.setTimeout(() => {
ownerDocument.addEventListener("pointerdown", handlePointerDown);
}, 0);
return () => {
if (window.clearTimeout(timerId), clickTimerId !== void 0)
window.clearTimeout(clickTimerId);
resetOutsideInteraction(), ownerDocument.removeEventListener("pointerdown", handlePointerDown), ownerDocument.removeEventListener("click", handleClickRef.current);
for (let eventName of outsideInteractionEvents)
ownerDocument.removeEventListener(eventName, handleInteractionCapture, !0), ownerDocument.removeEventListener(eventName, handleInteractionBubble);
};
}, [
deferPointerDownOutside,
dismissableSurfaces,
handlePointerDownOutside,
isDeferredPointerDownOutsideRef,
ownerDocument,
shouldHandlePointerDownOutside
]), {
onPointerDownCapture: () => {
isPointerInsideReactTreeRef.current = !0;
}
};
}
function useFocusOutside(onFocusOutside, ownerDocument = globalThis?.document) {
let handleFocusOutside = import_use_callback_ref.useCallbackRef(onFocusOutside), isFocusInsideReactTreeRef = import_react.useRef(!1);
return import_react.useEffect(() => {
let handleFocus = (event) => {
if (event.target && !isFocusInsideReactTreeRef.current)
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, { originalEvent: event }, {
discrete: !1
});
};
return ownerDocument.addEventListener("focusin", handleFocus), () => ownerDocument.removeEventListener("focusin", handleFocus);
}, [ownerDocument, handleFocusOutside]), {
onBlurCapture: () => {
isFocusInsideReactTreeRef.current = !1;
},
onFocusCapture: () => {
isFocusInsideReactTreeRef.current = !0;
}
};
}
function dispatchUpdate() {
let event = new CustomEvent(CONTEXT_UPDATE);
document.dispatchEvent(event);
}
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
let { target } = detail.originalEvent, event = new CustomEvent(name, {
bubbles: !1,
cancelable: !0,
detail
});
if (handler)
target.addEventListener(name, handler, { once: !0 });
if (discrete)
import_primitive.dispatchDiscreteCustomEvent(target, event);
else
target.dispatchEvent(event);
}
var Root = DismissableLayer, Branch = DismissableLayerBranch;