blowfish/node_modules/@headlessui/react/dist/components/menu/menu.js

2 lines
8.5 KiB
JavaScript
Raw Normal View History

2023-01-29 22:30:24 +00:00
import K,{Fragment as _,createContext as q,createRef as N,useContext as J,useEffect as z,useMemo as P,useReducer as X,useRef as H}from"react";import{match as j}from'../../utils/match.js';import{forwardRefWithAs as E,render as x,Features as Q}from'../../utils/render.js';import{disposables as L}from'../../utils/disposables.js';import{useDisposables as W}from'../../hooks/use-disposables.js';import{useIsoMorphicEffect as D}from'../../hooks/use-iso-morphic-effect.js';import{useSyncRefs as C}from'../../hooks/use-sync-refs.js';import{useId as k}from'../../hooks/use-id.js';import{Keys as m}from'../keyboard.js';import{Focus as I,calculateActiveIndex as Y}from'../../utils/calculate-active-index.js';import{isDisabledReactIssue7711 as Z}from'../../utils/bugs.js';import{isFocusableElement as ee,FocusableMode as te,sortByDomNode as ne,Focus as V,focusFrom as re,restoreFocusIfNecessary as $}from'../../utils/focus-management.js';import{useOutsideClick as oe}from'../../hooks/use-outside-click.js';import{useTreeWalker as ae}from'../../hooks/use-tree-walker.js';import{useOpenClosed as ie,State as w,OpenClosedProvider as ue}from'../../internal/open-closed.js';import{useResolveButtonType as se}from'../../hooks/use-resolve-button-type.js';import{useOwnerDocument as le}from'../../hooks/use-owner.js';import{useEvent as y}from'../../hooks/use-event.js';import{useTrackedPointer as ce}from'../../hooks/use-tracked-pointer.js';var pe=(o=>(o[o.Open=0]="Open",o[o.Closed=1]="Closed",o))(pe||{}),de=(o=>(o[o.Pointer=0]="Pointer",o[o.Other=1]="Other",o))(de||{}),me=(e=>(e[e.OpenMenu=0]="OpenMenu",e[e.CloseMenu=1]="CloseMenu",e[e.GoToItem=2]="GoToItem",e[e.Search=3]="Search",e[e.ClearSearch=4]="ClearSearch",e[e.RegisterItem=5]="RegisterItem",e[e.UnregisterItem=6]="UnregisterItem",e))(me||{});function U(t,i=o=>o){let o=t.activeItemIndex!==null?t.items[t.activeItemIndex]:null,s=ne(i(t.items.slice()),u=>u.dataRef.current.domRef.current),a=o?s.indexOf(o):null;return a===-1&&(a=null),{items:s,activeItemIndex:a}}let fe={[1](t){return t.menuState===1?t:{...t,activeItemIndex:null,menuState:1}},[0](t){return t.menuState===0?t:{...t,menuState:0}},[2]:(t,i)=>{var a;let o=U(t),s=Y(i,{resolveItems:()=>o.items,resolveActiveIndex:()=>o.activeItemIndex,resolveId:u=>u.id,resolveDisabled:u=>u.dataRef.current.disabled});return{...t,...o,searchQuery:"",activeItemIndex:s,activationTrigger:(a=i.trigger)!=null?a:1}},[3]:(t,i)=>{let s=t.searchQuery!==""?0:1,a=t.searchQuery+i.value.toLowerCase(),n=(t.activeItemIndex!==null?t.items.slice(t.activeItemIndex+s).concat(t.items.slice(0,t.activeItemIndex+s)):t.items).find(d=>{var l;return((l=d.dataRef.current.textValue)==null?void 0:l.startsWith(a))&&!d.dataRef.current.disabled}),e=n?t.items.indexOf(n):-1;return e===-1||e===t.activeItemIndex?{...t,searchQuery:a}:{...t,searchQuery:a,activeItemIndex:e,activationTrigger:1}},[4](t){return t.searchQuery===""?t:{...t,searchQuery:"",searchActiveItemIndex:null}},[5]:(t,i)=>{let o=U(t,s=>[...s,{id:i.id,dataRef:i.dataRef}]);return{...t,...o}},[6]:(t,i)=>{let o=U(t,s=>{let a=s.findIndex(u=>u.id===i.id);return a!==-1&&s.splice(a,1),s});return{...t,...o,activationTrigger:1}}},G=q(null);G.displayName="MenuContext";function O(t){let i=J(G);if(i===null){let o=new Error(`<${t} /> is missing a parent <Menu /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(o,O),o}return i}function Te(t,i){return j(i.type,fe,t,i)}let ye=_,Ie=E(function(i,o){let s=X(Te,{menuState:1,buttonRef:N(),itemsRef:N(),items:[],searchQuery:"",activeItemIndex:null,activationTrigger:1}),[{menuState:a,itemsRef:u,buttonRef:n},e]=s,d=C(o);oe([n,u],(R,A)=>{var g;e({type:1}),ee(A,te.Loose)||(R.preventDefault(),(g=n.current)==null||g.focus())},a===0);let l=y(()=>{e({type:1})}),f=P(()=>({open:a===0,close:l}),[a,l]),M=i,T={ref:d};return K.createElement(G.Provider,{value:s},K.createElement(ue,{value:j(a,{[0]:w.Open,[1]:w.Closed})},x({ourProps:T,theirProps:M,slot:f,defaultTag:ye,name:"Menu"})))}),ge="button",Me=E(function(i,o){var g;let s=k(),{id:a=`headlessui-menu-button-${s}`,...u}=i,[n,e]=O("Menu.Button"),d=C(n.buttonRef,o