UNPKG

dgz-ui

Version:

Custom ui library using React.js, Shadcn/ui, TailwindCSS, Typescript

2 lines (1 loc) 4.39 kB
"use strict";const B=require("react"),m=require("./index-Ch6ABCiC.cjs"),Y=require("./index-BxMXYUfY.cjs"),z=require("./index-t1JFNXJ1.cjs"),V=require("./index-DcWZbXs6.cjs"),P=require("./index-C1ZpNc2P.cjs"),D=require("./index-CMmIsz5H.cjs"),J=require("./index-hwV4SK1V.cjs"),Q=require("./index-C3BrtnXJ.cjs"),f=require("./jsx-runtime-DUiatUiq.cjs");function W(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=W(B);var y="rovingFocusGroup.onEntryFocus",X={bubbles:!1,cancelable:!0},b="RovingFocusGroup",[j,G,Z]=Y.createCollection(b),[ee,te]=V.createContextScope(b,[Z]),[oe,re]=ee(b),N=c.forwardRef((e,o)=>f.jsxRuntimeExports.jsx(j.Provider,{scope:e.__scopeRovingFocusGroup,children:f.jsxRuntimeExports.jsx(j.Slot,{scope:e.__scopeRovingFocusGroup,children:f.jsxRuntimeExports.jsx(ne,{...e,ref:o})})}));N.displayName=b;var ne=c.forwardRef((e,o)=>{const{__scopeRovingFocusGroup:r,orientation:t,loop:F=!1,dir:S,currentTabStopId:v,defaultCurrentTabStopId:T,onCurrentTabStopIdChange:w,onEntryFocus:p,preventScrollOnEntryFocus:u=!1,...I}=e,g=c.useRef(null),R=z.useComposedRefs(o,g),x=Q.useDirection(S),[E,n]=P.useControllableState({prop:v,defaultProp:T??null,onChange:w,caller:b}),[a,C]=c.useState(!1),i=J.useCallbackRef(p),l=G(r),_=c.useRef(!1),[k,A]=c.useState(0);return c.useEffect(()=>{const s=g.current;if(s)return s.addEventListener(y,i),()=>s.removeEventListener(y,i)},[i]),f.jsxRuntimeExports.jsx(oe,{scope:r,orientation:t,dir:x,loop:F,currentTabStopId:E,onItemFocus:c.useCallback(s=>n(s),[n]),onItemShiftTab:c.useCallback(()=>C(!0),[]),onFocusableItemAdd:c.useCallback(()=>A(s=>s+1),[]),onFocusableItemRemove:c.useCallback(()=>A(s=>s-1),[]),children:f.jsxRuntimeExports.jsx(D.Primitive.div,{tabIndex:a||k===0?-1:0,"data-orientation":t,...I,ref:R,style:{outline:"none",...e.style},onMouseDown:m.composeEventHandlers(e.onMouseDown,()=>{_.current=!0}),onFocus:m.composeEventHandlers(e.onFocus,s=>{const L=!_.current;if(s.target===s.currentTarget&&L&&!a){const O=new CustomEvent(y,X);if(s.currentTarget.dispatchEvent(O),!O.defaultPrevented){const h=l().filter(d=>d.focusable),U=h.find(d=>d.active),H=h.find(d=>d.id===E),$=[U,H,...h].filter(Boolean).map(d=>d.ref.current);M($,u)}}_.current=!1}),onBlur:m.composeEventHandlers(e.onBlur,()=>C(!1))})})}),q="RovingFocusGroupItem",K=c.forwardRef((e,o)=>{const{__scopeRovingFocusGroup:r,focusable:t=!0,active:F=!1,tabStopId:S,children:v,...T}=e,w=P.useId(),p=S||w,u=re(q,r),I=u.currentTabStopId===p,g=G(r),{onFocusableItemAdd:R,onFocusableItemRemove:x,currentTabStopId:E}=u;return c.useEffect(()=>{if(t)return R(),()=>x()},[t,R,x]),f.jsxRuntimeExports.jsx(j.ItemSlot,{scope:r,id:p,focusable:t,active:F,children:f.jsxRuntimeExports.jsx(D.Primitive.span,{tabIndex:I?0:-1,"data-orientation":u.orientation,...T,ref:o,onMouseDown:m.composeEventHandlers(e.onMouseDown,n=>{t?u.onItemFocus(p):n.preventDefault()}),onFocus:m.composeEventHandlers(e.onFocus,()=>u.onItemFocus(p)),onKeyDown:m.composeEventHandlers(e.onKeyDown,n=>{if(n.key==="Tab"&&n.shiftKey){u.onItemShiftTab();return}if(n.target!==n.currentTarget)return;const a=ue(n,u.orientation,u.dir);if(a!==void 0){if(n.metaKey||n.ctrlKey||n.altKey||n.shiftKey)return;n.preventDefault();let i=g().filter(l=>l.focusable).map(l=>l.ref.current);if(a==="last")i.reverse();else if(a==="prev"||a==="next"){a==="prev"&&i.reverse();const l=i.indexOf(n.currentTarget);i=u.loop?ie(i,l+1):i.slice(l+1)}setTimeout(()=>M(i))}}),children:typeof v=="function"?v({isCurrentTabStop:I,hasTabStop:E!=null}):v})})});K.displayName=q;var se={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function ce(e,o){return o!=="rtl"?e:e==="ArrowLeft"?"ArrowRight":e==="ArrowRight"?"ArrowLeft":e}function ue(e,o,r){const t=ce(e.key,r);if(!(o==="vertical"&&["ArrowLeft","ArrowRight"].includes(t))&&!(o==="horizontal"&&["ArrowUp","ArrowDown"].includes(t)))return se[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 ie(e,o){return e.map((r,t)=>e[(o+t)%e.length])}var ae=N,le=K;exports.Item=le;exports.Root=ae;exports.createRovingFocusGroupScope=te;