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