UNPKG

ivt

Version:

Ivt Components Library

3 lines (2 loc) 3.77 kB
import*as e from"react";import{c as o}from"./index-DVnblQ-a.mjs";import{createCollection as r}from"@radix-ui/react-collection";import{u as t}from"./index-C-j8pU3U.mjs";import{c as n}from"./index-Dr5ObK3L.mjs";import{u as a}from"./index-UQBMY2z8.mjs";import{P as i}from"./index-DiPp8h7C.mjs";import{u as s}from"./index-DSUHCtxx.mjs";import{u}from"./index-BOUg6o32.mjs";import{u as c}from"./index-DBjm5RwN.mjs";import{jsx as l}from"react/jsx-runtime";var f="rovingFocusGroup.onEntryFocus",p={bubbles:!1,cancelable:!0},d="RovingFocusGroup",[m,v,b]=r(d),[w,g]=n(d,[b]),[F,I]=w(d),h=e.forwardRef((e,o)=>l(m.Provider,{scope:e.__scopeRovingFocusGroup,children:l(m.Slot,{scope:e.__scopeRovingFocusGroup,children:l(R,{...e,ref:o})})}));h.displayName=d;var R=e.forwardRef((r,n)=>{const{__scopeRovingFocusGroup:a,orientation:m,loop:b=!1,dir:w,currentTabStopId:g,defaultCurrentTabStopId:I,onCurrentTabStopIdChange:h,onEntryFocus:R,preventScrollOnEntryFocus:x=!1,...y}=r,S=e.useRef(null),A=t(n,S),E=c(w),[D,C]=u({prop:g,defaultProp:I??null,onChange:h,caller:d}),[j,_]=e.useState(!1),G=s(R),K=v(a),k=e.useRef(!1),[L,P]=e.useState(0);return e.useEffect(()=>{const e=S.current;if(e)return e.addEventListener(f,G),()=>e.removeEventListener(f,G)},[G]),l(F,{scope:a,orientation:m,dir:E,loop:b,currentTabStopId:D,onItemFocus:e.useCallback(e=>C(e),[C]),onItemShiftTab:e.useCallback(()=>_(!0),[]),onFocusableItemAdd:e.useCallback(()=>P(e=>e+1),[]),onFocusableItemRemove:e.useCallback(()=>P(e=>e-1),[]),children:l(i.div,{tabIndex:j||0===L?-1:0,"data-orientation":m,...y,ref:A,style:{outline:"none",...r.style},onMouseDown:o(r.onMouseDown,()=>{k.current=!0}),onFocus:o(r.onFocus,e=>{const o=!k.current;if(e.target===e.currentTarget&&o&&!j){const o=new CustomEvent(f,p);if(e.currentTarget.dispatchEvent(o),!o.defaultPrevented){const e=K().filter(e=>e.focusable);T([e.find(e=>e.active),e.find(e=>e.id===D),...e].filter(Boolean).map(e=>e.ref.current),x)}}k.current=!1}),onBlur:o(r.onBlur,()=>_(!1))})})}),x="RovingFocusGroupItem",y=e.forwardRef((r,t)=>{const{__scopeRovingFocusGroup:n,focusable:s=!0,active:u=!1,tabStopId:c,children:f,...p}=r,d=a(),b=c||d,w=I(x,n),g=w.currentTabStopId===b,F=v(n),{onFocusableItemAdd:h,onFocusableItemRemove:R,currentTabStopId:y}=w;return e.useEffect(()=>{if(s)return h(),()=>R()},[s,h,R]),l(m.ItemSlot,{scope:n,id:b,focusable:s,active:u,children:l(i.span,{tabIndex:g?0:-1,"data-orientation":w.orientation,...p,ref:t,onMouseDown:o(r.onMouseDown,e=>{s?w.onItemFocus(b):e.preventDefault()}),onFocus:o(r.onFocus,()=>w.onItemFocus(b)),onKeyDown:o(r.onKeyDown,e=>{if("Tab"===e.key&&e.shiftKey)return void w.onItemShiftTab();if(e.target!==e.currentTarget)return;const o=function(e,o,r){const t=function(e,o){return"rtl"!==o?e:"ArrowLeft"===e?"ArrowRight":"ArrowRight"===e?"ArrowLeft":e}(e.key,r);return"vertical"===o&&["ArrowLeft","ArrowRight"].includes(t)||"horizontal"===o&&["ArrowUp","ArrowDown"].includes(t)?void 0:S[t]}(e,w.orientation,w.dir);if(void 0!==o){if(e.metaKey||e.ctrlKey||e.altKey||e.shiftKey)return;e.preventDefault();let n=F().filter(e=>e.focusable).map(e=>e.ref.current);if("last"===o)n.reverse();else if("prev"===o||"next"===o){"prev"===o&&n.reverse();const a=n.indexOf(e.currentTarget);n=w.loop?(t=a+1,(r=n).map((e,o)=>r[(t+o)%r.length])):n.slice(a+1)}setTimeout(()=>T(n))}var r,t}),children:"function"==typeof f?f({isCurrentTabStop:g,hasTabStop:null!=y}):f})})});y.displayName=x;var S={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function T(e,o=!1){const r=document.activeElement;for(const t of e){if(t===r)return;if(t.focus({preventScroll:o}),document.activeElement!==r)return}}var A=h,E=y;export{E as I,A as R,g as c}; //# sourceMappingURL=index-9IZ9Aw1I.mjs.map