UNPKG

@ebay/ui-core-react

Version:

Skin components build off React

2 lines (1 loc) 2.17 kB
"use strict";const c=require("react"),v=require("@floating-ui/react"),s=require("makeup-expander"),g=require("makeup-active-descendant"),{default:h=s}=s?.default?.__esModule?s.default:s;function y({open:e,options:r}){var o;const{floatingStyles:l,refs:d}=v.useFloating({placement:r?.reverse?"bottom-end":"bottom-start",open:e,middleware:[v.offset((o=r?.offset)!==null&&o!==void 0?o:4),v.flip(),v.shift()],whileElementsMounted:v.autoUpdate});return{overlayStyles:l,refs:{host:d.reference,overlay:d.floating,setHost:d.setReference,setOverlay:d.setFloating}}}function D({ref:e,expanded:r,options:o,onExpand:l,onCollapse:d},x){const t=c.useRef(null),a=c.useSyncExternalStore(n=>{var u,i;function f(){n(),l?.()}function E(){n(),d?.()}return(u=e?.current)===null||u===void 0||u.addEventListener("expander-expand",f),(i=e?.current)===null||i===void 0||i.addEventListener("expander-collapse",E),()=>{var p,_;(p=e?.current)===null||p===void 0||p.removeEventListener("expander-expand",f),(_=e?.current)===null||_===void 0||_.removeEventListener("expander-collapse",E)}},()=>{var n;return(n=t.current)===null||n===void 0?void 0:n.expanded},()=>!1);return c.useEffect(()=>(e?.current&&(t.current=new h(e.current,o)),()=>{var n;(n=t.current)===null||n===void 0||n.destroy()}),x||[]),c.useEffect(()=>{t.current&&r!==void 0&&(t.current.expanded=r)},[r]),{isExpanded:a,expand:()=>{t.current&&(t.current.expanded=!0)},collapse:()=>{t.current&&(t.current.expanded=!1)}}}function m({ref:e,focusElementRef:r,itemContainerRef:o,onChange:l=()=>null,disabled:d,options:x}){const t=c.useRef(null);return c.useEffect(()=>{const a=n=>{const u={toIndex:n.detail.toIndex};l(n,u)};return d||(t.current=g.createLinear(e.current,r?r.current:e.current,o?o.current:e.current,"[role=option]",x),e.current.addEventListener("activeDescendantChange",a)),()=>{var n,u,i;(n=t.current)===null||n===void 0||n.reset(),(u=t.current)===null||u===void 0||u.destroy(),(i=e.current)===null||i===void 0||i.removeEventListener("activeDescendantChange",a)}},[d,l]),{setIndex(a){t.current.index=a},getIndex(){return t.current.index}}}exports.useActiveDescendant=m;exports.useExpander=D;exports.useFloatingDropdown=y;