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