@ariakit/react
Version:
Toolkit for building accessible web apps with React
458 lines (400 loc) • 17.7 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _AIFRJ53Zcjs = require('./AIFRJ53Z.cjs');
var _4Q5TABN7cjs = require('./4Q5TABN7.cjs');
var _KJ5H7OHHcjs = require('./KJ5H7OHH.cjs');
var _5FQZLBJ7cjs = require('./5FQZLBJ7.cjs');
var _MTBSKKM4cjs = require('./MTBSKKM4.cjs');
var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs');
// ../ariakit-react-components/dist/hovercard/hovercard-context.js
var ctx = _LZ6L3ECGcjs.createStoreContext.call(void 0, [_4Q5TABN7cjs.PopoverContextProvider], [_4Q5TABN7cjs.PopoverScopedContextProvider]);
var useHovercardContext = ctx.useContext;
var useHovercardScopedContext = ctx.useScopedContext;
var useHovercardProviderContext = ctx.useProviderContext;
var HovercardContextProvider = ctx.ContextProvider;
var HovercardScopedContextProvider = ctx.ScopedContextProvider;
// ../ariakit-react-components/dist/hovercard/utils/polygon.js
function getEventPoint(event) {
return [event.clientX, event.clientY];
}
function isPointInPolygon(point, polygon) {
const [x, y] = point;
let inside = false;
const length = polygon.length;
for (let l = length, i = 0, j = l - 1; i < l; j = i++) {
const currentPoint = polygon[i];
const previousPoint = polygon[j];
const vertexPoint = polygon[j === 0 ? l - 1 : j - 1];
if (currentPoint == null) return false;
if (previousPoint == null) return false;
if (vertexPoint == null) return false;
const [xi, yi] = currentPoint;
const [xj, yj] = previousPoint;
const [, vy] = vertexPoint;
const where = (yi - yj) * (x - xi) - (xi - xj) * (y - yi);
if (yj < yi) {
if (y >= yj && y < yi) {
if (where === 0) return true;
if (where > 0) if (y === yj) {
if (y > vy) inside = !inside;
} else inside = !inside;
}
} else if (yi < yj) {
if (y > yi && y <= yj) {
if (where === 0) return true;
if (where < 0) if (y === yj) {
if (y < vy) inside = !inside;
} else inside = !inside;
}
} else if (y === yi && (x >= xj && x <= xi || x >= xi && x <= xj)) return true;
}
return inside;
}
function getEnterPointPlacement(enterPoint, rect) {
const { top, right, bottom, left } = rect;
const [x, y] = enterPoint;
return [x < left ? "left" : x > right ? "right" : null, y < top ? "top" : y > bottom ? "bottom" : null];
}
function getElementPolygon(element, enterPoint) {
const rect = element.getBoundingClientRect();
const { top, right, bottom, left } = rect;
const [x, y] = getEnterPointPlacement(enterPoint, rect);
const polygon = [enterPoint];
if (x) {
if (y !== "top") polygon.push([x === "left" ? left : right, top]);
polygon.push([x === "left" ? right : left, top]);
polygon.push([x === "left" ? right : left, bottom]);
if (y !== "bottom") polygon.push([x === "left" ? left : right, bottom]);
} else if (y === "top") {
polygon.push([left, top]);
polygon.push([left, bottom]);
polygon.push([right, bottom]);
polygon.push([right, top]);
} else {
polygon.push([left, bottom]);
polygon.push([left, top]);
polygon.push([right, top]);
polygon.push([right, bottom]);
}
return polygon;
}
// ../ariakit-react-components/dist/hovercard/hovercard.js
var _react = require('react');
var _jsxruntime = require('react/jsx-runtime');
var TagName = "div";
function isMovingOnHovercard(path, card, anchor, nested) {
if (_LZ6L3ECGcjs.hasFocusWithin.call(void 0, card)) return true;
if (path.includes(card)) return true;
if (anchor && path.includes(anchor)) return true;
if (nested == null ? void 0 : nested.some((card2) => _LZ6L3ECGcjs.hasFocusWithin.call(void 0, card2) || path.includes(card2))) return true;
return false;
}
function disableEventOnTransit({ event, element, enterPointRef, disablePointerEvents, refreshEnterPoint = false }) {
const enterPoint = enterPointRef.current;
if (!enterPoint) return false;
const currentPoint = getEventPoint(event);
if (!isPointInPolygon(currentPoint, getElementPolygon(element, enterPoint))) return false;
if (refreshEnterPoint) enterPointRef.current = currentPoint;
if (disablePointerEvents(event)) {
event.preventDefault();
event.stopPropagation();
}
return true;
}
function useAutoFocusOnHide({ store, ...props }) {
const [autoFocusOnHide, setAutoFocusOnHide] = _react.useState.call(void 0, false);
const mounted = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "mounted");
_react.useEffect.call(void 0, () => {
if (!mounted) setAutoFocusOnHide(false);
}, [mounted]);
const onFocusProp = props.onFocus;
const onFocus = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onFocusProp == null ? void 0 : onFocusProp(event);
if (event.defaultPrevented) return;
setAutoFocusOnHide(true);
});
const finalFocusRef = _react.useRef.call(void 0, null);
_react.useEffect.call(void 0, () => {
return _5FQZLBJ7cjs.sync.call(void 0, store, ["anchorElement"], (state) => {
finalFocusRef.current = state.anchorElement;
});
}, [store]);
props = {
autoFocusOnHide,
finalFocus: finalFocusRef,
...props,
onFocus
};
return props;
}
var NestedHovercardContext = _react.createContext.call(void 0, null);
var useHovercard = _LZ6L3ECGcjs.createHook.call(void 0, function useHovercard2({ store, modal = false, portal = modal, hideOnEscape = true, hideOnHoverOutside = true, disablePointerEventsOnApproach = !!hideOnHoverOutside, ...props }) {
const context = useHovercardProviderContext();
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "Hovercard must receive a `store` prop or be wrapped in a HovercardProvider component.");
const ref = _react.useRef.call(void 0, null);
const nestedHovercardsRef = _react.useRef.call(void 0, []);
const hideTimeoutRef = _react.useRef.call(void 0, 0);
const enterPointRef = _react.useRef.call(void 0, null);
const { portalRef, domReady } = _LZ6L3ECGcjs.usePortalRef.call(void 0, portal, props.portalRef);
const isMouseMoving = _LZ6L3ECGcjs.useIsMouseMoving.call(void 0, );
const mayHideOnHoverOutside = !!hideOnHoverOutside;
const hideOnHoverOutsideProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, hideOnHoverOutside);
const mayDisablePointerEvents = !!disablePointerEventsOnApproach;
const disablePointerEventsProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, disablePointerEventsOnApproach);
const open = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "open");
const mounted = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "mounted");
const clearHideTimeout = _react.useCallback.call(void 0, () => {
window.clearTimeout(hideTimeoutRef.current);
hideTimeoutRef.current = 0;
}, []);
_react.useEffect.call(void 0, () => {
if (!domReady) return;
if (!mounted) return;
if (!mayHideOnHoverOutside && !mayDisablePointerEvents) return;
const element = ref.current;
if (!element) return;
const onMouseMove = (event) => {
if (!store) return;
if (!isMouseMoving()) return;
const { anchorElement, hideTimeout, timeout } = store.getState();
const path = event.composedPath();
const anchor = anchorElement;
if (isMovingOnHovercard(path, element, anchor, nestedHovercardsRef.current)) {
enterPointRef.current = anchor && path.includes(anchor) ? getEventPoint(event) : null;
clearHideTimeout();
return;
}
if (hideTimeoutRef.current) return;
if (disableEventOnTransit({
event,
element,
enterPointRef,
disablePointerEvents: disablePointerEventsProp,
refreshEnterPoint: true
})) return;
if (!hideOnHoverOutsideProp(event)) return;
hideTimeoutRef.current = window.setTimeout(() => {
hideTimeoutRef.current = 0;
store == null ? void 0 : store.hide();
}, hideTimeout != null ? hideTimeout : timeout);
};
return _LZ6L3ECGcjs.chain.call(void 0, _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mousemove", onMouseMove, true), clearHideTimeout);
}, [
store,
isMouseMoving,
domReady,
mounted,
mayHideOnHoverOutside,
mayDisablePointerEvents,
nestedHovercardsRef,
clearHideTimeout,
disablePointerEventsProp,
hideOnHoverOutsideProp
]);
_react.useEffect.call(void 0, () => {
if (!domReady) return;
if (!mounted) return;
if (!mayDisablePointerEvents) return;
const disableEvent = (event) => {
const element = ref.current;
if (!element) return;
disableEventOnTransit({
event,
element,
enterPointRef,
disablePointerEvents: disablePointerEventsProp
});
};
return _LZ6L3ECGcjs.chain.call(void 0, _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mouseenter", disableEvent, true), _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mouseover", disableEvent, true), _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mouseout", disableEvent, true), _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mouseleave", disableEvent, true));
}, [
domReady,
mounted,
mayDisablePointerEvents,
disablePointerEventsProp
]);
_react.useEffect.call(void 0, () => {
if (!domReady) return;
if (open) return;
store == null ? void 0 : store.setAutoFocusOnShow(false);
}, [
store,
domReady,
open
]);
const openRef = _LZ6L3ECGcjs.useLiveRef.call(void 0, open);
_react.useEffect.call(void 0, () => {
if (!domReady) return;
return () => {
if (!openRef.current) store == null ? void 0 : store.setAutoFocusOnShow(false);
};
}, [store, domReady]);
const registerOnParent = _react.useContext.call(void 0, NestedHovercardContext);
_LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => {
if (modal) return;
if (!portal) return;
if (!mounted) return;
if (!domReady) return;
const element = ref.current;
if (!element) return;
return registerOnParent == null ? void 0 : registerOnParent(element);
}, [
modal,
portal,
mounted,
domReady
]);
const registerNestedHovercard = _react.useCallback.call(void 0, (element) => {
clearHideTimeout();
nestedHovercardsRef.current = [...nestedHovercardsRef.current, element];
const parentUnregister = registerOnParent == null ? void 0 : registerOnParent(element);
return () => {
clearHideTimeout();
nestedHovercardsRef.current = nestedHovercardsRef.current.filter((item) => item !== element);
parentUnregister == null ? void 0 : parentUnregister();
};
}, [clearHideTimeout, registerOnParent]);
props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, HovercardScopedContextProvider, {
value: store,
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, NestedHovercardContext.Provider, {
value: registerNestedHovercard,
children: element
})
}), [store, registerNestedHovercard]);
props = {
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref)
};
props = useAutoFocusOnHide({
store,
...props
});
const autoFocusOnShow = _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => modal || state.autoFocusOnShow);
props = _AIFRJ53Zcjs.usePopover.call(void 0, {
store,
modal,
portal,
autoFocusOnShow,
...props,
portalRef,
hideOnEscape(event) {
if (_LZ6L3ECGcjs.isFalsyBooleanCallback.call(void 0, hideOnEscape, event)) return false;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
store == null ? void 0 : store.hide();
});
});
return true;
}
});
return props;
});
var Hovercard = _KJ5H7OHHcjs.createDialogComponent.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function Hovercard2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useHovercard(props));
}), useHovercardProviderContext);
// ../ariakit-react-components/dist/hovercard/hovercard-anchor.js
var TagName2 = "a";
var useHovercardAnchor = _LZ6L3ECGcjs.createHook.call(void 0, function useHovercardAnchor2({ store, showOnHover = true, ...props }) {
const context = useHovercardProviderContext();
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "HovercardAnchor must receive a `store` prop or be wrapped in a HovercardProvider component.");
const disabled = _LZ6L3ECGcjs.disabledFromProps.call(void 0, props);
const showTimeoutRef = _react.useRef.call(void 0, 0);
_react.useEffect.call(void 0, () => () => window.clearTimeout(showTimeoutRef.current), []);
_react.useEffect.call(void 0, () => {
const onMouseLeave = (event) => {
if (!store) return;
const { anchorElement } = store.getState();
if (!anchorElement) return;
if (event.target !== anchorElement) return;
window.clearTimeout(showTimeoutRef.current);
showTimeoutRef.current = 0;
};
return _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mouseleave", onMouseLeave, true);
}, [store]);
const onMouseMoveProp = props.onMouseMove;
const showOnHoverProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, showOnHover);
const isMouseMoving = _LZ6L3ECGcjs.useIsMouseMoving.call(void 0, );
const onMouseMove = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onMouseMoveProp == null ? void 0 : onMouseMoveProp(event);
if (disabled) return;
if (!store) return;
if (event.defaultPrevented) return;
if (showTimeoutRef.current) return;
if (!isMouseMoving()) return;
if (!showOnHoverProp(event)) return;
const element = event.currentTarget;
store.setAnchorElement(element);
store.setDisclosureElement(element);
const { showTimeout, timeout } = store.getState();
const showHovercard = () => {
showTimeoutRef.current = 0;
if (!isMouseMoving()) return;
store == null ? void 0 : store.setAnchorElement(element);
store == null ? void 0 : store.show();
queueMicrotask(() => {
store == null ? void 0 : store.setDisclosureElement(element);
});
};
const timeoutMs = showTimeout != null ? showTimeout : timeout;
if (timeoutMs === 0) showHovercard();
else showTimeoutRef.current = window.setTimeout(showHovercard, timeoutMs);
});
const onClickProp = props.onClick;
const onClick = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onClickProp == null ? void 0 : onClickProp(event);
if (!store) return;
window.clearTimeout(showTimeoutRef.current);
showTimeoutRef.current = 0;
});
const ref = _react.useCallback.call(void 0, (element) => {
if (!store) return;
const { anchorElement } = store.getState();
if (anchorElement == null ? void 0 : anchorElement.isConnected) return;
store.setAnchorElement(element);
}, [store]);
props = {
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onMouseMove,
onClick
};
props = _MTBSKKM4cjs.useFocusable.call(void 0, props);
return props;
});
var HovercardAnchor = _LZ6L3ECGcjs.forwardRef.call(void 0, function HovercardAnchor2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName2, useHovercardAnchor(props));
});
// ../ariakit-components/dist/hovercard/hovercard-store.js
function createHovercardStore(props = {}) {
var _a;
const syncState = (_a = props.store) == null ? void 0 : _a.getState();
const popover = _AIFRJ53Zcjs.createPopoverStore.call(void 0, {
...props,
placement: _LZ6L3ECGcjs.defaultValue.call(void 0, props.placement, syncState == null ? void 0 : syncState.placement, "bottom")
});
const timeout = _LZ6L3ECGcjs.defaultValue.call(void 0, props.timeout, syncState == null ? void 0 : syncState.timeout, 500);
const hovercard = _5FQZLBJ7cjs.createStore.call(void 0, {
...popover.getState(),
timeout,
showTimeout: _LZ6L3ECGcjs.defaultValue.call(void 0, props.showTimeout, syncState == null ? void 0 : syncState.showTimeout),
hideTimeout: _LZ6L3ECGcjs.defaultValue.call(void 0, props.hideTimeout, syncState == null ? void 0 : syncState.hideTimeout),
autoFocusOnShow: _LZ6L3ECGcjs.defaultValue.call(void 0, syncState == null ? void 0 : syncState.autoFocusOnShow, false)
}, popover, props.store);
return {
...popover,
...hovercard,
setAutoFocusOnShow: (value) => hovercard.setState("autoFocusOnShow", value)
};
}
// ../ariakit-react-components/dist/hovercard/hovercard-store.js
function useHovercardStoreProps(store, update, props) {
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "timeout");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "showTimeout");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "hideTimeout");
return _AIFRJ53Zcjs.usePopoverStoreProps.call(void 0, store, update, props);
}
function useHovercardStore(props = {}) {
const [store, update] = _5FQZLBJ7cjs.useStore.call(void 0, createHovercardStore, props);
return useHovercardStoreProps(store, update, props);
}
exports.useHovercardContext = useHovercardContext; exports.useHovercardScopedContext = useHovercardScopedContext; exports.useHovercardProviderContext = useHovercardProviderContext; exports.HovercardContextProvider = HovercardContextProvider; exports.HovercardScopedContextProvider = HovercardScopedContextProvider; exports.useHovercard = useHovercard; exports.Hovercard = Hovercard; exports.useHovercardAnchor = useHovercardAnchor; exports.HovercardAnchor = HovercardAnchor; exports.createHovercardStore = createHovercardStore; exports.useHovercardStoreProps = useHovercardStoreProps; exports.useHovercardStore = useHovercardStore;