react-animated-select 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/trigger.js CHANGED
@@ -1,94 +1,99 @@
1
1
  "use client";
2
- import { jsxs as R, jsx as u } from "react/jsx-runtime";
3
- import { SelectConfigContext as N, SelectStateContext as F, SelectActionsContext as k } from "./state.js";
4
- import { optionDomId as W, flag as I, withClass as P, renderIcon as V, stopEvent as X, refocus as Y, optionContent as Z, dots as ee, followHeight as te } from "./utils.js";
5
- import { memo as O, useContext as g, useSyncExternalStore as oe, useState as $, useRef as D, useLayoutEffect as j, useEffect as ne } from "react";
6
- import { effectiveHighlight as re } from "./useSelectBehavior.js";
7
- import { toJSON as ie } from "./model.js";
8
- import { Presence as q, Collapse as E } from "./motion.js";
9
- const ae = [], le = () => {
10
- }, se = (b) => b == null ? "" : typeof b == "object" ? ie(b) ?? "" : String(b), ce = /* @__PURE__ */ O(function() {
11
- const { selectRef: m, multiple: o, name: s, form: p, required: c } = g(N), { selected: h, selectedIDs: x, disabled: f, fieldsetDisabled: n } = g(F), { reset: t, markInvalid: v } = g(k), a = D(null);
12
- ne(() => {
13
- const e = a.current?.getRootNode();
14
- let i;
15
- const y = (C) => {
16
- C.target === a.current?.form && (clearTimeout(i), i = setTimeout(() => C.defaultPrevented || t()));
2
+ import { jsxs as O, jsx as f } from "react/jsx-runtime";
3
+ import { SelectConfigContext as N, SelectStateContext as D, SelectActionsContext as j } from "./state.js";
4
+ import { optionDomId as Z, flag as w, withClass as $, renderIcon as P, stopEvent as ee, refocus as te, optionContent as oe, dots as ne, followHeight as re, watchMotion as ae, reducedMotion as ie } from "./utils.js";
5
+ import { memo as I, useContext as h, useSyncExternalStore as q, useState as E, useRef as T, useCallback as le, useLayoutEffect as V, useEffect as H } from "react";
6
+ import { effectiveHighlight as se } from "./useSelectBehavior.js";
7
+ import { toJSON as ce } from "./model.js";
8
+ import { Presence as L, Collapse as k } from "./motion.js";
9
+ const de = [], ue = () => {
10
+ }, fe = (v) => v == null ? "" : typeof v == "object" ? ce(v) ?? "" : String(v), me = /* @__PURE__ */ I(function() {
11
+ const { selectRef: m, multiple: t, name: i, form: l, required: g } = h(N), { selected: o, selectedIDs: d, disabled: e, fieldsetDisabled: p } = h(D), { reset: u, markInvalid: x } = h(j), y = T(null);
12
+ H(() => {
13
+ const r = y.current?.getRootNode();
14
+ let b;
15
+ const S = (R) => {
16
+ R.target === y.current?.form && (clearTimeout(b), b = setTimeout(() => R.defaultPrevented || u()));
17
17
  };
18
- return e?.addEventListener("reset", y, !0), () => {
19
- clearTimeout(i), e?.removeEventListener("reset", y, !0);
18
+ return r?.addEventListener("reset", S, !0), () => {
19
+ clearTimeout(b), r?.removeEventListener("reset", S, !0);
20
20
  };
21
- }, [t]);
22
- const d = o ? x : h ? [h] : ae, l = !c && (!s || o && !d.length), r = l || !d.length ? [""] : d.map((e) => se(e.original)), w = () => m.current?.focus();
23
- return r.map(
24
- (e, i) => /* @__PURE__ */ u("input", { className: "rac-input", ref: i ? void 0 : a, name: l ? void 0 : s, form: p, value: e, hidden: l, required: c && !i, disabled: f && !n, tabIndex: -1, "aria-hidden": "true", autoComplete: "off", onChange: le, onInvalid: v, onFocus: w }, i)
21
+ }, [u]);
22
+ const s = t ? d : o ? [o] : de, n = !g && (!i || t && !s.length), c = n || !s.length ? [""] : s.map((r) => fe(r.original)), a = () => m.current?.focus();
23
+ return c.map(
24
+ (r, b) => /* @__PURE__ */ f("input", { className: "rac-input", ref: b ? void 0 : y, name: n ? void 0 : i, form: l, value: r, hidden: n, required: g && !b, disabled: e && !p, tabIndex: -1, "aria-hidden": "true", autoComplete: "off", onChange: ue, onInvalid: x, onFocus: a }, b)
25
25
  );
26
- }), de = /* @__PURE__ */ O(function({ hidden: m, settle: o }) {
27
- const { multiple: s, selectedText: p, valueAsOption: c, renderOption: h, texts: x } = g(N), { selectedIDs: f, title: n, valueOption: t, hasActualValue: v, loading: a, error: d } = g(F), [l] = $(() => /* @__PURE__ */ new Set()), r = c && t ? Z(t, h, !0) : null, w = a && !d && !v, e = s && f.length > 0 && !p, i = `${e ? "picked" : r ? t.id : n}${w ? "-loading" : ""}`, y = t?.type === "boolean" ? String(t.raw) : void 0;
28
- return /* @__PURE__ */ u(q, { children: !m && /* @__PURE__ */ R(E, { axis: "x", fade: !0, group: l, className: P("rac-title", r && t.className), style: r ? t.style : void 0, "data-bool": y, onEntered: o, onExited: o, children: [
29
- r ? /* @__PURE__ */ u("div", { className: "rac-option-jsx", children: r }) : e ? f.map((C) => C.name || x.emptyOption).join(", ") : n,
30
- w && ee
31
- ] }, i) });
32
- }), ue = /* @__PURE__ */ O(function() {
33
- const { selectRef: m, multiple: o, valueAsOption: s, duration: p, easing: c, ext: h } = g(N), x = o ? h.Value : void 0, f = D(null), [n, t] = $(s);
34
- s && !n && t(!0);
35
- const v = D({ duration: p, easing: c });
36
- return j(() => {
37
- v.current = { duration: p, easing: c };
38
- }, [p, c]), j(() => {
39
- const a = f.current;
40
- if (!n || o || !a || typeof ResizeObserver > "u") return;
41
- const d = { value: 0, root: "", anim: null }, l = new ResizeObserver(() => te(m.current, d, a.offsetHeight, v.current.duration, v.current.easing));
42
- return l.observe(a), () => {
43
- l.disconnect(), d.anim?.cancel();
44
- };
45
- }, [n, o, m]), x ? /* @__PURE__ */ u(x, {}) : /* @__PURE__ */ u("div", { className: "rac-value", ref: f, children: /* @__PURE__ */ u(de, {}) });
46
- }), xe = /* @__PURE__ */ O(function() {
47
- const { selectRef: m, selectId: o, highlightStore: s, className: p, style: c, duration: h, easing: x, deleteInline: f, icons: n, texts: t, placeholder: v, id: a, required: d, popup: l, attrs: r } = g(N), { visibility: w, active: e, hasActualValue: i, deleting: y, loading: C, loadPending: H, error: T, invalid: A, normalizedOptions: z } = g(F), { handleBlur: B, handleFocus: K, handleKeyDown: L, toggleVisibility: G, clear: J } = g(k), M = oe(s.subscribe, () => {
48
- const S = w ? z[re(s.get())] : null;
49
- return S ? W(o, S.id) : void 0;
26
+ }), z = /* @__PURE__ */ I(function({ hidden: m, settle: t, wait: i, children: l }) {
27
+ const { valueAsOption: g, renderOption: o } = h(N), { title: d, valueOption: e, hasActualValue: p, loading: u, error: x } = h(D), [y] = E(() => /* @__PURE__ */ new Set()), s = g && e ? oe(e, o, !0) : null, n = u && !x && !p, c = `${s ? e.id : d}${n ? "-loading" : ""}`, a = e?.type === "boolean" ? String(e.raw) : void 0;
28
+ return /* @__PURE__ */ O(L, { wait: i, children: [
29
+ !m && /* @__PURE__ */ O(k, { axis: "x", fade: !0, group: y, className: $("rac-title", s && e.className), style: s ? e.style : void 0, "data-bool": a, onEntered: t, onExited: t, children: [
30
+ s ? /* @__PURE__ */ f("div", { className: "rac-option-jsx", children: s }) : d,
31
+ n && ne
32
+ ] }, c),
33
+ l
34
+ ] });
35
+ }), pe = /* @__PURE__ */ I(function() {
36
+ const { selectedText: m, duration: t, texts: i } = h(N), { selectedIDs: l } = h(D), g = q(ae, ie, () => !1), o = l.length > 0 && !m;
37
+ return /* @__PURE__ */ f(z, { hidden: o, wait: t > 0 && !g, children: o && l.map((d, e) => /* @__PURE__ */ f(k, { as: "span", axis: "none", exitAxis: "x", fade: !0, className: "rac-pick", "data-last": w(e === l.length - 1), children: d.name || i.emptyOption }, d.id)) });
38
+ }), he = /* @__PURE__ */ I(function() {
39
+ const { selectRef: m, multiple: t, duration: i, easing: l, ext: g } = h(N), o = t ? g.Value : void 0, d = T(null), [e, p] = E({ Slot: o, multiple: t, enter: !1, left: !1 });
40
+ let u = e;
41
+ (o ? e.Slot !== o || e.left : e.Slot && (!t || e.left)) ? u = { Slot: o, multiple: t, enter: !!o && !e.Slot && e.multiple, left: !1 } : e.multiple !== t && (u = { ...e, multiple: t }), u !== e && p(u);
42
+ const { Slot: x, enter: y } = u, s = le(() => p((a) => a.left ? a : { ...a, left: !0 }), []), [n] = E(() => ({ value: 0, root: "", anim: null })), c = T({ duration: i, easing: l });
43
+ return V(() => {
44
+ c.current = { duration: i, easing: l };
45
+ }, [i, l]), V(() => {
46
+ const a = d.current;
47
+ if (!a || typeof ResizeObserver > "u") return;
48
+ const r = new ResizeObserver(() => re(m.current, n, a.offsetHeight, c.current.duration, c.current.easing));
49
+ return r.observe(a), () => r.disconnect();
50
+ }, [x, m, n]), H(() => () => n.anim?.cancel(), [n]), x ? /* @__PURE__ */ f(x, { height: n, enter: y, leaving: !o, onLeft: s }) : /* @__PURE__ */ f("div", { className: "rac-value", ref: d, children: t ? /* @__PURE__ */ f(pe, {}) : /* @__PURE__ */ f(z, {}) });
51
+ }), Ce = /* @__PURE__ */ I(function() {
52
+ const { selectRef: m, selectId: t, highlightStore: i, className: l, style: g, duration: o, easing: d, deleteInline: e, icons: p, texts: u, placeholder: x, id: y, required: s, popup: n, attrs: c } = h(N), { visibility: a, active: r, hasActualValue: b, deleting: S, loading: R, loadPending: B, error: A, invalid: F, normalizedOptions: K } = h(D), { handleBlur: M, handleFocus: G, handleKeyDown: J, toggleVisibility: _, clear: Q } = h(j), U = q(i.subscribe, () => {
53
+ const C = a ? K[se(i.get())] : null;
54
+ return C ? Z(t, C.id) : void 0;
50
55
  }, () => {
51
- }), _ = !!n.clear && i && e && !y, Q = !!n.arrow && e && !y && l, U = l ? { role: "combobox", "aria-haspopup": "listbox", "aria-controls": `${o}-listbox`, "aria-expanded": w, "aria-activedescendant": M, "aria-required": d || void 0 } : { role: "group" };
52
- return /* @__PURE__ */ R(
56
+ }), W = !!p.clear && b && r && !S, X = !!p.arrow && r && !S && n, Y = n ? { role: "combobox", "aria-haspopup": "listbox", "aria-controls": `${t}-listbox`, "aria-expanded": a, "aria-activedescendant": U, "aria-required": s || void 0 } : { role: "group" };
57
+ return /* @__PURE__ */ O(
53
58
  "div",
54
59
  {
55
- ...r,
56
- id: a,
60
+ ...c,
61
+ id: y,
57
62
  ref: m,
58
- style: { "--rac-duration": `${h}ms`, "--rac-ease": x, ...c },
59
- className: P("rac-select", p),
60
- ...e && {
61
- onKeyDown: L,
62
- onFocus: K,
63
- onClick: G,
64
- onBlur: B
63
+ style: { "--rac-duration": `${o}ms`, "--rac-ease": d, ...g },
64
+ className: $("rac-select", l),
65
+ ...r && {
66
+ onKeyDown: J,
67
+ onFocus: G,
68
+ onClick: _,
69
+ onBlur: M
65
70
  },
66
- ...U,
67
- "aria-disabled": !e,
68
- tabIndex: e ? 0 : -1,
69
- "aria-busy": C || H || void 0,
70
- "aria-label": r["aria-label"] ?? (r["aria-labelledby"] ? void 0 : v),
71
- "aria-invalid": A || r["aria-invalid"],
72
- "aria-describedby": [r["aria-describedby"], T && `${o}-error`].filter(Boolean).join(" ") || void 0,
73
- "data-inline-delete": I(f),
74
- "data-empty": I(!i),
75
- "data-deleting": I(y),
76
- "data-error": I(T),
77
- "data-invalid": I(A),
71
+ ...Y,
72
+ "aria-disabled": !r,
73
+ tabIndex: r ? 0 : -1,
74
+ "aria-busy": R || B || void 0,
75
+ "aria-label": c["aria-label"] ?? (c["aria-labelledby"] ? void 0 : x),
76
+ "aria-invalid": F || c["aria-invalid"],
77
+ "aria-describedby": [c["aria-describedby"], A && `${t}-error`].filter(Boolean).join(" ") || void 0,
78
+ "data-inline-delete": w(e),
79
+ "data-empty": w(!b),
80
+ "data-deleting": w(S),
81
+ "data-error": w(A),
82
+ "data-invalid": w(F),
78
83
  children: [
79
- /* @__PURE__ */ u(ue, {}),
80
- /* @__PURE__ */ R(q, { children: [
81
- _ && /* @__PURE__ */ u(E, { as: "button", type: "button", axis: "x", className: "rac-clear", tabIndex: -1, "aria-label": t.clear, onMouseDown: X, onClick: (S) => {
82
- Y(S, m), J(S);
83
- }, children: V(n.clear) }, "clear"),
84
- Q && /* @__PURE__ */ u(E, { axis: "x", className: "rac-arrow", "aria-hidden": "true", children: V(n.arrow) }, "arrow")
84
+ /* @__PURE__ */ f(he, {}),
85
+ /* @__PURE__ */ O(L, { children: [
86
+ W && /* @__PURE__ */ f(k, { as: "button", type: "button", axis: "x", className: "rac-clear", tabIndex: -1, "aria-label": u.clear, onMouseDown: ee, onClick: (C) => {
87
+ te(C, m), Q(C);
88
+ }, children: P(p.clear) }, "clear"),
89
+ X && /* @__PURE__ */ f(k, { axis: "x", className: "rac-arrow", "aria-hidden": "true", children: P(p.arrow) }, "arrow")
85
90
  ] }),
86
- /* @__PURE__ */ u(ce, {})
91
+ /* @__PURE__ */ f(me, {})
87
92
  ]
88
93
  }
89
94
  );
90
95
  });
91
96
  export {
92
- de as Title,
93
- xe as default
97
+ z as Title,
98
+ Ce as default
94
99
  };
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react"),F=require("react-dom"),R=require("./state.cjs"),g=require("./utils.cjs"),J=require("./model.cjs"),o=require("./chipGeometry.cjs"),Q=.25,U={hoverId:null,swipedId:null,held:!1,breaks:o.NONE},G=(i,a)=>{const u=new Set(i.map(l=>l.id)),c=new Map(a.map(([l,v])=>[v.id,l])),p=a.filter(([,l])=>!u.has(l.id)),E=new Set(a.map(([l])=>l));return i.map(l=>{if(c.has(l.id))return[c.get(l.id),l];const v=p.findIndex(([,k])=>J.sameValue(k.original,l.original));if(v>=0)return[p.splice(v,1)[0][0],l];let w=l.id;for(;E.has(w);)w+="~";return E.add(w),[w,l]})},X=i=>{const[a,u]=n.useState(()=>({chips:i,keyed:G(i,[])}));if(a.chips===i)return a.keyed;const c=G(i,a.keyed);return u({chips:i,keyed:c}),c};function Y(i){const{selectRef:a,duration:u,easing:c,deleteInline:p,deleteAlways:E,icons:l,valueAsOption:v,renderOption:w}=n.useContext(R.SelectConfigContext),{selectedIDs:k,deleting:C,visibility:B,active:L}=n.useContext(R.SelectStateContext),{setDeleting:q,setVisibility:H}=n.useContext(R.SelectActionsContext),[j,V]=n.useState(0),[s]=n.useState(()=>R.createStore(U)),[W]=n.useState(()=>new Set),A=n.useSyncExternalStore(s.subscribe,()=>s.get().held,()=>!1),O=n.useSyncExternalStore(s.subscribe,()=>s.get().breaks,()=>o.NONE),_=n.useSyncExternalStore(g.watchMotion,g.reducedMotion,()=>!1),S=n.useRef({rows:null,reserve:0,chips:i,swap:!1,duration:u,easing:c}),m=n.useRef(null),h=n.useRef({value:0,root:"",anim:null}),d=n.useRef(null),D=n.useRef(null),N=p&&!E&&!C&&!!l.remove,K=N?j+Q:0,z=N&&!j&&i.length>0,x=v?w?2:1:0,I=n.useEffectEvent((e,t)=>{const r=d.current,f=S.current;if(!r)return;const b=o.isResizing(r);if(b&&t)return;e&&s.get().held&&o.freeze(s,r,f);const y=f.swap?f.duration/2:f.duration;b?f.fit!=null&&g.followHeight(a.current,h.current,f.fit,y,f.easing,r):(t||h.current.value)&&g.followHeight(a.current,h.current,r.offsetHeight,y,f.easing),s.get().held||s.set({breaks:o.restBreaks(s,r,f.chips,f.reserve)})});n.useLayoutEffect(()=>{Object.assign(S.current,{duration:u,easing:c})},[u,c]),n.useLayoutEffect(()=>{const e=S.current,t=d.current,r=e.chips!==i;Object.assign(e,{chips:i,reserve:K}),r&&t&&o.isBusy(t)&&o.freeze(s,t,e),I(!r)},[i,K,s]),n.useLayoutEffect(()=>{const e=()=>I(!0,!0),t=typeof ResizeObserver>"u"?null:new ResizeObserver(e);return t?t.observe(d.current):e(),document.fonts?.addEventListener("loadingdone",e),()=>{t?.disconnect(),document.fonts?.removeEventListener("loadingdone",e)}},[]);const M=n.useCallback(()=>{const e=d.current;if(!e||!s.get().held||o.isBusy(e))return;const t=S.current,r=o.slotsOf(e),f=r.map(y=>y.getBoundingClientRect()),b=o.restBreaks(s,e,t.chips,t.reserve);Object.assign(t,{rows:null,floor:null,fit:null}),F.flushSync(()=>s.set({held:!1,breaks:b})),t.swap=o.isBusy(e),t.swap&&F.flushSync(()=>o.freeze(s,e,t)),g.reducedMotion()||r.forEach((y,T)=>y.isConnected&&o.flip(y,f[T],t.swap?u/2:u,c))},[s,u,c]),P=n.useCallback(e=>{d.current&&(m.current=o.snapOf(d.current,e))},[]);return n.useLayoutEffect(()=>{const e=d.current,t=S.current;if(!e||!m.current||!u||g.reducedMotion())return;const r=o.resizesOf(m.current);r.length&&(t.floor=new Map(r.map(({id:f,from:b})=>[f,b.width])),o.freeze(s,e,t),g.followHeight(a.current,h.current,t.fit,u,c,e),o.resize(r,u,c,()=>{M(),I(!1,!0)}))},[x,s,u,c,a,M]),n.useLayoutEffect(()=>{const e=m.current;!e||O!==s.get().breaks||(m.current=null,g.reducedMotion()||e.forEach((t,r)=>r.isConnected&&o.flip(r,t,u,c)))},[O,i,x,s,u,c]),n.useLayoutEffect(()=>{const e=d.current,t=S.current;!e||!h.current.value||o.isResizing(e)||g.followHeight(a.current,h.current,e.offsetHeight,t.swap?t.duration/2:t.duration,t.easing)},[O,A,a]),n.useEffect(()=>()=>h.current.anim?.cancel(),[]),n.useEffect(()=>{C&&B&&H(!1),C&&(!k.length||!L)&&q(!1),s.set({hoverId:null,...!L&&{swipedId:null}})},[k.length,C,B,L,q,H,s]),n.useLayoutEffect(()=>{const e=D.current;if(!z||!e)return;const t=getComputedStyle(e);V(e.getBoundingClientRect().width+parseFloat(t.marginLeft)+parseFloat(t.marginRight))},[z]),{chipStore:s,delGroup:W,valueRef:d,delIconRef:D,probe:z,settle:M,held:A,snapshot:P,look:x,wait:u>0&&!_}}exports.default=Y;exports.useChipKeys=X;
1
+ "use client";"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react"),V=require("react-dom"),R=require("./state.cjs"),f=require("./utils.cjs"),a=require("./chipGeometry.cjs"),i=require("./chipMotion.cjs"),se=.25,re={hoverId:null,swipedId:null,held:!1,breaks:a.NONE};function oe(y,{height:p,enter:J,leaving:C,onLeft:q}){const{selectRef:m,duration:u,easing:l,deleteInline:B,deleteAlways:j,icons:Q,valueAsOption:U,renderOption:X}=n.useContext(R.SelectConfigContext),{selectedIDs:F,deleting:v,visibility:H,active:O}=n.useContext(R.SelectStateContext),{setDeleting:D,setVisibility:N}=n.useContext(R.SelectActionsContext),[T,Y]=n.useState(0),[s]=n.useState(()=>R.createStore(re)),[Z]=n.useState(()=>new Set),G=n.useSyncExternalStore(s.subscribe,()=>s.get().held,()=>!1),L=n.useSyncExternalStore(s.subscribe,()=>s.get().breaks,()=>a.NONE),$=n.useSyncExternalStore(f.watchMotion,f.reducedMotion,()=>!1),h=n.useRef({rows:null,reserve:0,chips:y,swap:!1,dress:!1,wide:!1,duration:u,easing:l}),w=n.useRef(null),k=n.useRef(null),d=n.useRef(null),W=n.useRef(null),[z,ee]=n.useState(()=>!!J&&u>0&&!f.reducedMotion()),M=C||z,_=!!Q.remove,K=B&&!j&&!v&&_,E=K&&!M?T+se:0,x=K&&!T&&y.length>0,A=M?3:U?X?2:1:0,g=_?(B||v?1:0)|(j||v?2:0):0,P=n.useRef(g),I=n.useEffectEvent((e,t)=>{const r=d.current,o=h.current;if(!r)return;const c=i.isResizing(r);c&&t||(e&&s.get().held&&a.freeze(s,r,o),c?o.fit!=null&&f.followHeight(m.current,p,o.fit,o.duration,o.easing,r):(t||p.value)&&f.followHeight(m.current,p,r.offsetHeight,o.duration,o.easing),s.get().held||s.set({breaks:a.restBreaks(s,r,o.chips,o.reserve)}))}),S=n.useCallback(()=>{const e=d.current,t=h.current;if(!e||!s.get().held||i.isBusy(e,t.dress))return;const r=a.slotsOf(e),o=r.map(b=>b.getBoundingClientRect()),c=a.restBreaks(s,e,t.chips,t.reserve);Object.assign(t,{rows:null,floor:null,fit:null,dress:!1,wide:!1}),V.flushSync(()=>s.set({held:!1,breaks:c})),t.swap=i.isBusy(e),t.swap&&V.flushSync(()=>a.freeze(s,e,t)),f.reducedMotion()||r.forEach((b,ne)=>b.isConnected&&i.flip(b,o[ne],t.swap?u/2:u,l))},[s,u,l]);n.useLayoutEffect(()=>{Object.assign(h.current,{duration:u,easing:l})},[u,l]),n.useLayoutEffect(()=>{const e=P.current,t=d.current,r=h.current;if(P.current=g,e===g||!t||!u||f.reducedMotion()||((e^g)&1&&i.shift(t,!!(g&1),u,l,S),!((e|g)&1)||!i.isBusy(t,!0)))return;const o=r.rows,c=o??a.snapRows(w.current);Object.assign(r,{dress:!0,wide:!1,rows:c,reserve:E}),c&&a.freeze(s,t,r),!(c&&r.rows.length<=c.length)&&(Object.assign(r,{wide:!0,rows:o}),a.freeze(s,t,r))},[g,E,s,u,l,S]),n.useLayoutEffect(()=>{const e=h.current,t=d.current,r=e.chips!==y;Object.assign(e,{chips:y,reserve:E}),r&&t&&i.isBusy(t,e.dress)&&a.freeze(s,t,e),I(!r)},[y,E,s]),n.useLayoutEffect(()=>{const e=()=>I(!0,!0),t=typeof ResizeObserver>"u"?null:new ResizeObserver(e);return t?t.observe(d.current):e(),document.fonts?.addEventListener("loadingdone",e),()=>{t?.disconnect(),document.fonts?.removeEventListener("loadingdone",e)}},[]);const te=n.useCallback(e=>{const t=d.current;t&&(w.current=i.snapOf(t,e),e&&(k.current=i.padOf(t)))},[]);return n.useLayoutEffect(()=>{const e=d.current,t=h.current,r=k.current;if(k.current=null,!e||!w.current||!u||f.reducedMotion())return;i.repad(e,r,u,l);const o=i.resizesOf(w.current);o.length&&(t.floor=new Map(o.map(({id:c,width:b})=>[c,b])),a.freeze(s,e,t),f.followHeight(m.current,p,t.fit,u,l,e),i.resize(o,u,l,()=>{S(),I(!1,!0)}))},[A,s,u,l,m,S,p]),n.useEffect(()=>{const e=z&&requestAnimationFrame(()=>ee(!1));return()=>cancelAnimationFrame(e)},[z]),n.useLayoutEffect(()=>{const e=d.current;if(!C)return;let t;const r=()=>s.get().held||!!e?.getAnimations({subtree:!0}).some(c=>c.playState==="running"&&c.effect?.getComputedTiming().endTime<1/0),o=()=>{r()?t=requestAnimationFrame(o):q()};return o(),()=>cancelAnimationFrame(t)},[C,q,s]),n.useLayoutEffect(()=>{const e=w.current;!e||L!==s.get().breaks||(w.current=null,f.reducedMotion()||e.forEach((t,r)=>r.isConnected&&i.flip(r,t,u,l)))},[L,y,A,g,s,u,l]),n.useLayoutEffect(()=>{const e=d.current,t=h.current;!e||!p.value||i.isResizing(e)||f.followHeight(m.current,p,e.offsetHeight,t.duration,t.easing)},[L,G,m,p]),n.useEffect(()=>{v&&H&&N(!1),v&&(!F.length||!O)&&D(!1),s.set({hoverId:null,...!O&&{swipedId:null}})},[F.length,v,H,O,D,N,s]),n.useLayoutEffect(()=>{const e=W.current;if(!x||!e)return;const t=getComputedStyle(e);Y(e.getBoundingClientRect().width+parseFloat(t.marginLeft)+parseFloat(t.marginRight))},[x]),{chipStore:s,delGroup:Z,valueRef:d,delIconRef:W,probe:x,settle:S,held:G,snapshot:te,look:A,dress:g,plain:M,wait:u>0&&!$}}exports.default=oe;
@@ -1,78 +1,77 @@
1
1
  "use client";
2
- import { useContext as j, useState as I, useSyncExternalStore as D, useRef as b, useEffectEvent as se, useLayoutEffect as g, useCallback as P, useEffect as Q } from "react";
3
- import { flushSync as T } from "react-dom";
4
- import { SelectConfigContext as oe, SelectStateContext as le, SelectActionsContext as ie, createStore as ce } from "./state.js";
5
- import { watchMotion as ue, reducedMotion as R, followHeight as O } from "./utils.js";
6
- import { sameValue as ae } from "./model.js";
7
- import { NONE as _, isResizing as U, freeze as E, restBreaks as X, isBusy as H, slotsOf as fe, flip as Y, snapOf as de, resizesOf as ge, resize as he } from "./chipGeometry.js";
8
- const me = 0.25, pe = { hoverId: null, swipedId: null, held: !1, breaks: _ }, Z = (i, c) => {
9
- const s = new Set(i.map((o) => o.id)), l = new Map(c.map(([o, h]) => [h.id, o])), y = c.filter(([, o]) => !s.has(o.id)), k = new Set(c.map(([o]) => o));
10
- return i.map((o) => {
11
- if (l.has(o.id)) return [l.get(o.id), o];
12
- const h = y.findIndex(([, S]) => ae(S.original, o.original));
13
- if (h >= 0) return [y.splice(h, 1)[0][0], o];
14
- let m = o.id;
15
- for (; k.has(m); ) m += "~";
16
- return k.add(m), [m, o];
17
- });
18
- }, Ce = (i) => {
19
- const [c, s] = I(() => ({ chips: i, keyed: Z(i, []) }));
20
- if (c.chips === i) return c.keyed;
21
- const l = Z(i, c.keyed);
22
- return s({ chips: i, keyed: l }), l;
23
- };
24
- function Re(i) {
25
- const { selectRef: c, duration: s, easing: l, deleteInline: y, deleteAlways: k, icons: o, valueAsOption: h, renderOption: m } = j(oe), { selectedIDs: S, deleting: C, visibility: F, active: x } = j(le), { setDeleting: K, setVisibility: N } = j(ie), [V, $] = I(0), [n] = I(() => ce(pe)), [ee] = I(() => /* @__PURE__ */ new Set()), W = D(n.subscribe, () => n.get().held, () => !1), z = D(n.subscribe, () => n.get().breaks, () => _), te = D(ue, R, () => !1), p = b({ rows: null, reserve: 0, chips: i, swap: !1, duration: s, easing: l }), w = b(null), f = b({ value: 0, root: "", anim: null }), a = b(null), G = b(null), q = y && !k && !C && !!o.remove, J = q ? V + me : 0, L = q && !V && i.length > 0, A = h ? m ? 2 : 1 : 0, B = se((e, t) => {
26
- const r = a.current, u = p.current;
2
+ import { useContext as D, useState as C, useSyncExternalStore as H, useRef as w, useEffectEvent as me, useCallback as Y, useLayoutEffect as a, useEffect as Z } from "react";
3
+ import { flushSync as _ } from "react-dom";
4
+ import { SelectConfigContext as ge, SelectStateContext as pe, SelectActionsContext as ve, createStore as he } from "./state.js";
5
+ import { watchMotion as we, reducedMotion as b, followHeight as O } from "./utils.js";
6
+ import { NONE as ne, freeze as y, restBreaks as $, slotsOf as be, snapRows as ye } from "./chipGeometry.js";
7
+ import { isResizing as ee, isBusy as E, flip as te, shift as Re, snapOf as Se, padOf as Ce, repad as Oe, resizesOf as Ee, resize as ke } from "./chipMotion.js";
8
+ const Ae = 0.25, Ie = { hoverId: null, swipedId: null, held: !1, breaks: ne };
9
+ function De(m, { height: f, enter: re, leaving: k, onLeft: M }) {
10
+ const { selectRef: g, duration: o, easing: l, deleteInline: q, deleteAlways: N, icons: se, valueAsOption: oe, renderOption: ce } = D(ge), { selectedIDs: T, deleting: p, visibility: W, active: A } = D(pe), { setDeleting: G, setVisibility: K } = D(ve), [V, le] = C(0), [n] = C(() => he(Ie)), [ie] = C(() => /* @__PURE__ */ new Set()), J = H(n.subscribe, () => n.get().held, () => !1), I = H(n.subscribe, () => n.get().breaks, () => ne), ue = H(we, b, () => !1), d = w({ rows: null, reserve: 0, chips: m, swap: !1, dress: !1, wide: !1, duration: o, easing: l }), v = w(null), x = w(null), i = w(null), P = w(null), [z, ae] = C(() => !!re && o > 0 && !b()), F = k || z, Q = !!se.remove, U = q && !N && !p && Q, S = U && !F ? V + Ae : 0, j = U && !V && m.length > 0, L = F ? 3 : oe ? ce ? 2 : 1 : 0, u = Q ? (q || p ? 1 : 0) | (N || p ? 2 : 0) : 0, X = w(u), B = me((e, t) => {
11
+ const r = i.current, s = d.current;
27
12
  if (!r) return;
28
- const v = U(r);
29
- if (v && t) return;
30
- e && n.get().held && E(n, r, u);
31
- const d = u.swap ? u.duration / 2 : u.duration;
32
- v ? u.fit != null && O(c.current, f.current, u.fit, d, u.easing, r) : (t || f.current.value) && O(c.current, f.current, r.offsetHeight, d, u.easing), n.get().held || n.set({ breaks: X(n, r, u.chips, u.reserve) });
33
- });
34
- g(() => {
35
- Object.assign(p.current, { duration: s, easing: l });
36
- }, [s, l]), g(() => {
37
- const e = p.current, t = a.current, r = e.chips !== i;
38
- Object.assign(e, { chips: i, reserve: J }), r && t && H(t) && E(n, t, e), B(!r);
39
- }, [i, J, n]), g(() => {
13
+ const c = ee(r);
14
+ c && t || (e && n.get().held && y(n, r, s), c ? s.fit != null && O(g.current, f, s.fit, s.duration, s.easing, r) : (t || f.value) && O(g.current, f, r.offsetHeight, s.duration, s.easing), n.get().held || n.set({ breaks: $(n, r, s.chips, s.reserve) }));
15
+ }), R = Y(() => {
16
+ const e = i.current, t = d.current;
17
+ if (!e || !n.get().held || E(e, t.dress)) return;
18
+ const r = be(e), s = r.map((h) => h.getBoundingClientRect()), c = $(n, e, t.chips, t.reserve);
19
+ Object.assign(t, { rows: null, floor: null, fit: null, dress: !1, wide: !1 }), _(() => n.set({ held: !1, breaks: c })), t.swap = E(e), t.swap && _(() => y(n, e, t)), b() || r.forEach((h, de) => h.isConnected && te(h, s[de], t.swap ? o / 2 : o, l));
20
+ }, [n, o, l]);
21
+ a(() => {
22
+ Object.assign(d.current, { duration: o, easing: l });
23
+ }, [o, l]), a(() => {
24
+ const e = X.current, t = i.current, r = d.current;
25
+ if (X.current = u, e === u || !t || !o || b() || ((e ^ u) & 1 && Re(t, !!(u & 1), o, l, R), !((e | u) & 1) || !E(t, !0))) return;
26
+ const s = r.rows, c = s ?? ye(v.current);
27
+ Object.assign(r, { dress: !0, wide: !1, rows: c, reserve: S }), c && y(n, t, r), !(c && r.rows.length <= c.length) && (Object.assign(r, { wide: !0, rows: s }), y(n, t, r));
28
+ }, [u, S, n, o, l, R]), a(() => {
29
+ const e = d.current, t = i.current, r = e.chips !== m;
30
+ Object.assign(e, { chips: m, reserve: S }), r && t && E(t, e.dress) && y(n, t, e), B(!r);
31
+ }, [m, S, n]), a(() => {
40
32
  const e = () => B(!0, !0), t = typeof ResizeObserver > "u" ? null : new ResizeObserver(e);
41
- return t ? t.observe(a.current) : e(), document.fonts?.addEventListener("loadingdone", e), () => {
33
+ return t ? t.observe(i.current) : e(), document.fonts?.addEventListener("loadingdone", e), () => {
42
34
  t?.disconnect(), document.fonts?.removeEventListener("loadingdone", e);
43
35
  };
44
36
  }, []);
45
- const M = P(() => {
46
- const e = a.current;
47
- if (!e || !n.get().held || H(e)) return;
48
- const t = p.current, r = fe(e), u = r.map((d) => d.getBoundingClientRect()), v = X(n, e, t.chips, t.reserve);
49
- Object.assign(t, { rows: null, floor: null, fit: null }), T(() => n.set({ held: !1, breaks: v })), t.swap = H(e), t.swap && T(() => E(n, e, t)), R() || r.forEach((d, re) => d.isConnected && Y(d, u[re], t.swap ? s / 2 : s, l));
50
- }, [n, s, l]), ne = P((e) => {
51
- a.current && (w.current = de(a.current, e));
37
+ const fe = Y((e) => {
38
+ const t = i.current;
39
+ t && (v.current = Se(t, e), e && (x.current = Ce(t)));
52
40
  }, []);
53
- return g(() => {
54
- const e = a.current, t = p.current;
55
- if (!e || !w.current || !s || R()) return;
56
- const r = ge(w.current);
57
- r.length && (t.floor = new Map(r.map(({ id: u, from: v }) => [u, v.width])), E(n, e, t), O(c.current, f.current, t.fit, s, l, e), he(r, s, l, () => {
58
- M(), B(!1, !0);
41
+ return a(() => {
42
+ const e = i.current, t = d.current, r = x.current;
43
+ if (x.current = null, !e || !v.current || !o || b()) return;
44
+ Oe(e, r, o, l);
45
+ const s = Ee(v.current);
46
+ s.length && (t.floor = new Map(s.map(({ id: c, width: h }) => [c, h])), y(n, e, t), O(g.current, f, t.fit, o, l, e), ke(s, o, l, () => {
47
+ R(), B(!1, !0);
59
48
  }));
60
- }, [A, n, s, l, c, M]), g(() => {
61
- const e = w.current;
62
- !e || z !== n.get().breaks || (w.current = null, R() || e.forEach((t, r) => r.isConnected && Y(r, t, s, l)));
63
- }, [z, i, A, n, s, l]), g(() => {
64
- const e = a.current, t = p.current;
65
- !e || !f.current.value || U(e) || O(c.current, f.current, e.offsetHeight, t.swap ? t.duration / 2 : t.duration, t.easing);
66
- }, [z, W, c]), Q(() => () => f.current.anim?.cancel(), []), Q(() => {
67
- C && F && N(!1), C && (!S.length || !x) && K(!1), n.set({ hoverId: null, ...!x && { swipedId: null } });
68
- }, [S.length, C, F, x, K, N, n]), g(() => {
69
- const e = G.current;
70
- if (!L || !e) return;
49
+ }, [L, n, o, l, g, R, f]), Z(() => {
50
+ const e = z && requestAnimationFrame(() => ae(!1));
51
+ return () => cancelAnimationFrame(e);
52
+ }, [z]), a(() => {
53
+ const e = i.current;
54
+ if (!k) return;
55
+ let t;
56
+ const r = () => n.get().held || !!e?.getAnimations({ subtree: !0 }).some((c) => c.playState === "running" && c.effect?.getComputedTiming().endTime < 1 / 0), s = () => {
57
+ r() ? t = requestAnimationFrame(s) : M();
58
+ };
59
+ return s(), () => cancelAnimationFrame(t);
60
+ }, [k, M, n]), a(() => {
61
+ const e = v.current;
62
+ !e || I !== n.get().breaks || (v.current = null, b() || e.forEach((t, r) => r.isConnected && te(r, t, o, l)));
63
+ }, [I, m, L, u, n, o, l]), a(() => {
64
+ const e = i.current, t = d.current;
65
+ !e || !f.value || ee(e) || O(g.current, f, e.offsetHeight, t.duration, t.easing);
66
+ }, [I, J, g, f]), Z(() => {
67
+ p && W && K(!1), p && (!T.length || !A) && G(!1), n.set({ hoverId: null, ...!A && { swipedId: null } });
68
+ }, [T.length, p, W, A, G, K, n]), a(() => {
69
+ const e = P.current;
70
+ if (!j || !e) return;
71
71
  const t = getComputedStyle(e);
72
- $(e.getBoundingClientRect().width + parseFloat(t.marginLeft) + parseFloat(t.marginRight));
73
- }, [L]), { chipStore: n, delGroup: ee, valueRef: a, delIconRef: G, probe: L, settle: M, held: W, snapshot: ne, look: A, wait: s > 0 && !te };
72
+ le(e.getBoundingClientRect().width + parseFloat(t.marginLeft) + parseFloat(t.marginRight));
73
+ }, [j]), { chipStore: n, delGroup: ie, valueRef: i, delIconRef: P, probe: j, settle: R, held: J, snapshot: fe, look: L, dress: u, plain: F, wait: o > 0 && !ue };
74
74
  }
75
75
  export {
76
- Re as default,
77
- Ce as useChipKeys
76
+ De as default
78
77
  };
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const h=require("react"),L=require("./state.cjs"),D=require("./utils.cjs"),S={index:-1,fallback:-1,ready:!1},F=({index:r,fallback:f})=>r===-1?f:r,U=10,C=500,V=new Set(["ArrowDown","ArrowUp","PageDown","PageUp","Home","End"]),_=(r,f)=>h.useMemo(()=>{const c=new Set(r.map(n=>n.group).filter(Boolean)),i=n=>!!n&&!n.disabled&&!n.loading&&(n.groupHeader?c.has(n.name):!n.group||f.has(n.group));return{reach:i,list:r.flatMap((n,o)=>i(n)?[o]:[])}},[r,f]),z=(r,f,c,i)=>{const n=r.length-1;if(n<0)return-1;if(c==="Home")return r[0];if(c==="End")return r[n];const o=c==="ArrowUp"||c==="PageUp"?-1:1,l=r.indexOf(f);if(l===-1)return r[o>0?0:n];const s=l+o*(c.startsWith("Page")?U:1);return s>=0&&s<=n?r[s]:c.startsWith("Page")||i?r[s<0?0:n]:r[(s+r.length)%r.length]},R=(r,f,c,i)=>{const n=[...i].every(u=>u===i[0]),o=n?i[0]:i,l=f.filter(u=>"original"in r[u]),s=Math.max(l.indexOf(c)+(n?1:0),0);for(let u=0;u<l.length;u++){const T=l[(s+u)%l.length];if(String(r[T].name).toLowerCase().startsWith(o))return T}return-1};function I({props:r,state:f,setState:c,normalizedOptions:i,expandedGroups:n,selected:o,selectedIDs:l,visibility:s,setVisibility:u,modelActions:T,highlightStore:w}){const{disabled:E,multiple:y,hasMore:P,loadButton:k,icons:x,onFocus:q,onBlur:M,onKeyDown:A}=r,{selectOption:B,clear:W,removeOption:p}=T,{deleting:v}=f,{reach:H,list:m}=_(i,n),d=h.useRef({lastWindowFocusTime:0,justFocused:!1,focusTimeout:null,query:"",queryTimeout:null}),j=h.useMemo(()=>{const e=new Set(y?l.map(a=>a.id):o?[o.id]:[]),t=e.size?i.findIndex(a=>e.has(a.id)&&H(a)):-1;return t!==-1?t:m.find(a=>!i[a].groupHeader)??m[0]??-1},[i,o,l,y,H,m]);h.useLayoutEffect(()=>{const{index:e}=w.get();w.set({fallback:j,index:e!==-1&&H(i[e])?e:-1})}),h.useEffect(()=>{const e=d.current,t=()=>e.lastWindowFocusTime=Date.now();return window.addEventListener("focus",t),()=>{window.removeEventListener("focus",t),clearTimeout(e.focusTimeout),clearTimeout(e.queryTimeout)}},[]);const b=e=>w.set({index:e}),K=(e,t)=>{const a=d.current;clearTimeout(a.queryTimeout),a.queryTimeout=setTimeout(()=>a.query="",C),a.query+=e.toLowerCase();const g=R(i,m,t,a.query);g!==-1&&b(g),s||u(!0)};return L.useStableActions({highlight:b,handleBlur:e=>{const t=e.relatedTarget?.closest(".rac-options");e.currentTarget.contains(e.relatedTarget)||t||(M?.(e),v&&c({deleting:!1}),s&&u(!1))},handleFocus:e=>{const t=e.currentTarget.contains(e.relatedTarget);t||q?.(e),!(t||e.target!==e.currentTarget||s||E||v||document.hidden||Date.now()-d.current.lastWindowFocusTime<100)&&(clearTimeout(d.current.focusTimeout),d.current.focusTimeout=setTimeout(()=>d.current.justFocused=!1,200),d.current.justFocused=!0,u(!0))},toggleVisibility:e=>{if(v){D.stopEvent(e),c({deleting:!1});return}E||d.current.justFocused||u(!s)},handleKeyDown:e=>{if(A?.(e),e.defaultPrevented||E||e.nativeEvent?.isComposing)return;const{key:t}=e;if(e.target!==e.currentTarget&&(t==="Enter"||t.length===1))return;const a=F(w.get());if(t.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(t!==" "||d.current.query)){e.preventDefault(),K(t,a);return}if(V.has(t)){e.preventDefault(),(s||t==="Home"||t==="End")&&b(z(m,a,t,!k&&P)),s||u(!0);return}switch(t){case"Enter":case" ":e.preventDefault(),s?a!==-1&&i[a]&&(b(a),B(i[a],e)):u(!0);break;case"Escape":if(v)c({deleting:!1});else if(s)u(!1);else break;D.stopEvent(e);break;case"Tab":s&&u(!1);break;case"Backspace":case"Delete":{const g=l.at(-1);if(y&&t==="Backspace"&&x.remove&&g)p(g.id);else if(x.clear&&(o||g))W();else break;e.preventDefault();break}}}})}exports.default=I;exports.effectiveHighlight=F;exports.initialHighlight=S;
1
+ "use client";"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const T=require("react"),L=require("./state.cjs"),M=require("./utils.cjs"),S={index:-1,fallback:-1,ready:!1},D=({index:r,fallback:l})=>r===-1?l:r,U=10,C=500,V=new Set(["ArrowDown","ArrowUp","PageDown","PageUp","Home","End"]),_=(r,l)=>T.useMemo(()=>{const c=new Set(r.map(s=>s.group).filter(Boolean)),i=s=>!!s&&!s.disabled&&!s.loading&&(s.groupHeader?c.has(s.name):!s.group||l.has(s.group));return{reach:i,list:r.flatMap((s,f)=>i(s)?[f]:[])}},[r,l]),z=(r,l,c,i)=>{const s=r.length-1;if(s<0)return-1;if(c==="Home")return r[0];if(c==="End")return r[s];const f=c==="ArrowUp"||c==="PageUp"?-1:1,u=r.findIndex(h=>h>=l),a=(r[u]===l?u:(u===-1?r.length:u)-(f>0?1:0))+f*(c.startsWith("Page")?U:1);return a>=0&&a<=s?r[a]:c.startsWith("Page")||i?r[a<0?0:s]:r[(a+r.length)%r.length]},I=(r,l,c,i)=>{const s=[...i].every(a=>a===i[0]),f=s?i[0]:i,u=l.filter(a=>"original"in r[a]),o=Math.max(u.indexOf(c)+(s?1:0),0);for(let a=0;a<u.length;a++){const h=u[(o+a)%u.length];if(String(r[h].name).toLowerCase().startsWith(f))return h}return-1};function R({props:r,state:l,setState:c,normalizedOptions:i,expandedGroups:s,selected:f,selectedIDs:u,visibility:o,setVisibility:a,modelActions:h,highlightStore:w}){const{disabled:E,multiple:y,hasMore:F,loadButton:P,icons:H,onFocus:k,onBlur:q,onKeyDown:A}=r,{selectOption:B,clear:W,removeOption:j}=h,{deleting:v}=l,{reach:x,list:m}=_(i,s),d=T.useRef({lastWindowFocusTime:0,justFocused:!1,focusTimeout:null,query:"",queryTimeout:null,row:-1}),p=T.useMemo(()=>{const e=new Set(y?u.map(n=>n.id):f?[f.id]:[]),t=e.size?i.findIndex(n=>e.has(n.id)&&x(n)):-1;return t!==-1?t:m.find(n=>!i[n].groupHeader)??m[0]??-1},[i,f,u,y,x,m]);T.useLayoutEffect(()=>{const{index:e}=w.get(),{row:t}=d.current,n=e===-1||x(i[e])||i[e]?.loadMore?e:e===t?m.find(g=>g>t)??m.at(-1)??-1:-1;d.current.row=i[n]?.loadMore?n:-1,w.set({fallback:p,index:n})}),T.useEffect(()=>{const e=d.current,t=()=>e.lastWindowFocusTime=Date.now();return window.addEventListener("focus",t),()=>{window.removeEventListener("focus",t),clearTimeout(e.focusTimeout),clearTimeout(e.queryTimeout)}},[]);const b=e=>w.set({index:e}),K=(e,t)=>{const n=d.current;clearTimeout(n.queryTimeout),n.queryTimeout=setTimeout(()=>n.query="",C),n.query+=e.toLowerCase();const g=I(i,m,t,n.query);g!==-1&&b(g),o||a(!0)};return L.useStableActions({highlight:b,handleBlur:e=>{const t=e.relatedTarget?.closest(".rac-options");e.currentTarget.contains(e.relatedTarget)||t||(q?.(e),v&&c({deleting:!1}),o&&a(!1))},handleFocus:e=>{const t=e.currentTarget.contains(e.relatedTarget);t||k?.(e),!(t||e.target!==e.currentTarget||o||E||v||document.hidden||Date.now()-d.current.lastWindowFocusTime<100)&&(clearTimeout(d.current.focusTimeout),d.current.focusTimeout=setTimeout(()=>d.current.justFocused=!1,200),d.current.justFocused=!0,a(!0))},toggleVisibility:e=>{if(v){M.stopEvent(e),c({deleting:!1});return}E||d.current.justFocused||a(!o)},handleKeyDown:e=>{if(A?.(e),e.defaultPrevented||E||e.nativeEvent?.isComposing)return;const{key:t}=e;if(e.target!==e.currentTarget&&(t==="Enter"||t.length===1))return;const n=D(w.get());if(t.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(t!==" "||d.current.query)){e.preventDefault(),K(t,n);return}if(V.has(t)){e.preventDefault(),(o||t==="Home"||t==="End")&&b(z(m,n,t,!P&&F)),o||a(!0);return}switch(t){case"Enter":case" ":e.preventDefault(),o?n!==-1&&i[n]&&(b(n),B(i[n],e)):a(!0);break;case"Escape":if(v)c({deleting:!1});else if(o)a(!1);else break;M.stopEvent(e);break;case"Tab":o&&a(!1);break;case"Backspace":case"Delete":{const g=u.at(-1);if(y&&t==="Backspace"&&H.remove&&g)j(g.id);else if(H.clear&&(f||g))W();else break;e.preventDefault();break}}}})}exports.default=R;exports.effectiveHighlight=D;exports.initialHighlight=S;
@@ -2,98 +2,96 @@
2
2
  import { useMemo as F, useRef as L, useLayoutEffect as j, useEffect as U } from "react";
3
3
  import { useStableActions as C } from "./state.js";
4
4
  import { stopEvent as D } from "./utils.js";
5
- const Q = { index: -1, fallback: -1, ready: !1 }, S = ({ index: r, fallback: f }) => r === -1 ? f : r, V = 10, z = 500, R = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp", "PageDown", "PageUp", "Home", "End"]), I = (r, f) => F(() => {
6
- const u = new Set(r.map((n) => n.group).filter(Boolean)), i = (n) => !!n && !n.disabled && !n.loading && (n.groupHeader ? u.has(n.name) : !n.group || f.has(n.group));
7
- return { reach: i, list: r.flatMap((n, o) => i(n) ? [o] : []) };
8
- }, [r, f]), Y = (r, f, u, i) => {
9
- const n = r.length - 1;
10
- if (n < 0) return -1;
11
- if (u === "Home") return r[0];
12
- if (u === "End") return r[n];
13
- const o = u === "ArrowUp" || u === "PageUp" ? -1 : 1, l = r.indexOf(f);
14
- if (l === -1) return r[o > 0 ? 0 : n];
15
- const s = l + o * (u.startsWith("Page") ? V : 1);
16
- return s >= 0 && s <= n ? r[s] : u.startsWith("Page") || i ? r[s < 0 ? 0 : n] : r[(s + r.length) % r.length];
17
- }, _ = (r, f, u, i) => {
18
- const n = [...i].every((c) => c === i[0]), o = n ? i[0] : i, l = f.filter((c) => "original" in r[c]), s = Math.max(l.indexOf(u) + (n ? 1 : 0), 0);
19
- for (let c = 0; c < l.length; c++) {
20
- const h = l[(s + c) % l.length];
21
- if (String(r[h].name).toLowerCase().startsWith(o)) return h;
5
+ const Q = { index: -1, fallback: -1, ready: !1 }, S = ({ index: r, fallback: l }) => r === -1 ? l : r, V = 10, z = 500, I = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp", "PageDown", "PageUp", "Home", "End"]), R = (r, l) => F(() => {
6
+ const s = new Set(r.map((o) => o.group).filter(Boolean)), c = (o) => !!o && !o.disabled && !o.loading && (o.groupHeader ? s.has(o.name) : !o.group || l.has(o.group));
7
+ return { reach: c, list: r.flatMap((o, f) => c(o) ? [f] : []) };
8
+ }, [r, l]), Y = (r, l, s, c) => {
9
+ const o = r.length - 1;
10
+ if (o < 0) return -1;
11
+ if (s === "Home") return r[0];
12
+ if (s === "End") return r[o];
13
+ const f = s === "ArrowUp" || s === "PageUp" ? -1 : 1, i = r.findIndex((h) => h >= l), a = (r[i] === l ? i : (i === -1 ? r.length : i) - (f > 0 ? 1 : 0)) + f * (s.startsWith("Page") ? V : 1);
14
+ return a >= 0 && a <= o ? r[a] : s.startsWith("Page") || c ? r[a < 0 ? 0 : o] : r[(a + r.length) % r.length];
15
+ }, _ = (r, l, s, c) => {
16
+ const o = [...c].every((a) => a === c[0]), f = o ? c[0] : c, i = l.filter((a) => "original" in r[a]), u = Math.max(i.indexOf(s) + (o ? 1 : 0), 0);
17
+ for (let a = 0; a < i.length; a++) {
18
+ const h = i[(u + a) % i.length];
19
+ if (String(r[h].name).toLowerCase().startsWith(f)) return h;
22
20
  }
23
21
  return -1;
24
22
  };
25
- function X({ props: r, state: f, setState: u, normalizedOptions: i, expandedGroups: n, selected: o, selectedIDs: l, visibility: s, setVisibility: c, modelActions: h, highlightStore: T }) {
26
- const { disabled: v, multiple: E, hasMore: k, loadButton: H, icons: x, onFocus: P, onBlur: p, onKeyDown: A } = r, { selectOption: B, clear: W, removeOption: q } = h, { deleting: w } = f, { reach: y, list: g } = I(i, n), d = L({ lastWindowFocusTime: 0, justFocused: !1, focusTimeout: null, query: "", queryTimeout: null }), M = F(() => {
27
- const e = new Set(E ? l.map((a) => a.id) : o ? [o.id] : []), t = e.size ? i.findIndex((a) => e.has(a.id) && y(a)) : -1;
28
- return t !== -1 ? t : g.find((a) => !i[a].groupHeader) ?? g[0] ?? -1;
29
- }, [i, o, l, E, y, g]);
23
+ function X({ props: r, state: l, setState: s, normalizedOptions: c, expandedGroups: o, selected: f, selectedIDs: i, visibility: u, setVisibility: a, modelActions: h, highlightStore: w }) {
24
+ const { disabled: v, multiple: E, hasMore: k, loadButton: H, icons: x, onFocus: P, onBlur: M, onKeyDown: p } = r, { selectOption: A, clear: B, removeOption: W } = h, { deleting: T } = l, { reach: y, list: g } = R(c, o), d = L({ lastWindowFocusTime: 0, justFocused: !1, focusTimeout: null, query: "", queryTimeout: null, row: -1 }), q = F(() => {
25
+ const e = new Set(E ? i.map((n) => n.id) : f ? [f.id] : []), t = e.size ? c.findIndex((n) => e.has(n.id) && y(n)) : -1;
26
+ return t !== -1 ? t : g.find((n) => !c[n].groupHeader) ?? g[0] ?? -1;
27
+ }, [c, f, i, E, y, g]);
30
28
  j(() => {
31
- const { index: e } = T.get();
32
- T.set({ fallback: M, index: e !== -1 && y(i[e]) ? e : -1 });
29
+ const { index: e } = w.get(), { row: t } = d.current, n = e === -1 || y(c[e]) || c[e]?.loadMore ? e : e === t ? g.find((m) => m > t) ?? g.at(-1) ?? -1 : -1;
30
+ d.current.row = c[n]?.loadMore ? n : -1, w.set({ fallback: q, index: n });
33
31
  }), U(() => {
34
32
  const e = d.current, t = () => e.lastWindowFocusTime = Date.now();
35
33
  return window.addEventListener("focus", t), () => {
36
34
  window.removeEventListener("focus", t), clearTimeout(e.focusTimeout), clearTimeout(e.queryTimeout);
37
35
  };
38
36
  }, []);
39
- const b = (e) => T.set({ index: e }), K = (e, t) => {
40
- const a = d.current;
41
- clearTimeout(a.queryTimeout), a.queryTimeout = setTimeout(() => a.query = "", z), a.query += e.toLowerCase();
42
- const m = _(i, g, t, a.query);
43
- m !== -1 && b(m), s || c(!0);
37
+ const b = (e) => w.set({ index: e }), K = (e, t) => {
38
+ const n = d.current;
39
+ clearTimeout(n.queryTimeout), n.queryTimeout = setTimeout(() => n.query = "", z), n.query += e.toLowerCase();
40
+ const m = _(c, g, t, n.query);
41
+ m !== -1 && b(m), u || a(!0);
44
42
  };
45
43
  return C({
46
44
  highlight: b,
47
45
  handleBlur: (e) => {
48
46
  const t = e.relatedTarget?.closest(".rac-options");
49
- e.currentTarget.contains(e.relatedTarget) || t || (p?.(e), w && u({ deleting: !1 }), s && c(!1));
47
+ e.currentTarget.contains(e.relatedTarget) || t || (M?.(e), T && s({ deleting: !1 }), u && a(!1));
50
48
  },
51
49
  // [DOC: select-behavior]
52
50
  handleFocus: (e) => {
53
51
  const t = e.currentTarget.contains(e.relatedTarget);
54
- t || P?.(e), !(t || e.target !== e.currentTarget || s || v || w || document.hidden || Date.now() - d.current.lastWindowFocusTime < 100) && (clearTimeout(d.current.focusTimeout), d.current.focusTimeout = setTimeout(() => d.current.justFocused = !1, 200), d.current.justFocused = !0, c(!0));
52
+ t || P?.(e), !(t || e.target !== e.currentTarget || u || v || T || document.hidden || Date.now() - d.current.lastWindowFocusTime < 100) && (clearTimeout(d.current.focusTimeout), d.current.focusTimeout = setTimeout(() => d.current.justFocused = !1, 200), d.current.justFocused = !0, a(!0));
55
53
  },
56
54
  toggleVisibility: (e) => {
57
- if (w) {
58
- D(e), u({ deleting: !1 });
55
+ if (T) {
56
+ D(e), s({ deleting: !1 });
59
57
  return;
60
58
  }
61
- v || d.current.justFocused || c(!s);
59
+ v || d.current.justFocused || a(!u);
62
60
  },
63
61
  handleKeyDown: (e) => {
64
- if (A?.(e), e.defaultPrevented || v || e.nativeEvent?.isComposing) return;
62
+ if (p?.(e), e.defaultPrevented || v || e.nativeEvent?.isComposing) return;
65
63
  const { key: t } = e;
66
64
  if (e.target !== e.currentTarget && (t === "Enter" || t.length === 1)) return;
67
- const a = S(T.get());
65
+ const n = S(w.get());
68
66
  if (t.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && (t !== " " || d.current.query)) {
69
- e.preventDefault(), K(t, a);
67
+ e.preventDefault(), K(t, n);
70
68
  return;
71
69
  }
72
- if (R.has(t)) {
73
- e.preventDefault(), (s || t === "Home" || t === "End") && b(Y(g, a, t, !H && k)), s || c(!0);
70
+ if (I.has(t)) {
71
+ e.preventDefault(), (u || t === "Home" || t === "End") && b(Y(g, n, t, !H && k)), u || a(!0);
74
72
  return;
75
73
  }
76
74
  switch (t) {
77
75
  case "Enter":
78
76
  case " ":
79
- e.preventDefault(), s ? a !== -1 && i[a] && (b(a), B(i[a], e)) : c(!0);
77
+ e.preventDefault(), u ? n !== -1 && c[n] && (b(n), A(c[n], e)) : a(!0);
80
78
  break;
81
79
  // [DOC: select-behavior]
82
80
  case "Escape":
83
- if (w) u({ deleting: !1 });
84
- else if (s) c(!1);
81
+ if (T) s({ deleting: !1 });
82
+ else if (u) a(!1);
85
83
  else break;
86
84
  D(e);
87
85
  break;
88
86
  case "Tab":
89
- s && c(!1);
87
+ u && a(!1);
90
88
  break;
91
89
  // [DOC: select-behavior]
92
90
  case "Backspace":
93
91
  case "Delete": {
94
- const m = l.at(-1);
95
- if (E && t === "Backspace" && x.remove && m) q(m.id);
96
- else if (x.clear && (o || m)) W();
92
+ const m = i.at(-1);
93
+ if (E && t === "Backspace" && x.remove && m) W(m.id);
94
+ else if (x.clear && (f || m)) B();
97
95
  else break;
98
96
  e.preventDefault();
99
97
  break;
package/dist/utils.cjs CHANGED
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),d=require("react"),p={XMark:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 320 512",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M310.6 361.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L160 301.3 54.6 406.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L114.7 256 9.4 150.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0L160 210.7 265.4 105.4c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3L205.3 256l105.3 105.4z"})}),ArrowUp:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 448 512",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M34.9 289.5l175.9-175.8c9.4-9.4 24.6-9.4 33.9 0L420.1 289.5c15.1 15.1 4.4 41-17 41H51.9c-21.4 0-32.1-25.9-17-41z"})}),Checkmark:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M20.285 6.708a1 1 0 0 0-1.414-1.416l-9.192 9.192-4.243-4.244a1 1 0 1 0-1.414 1.416l5 5a1 1 0 0 0 1.414 0l9.849-9.948z"})})},j=(t,e)=>{if(!t)return null;const i=(s={})=>({...e,...s,style:{...e?.style,...s?.style}});if(typeof t=="string")return n.jsx("img",{src:t,...i(),className:u("rac-icon",e?.className),alt:""});if(d.isValidElement(t))return d.cloneElement(t,i(t.props));if(typeof t=="function"||typeof t=="object"&&t.$$typeof){const s=t;return n.jsx(s,{...i()})}return null},v=(t,e)=>`${t}-${String(e).replace(/[^A-Za-z0-9-]/gu,i=>`_${i.codePointAt(0).toString(36)}_`)}`,m=t=>t?"":void 0,u=(t,e)=>e?`${t} ${e}`:t,y=(t,e,i)=>t.jsx||(e&&"original"in t?e(t.original,{selected:i,disabled:!!t.disabled}):null),E=n.jsxs("span",{className:"rac-dots","aria-hidden":"true",children:[n.jsx("i",{}),n.jsx("i",{}),n.jsx("i",{})]}),C=(()=>{try{return process.env.NODE_ENV!=="production"}catch{return!1}})(),h=new Set,M=(t,e)=>{!C||h.has(t)||(h.add(t),console.warn(`[react-animated-select] ${e}`))},b=t=>{t?.stopPropagation(),t?.preventDefault()},$=(t,e)=>{t?.currentTarget.contains(document.activeElement)&&e.current?.focus()},g="(prefers-reduced-motion: reduce)",f=()=>!!window.matchMedia?.(g).matches,S=t=>{const e=window.matchMedia?.(g);return e?.addEventListener("change",t),()=>e?.removeEventListener("change",t)},D=(t,e,i,s,w,r)=>{if(!t||i===e.value)return;const o=getComputedStyle(t),c=e.anim?o.height:e.root;e.anim?.cancel(),r&&Object.assign(r.style,{height:`${i}px`,boxSizing:"border-box"}),e.root=o.height,r&&Object.assign(r.style,{height:"",boxSizing:""});const x=!e.value||c===e.root||!t.animate||f();if(e.value=i,e.anim=null,x)return;const a={overflow:"hidden",alignItems:"flex-start"},l=e.anim=t.animate([{...a,height:c},{...a,height:e.root}],{duration:s,easing:w});l.onfinish=()=>{e.anim===l&&(e.anim=null)}};exports.dots=E;exports.flag=m;exports.followHeight=D;exports.icons=p;exports.optionContent=y;exports.optionDomId=v;exports.reducedMotion=f;exports.refocus=$;exports.renderIcon=j;exports.stopEvent=b;exports.warnOnce=M;exports.watchMotion=S;exports.withClass=u;
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),d=require("react"),p={XMark:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 320 512",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M310.6 361.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L160 301.3 54.6 406.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L114.7 256 9.4 150.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0L160 210.7 265.4 105.4c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3L205.3 256l105.3 105.4z"})}),ArrowUp:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 448 512",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M34.9 289.5l175.9-175.8c9.4-9.4 24.6-9.4 33.9 0L420.1 289.5c15.1 15.1 4.4 41-17 41H51.9c-21.4 0-32.1-25.9-17-41z"})}),Checkmark:({className:t="",...e})=>n.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":"true",height:"1em",width:"1em",...e,children:n.jsx("path",{d:"M20.285 6.708a1 1 0 0 0-1.414-1.416l-9.192 9.192-4.243-4.244a1 1 0 1 0-1.414 1.416l5 5a1 1 0 0 0 1.414 0l9.849-9.948z"})})},j=(t,e)=>{if(!t)return null;const i=(r={})=>({...e,...r,style:{...e?.style,...r?.style}});if(typeof t=="string")return n.jsx("img",{src:t,...i(),className:u("rac-icon",e?.className),alt:""});if(d.isValidElement(t))return d.cloneElement(t,i(t.props));if(typeof t=="function"||typeof t=="object"&&t.$$typeof){const r=t;return n.jsx(r,{...i()})}return null},v=(t,e)=>`${t}-${String(e).replace(/[^A-Za-z0-9-]/gu,i=>`_${i.codePointAt(0).toString(36)}_`)}`,m=t=>t?"":void 0,u=(t,e)=>e?`${t} ${e}`:t,y=(t,e,i)=>t.jsx||(e&&"original"in t?e(t.original,{selected:i,disabled:!!t.disabled}):null),E=n.jsxs("span",{className:"rac-dots","aria-hidden":"true",children:[n.jsx("i",{}),n.jsx("i",{}),n.jsx("i",{})]}),C=(()=>{try{return process.env.NODE_ENV!=="production"}catch{return!1}})(),h=new Set,M=(t,e)=>{!C||h.has(t)||(h.add(t),console.warn(`[react-animated-select] ${e}`))},b=t=>{t?.stopPropagation(),t?.preventDefault()},S=(t,e)=>{t?.currentTarget.contains(document.activeElement)&&e.current?.focus()},g="(prefers-reduced-motion: reduce)",f=()=>!!window.matchMedia?.(g).matches,$=t=>{const e=window.matchMedia?.(g);return e?.addEventListener("change",t),()=>e?.removeEventListener("change",t)},D=(t,e,i,r,w,s)=>{if(!t||i===e.value)return;const o=getComputedStyle(t),c=e.anim?.playState==="running"?o.height:e.root;e.anim?.cancel(),s&&Object.assign(s.style,{height:`${i}px`,boxSizing:"border-box"}),e.root=o.height,s&&Object.assign(s.style,{height:"",boxSizing:""});const x=!e.value||c===e.root||!t.animate||f();if(e.value=i,e.anim=null,x)return;const a={overflow:"hidden",alignItems:"flex-start"},l=e.anim=t.animate([{...a,height:c},{...a,height:e.root}],{duration:r,easing:w});l.onfinish=()=>{e.anim===l&&(e.anim=null)}};exports.dots=E;exports.flag=m;exports.followHeight=D;exports.icons=p;exports.optionContent=y;exports.optionDomId=v;exports.reducedMotion=f;exports.refocus=S;exports.renderIcon=j;exports.stopEvent=b;exports.warnOnce=M;exports.watchMotion=$;exports.withClass=u;