UNPKG

dgz-ui

Version:

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

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