@stanfordspezi/spezi-web-design-system
Version:
Stanford Biodesign Digital Health Spezi Web Design System
2 lines (1 loc) • 4.03 kB
JavaScript
;const $=require("react"),l=require("./index-CLdeiAAB.js"),P=require("./index-BM0VQ-wq.js"),Y=require("./index-UhBwhhTY.js"),z=require("./index-Bqh8zfKH.js"),D=require("./index-14SsMENa.js"),m=require("react/jsx-runtime");function V(e){const o=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const t=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(o,r,t.get?t:{enumerable:!0,get:()=>e[r]})}}return o.default=e,Object.freeze(o)}const c=V($);var y="rovingFocusGroup.onEntryFocus",J={bubbles:!1,cancelable:!0},I="RovingFocusGroup",[A,G,Q]=P.createCollection(I),[W,X]=l.createContextScope(I,[Q]),[Z,ee]=W(I),N=c.forwardRef((e,o)=>m.jsx(A.Provider,{scope:e.__scopeRovingFocusGroup,children:m.jsx(A.Slot,{scope:e.__scopeRovingFocusGroup,children:m.jsx(te,{...e,ref:o})})}));N.displayName=I;var te=c.forwardRef((e,o)=>{const{__scopeRovingFocusGroup:r,orientation:t,loop:g=!1,dir:F,currentTabStopId:R,defaultCurrentTabStopId:E,onCurrentTabStopIdChange:p,onEntryFocus:u,preventScrollOnEntryFocus:w=!1,...S}=e,v=c.useRef(null),b=Y.useComposedRefs(o,v),n=P.useDirection(F),[f=null,C]=l.useControllableState({prop:R,defaultProp:E,onChange:p}),[a,i]=c.useState(!1),T=l.useCallbackRef(u),k=G(r),x=c.useRef(!1),[L,h]=c.useState(0);return c.useEffect(()=>{const s=v.current;if(s)return s.addEventListener(y,T),()=>s.removeEventListener(y,T)},[T]),m.jsx(Z,{scope:r,orientation:t,dir:n,loop:g,currentTabStopId:f,onItemFocus:c.useCallback(s=>C(s),[C]),onItemShiftTab:c.useCallback(()=>i(!0),[]),onFocusableItemAdd:c.useCallback(()=>h(s=>s+1),[]),onFocusableItemRemove:c.useCallback(()=>h(s=>s-1),[]),children:m.jsx(D.Primitive.div,{tabIndex:a||L===0?-1:0,"data-orientation":t,...S,ref:b,style:{outline:"none",...e.style},onMouseDown:l.composeEventHandlers(e.onMouseDown,()=>{x.current=!0}),onFocus:l.composeEventHandlers(e.onFocus,s=>{const U=!x.current;if(s.target===s.currentTarget&&U&&!a){const O=new CustomEvent(y,J);if(s.currentTarget.dispatchEvent(O),!O.defaultPrevented){const _=k().filter(d=>d.focusable),q=_.find(d=>d.active),H=_.find(d=>d.id===f),B=[q,H,..._].filter(Boolean).map(d=>d.ref.current);M(B,w)}}x.current=!1}),onBlur:l.composeEventHandlers(e.onBlur,()=>i(!1))})})}),j="RovingFocusGroupItem",K=c.forwardRef((e,o)=>{const{__scopeRovingFocusGroup:r,focusable:t=!0,active:g=!1,tabStopId:F,...R}=e,E=z.useId(),p=F||E,u=ee(j,r),w=u.currentTabStopId===p,S=G(r),{onFocusableItemAdd:v,onFocusableItemRemove:b}=u;return c.useEffect(()=>{if(t)return v(),()=>b()},[t,v,b]),m.jsx(A.ItemSlot,{scope:r,id:p,focusable:t,active:g,children:m.jsx(D.Primitive.span,{tabIndex:w?0:-1,"data-orientation":u.orientation,...R,ref:o,onMouseDown:l.composeEventHandlers(e.onMouseDown,n=>{t?u.onItemFocus(p):n.preventDefault()}),onFocus:l.composeEventHandlers(e.onFocus,()=>u.onItemFocus(p)),onKeyDown:l.composeEventHandlers(e.onKeyDown,n=>{if(n.key==="Tab"&&n.shiftKey){u.onItemShiftTab();return}if(n.target!==n.currentTarget)return;const f=ne(n,u.orientation,u.dir);if(f!==void 0){if(n.metaKey||n.ctrlKey||n.altKey||n.shiftKey)return;n.preventDefault();let a=S().filter(i=>i.focusable).map(i=>i.ref.current);if(f==="last")a.reverse();else if(f==="prev"||f==="next"){f==="prev"&&a.reverse();const i=a.indexOf(n.currentTarget);a=u.loop?se(a,i+1):a.slice(i+1)}setTimeout(()=>M(a))}})})})});K.displayName=j;var oe={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function re(e,o){return o!=="rtl"?e:e==="ArrowLeft"?"ArrowRight":e==="ArrowRight"?"ArrowLeft":e}function ne(e,o,r){const t=re(e.key,r);if(!(o==="vertical"&&["ArrowLeft","ArrowRight"].includes(t))&&!(o==="horizontal"&&["ArrowUp","ArrowDown"].includes(t)))return oe[t]}function M(e,o=!1){const r=document.activeElement;for(const t of e)if(t===r||(t.focus({preventScroll:o}),document.activeElement!==r))return}function se(e,o){return e.map((r,t)=>e[(o+t)%e.length])}var ce=N,ue=K;exports.Item=ue;exports.Root=ce;exports.createRovingFocusGroupScope=X;