UNPKG

sui-explorer-local

Version:
2 lines (1 loc) 4.46 kB
import{_ as h}from"./extends-98964cd2.js";import{R as b,r as o}from"./index-76fb7be0.js";import{$ as y,a as Y,g as q,c as G,d as R,b as z}from"./index-378103fb.js";import{$ as A,a as N}from"./index-d5a566d3.js";function H(e){const n=e+"CollectionProvider",[a,s]=y(n),[g,m]=a(n,{collectionRef:{current:null},itemMap:new Map}),x=i=>{const{scope:t,children:f}=i,l=b.useRef(null),r=b.useRef(new Map).current;return b.createElement(g,{scope:t,itemMap:r,collectionRef:l},f)},F=e+"CollectionSlot",$=b.forwardRef((i,t)=>{const{scope:f,children:l}=i,r=m(F,f),c=A(t,r.collectionRef);return b.createElement(N,{ref:c},l)}),d=e+"CollectionItemSlot",I="data-radix-collection-item",C=b.forwardRef((i,t)=>{const{scope:f,children:l,...r}=i,c=b.useRef(null),T=A(t,c),p=m(d,f);return b.useEffect(()=>(p.itemMap.set(c,{ref:c,...r}),()=>void p.itemMap.delete(c))),b.createElement(N,{[I]:"",ref:T},l)});function E(i){const t=m(e+"CollectionConsumer",i);return b.useCallback(()=>{const l=t.collectionRef.current;if(!l)return[];const r=Array.from(l.querySelectorAll(`[${I}]`));return Array.from(t.itemMap.values()).sort((p,S)=>r.indexOf(p.ref.current)-r.indexOf(S.ref.current))},[t.collectionRef,t.itemMap])}return[{Provider:x,Slot:$,ItemSlot:C},E,s]}const j=o.createContext(void 0);function J(e){const n=o.useContext(j);return e||n||"ltr"}const w="rovingFocusGroup.onEntryFocus",Q={bubbles:!1,cancelable:!0},D="RovingFocusGroup",[M,L,W]=H(D),[X,$e]=y(D,[W]),[Z,ee]=X(D),te=o.forwardRef((e,n)=>o.createElement(M.Provider,{scope:e.__scopeRovingFocusGroup},o.createElement(M.Slot,{scope:e.__scopeRovingFocusGroup},o.createElement(oe,h({},e,{ref:n}))))),oe=o.forwardRef((e,n)=>{const{__scopeRovingFocusGroup:a,orientation:s,loop:g=!1,dir:m,currentTabStopId:x,defaultCurrentTabStopId:F,onCurrentTabStopIdChange:$,onEntryFocus:d,...I}=e,C=o.useRef(null),E=A(n,C),i=J(m),[t=null,f]=Y({prop:x,defaultProp:F,onChange:$}),[l,r]=o.useState(!1),c=q(d),T=L(a),p=o.useRef(!1),[S,O]=o.useState(0);return o.useEffect(()=>{const u=C.current;if(u)return u.addEventListener(w,c),()=>u.removeEventListener(w,c)},[c]),o.createElement(Z,{scope:a,orientation:s,dir:i,loop:g,currentTabStopId:t,onItemFocus:o.useCallback(u=>f(u),[f]),onItemShiftTab:o.useCallback(()=>r(!0),[]),onFocusableItemAdd:o.useCallback(()=>O(u=>u+1),[]),onFocusableItemRemove:o.useCallback(()=>O(u=>u-1),[])},o.createElement(G.div,h({tabIndex:l||S===0?-1:0,"data-orientation":s},I,{ref:E,style:{outline:"none",...e.style},onMouseDown:R(e.onMouseDown,()=>{p.current=!0}),onFocus:R(e.onFocus,u=>{const U=!p.current;if(u.target===u.currentTarget&&U&&!l){const P=new CustomEvent(w,Q);if(u.currentTarget.dispatchEvent(P),!P.defaultPrevented){const _=T().filter(v=>v.focusable),K=_.find(v=>v.active),B=_.find(v=>v.id===t),V=[K,B,..._].filter(Boolean).map(v=>v.ref.current);k(V)}}p.current=!1}),onBlur:R(e.onBlur,()=>r(!1))})))}),ne="RovingFocusGroupItem",re=o.forwardRef((e,n)=>{const{__scopeRovingFocusGroup:a,focusable:s=!0,active:g=!1,tabStopId:m,...x}=e,F=z(),$=m||F,d=ee(ne,a),I=d.currentTabStopId===$,C=L(a),{onFocusableItemAdd:E,onFocusableItemRemove:i}=d;return o.useEffect(()=>{if(s)return E(),()=>i()},[s,E,i]),o.createElement(M.ItemSlot,{scope:a,id:$,focusable:s,active:g},o.createElement(G.span,h({tabIndex:I?0:-1,"data-orientation":d.orientation},x,{ref:n,onMouseDown:R(e.onMouseDown,t=>{s?d.onItemFocus($):t.preventDefault()}),onFocus:R(e.onFocus,()=>d.onItemFocus($)),onKeyDown:R(e.onKeyDown,t=>{if(t.key==="Tab"&&t.shiftKey){d.onItemShiftTab();return}if(t.target!==t.currentTarget)return;const f=fe(t,d.orientation,d.dir);if(f!==void 0){t.preventDefault();let r=C().filter(c=>c.focusable).map(c=>c.ref.current);if(f==="last")r.reverse();else if(f==="prev"||f==="next"){f==="prev"&&r.reverse();const c=r.indexOf(t.currentTarget);r=d.loop?ae(r,c+1):r.slice(c+1)}setTimeout(()=>k(r))}})})))}),ce={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function se(e,n){return n!=="rtl"?e:e==="ArrowLeft"?"ArrowRight":e==="ArrowRight"?"ArrowLeft":e}function fe(e,n,a){const s=se(e.key,a);if(!(n==="vertical"&&["ArrowLeft","ArrowRight"].includes(s))&&!(n==="horizontal"&&["ArrowUp","ArrowDown"].includes(s)))return ce[s]}function k(e){const n=document.activeElement;for(const a of e)if(a===n||(a.focus(),document.activeElement!==n))return}function ae(e,n){return e.map((a,s)=>e[(n+s)%e.length])}const pe=te,me=re;export{$e as $,J as a,pe as b,me as c,H as d};