@nextlyhq/ui 0.0.2-alpha.54 → 0.0.2-alpha.56

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/index.cjs CHANGED
@@ -29,8 +29,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
29
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
30
 
31
31
  // src/index.ts
32
- var index_exports = {};
33
- __export(index_exports, {
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
34
  Accordion: () => Accordion,
35
35
  AccordionContent: () => AccordionContent,
36
36
  AccordionItem: () => AccordionItem,
@@ -149,7 +149,10 @@ __export(index_exports, {
149
149
  SheetPortal: () => SheetPortal,
150
150
  SheetTitle: () => SheetTitle,
151
151
  SheetTrigger: () => SheetTrigger,
152
+ ShortcutProvider: () => ShortcutProvider,
153
+ ShortcutScope: () => ShortcutScope,
152
154
  Skeleton: () => Skeleton,
155
+ Slider: () => Slider,
153
156
  Spinner: () => Spinner,
154
157
  Stack: () => Stack,
155
158
  Stat: () => Stat,
@@ -183,16 +186,21 @@ __export(index_exports, {
183
186
  badgeVariants: () => badgeVariants,
184
187
  buttonVariants: () => buttonVariants,
185
188
  cardVariants: () => cardVariants,
189
+ createShortcutManager: () => createShortcutManager,
186
190
  dialogContentVariants: () => dialogContentVariants,
187
191
  inputVariants: () => inputVariants,
192
+ parseKeys: () => parseKeys,
188
193
  progressVariants: () => progressVariants,
189
194
  selectTriggerVariants: () => selectTriggerVariants,
190
195
  sheetVariants: () => sheetVariants,
191
196
  spinnerVariants: () => spinnerVariants,
192
197
  toast: () => import_sonner.toast,
193
- usePortalContainer: () => usePortalContainer
198
+ useActiveShortcuts: () => useActiveShortcuts,
199
+ usePortalContainer: () => usePortalContainer,
200
+ useShortcutManager: () => useShortcutManager,
201
+ useShortcuts: () => useShortcuts
194
202
  });
195
- module.exports = __toCommonJS(index_exports);
203
+ module.exports = __toCommonJS(src_exports);
196
204
 
197
205
  // src/components/button.tsx
198
206
  var import_react_slot = require("@radix-ui/react-slot");
@@ -216,11 +224,15 @@ var buttonVariants = (0, import_class_variance_authority.cva)(
216
224
  variant: {
217
225
  default: "bg-primary text-primary-foreground border border-transparent hover:opacity-90",
218
226
  primary: "bg-primary text-primary-foreground border border-transparent hover:opacity-90",
219
- // Solid fill uses the emphasis token so white on-color text stays AA in
220
- // dark mode (the base token is the readable text color, too light here).
221
- // Hover darkens to a deeper shade instead of opacity-90, which would
222
- // composite the fill toward the page and drop white text under 4.5:1.
223
- destructive: "bg-destructive-solid text-destructive-foreground border border-transparent hover:bg-destructive-700",
227
+ // Solid fill uses the emphasis token so on-color text stays AA in dark
228
+ // mode (the base token is the readable text color, too light here).
229
+ // Hover darkens to a deeper shade rather than opacity-90, which would
230
+ // composite the fill toward the page and drop the label under 4.5:1.
231
+ // One step, not two: the label is white in light mode and black in
232
+ // dark, so mixing the fill toward black moves it away from the label in
233
+ // one mode and into it in the other. `-600` clears both (5.92:1 light,
234
+ // 5.67:1 dark); `-700` reads at 3.70:1 against the dark label.
235
+ destructive: "bg-destructive-solid text-destructive-foreground border border-transparent hover:bg-destructive-600",
224
236
  // border-border is the decorative separator token, and it is the right
225
237
  // one here: a button is identified by its label and fill, so its edge
226
238
  // carries no meaning on its own and is not held to the 3:1 minimum that
@@ -2809,6 +2821,836 @@ var TreeView = React10.forwardRef(
2809
2821
  }
2810
2822
  );
2811
2823
  TreeView.displayName = "TreeView";
2824
+
2825
+ // src/components/slider.tsx
2826
+ var SliderPrimitive = __toESM(require("@radix-ui/react-slider"), 1);
2827
+ var React11 = __toESM(require("react"), 1);
2828
+
2829
+ // src/lib/dev-warn.ts
2830
+ var emitted = /* @__PURE__ */ new Set();
2831
+ var SPEAKING_ENVIRONMENTS = /* @__PURE__ */ new Set(["development", "test"]);
2832
+ function isDevelopmentRuntime() {
2833
+ if (typeof process === "undefined") return false;
2834
+ const env = process?.env?.NODE_ENV;
2835
+ return env !== void 0 && SPEAKING_ENVIRONMENTS.has(env);
2836
+ }
2837
+ function devWarnOnce(condition, message) {
2838
+ if (condition) return;
2839
+ if (!isDevelopmentRuntime()) return;
2840
+ if (emitted.has(message)) return;
2841
+ emitted.add(message);
2842
+ console.warn(`[@nextlyhq/ui] ${message}`);
2843
+ }
2844
+
2845
+ // src/components/slider.tsx
2846
+ var import_jsx_runtime37 = (
2847
+ // `aria-label`/`aria-labelledby` are destructured out above rather than
2848
+ // spread here: left on the root they would be a second, roleless copy
2849
+ // of a name only the thumb is read for.
2850
+ require("react/jsx-runtime")
2851
+ );
2852
+ function thumbCount(value, defaultValue) {
2853
+ return Math.max(1, value?.length ?? defaultValue?.length ?? 1);
2854
+ }
2855
+ function hasAccessibleName(value) {
2856
+ return value !== void 0 && value.trim() !== "";
2857
+ }
2858
+ var Slider = React11.forwardRef(
2859
+ ({
2860
+ className,
2861
+ value,
2862
+ defaultValue,
2863
+ thumbs,
2864
+ orientation = "horizontal",
2865
+ "aria-label": ariaLabel,
2866
+ "aria-labelledby": ariaLabelledBy,
2867
+ ...props
2868
+ }, ref) => {
2869
+ const initialUncontrolledCount = React11.useRef(
2870
+ thumbCount(void 0, defaultValue)
2871
+ ).current;
2872
+ const count = value?.length ?? initialUncontrolledCount;
2873
+ const isEmptyDefault = defaultValue !== void 0 && defaultValue.length === 0;
2874
+ const isEmptyControlled = value !== void 0 && value.length === 0;
2875
+ devWarnOnce(
2876
+ !isEmptyDefault && !isEmptyControlled,
2877
+ "Slider: `value`/`defaultValue` must hold one number per thumb, and an empty array holds none \u2014 the control has nothing to slide. An empty `defaultValue` falls back to `min`; an empty `value` renders nothing at all, because a controlled slider cannot be given a value without taking state the caller owns. Render nothing until the value is loaded rather than passing `[]`."
2878
+ );
2879
+ if (isEmptyControlled) return null;
2880
+ const isNamed = (index) => {
2881
+ const own = thumbs?.[index];
2882
+ if (hasAccessibleName(own?.["aria-label"])) return true;
2883
+ if (hasAccessibleName(own?.["aria-labelledby"])) return true;
2884
+ return count === 1 && (hasAccessibleName(ariaLabel) || hasAccessibleName(ariaLabelledBy));
2885
+ };
2886
+ devWarnOnce(
2887
+ Array.from({ length: count }).every((_, i) => isNamed(i)),
2888
+ "Slider: every thumb needs an accessible name. A single thumb may take it from the root's `aria-label`/`aria-labelledby`; a range needs one `thumbs` entry per thumb, because the root's name is not inherited and two thumbs sharing one name are announced identically."
2889
+ );
2890
+ const ariaFor = (index) => {
2891
+ const supplied = thumbs?.[index] ?? {};
2892
+ const ownLabel = hasAccessibleName(supplied["aria-label"]) ? supplied["aria-label"] : void 0;
2893
+ const ownLabelledBy = hasAccessibleName(supplied["aria-labelledby"]) ? supplied["aria-labelledby"] : void 0;
2894
+ if (count !== 1) {
2895
+ return {
2896
+ ...supplied,
2897
+ "aria-label": ownLabel,
2898
+ "aria-labelledby": ownLabelledBy
2899
+ };
2900
+ }
2901
+ const namesItself = ownLabel !== void 0 || ownLabelledBy !== void 0;
2902
+ return {
2903
+ "aria-label": namesItself ? ownLabel : ariaLabel,
2904
+ "aria-labelledby": namesItself ? ownLabelledBy : ariaLabelledBy,
2905
+ "aria-valuetext": supplied["aria-valuetext"],
2906
+ "aria-describedby": supplied["aria-describedby"]
2907
+ };
2908
+ };
2909
+ const isVertical = orientation === "vertical";
2910
+ return /* @__PURE__ */ (0, import_jsx_runtime37.jsxs)(
2911
+ SliderPrimitive.Root,
2912
+ {
2913
+ ref,
2914
+ className: cn(
2915
+ "relative flex touch-none select-none items-center",
2916
+ // WCAG 2.5.8 wants a 24px target. Padding alone does not reach it: the
2917
+ // thumb is absolutely positioned, so the cross-axis size is the 6px
2918
+ // track plus the padding — 22px with `py-2`. An explicit minimum
2919
+ // states the target rather than leaving it to arithmetic that moves
2920
+ // whenever the track thickness does.
2921
+ isVertical ? (
2922
+ // A vertical slider needs a LENGTH, and it cannot inherit one:
2923
+ // `h-full` inside an auto-height parent resolves to zero, leaving
2924
+ // a control with no track to drag along. A concrete default is
2925
+ // usable everywhere and, being a plain utility, is replaced by a
2926
+ // caller's own `h-*` — including `h-full`, for the fill-the-parent
2927
+ // case this default gives up.
2928
+ "h-44 min-w-6 flex-col px-2"
2929
+ ) : "min-h-6 w-full py-2",
2930
+ "data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
2931
+ className
2932
+ ),
2933
+ orientation,
2934
+ value,
2935
+ defaultValue: isEmptyDefault ? void 0 : defaultValue,
2936
+ ...props,
2937
+ children: [
2938
+ /* @__PURE__ */ (0, import_jsx_runtime37.jsx)(
2939
+ SliderPrimitive.Track,
2940
+ {
2941
+ className: cn(
2942
+ "bg-secondary relative grow overflow-hidden rounded-full",
2943
+ isVertical ? "h-full w-1.5" : "h-1.5 w-full"
2944
+ ),
2945
+ children: /* @__PURE__ */ (0, import_jsx_runtime37.jsx)(
2946
+ SliderPrimitive.Range,
2947
+ {
2948
+ className: cn(
2949
+ "bg-primary absolute",
2950
+ isVertical ? "w-full" : "h-full"
2951
+ )
2952
+ }
2953
+ )
2954
+ }
2955
+ ),
2956
+ Array.from({ length: count }, (_, i) => /* @__PURE__ */ (0, import_jsx_runtime37.jsx)(
2957
+ SliderPrimitive.Thumb,
2958
+ {
2959
+ ...ariaFor(i),
2960
+ className: cn(
2961
+ "border-primary bg-background block h-4 w-4 rounded-full border-2",
2962
+ "ring-offset-background transition-colors",
2963
+ "focus-visible:ring-ring focus-visible:outline-none focus-visible:ring-2",
2964
+ "focus-visible:ring-offset-2",
2965
+ "disabled:pointer-events-none disabled:opacity-50"
2966
+ )
2967
+ },
2968
+ i
2969
+ ))
2970
+ ]
2971
+ }
2972
+ );
2973
+ }
2974
+ );
2975
+ Slider.displayName = SliderPrimitive.Root.displayName;
2976
+
2977
+ // src/lib/shortcuts/react.tsx
2978
+ var React12 = __toESM(require("react"), 1);
2979
+
2980
+ // src/lib/shortcuts/key-spec.ts
2981
+ function normalizeKey(key) {
2982
+ return [...key].length === 1 ? key.toLowerCase() : key;
2983
+ }
2984
+ function shiftIsMeaningful(key) {
2985
+ if (key.length > 1) return true;
2986
+ if (key === " ") return true;
2987
+ return /[\p{L}\p{N}]/u.test(key);
2988
+ }
2989
+ function parseKeys(spec) {
2990
+ const steps = spec.trim().split(/\s+/).filter(Boolean);
2991
+ if (steps.length === 0) {
2992
+ throw new Error(`Shortcut spec is empty: ${JSON.stringify(spec)}`);
2993
+ }
2994
+ return steps.map((step) => parseChord(step, spec));
2995
+ }
2996
+ function parseChord(step, spec) {
2997
+ const trailingPlusIsKey = step.length > 2 && step.endsWith("++");
2998
+ const body = trailingPlusIsKey ? step.slice(0, -1) : step;
2999
+ const parts = step === "+" ? ["+"] : body.split("+").filter(Boolean);
3000
+ let mod = false;
3001
+ let ctrl = false;
3002
+ let meta = false;
3003
+ let alt = false;
3004
+ let shift = false;
3005
+ let key;
3006
+ for (const raw of parts) {
3007
+ switch (raw.toLowerCase()) {
3008
+ case "mod":
3009
+ mod = true;
3010
+ break;
3011
+ case "ctrl":
3012
+ case "control":
3013
+ ctrl = true;
3014
+ break;
3015
+ case "meta":
3016
+ case "cmd":
3017
+ case "command":
3018
+ meta = true;
3019
+ break;
3020
+ case "alt":
3021
+ case "option":
3022
+ alt = true;
3023
+ break;
3024
+ case "shift":
3025
+ shift = true;
3026
+ break;
3027
+ case "space":
3028
+ key = " ";
3029
+ break;
3030
+ default:
3031
+ if (key !== void 0) {
3032
+ throw new Error(
3033
+ `Shortcut step "${step}" names two keys ("${key}" and "${raw}") in ${JSON.stringify(spec)}`
3034
+ );
3035
+ }
3036
+ key = raw;
3037
+ }
3038
+ }
3039
+ if (trailingPlusIsKey) {
3040
+ if (key !== void 0) {
3041
+ throw new Error(
3042
+ `Shortcut step has more than one key: ${JSON.stringify(step)} in ${JSON.stringify(spec)}`
3043
+ );
3044
+ }
3045
+ key = "+";
3046
+ }
3047
+ if (key === void 0) {
3048
+ throw new Error(
3049
+ `Shortcut step "${step}" names modifiers but no key, in ${JSON.stringify(spec)}`
3050
+ );
3051
+ }
3052
+ return { key: normalizeKey(key), mod, ctrl, meta, alt, shift };
3053
+ }
3054
+ function chordMatches(chord, key, state, isApple) {
3055
+ if (normalizeKey(key) !== chord.key) return false;
3056
+ const wantsCtrl = chord.ctrl || chord.mod && !isApple;
3057
+ const wantsMeta = chord.meta || chord.mod && isApple;
3058
+ if (state.metaKey !== wantsMeta) return false;
3059
+ const altGraph = state.getModifierState?.("AltGraph") ?? false;
3060
+ const synthetic = altGraph && [...chord.key].length === 1 && !wantsCtrl && !chord.alt;
3061
+ if (!synthetic) {
3062
+ if (state.ctrlKey !== wantsCtrl) return false;
3063
+ if (state.altKey !== chord.alt) return false;
3064
+ }
3065
+ if (shiftIsMeaningful(chord.key) && state.shiftKey !== chord.shift)
3066
+ return false;
3067
+ return true;
3068
+ }
3069
+ function detectApplePlatform() {
3070
+ if (typeof navigator === "undefined") return false;
3071
+ const candidate = navigator;
3072
+ const platform = candidate.userAgentData?.platform ?? navigator.platform ?? "";
3073
+ return /mac|iphone|ipad|ipod/i.test(platform);
3074
+ }
3075
+
3076
+ // src/lib/shortcuts/manager.ts
3077
+ var DEFAULT_SEQUENCE_TIMEOUT_MS = 1e3;
3078
+ function signature(event) {
3079
+ return event.code || event.key;
3080
+ }
3081
+ function eventTarget(event) {
3082
+ const path = event.composedPath?.();
3083
+ return path && path.length > 0 ? path[0] ?? null : event.target;
3084
+ }
3085
+ function asElement(target) {
3086
+ if (target === null || typeof target !== "object") return null;
3087
+ const node = target;
3088
+ if (node.nodeType !== 1 || typeof node.tagName !== "string") return null;
3089
+ return target;
3090
+ }
3091
+ function inputType(element) {
3092
+ if (element.tagName !== "INPUT") return "";
3093
+ const value = element.type;
3094
+ return typeof value === "string" ? value.toLowerCase() : "";
3095
+ }
3096
+ function controlOwnsKey(target, event) {
3097
+ if (event.ctrlKey || event.metaKey || event.altKey) return false;
3098
+ const element = asElement(target);
3099
+ if (!element) return false;
3100
+ const tag = element.tagName;
3101
+ const type = inputType(element);
3102
+ if (tag === "BUTTON" || type === "button" || type === "submit" || type === "reset" || type === "image") {
3103
+ return event.key === " " || event.key === "Enter";
3104
+ }
3105
+ if (tag === "A" && element.getAttribute("href") !== null) {
3106
+ return event.key === "Enter";
3107
+ }
3108
+ if (tag === "SUMMARY") return event.key === " " || event.key === "Enter";
3109
+ if (type === "checkbox") return event.key === " ";
3110
+ if (type === "color") return event.key === " " || event.key === "Enter";
3111
+ if (type === "file") return event.key === " " || event.key === "Enter";
3112
+ if (type === "range") {
3113
+ return event.key.startsWith("Arrow") || RANGE_KEYS.has(event.key);
3114
+ }
3115
+ if (type === "radio") {
3116
+ return event.key === " " || event.key.startsWith("Arrow");
3117
+ }
3118
+ return false;
3119
+ }
3120
+ function isTypingTarget(target) {
3121
+ const element = asElement(target);
3122
+ if (!element) return false;
3123
+ if (element.isContentEditable) return true;
3124
+ const tag = element.tagName;
3125
+ if (tag === "TEXTAREA") return true;
3126
+ if (tag === "SELECT") return true;
3127
+ if (tag === "INPUT") {
3128
+ return !NON_TEXT_INPUT_TYPES.has(inputType(element));
3129
+ }
3130
+ const role = element.getAttribute("role");
3131
+ return role !== null && TYPE_AHEAD_ROLES.has(role);
3132
+ }
3133
+ var NON_TEXT_INPUT_TYPES = /* @__PURE__ */ new Set([
3134
+ "button",
3135
+ "checkbox",
3136
+ "color",
3137
+ "file",
3138
+ "hidden",
3139
+ "image",
3140
+ "radio",
3141
+ "range",
3142
+ "reset",
3143
+ "submit"
3144
+ ]);
3145
+ function firesWhileTyping(prepared) {
3146
+ const explicit = prepared.binding.whenTyping;
3147
+ if (explicit !== void 0) return explicit;
3148
+ const first = prepared.keys[0];
3149
+ if (first === void 0) return false;
3150
+ return first.mod || first.ctrl || first.meta || first.alt || first.key === "Escape";
3151
+ }
3152
+ function createShortcutManager(options = {}) {
3153
+ const isApple = options.isApple ?? detectApplePlatform();
3154
+ const sequenceTimeoutMs = options.sequenceTimeoutMs ?? DEFAULT_SEQUENCE_TIMEOUT_MS;
3155
+ const now = options.now ?? (() => Date.now());
3156
+ const layers = /* @__PURE__ */ new Set();
3157
+ let nextSequence = 0;
3158
+ let pendingAt = null;
3159
+ let pendingLayer = null;
3160
+ const consumedPresses = /* @__PURE__ */ new Map();
3161
+ let pendingKey = null;
3162
+ function layerShape(bindings, options2) {
3163
+ const keys = bindings.map((b) => b.binding.keys).join("\0");
3164
+ return `${keys}${options2.depth}${options2.blocking === true}${options2.enabled !== false}`;
3165
+ }
3166
+ function blocking() {
3167
+ return ordered().some((layer) => layer.options.blocking === true);
3168
+ }
3169
+ function abandonSequence() {
3170
+ pendingAt = null;
3171
+ pressedEvents.length = 0;
3172
+ pendingLayer = null;
3173
+ pendingKey = null;
3174
+ }
3175
+ function prepare(bindings) {
3176
+ return bindings.map((binding) => ({
3177
+ binding,
3178
+ keys: parseKeys(binding.keys)
3179
+ }));
3180
+ }
3181
+ function ordered() {
3182
+ return [...layers].filter((layer) => layer.options.enabled !== false).sort(
3183
+ (a, b) => b.options.depth - a.options.depth || b.sequence - a.sequence
3184
+ );
3185
+ }
3186
+ function matchDepth(prepared, pressed) {
3187
+ if (pressed.length > prepared.keys.length) return "none";
3188
+ for (let i = 0; i < pressed.length; i++) {
3189
+ const chord = prepared.keys[i];
3190
+ const event = pressed[i];
3191
+ if (chord === void 0 || event === void 0) return "none";
3192
+ if (!chordMatches(chord, event.key, event, isApple)) return "none";
3193
+ }
3194
+ return pressed.length === prepared.keys.length ? "exact" : "prefix";
3195
+ }
3196
+ function fire(prepared, event, invoke) {
3197
+ if (prepared.binding.preventDefault !== false) event.preventDefault();
3198
+ if (invoke) prepared.binding.run(event);
3199
+ }
3200
+ function insertsText(event, typing) {
3201
+ if (event.key === "Tab")
3202
+ return !event.ctrlKey && !event.metaKey && !event.altKey;
3203
+ if (!typing) return false;
3204
+ const altGraph = event.getModifierState?.("AltGraph") ?? false;
3205
+ if (!altGraph && (event.ctrlKey || event.metaKey)) {
3206
+ const letter = event.key.length === 1 ? event.key.toLowerCase() : event.key;
3207
+ if (letter === "z" && event.shiftKey) return !event.altKey;
3208
+ if (letter === REDO_LETTER)
3209
+ return !isApple && !event.shiftKey && !event.altKey;
3210
+ if (EDITING_NAVIGATION.has(event.key)) return !event.altKey || isApple;
3211
+ if (event.shiftKey || event.altKey) return false;
3212
+ return EDITING_LETTERS.has(letter);
3213
+ }
3214
+ if (!altGraph && event.altKey) {
3215
+ if ((event.key === "ArrowDown" || event.key === "ArrowUp") && asElement(eventTarget(event))?.tagName === "SELECT") {
3216
+ return true;
3217
+ }
3218
+ if (!isApple) return false;
3219
+ if (EDITING_NAVIGATION.has(event.key)) return true;
3220
+ }
3221
+ if (event.key === "Dead" || event.key === "Process") return true;
3222
+ if ([...event.key].length === 1) return true;
3223
+ if (AMBIGUOUS_KEYS.has(event.key)) return targetOwnsAmbiguousKey(event);
3224
+ return FIELD_KEYS.has(event.key);
3225
+ }
3226
+ function offer(pressed, event, typing, invoke) {
3227
+ for (const layer of ordered()) {
3228
+ const mayMatch = pressed.length <= 1 || pendingLayer === null || layer === pendingLayer;
3229
+ if (mayMatch) {
3230
+ let prefixed = false;
3231
+ for (const prepared of layer.bindings) {
3232
+ if (typing && !firesWhileTyping(prepared)) continue;
3233
+ if (prepared.binding.when && !prepared.binding.when()) continue;
3234
+ const depth = matchDepth(prepared, pressed);
3235
+ if (depth === "exact") {
3236
+ fire(prepared, event, invoke);
3237
+ return "fired";
3238
+ }
3239
+ if (depth === "prefix") prefixed = true;
3240
+ }
3241
+ if (prefixed) {
3242
+ pendingLayer = layer;
3243
+ event.preventDefault();
3244
+ return "pending";
3245
+ }
3246
+ }
3247
+ if (layer.options.blocking) return "blocked";
3248
+ }
3249
+ return "none";
3250
+ }
3251
+ function warnOnPrefixConflicts(prepared, layerName) {
3252
+ const resolved = (chord) => {
3253
+ const ctrl = chord.ctrl || chord.mod && !isApple;
3254
+ const meta = chord.meta || chord.mod && isApple;
3255
+ const shift = shiftIsMeaningful(chord.key) ? chord.shift : false;
3256
+ return `${chord.key}\0${ctrl}${meta}${chord.alt}${shift}`;
3257
+ };
3258
+ const sameChord = (a, b) => resolved(a) === resolved(b);
3259
+ for (const short of prepared) {
3260
+ for (const long of prepared) {
3261
+ if (short === long || short.keys.length >= long.keys.length) continue;
3262
+ if (short.binding.when !== void 0) continue;
3263
+ if (!firesWhileTyping(short) && firesWhileTyping(long)) continue;
3264
+ if (short.keys.every((chord, i) => sameChord(chord, long.keys[i]))) {
3265
+ devWarnOnce(
3266
+ false,
3267
+ `shortcuts: in layer "${layerName}", "${short.binding.keys}" is a prefix of "${long.binding.keys}", so the longer one can never fire. Bind one or the other.`
3268
+ );
3269
+ }
3270
+ }
3271
+ }
3272
+ }
3273
+ const watchers = /* @__PURE__ */ new Set();
3274
+ let snapshot = null;
3275
+ function computeSnapshot() {
3276
+ return ordered().flatMap(
3277
+ (layer) => layer.bindings.map((prepared) => ({
3278
+ keys: prepared.binding.keys,
3279
+ description: prepared.binding.description,
3280
+ layer: layer.options.name
3281
+ }))
3282
+ );
3283
+ }
3284
+ function sameShortcuts(a, b) {
3285
+ return a.length === b.length && a.every(
3286
+ (entry, index) => entry.keys === b[index]?.keys && entry.description === b[index]?.description && entry.layer === b[index]?.layer
3287
+ );
3288
+ }
3289
+ function changed() {
3290
+ const previous = snapshot;
3291
+ snapshot = null;
3292
+ if (watchers.size === 0) return;
3293
+ const next = computeSnapshot();
3294
+ if (previous && sameShortcuts(previous, next)) {
3295
+ snapshot = previous;
3296
+ return;
3297
+ }
3298
+ snapshot = next;
3299
+ for (const watcher of watchers) watcher();
3300
+ }
3301
+ const pressedEvents = [];
3302
+ function runOffer(pressed, event, typing) {
3303
+ try {
3304
+ return offer(pressed, event, typing, true);
3305
+ } catch (error) {
3306
+ abandonSequence();
3307
+ consumedPresses.delete(signature(event));
3308
+ throw error;
3309
+ }
3310
+ }
3311
+ function handle(event) {
3312
+ if (event.defaultPrevented) {
3313
+ abandonSequence();
3314
+ return true;
3315
+ }
3316
+ if (event.isComposing) {
3317
+ abandonSequence();
3318
+ return true;
3319
+ }
3320
+ if (MODIFIER_KEYS.has(event.key)) return blocking();
3321
+ if (controlOwnsKey(eventTarget(event), event)) {
3322
+ abandonSequence();
3323
+ return true;
3324
+ }
3325
+ const typing = isTypingTarget(eventTarget(event));
3326
+ if (event.repeat) {
3327
+ if (pendingKey !== signature(event)) {
3328
+ abandonSequence();
3329
+ }
3330
+ const held = consumedPresses.get(signature(event));
3331
+ if (held) {
3332
+ if (held.prevented) {
3333
+ event.preventDefault();
3334
+ } else {
3335
+ const still = offer([event], event, typing, false);
3336
+ if (still !== "fired" && !insertsText(event, typing)) {
3337
+ event.preventDefault();
3338
+ }
3339
+ }
3340
+ return true;
3341
+ }
3342
+ const repeated = offer([event], event, typing, false);
3343
+ if (repeated === "blocked" && !insertsText(event, typing)) {
3344
+ event.preventDefault();
3345
+ }
3346
+ return repeated !== "none";
3347
+ }
3348
+ if (pendingAt !== null && now() - pendingAt > sequenceTimeoutMs) {
3349
+ abandonSequence();
3350
+ }
3351
+ pressedEvents.push(event);
3352
+ let outcome = runOffer(pressedEvents, event, typing);
3353
+ if (pressedEvents.length > 1 && (outcome === "none" || outcome === "blocked")) {
3354
+ abandonSequence();
3355
+ pressedEvents.push(event);
3356
+ outcome = runOffer(pressedEvents, event, typing);
3357
+ }
3358
+ if (outcome === "pending") {
3359
+ pendingAt = now();
3360
+ pendingKey = signature(event);
3361
+ consumedPresses.set(signature(event), {
3362
+ prevented: event.defaultPrevented
3363
+ });
3364
+ return true;
3365
+ }
3366
+ abandonSequence();
3367
+ if (outcome === "blocked") {
3368
+ if (!insertsText(event, typing)) event.preventDefault();
3369
+ }
3370
+ const consumed = outcome === "fired" || outcome === "blocked";
3371
+ if (consumed) {
3372
+ consumedPresses.set(signature(event), {
3373
+ prevented: event.defaultPrevented
3374
+ });
3375
+ } else {
3376
+ consumedPresses.delete(signature(event));
3377
+ }
3378
+ return consumed;
3379
+ }
3380
+ return {
3381
+ register(bindings, layerOptions) {
3382
+ const prepared = prepare(bindings);
3383
+ const layer = {
3384
+ options: layerOptions,
3385
+ bindings: prepared,
3386
+ sequence: nextSequence++,
3387
+ shape: layerShape(prepared, layerOptions)
3388
+ };
3389
+ warnOnPrefixConflicts(layer.bindings, layerOptions.name);
3390
+ layers.add(layer);
3391
+ changed();
3392
+ return {
3393
+ update(nextBindings, nextOptions) {
3394
+ layer.bindings = prepare(nextBindings);
3395
+ layer.options = nextOptions;
3396
+ warnOnPrefixConflicts(layer.bindings, nextOptions.name);
3397
+ const shape = layerShape(layer.bindings, nextOptions);
3398
+ const shapeChanged = shape !== layer.shape;
3399
+ layer.shape = shape;
3400
+ if (shapeChanged && pendingLayer === layer) abandonSequence();
3401
+ changed();
3402
+ },
3403
+ dispose() {
3404
+ layers.delete(layer);
3405
+ changed();
3406
+ if (pendingLayer === layer) abandonSequence();
3407
+ }
3408
+ };
3409
+ },
3410
+ handle,
3411
+ attach(target) {
3412
+ const listener = (event) => {
3413
+ try {
3414
+ if (handle(event)) event.stopPropagation();
3415
+ } catch (error) {
3416
+ event.stopPropagation();
3417
+ throw error;
3418
+ }
3419
+ };
3420
+ target.addEventListener("keydown", listener);
3421
+ return () => {
3422
+ target.removeEventListener("keydown", listener);
3423
+ abandonSequence();
3424
+ consumedPresses.clear();
3425
+ };
3426
+ },
3427
+ subscribe(onChange) {
3428
+ watchers.add(onChange);
3429
+ return () => {
3430
+ watchers.delete(onChange);
3431
+ };
3432
+ },
3433
+ activeBindings() {
3434
+ if (snapshot) return snapshot;
3435
+ snapshot = computeSnapshot();
3436
+ return snapshot;
3437
+ }
3438
+ };
3439
+ }
3440
+ var MODIFIER_KEYS = /* @__PURE__ */ new Set([
3441
+ "Control",
3442
+ "Meta",
3443
+ "Alt",
3444
+ "Shift",
3445
+ // A dedicated AltGraph key reports its own keydown before the character-producing one. Without
3446
+ // it here, pressing AltGraph mid-sequence abandons the sequence before the character that would
3447
+ // have completed it ever arrives.
3448
+ "AltGraph"
3449
+ ]);
3450
+ var TYPE_AHEAD_ROLES = /* @__PURE__ */ new Set([
3451
+ "textbox",
3452
+ "combobox",
3453
+ "listbox",
3454
+ // The focused element inside an open listbox is the OPTION, and it is what the event reports;
3455
+ // the listbox itself is only its ancestor.
3456
+ "option",
3457
+ "menu",
3458
+ "menuitem",
3459
+ "menuitemcheckbox",
3460
+ "menuitemradio"
3461
+ ]);
3462
+ var RANGE_KEYS = /* @__PURE__ */ new Set(["Home", "End", "PageUp", "PageDown"]);
3463
+ var EDITING_LETTERS = /* @__PURE__ */ new Set(["a", "c", "v", "x", "z"]);
3464
+ var REDO_LETTER = "y";
3465
+ var EDITING_NAVIGATION = /* @__PURE__ */ new Set([
3466
+ "Insert",
3467
+ "ArrowLeft",
3468
+ "ArrowRight",
3469
+ "ArrowUp",
3470
+ "ArrowDown",
3471
+ "Home",
3472
+ "End",
3473
+ "Backspace",
3474
+ "Delete"
3475
+ ]);
3476
+ var AMBIGUOUS_KEYS = /* @__PURE__ */ new Set(["Enter", "PageUp", "PageDown"]);
3477
+ function targetOwnsAmbiguousKey(event) {
3478
+ const element = asElement(eventTarget(event));
3479
+ if (element === null) return false;
3480
+ const multiline = element.tagName === "TEXTAREA" || element.isContentEditable;
3481
+ if (event.key === "Enter") return multiline;
3482
+ return multiline || element.tagName === "SELECT";
3483
+ }
3484
+ var FIELD_KEYS = /* @__PURE__ */ new Set([
3485
+ "Backspace",
3486
+ "Delete",
3487
+ "ArrowUp",
3488
+ "ArrowDown",
3489
+ "ArrowLeft",
3490
+ "ArrowRight",
3491
+ "Home",
3492
+ "End",
3493
+ // Shift+Insert pastes and carries no ctrl or meta, so it arrives here rather than at the
3494
+ // chord branch where Ctrl+Insert is recognised.
3495
+ "Insert",
3496
+ // Tab moves focus, and a blocking layer must NOT take it. The documented case for blocking is
3497
+ // a modal, whose focus trap only calls `preventDefault()` at the first and last tabbable
3498
+ // element — ordinary moves between the controls inside it rely on the browser default.
3499
+ // Suppressing every Tab therefore pinned focus to one control in exactly the situation
3500
+ // blocking exists to serve. A layer that genuinely wants Tab binds it.
3501
+ "Tab"
3502
+ ]);
3503
+
3504
+ // src/lib/shortcuts/react.tsx
3505
+ var import_jsx_runtime38 = require("react/jsx-runtime");
3506
+ var ShortcutContext = React12.createContext(null);
3507
+ var ownersByTarget = /* @__PURE__ */ new WeakMap();
3508
+ var useIsomorphicLayoutEffect = typeof document === "undefined" ? React12.useEffect : React12.useLayoutEffect;
3509
+ function optionsFingerprint(options) {
3510
+ return [
3511
+ options.isApple ?? "auto",
3512
+ options.sequenceTimeoutMs ?? "default",
3513
+ options.now ? "clock" : "no-clock"
3514
+ ].join("\0");
3515
+ }
3516
+ function ShortcutProvider({
3517
+ children,
3518
+ target,
3519
+ ...managerOptions
3520
+ }) {
3521
+ const parent = React12.useContext(ShortcutContext);
3522
+ const resolvedTarget = target === null ? null : target ?? (typeof document === "undefined" ? null : document);
3523
+ const nestedOnSameTarget = parent !== null && parent.target === resolvedTarget;
3524
+ const optionsRef = React12.useRef(managerOptions);
3525
+ const ownManagers = React12.useRef(/* @__PURE__ */ new WeakMap());
3526
+ const ownDetached = React12.useRef(null);
3527
+ const detached = React12.useMemo(() => {
3528
+ if (resolvedTarget === null) {
3529
+ ownDetached.current ??= createShortcutManager(optionsRef.current);
3530
+ return ownDetached.current;
3531
+ }
3532
+ const existing = ownManagers.current.get(resolvedTarget);
3533
+ if (existing) return existing;
3534
+ const created = createShortcutManager(optionsRef.current);
3535
+ ownManagers.current.set(resolvedTarget, created);
3536
+ return created;
3537
+ }, [resolvedTarget]);
3538
+ let owner = resolvedTarget === null ? null : ownersByTarget.get(resolvedTarget);
3539
+ const fingerprint = optionsFingerprint(optionsRef.current);
3540
+ if (resolvedTarget !== null && (!owner || owner.retired)) {
3541
+ owner = {
3542
+ manager: detached,
3543
+ providers: 0,
3544
+ retired: false,
3545
+ options: fingerprint
3546
+ };
3547
+ ownersByTarget.set(resolvedTarget, owner);
3548
+ }
3549
+ const adoptedDiffers = Boolean(owner) && owner?.options !== fingerprint || nestedOnSameTarget && parent !== null && parent.options !== fingerprint;
3550
+ devWarnOnce(
3551
+ !adoptedDiffers,
3552
+ "ShortcutProvider: another provider is already listening on this target with different options, so the ones passed here are being ignored. Managers are shared per target; give the providers matching options, or a target of their own."
3553
+ );
3554
+ const manager = nestedOnSameTarget && parent ? parent.manager : owner ? owner.manager : detached;
3555
+ useIsomorphicLayoutEffect(() => {
3556
+ if (resolvedTarget === null) return;
3557
+ let entry = ownersByTarget.get(resolvedTarget);
3558
+ if (!entry) {
3559
+ entry = {
3560
+ manager,
3561
+ providers: 0,
3562
+ retired: false,
3563
+ options: optionsFingerprint(optionsRef.current)
3564
+ };
3565
+ ownersByTarget.set(resolvedTarget, entry);
3566
+ }
3567
+ const owned = entry;
3568
+ owned.providers += 1;
3569
+ owned.retired = false;
3570
+ if (owned.providers === 1) {
3571
+ owned.detach = owned.manager.attach(resolvedTarget);
3572
+ }
3573
+ return () => {
3574
+ owned.providers -= 1;
3575
+ if (owned.providers === 0) {
3576
+ owned.detach?.();
3577
+ owned.detach = void 0;
3578
+ owned.retired = true;
3579
+ }
3580
+ };
3581
+ }, [resolvedTarget, manager]);
3582
+ const depth = nestedOnSameTarget && parent ? parent.depth : 0;
3583
+ const value = React12.useMemo(
3584
+ () => ({ manager, depth, target: resolvedTarget, options: fingerprint }),
3585
+ [manager, depth, resolvedTarget, fingerprint]
3586
+ );
3587
+ return /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(ShortcutContext.Provider, { value, children });
3588
+ }
3589
+ function ShortcutScope({
3590
+ children
3591
+ }) {
3592
+ const parent = React12.useContext(ShortcutContext);
3593
+ if (!parent) {
3594
+ throw new Error("ShortcutScope must be rendered inside a ShortcutProvider");
3595
+ }
3596
+ const value = React12.useMemo(
3597
+ () => ({
3598
+ manager: parent.manager,
3599
+ depth: parent.depth + 1,
3600
+ target: parent.target,
3601
+ // Inherited: a scope raises precedence, it does not build a manager, so the options in force
3602
+ // are still the ones the provider above it used.
3603
+ options: parent.options
3604
+ }),
3605
+ [parent.manager, parent.depth, parent.target, parent.options]
3606
+ );
3607
+ return /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(ShortcutContext.Provider, { value, children });
3608
+ }
3609
+ function useShortcuts(bindings, options) {
3610
+ const context = React12.useContext(ShortcutContext);
3611
+ if (!context) {
3612
+ throw new Error("useShortcuts must be called inside a ShortcutProvider");
3613
+ }
3614
+ const { manager, depth } = context;
3615
+ const registration = React12.useRef(null);
3616
+ const latest = React12.useRef({ bindings, options });
3617
+ useIsomorphicLayoutEffect(() => {
3618
+ registration.current = manager.register([], {
3619
+ name: latest.current.options.name,
3620
+ depth
3621
+ });
3622
+ return () => {
3623
+ registration.current?.dispose();
3624
+ registration.current = null;
3625
+ };
3626
+ }, [manager, depth]);
3627
+ useIsomorphicLayoutEffect(() => {
3628
+ latest.current = { bindings, options };
3629
+ registration.current?.update(bindings, {
3630
+ name: options.name,
3631
+ depth,
3632
+ enabled: options.enabled,
3633
+ blocking: options.blocking
3634
+ });
3635
+ });
3636
+ }
3637
+ function useShortcutManager() {
3638
+ const context = React12.useContext(ShortcutContext);
3639
+ if (!context) {
3640
+ throw new Error(
3641
+ "useShortcutManager must be called inside a ShortcutProvider"
3642
+ );
3643
+ }
3644
+ return context.manager;
3645
+ }
3646
+ function useActiveShortcuts() {
3647
+ const manager = useShortcutManager();
3648
+ return React12.useSyncExternalStore(
3649
+ manager.subscribe,
3650
+ manager.activeBindings,
3651
+ manager.activeBindings
3652
+ );
3653
+ }
2812
3654
  // Annotate the CommonJS export names for ESM import in node:
2813
3655
  0 && (module.exports = {
2814
3656
  Accordion,
@@ -2929,7 +3771,10 @@ TreeView.displayName = "TreeView";
2929
3771
  SheetPortal,
2930
3772
  SheetTitle,
2931
3773
  SheetTrigger,
3774
+ ShortcutProvider,
3775
+ ShortcutScope,
2932
3776
  Skeleton,
3777
+ Slider,
2933
3778
  Spinner,
2934
3779
  Stack,
2935
3780
  Stat,
@@ -2963,13 +3808,18 @@ TreeView.displayName = "TreeView";
2963
3808
  badgeVariants,
2964
3809
  buttonVariants,
2965
3810
  cardVariants,
3811
+ createShortcutManager,
2966
3812
  dialogContentVariants,
2967
3813
  inputVariants,
3814
+ parseKeys,
2968
3815
  progressVariants,
2969
3816
  selectTriggerVariants,
2970
3817
  sheetVariants,
2971
3818
  spinnerVariants,
2972
3819
  toast,
2973
- usePortalContainer
3820
+ useActiveShortcuts,
3821
+ usePortalContainer,
3822
+ useShortcutManager,
3823
+ useShortcuts
2974
3824
  });
2975
3825
  //# sourceMappingURL=index.cjs.map