@diegopluna/narvi 0.3.4 → 0.3.6

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.
Files changed (63) hide show
  1. package/dist/components/app-shell/AppShellPanel.svelte +35 -4
  2. package/dist/components/badge/Badge.svelte +13 -1
  3. package/dist/components/badge/Badge.svelte.d.ts +6 -0
  4. package/dist/components/button/Button.svelte +80 -33
  5. package/dist/components/button/Button.svelte.d.ts +18 -0
  6. package/dist/components/command/CommandDialog.svelte +24 -2
  7. package/dist/components/command/CommandDialog.svelte.d.ts +12 -0
  8. package/dist/components/command/CommandItem.svelte +15 -4
  9. package/dist/components/command/CommandItem.svelte.d.ts +20 -4
  10. package/dist/components/command/CommandRoot.svelte +57 -2
  11. package/dist/components/command/CommandRoot.svelte.d.ts +25 -1
  12. package/dist/components/composer/ComposerModelPicker.svelte +608 -53
  13. package/dist/components/composer/ComposerModelPicker.svelte.d.ts +56 -1
  14. package/dist/components/composer/ComposerRoot.svelte.d.ts +1 -1
  15. package/dist/components/composer/ComposerSubmit.svelte +12 -2
  16. package/dist/components/composer/ComposerSubmit.svelte.d.ts +6 -0
  17. package/dist/components/composer/index.d.ts +1 -1
  18. package/dist/components/input/Input.svelte.d.ts +1 -1
  19. package/dist/components/menu/MenuCheckboxItem.svelte +14 -5
  20. package/dist/components/menu/MenuCheckboxItem.svelte.d.ts +15 -4
  21. package/dist/components/menu/MenuItem.svelte +9 -3
  22. package/dist/components/menu/MenuItem.svelte.d.ts +11 -4
  23. package/dist/components/menu/MenuRadioItem.svelte +18 -9
  24. package/dist/components/menu/MenuRadioItem.svelte.d.ts +15 -4
  25. package/dist/components/menu/MenuSubTrigger.svelte +9 -3
  26. package/dist/components/menu/MenuSubTrigger.svelte.d.ts +11 -4
  27. package/dist/components/questionnaire/Questionnaire.svelte +100 -9
  28. package/dist/components/questionnaire/Questionnaire.svelte.d.ts +11 -0
  29. package/dist/components/queued-message/QueuedMessage.svelte +3 -2
  30. package/dist/components/rail/RailRoot.svelte +3 -3
  31. package/dist/components/segmented/SegmentedControl.svelte +2 -1
  32. package/dist/components/select/Select.svelte +11 -1
  33. package/dist/components/select/Select.svelte.d.ts +5 -0
  34. package/dist/components/settings-row/SettingsRow.svelte +3 -2
  35. package/dist/components/slider/Slider.svelte.d.ts +2 -2
  36. package/dist/components/split-button/SplitButton.svelte +14 -1
  37. package/dist/components/split-button/SplitButton.svelte.d.ts +7 -0
  38. package/dist/components/tab-strip/TabStrip.svelte +77 -20
  39. package/dist/components/tab-strip/TabStrip.svelte.d.ts +16 -3
  40. package/dist/components/tabs/Tabs.svelte +2 -1
  41. package/dist/components/textarea/Textarea.svelte.d.ts +1 -1
  42. package/dist/components/toggle-group/ToggleGroupRoot.svelte.d.ts +1 -1
  43. package/dist/components/tooltip/TooltipProvider.svelte +23 -0
  44. package/dist/components/tooltip/TooltipProvider.svelte.d.ts +8 -0
  45. package/dist/components/tooltip/index.d.ts +4 -5
  46. package/dist/components/tooltip/index.js +3 -5
  47. package/dist/index.d.ts +3 -3
  48. package/dist/internal/ItemDescription.svelte +34 -0
  49. package/dist/internal/ItemDescription.svelte.d.ts +10 -0
  50. package/dist/internal/TooltipScope.svelte +19 -0
  51. package/dist/internal/TooltipScope.svelte.d.ts +7 -0
  52. package/dist/internal/roving.d.ts +3 -1
  53. package/dist/internal/roving.js +6 -3
  54. package/dist/media.stylex.d.ts +6 -0
  55. package/dist/media.stylex.js +6 -0
  56. package/dist/vite.js +4 -0
  57. package/package.json +20 -1
  58. package/skills/building-ui/SKILL.md +45 -37
  59. package/skills/building-ui/references/components.md +42 -42
  60. package/skills/markdown/SKILL.md +1 -1
  61. package/skills/setup/SKILL.md +1 -1
  62. package/skills/styling/SKILL.md +11 -2
  63. package/skills/theming/SKILL.md +1 -1
@@ -5,6 +5,11 @@
5
5
  Where its row has no room for it beside the page (the row's width less the
6
6
  sidebar's, not the window's), it takes the page's place under the page's
7
7
  header. Maximized, it takes the page's place whole, header and all.
8
+
9
+ It can also stand in a row of your own inside AppShell.Main, beside a thread
10
+ rather than beside the page: a flex row with `position: relative`. It then
11
+ measures that row, and narrow or maximized it lies over the row, whose other
12
+ children are inert underneath.
8
13
  -->
9
14
  <script lang="ts" module>
10
15
  import * as stylex from '@stylexjs/stylex';
@@ -70,6 +75,8 @@
70
75
  },
71
76
  // Under the page's header, which stays in reach with the sidebar's toggle
72
77
  narrow: { insetBlockStart: '3rem' },
78
+ // In a row of the caller's own, over the whole row: its header is the panel's
79
+ inRow: { insetBlockStart: 0, insetInlineStart: 0 },
73
80
  // Over all of the page. Where the page's header kept room for the traffic lights, the
74
81
  // panel's own header keeps it now.
75
82
  maximized: { insetBlockStart: 0, '--narvi-inset-page': 'inherit' },
@@ -97,6 +104,8 @@
97
104
  // The room the row gives the page and the panel together, and where that room starts
98
105
  let row = $state(Infinity);
99
106
  let start = $state(0);
107
+ // Placed straight in the sheet, beside the page; otherwise in a row of the caller's own
108
+ let inSheet = $state(true);
100
109
 
101
110
  const sidebarOf = (sheet: Element | null | undefined) =>
102
111
  sheet?.querySelector<HTMLElement>(':scope > [data-slot="app-shell-sidebar"]') ?? null;
@@ -105,10 +114,11 @@
105
114
  $effect(() => {
106
115
  const sheet = element?.parentElement;
107
116
  if (!sheet) return;
117
+ inSheet = sheet.dataset.slot === 'app-shell-sheet';
108
118
  const measure = () => {
109
119
  const sidebar = sidebarOf(sheet);
110
120
  // A drawer is over the page, not beside it
111
- const beside = shell.narrow ? 0 : (sidebar?.offsetWidth ?? 0);
121
+ const beside = shell.narrow && inSheet ? 0 : (sidebar?.offsetWidth ?? 0);
112
122
  row = sheet.clientWidth - beside;
113
123
  start = beside;
114
124
  };
@@ -120,16 +130,31 @@
120
130
  return () => observer.disconnect();
121
131
  });
122
132
 
123
- const narrow = $derived(shell.narrow || row < minWidth + Math.max(mainMinWidth, 0));
133
+ const narrow = $derived((inSheet && shell.narrow) || row < minWidth + Math.max(mainMinWidth, 0));
124
134
  const over = $derived(maximized || narrow);
125
135
 
126
136
  // The page steps out of view under a maximized panel
127
137
  $effect(() => {
128
- if (!sheetContext || !maximized) return;
138
+ if (!sheetContext || !maximized || !inSheet) return;
129
139
  sheetContext.setCovered(true);
130
140
  return () => sheetContext.setCovered(false);
131
141
  });
132
142
 
143
+ // In a row of the caller's own, what the panel lies over is out of reach underneath:
144
+ // its siblings are inert, and come back as they were
145
+ $effect(() => {
146
+ const parent = element?.parentElement;
147
+ if (inSheet || !over || !parent || !element) return;
148
+ const covered = [...parent.children].filter(
149
+ (child): child is HTMLElement =>
150
+ child instanceof HTMLElement && child !== element && !child.inert
151
+ );
152
+ for (const child of covered) child.inert = true;
153
+ return () => {
154
+ for (const child of covered) child.inert = false;
155
+ };
156
+ });
157
+
133
158
  // It never takes so much that the main area falls below its minimum
134
159
  const largest = () => {
135
160
  // On the server there is no window to measure: no limit yet
@@ -166,7 +191,13 @@
166
191
  style:--narvi-panel-start="{start}px"
167
192
  {...rest}
168
193
  {...attrs(
169
- [styles.panel, over && styles.over, maximized ? styles.maximized : narrow && styles.narrow, sx],
194
+ [
195
+ styles.panel,
196
+ over && styles.over,
197
+ maximized ? styles.maximized : narrow && styles.narrow,
198
+ over && !inSheet && styles.inRow,
199
+ sx
200
+ ],
170
201
  className
171
202
  )}
172
203
  >
@@ -12,6 +12,12 @@
12
12
  export type BadgeProps = Omit<HTMLAttributes<HTMLSpanElement>, 'children'> & {
13
13
  /** Colour of the dot. The words stay in the text colour, so they are always readable. */
14
14
  tone?: BadgeTone | undefined;
15
+ /**
16
+ * The dot in a colour of its own, any CSS colour, in place of a tone's: a label's colour
17
+ * as its owner chose it, such as a GitHub label's `#d73a4a`. Only the dot takes it, so the
18
+ * words stay readable whatever the colour.
19
+ */
20
+ color?: string | undefined;
15
21
  /**
16
22
  * Hides the dot, for a plain label or a count. A tone then colours the words, in its
17
23
  * text colour, which holds its contrast on a badge's own surface.
@@ -79,6 +85,7 @@
79
85
  <script lang="ts">
80
86
  let {
81
87
  tone = 'neutral',
88
+ color,
82
89
  plain = false,
83
90
  size = 'md',
84
91
  sx,
@@ -104,7 +111,12 @@
104
111
  )}
105
112
  >
106
113
  {#if !plain}
107
- <span aria-hidden="true" {...stylex.attrs(styles.dot, dots[tone])}></span>
114
+ <!-- A colour of the caller's own is set on the dot itself; forced colours keep it, as a tone's -->
115
+ <span
116
+ aria-hidden="true"
117
+ {...stylex.attrs(styles.dot, !color && dots[tone])}
118
+ style:background-color={color}
119
+ ></span>
108
120
  {/if}
109
121
  {@render children?.()}
110
122
  </span>
@@ -6,6 +6,12 @@ export type BadgeTone = 'neutral' | 'accent' | 'info' | 'success' | 'warning' |
6
6
  export type BadgeProps = Omit<HTMLAttributes<HTMLSpanElement>, 'children'> & {
7
7
  /** Colour of the dot. The words stay in the text colour, so they are always readable. */
8
8
  tone?: BadgeTone | undefined;
9
+ /**
10
+ * The dot in a colour of its own, any CSS colour, in place of a tone's: a label's colour
11
+ * as its owner chose it, such as a GitHub label's `#d73a4a`. Only the dot takes it, so the
12
+ * words stay readable whatever the colour.
13
+ */
14
+ color?: string | undefined;
9
15
  /**
10
16
  * Hides the dot, for a plain label or a count. A tone then colours the words, in its
11
17
  * text colour, which holds its contrast on a badge's own surface.
@@ -7,6 +7,9 @@
7
7
  import { colors, font, motion, radius } from '../../tokens.stylex.js';
8
8
  import { media } from '../../media.stylex.js';
9
9
  import Spinner from '../spinner/Spinner.svelte';
10
+ import * as Tooltip from '../tooltip/index.js';
11
+ import TooltipScope from '../../internal/TooltipScope.svelte';
12
+ import { mergeProps } from 'bits-ui';
10
13
  import '../../internal/icons.css';
11
14
 
12
15
  /**
@@ -30,12 +33,20 @@
30
33
  | 'success-outline';
31
34
  export type ButtonSize = 'micro' | 'xs' | 'sm' | 'md' | 'lg';
32
35
  export type ButtonShape = 'default' | 'pill';
36
+ export type ButtonJustify = 'center' | 'start';
37
+ export type ButtonTooltipSide = 'top' | 'right' | 'bottom' | 'left';
33
38
 
34
39
  export type ButtonProps = Omit<HTMLButtonAttributes & HTMLAnchorAttributes, 'children'> & {
35
40
  variant?: ButtonVariant | undefined;
36
41
  size?: ButtonSize | undefined;
37
42
  /** `pill` rounds the ends fully: for a chip, or a button that floats. */
38
43
  shape?: ButtonShape | undefined;
44
+ /**
45
+ * Where its contents sit: in the `center`, or at the `start`, for a wide button that reads
46
+ * as a menu's row. Either way they fill the button, so a margin of `auto` on the last
47
+ * pushes it to the end.
48
+ */
49
+ justify?: ButtonJustify | undefined;
39
50
  /** Shows a spinner and blocks interaction while keeping the button's width. */
40
51
  loading?: boolean | undefined;
41
52
  /**
@@ -53,6 +64,16 @@
53
64
  pressed?: boolean | undefined;
54
65
  /** Square button for a single icon. Give it an `aria-label`. */
55
66
  iconOnly?: boolean | undefined;
67
+ /**
68
+ * A tooltip, shown as the pointer rests on it or the keyboard reaches it: words, or a
69
+ * snippet for words with a shortcut. It says more than the button's name, which stays as
70
+ * it is. It waits as long as the app's Tooltip.Provider says, or 400ms without one.
71
+ */
72
+ tooltip?: string | Snippet | undefined;
73
+ /** The side of the button the tooltip opens on. Above it unless there is no room. */
74
+ tooltipSide?: ButtonTooltipSide | undefined;
75
+ /** The keys that do the same, shown after the tooltip's words: `['⌘', 'K']`. */
76
+ tooltipShortcut?: string[] | undefined;
56
77
  /** Renders a link styled as a button. */
57
78
  href?: string | undefined;
58
79
  sx?: Sx | undefined;
@@ -196,6 +217,7 @@
196
217
  },
197
218
 
198
219
  pill: { borderRadius: radius.pill },
220
+ start: { justifyContent: 'flex-start', textAlign: 'start' },
199
221
 
200
222
  // Primary: the one solid, flat, in the text's own colour
201
223
  primary: {
@@ -339,10 +361,11 @@
339
361
  pointerEvents: 'none'
340
362
  },
341
363
  // As wide as the button, so in a wide one something inside can be pushed to the end
364
+ // It takes the button's justification, so `sx` can set that on the button alone
342
365
  content: {
343
366
  display: 'inline-flex',
344
367
  alignItems: 'center',
345
- justifyContent: 'center',
368
+ justifyContent: 'inherit',
346
369
  flexGrow: 1,
347
370
  minWidth: 0,
348
371
  gap: 'inherit',
@@ -450,11 +473,15 @@
450
473
  variant = 'secondary',
451
474
  size = 'md',
452
475
  shape = 'default',
476
+ justify = 'center',
453
477
  loading = false,
454
478
  loadingLabel,
455
479
  disabled = false,
456
480
  pressed,
457
481
  iconOnly = false,
482
+ tooltip,
483
+ tooltipSide,
484
+ tooltipShortcut,
458
485
  href,
459
486
  type = 'button',
460
487
  sx,
@@ -474,6 +501,7 @@
474
501
  styles[size],
475
502
  iconOnly && iconStyles[size],
476
503
  (shape === 'pill' || variant === 'floating') && styles.pill,
504
+ justify === 'start' && styles.start,
477
505
  variantStyles[variant],
478
506
  disabled && styles.disabled,
479
507
  loading && styles.loading,
@@ -502,37 +530,56 @@
502
530
  {/if}
503
531
  {/snippet}
504
532
 
505
- {#if href !== undefined}
506
- <a
507
- bind:this={ref}
508
- data-icons
509
- href={inert ? undefined : href}
510
- role={inert ? 'link' : undefined}
511
- aria-disabled={inert ? true : undefined}
512
- aria-busy={loading ? true : undefined}
513
- {...rest}
514
- {...rootAttrs}
515
- >
516
- {@render content()}
517
- </a>
533
+ <!-- The element, with a tooltip trigger's props when it has a tooltip -->
534
+ {#snippet control(trigger: Record<string, unknown>)}
535
+ {@const own = Object.keys(trigger).length ? mergeProps(rest, trigger) : rest}
536
+ {#if href !== undefined}
537
+ <a
538
+ bind:this={ref}
539
+ data-icons
540
+ href={inert ? undefined : href}
541
+ role={inert ? 'link' : undefined}
542
+ aria-disabled={inert ? true : undefined}
543
+ aria-busy={loading ? true : undefined}
544
+ {...own}
545
+ {...rootAttrs}
546
+ >
547
+ {@render content()}
548
+ </a>
549
+ {:else}
550
+ <button
551
+ bind:this={ref}
552
+ data-icons
553
+ {type}
554
+ disabled={disabled || undefined}
555
+ aria-pressed={pressed}
556
+ aria-disabled={loading ? true : undefined}
557
+ aria-busy={loading ? true : undefined}
558
+ {...own}
559
+ {...rootAttrs}
560
+ onclick={loading
561
+ ? (event: MouseEvent) => {
562
+ event.preventDefault();
563
+ event.stopPropagation();
564
+ }
565
+ : (own.onclick as HTMLButtonAttributes['onclick'])}
566
+ >
567
+ {@render content()}
568
+ </button>
569
+ {/if}
570
+ {/snippet}
571
+
572
+ {#if tooltip}
573
+ <TooltipScope>
574
+ <Tooltip.Root>
575
+ <Tooltip.Trigger>
576
+ {#snippet child({ props })}{@render control(props)}{/snippet}
577
+ </Tooltip.Trigger>
578
+ <Tooltip.Content side={tooltipSide} shortcut={tooltipShortcut}>
579
+ {#if typeof tooltip === 'function'}{@render tooltip()}{:else}{tooltip}{/if}
580
+ </Tooltip.Content>
581
+ </Tooltip.Root>
582
+ </TooltipScope>
518
583
  {:else}
519
- <button
520
- bind:this={ref}
521
- data-icons
522
- {type}
523
- disabled={disabled || undefined}
524
- aria-pressed={pressed}
525
- aria-disabled={loading ? true : undefined}
526
- aria-busy={loading ? true : undefined}
527
- {...rest}
528
- {...rootAttrs}
529
- onclick={loading
530
- ? (event) => {
531
- event.preventDefault();
532
- event.stopPropagation();
533
- }
534
- : rest.onclick}
535
- >
536
- {@render content()}
537
- </button>
584
+ {@render control({})}
538
585
  {/if}
@@ -11,11 +11,19 @@ import '../../internal/icons.css';
11
11
  export type ButtonVariant = 'primary' | 'secondary' | 'soft' | 'ghost' | 'bare' | 'link' | 'floating' | 'danger' | 'danger-outline' | 'ghost-danger' | 'warning-outline' | 'success-outline';
12
12
  export type ButtonSize = 'micro' | 'xs' | 'sm' | 'md' | 'lg';
13
13
  export type ButtonShape = 'default' | 'pill';
14
+ export type ButtonJustify = 'center' | 'start';
15
+ export type ButtonTooltipSide = 'top' | 'right' | 'bottom' | 'left';
14
16
  export type ButtonProps = Omit<HTMLButtonAttributes & HTMLAnchorAttributes, 'children'> & {
15
17
  variant?: ButtonVariant | undefined;
16
18
  size?: ButtonSize | undefined;
17
19
  /** `pill` rounds the ends fully: for a chip, or a button that floats. */
18
20
  shape?: ButtonShape | undefined;
21
+ /**
22
+ * Where its contents sit: in the `center`, or at the `start`, for a wide button that reads
23
+ * as a menu's row. Either way they fill the button, so a margin of `auto` on the last
24
+ * pushes it to the end.
25
+ */
26
+ justify?: ButtonJustify | undefined;
19
27
  /** Shows a spinner and blocks interaction while keeping the button's width. */
20
28
  loading?: boolean | undefined;
21
29
  /**
@@ -33,6 +41,16 @@ export type ButtonProps = Omit<HTMLButtonAttributes & HTMLAnchorAttributes, 'chi
33
41
  pressed?: boolean | undefined;
34
42
  /** Square button for a single icon. Give it an `aria-label`. */
35
43
  iconOnly?: boolean | undefined;
44
+ /**
45
+ * A tooltip, shown as the pointer rests on it or the keyboard reaches it: words, or a
46
+ * snippet for words with a shortcut. It says more than the button's name, which stays as
47
+ * it is. It waits as long as the app's Tooltip.Provider says, or 400ms without one.
48
+ */
49
+ tooltip?: string | Snippet | undefined;
50
+ /** The side of the button the tooltip opens on. Above it unless there is no room. */
51
+ tooltipSide?: ButtonTooltipSide | undefined;
52
+ /** The keys that do the same, shown after the tooltip's words: `['⌘', 'K']`. */
53
+ tooltipShortcut?: string[] | undefined;
36
54
  /** Renders a link styled as a button. */
37
55
  href?: string | undefined;
38
56
  sx?: Sx | undefined;
@@ -11,6 +11,18 @@
11
11
  export type CommandDialogProps = {
12
12
  /** Whether it is open. Bindable. */
13
13
  open?: boolean | undefined;
14
+ /** Called as it opens or closes. */
15
+ onopenchange?: ((open: boolean) => void) | undefined;
16
+ /**
17
+ * Called as Escape is pressed. Call `event.preventDefault()` to keep it open: to go back
18
+ * a step of a palette that has several, or to wait while something is on its way.
19
+ */
20
+ onescape?: ((event: KeyboardEvent) => void) | undefined;
21
+ /**
22
+ * Whether focus goes back where it was as it closes. Pass `false` after a command that
23
+ * put the focus somewhere of its own, such as a thread it opened.
24
+ */
25
+ restoreFocus?: boolean | undefined;
14
26
  /** What the palette is, for assistive technology. */
15
27
  label?: string | undefined;
16
28
  /** A Command.Root with its Input, List and Footer. */
@@ -22,14 +34,24 @@
22
34
  let {
23
35
  open = $bindable(false),
24
36
  label = 'Command palette',
37
+ onopenchange,
38
+ onescape,
39
+ restoreFocus = true,
25
40
  children
26
41
  }: CommandDialogProps = $props();
27
42
  </script>
28
43
 
29
- <Dialog.Root bind:open>
44
+ <Dialog.Root bind:open onOpenChange={onopenchange}>
30
45
  <Dialog.Portal>
31
46
  <Dialog.Overlay {...stylex.attrs(command.overlay)} />
32
- <Dialog.Content data-slot="command-dialog" {...stylex.attrs(command.dialog)}>
47
+ <Dialog.Content
48
+ data-slot="command-dialog"
49
+ onEscapeKeydown={onescape}
50
+ onCloseAutoFocus={(event) => {
51
+ if (!restoreFocus) event.preventDefault();
52
+ }}
53
+ {...stylex.attrs(command.dialog)}
54
+ >
33
55
  <Dialog.Title {...stylex.attrs(command.hidden)}>{label}</Dialog.Title>
34
56
  {@render children?.()}
35
57
  </Dialog.Content>
@@ -2,6 +2,18 @@ import type { Snippet } from 'svelte';
2
2
  export type CommandDialogProps = {
3
3
  /** Whether it is open. Bindable. */
4
4
  open?: boolean | undefined;
5
+ /** Called as it opens or closes. */
6
+ onopenchange?: ((open: boolean) => void) | undefined;
7
+ /**
8
+ * Called as Escape is pressed. Call `event.preventDefault()` to keep it open: to go back
9
+ * a step of a palette that has several, or to wait while something is on its way.
10
+ */
11
+ onescape?: ((event: KeyboardEvent) => void) | undefined;
12
+ /**
13
+ * Whether focus goes back where it was as it closes. Pass `false` after a command that
14
+ * put the focus somewhere of its own, such as a thread it opened.
15
+ */
16
+ restoreFocus?: boolean | undefined;
5
17
  /** What the palette is, for assistive technology. */
6
18
  label?: string | undefined;
7
19
  /** A Command.Root with its Input, List and Footer. */
@@ -12,8 +12,16 @@
12
12
  Omit<Command.ItemProps, 'children' | 'child'> & {
13
13
  /** A 16px icon before the label. */
14
14
  icon?: Snippet | undefined;
15
- /** A quieter note after the label, such as where a thread lives. */
16
- detail?: string | undefined;
15
+ /**
16
+ * A quieter note after the label, such as where a thread lives: words, or a snippet,
17
+ * such as one with the words that match the search marked.
18
+ */
19
+ detail?: string | Snippet | undefined;
20
+ /**
21
+ * What the key cap on the highlighted item says Enter does. `↵` by default; words such
22
+ * as "Open" for an item that does something of its own, or `null` for none.
23
+ */
24
+ hint?: string | null | undefined;
17
25
  /** The keys that do the same from anywhere, such as "⌘N". */
18
26
  shortcut?: string | undefined;
19
27
  sx?: Sx | undefined;
@@ -27,6 +35,7 @@
27
35
  icon,
28
36
  detail,
29
37
  shortcut,
38
+ hint = '↵',
30
39
  sx,
31
40
  class: className,
32
41
  children,
@@ -40,12 +49,14 @@
40
49
  {#if icon}<span data-icons {...stylex.attrs(layer.icon)}>{@render icon()}</span>{/if}
41
50
  <span {...stylex.attrs(layer.label)}>
42
51
  {@render children?.()}
43
- {#if detail}<span {...stylex.attrs(command.detail)}>{detail}</span>{/if}
52
+ {#if detail}<span {...stylex.attrs(command.detail)}
53
+ >{#if typeof detail === 'function'}{@render detail()}{:else}{detail}{/if}</span
54
+ >{/if}
44
55
  </span>
45
56
  <span {...stylex.attrs(layer.trailing)}>
46
57
  {#if shortcut}{shortcut}{/if}
47
58
  <!-- Enter is the way to choose, shown only on the highlighted row -->
48
- {#if props['data-selected'] !== undefined}<Kbd>↵</Kbd>{/if}
59
+ {#if hint !== null && props['data-selected'] !== undefined}<Kbd>{hint}</Kbd>{/if}
49
60
  </span>
50
61
  </div>
51
62
  {/snippet}
@@ -5,8 +5,16 @@ import { type Sx } from '../../internal/sx.js';
5
5
  export type CommandItemProps = Loose<Omit<Command.ItemProps, 'children' | 'child'> & {
6
6
  /** A 16px icon before the label. */
7
7
  icon?: Snippet | undefined;
8
- /** A quieter note after the label, such as where a thread lives. */
9
- detail?: string | undefined;
8
+ /**
9
+ * A quieter note after the label, such as where a thread lives: words, or a snippet,
10
+ * such as one with the words that match the search marked.
11
+ */
12
+ detail?: string | Snippet | undefined;
13
+ /**
14
+ * What the key cap on the highlighted item says Enter does. `↵` by default; words such
15
+ * as "Open" for an item that does something of its own, or `null` for none.
16
+ */
17
+ hint?: string | null | undefined;
10
18
  /** The keys that do the same from anywhere, such as "⌘N". */
11
19
  shortcut?: string | undefined;
12
20
  sx?: Sx | undefined;
@@ -15,8 +23,16 @@ export type CommandItemProps = Loose<Omit<Command.ItemProps, 'children' | 'child
15
23
  declare const CommandItem: import("svelte").Component<Loose<Omit<Command.ItemProps, "children" | "child"> & {
16
24
  /** A 16px icon before the label. */
17
25
  icon?: Snippet | undefined;
18
- /** A quieter note after the label, such as where a thread lives. */
19
- detail?: string | undefined;
26
+ /**
27
+ * A quieter note after the label, such as where a thread lives: words, or a snippet,
28
+ * such as one with the words that match the search marked.
29
+ */
30
+ detail?: string | Snippet | undefined;
31
+ /**
32
+ * What the key cap on the highlighted item says Enter does. `↵` by default; words such
33
+ * as "Open" for an item that does something of its own, or `null` for none.
34
+ */
35
+ hint?: string | null | undefined;
20
36
  /** The keys that do the same from anywhere, such as "⌘N". */
21
37
  shortcut?: string | undefined;
22
38
  sx?: Sx | undefined;
@@ -7,17 +7,72 @@
7
7
 
8
8
  export type CommandRootProps = Loose<
9
9
  Omit<Command.RootProps, 'children' | 'child'> & {
10
+ /**
11
+ * The value of the highlighted item. Bindable: set it to move the highlight from
12
+ * outside, such as back to the first result as the app's own results change.
13
+ */
14
+ value?: string | undefined;
15
+ /**
16
+ * Whether it filters and sorts the items itself, as the search changes. Pass `false`
17
+ * to rank them yourself and list only those that match: results from a server, say.
18
+ */
19
+ shouldFilter?: boolean | undefined;
10
20
  sx?: Sx | undefined;
11
21
  /** A Command.Input, a Command.List and, if you like, a Command.Footer. */
12
22
  children?: Snippet | undefined;
13
23
  }
14
24
  >;
25
+
26
+ // Page Up and Page Down move the highlight this many items
27
+ const PAGE = 8;
15
28
  </script>
16
29
 
17
30
  <script lang="ts">
18
- let { loop = true, sx, class: className, children, ...rest }: CommandRootProps = $props();
31
+ let {
32
+ value = $bindable(''),
33
+ loop = true,
34
+ sx,
35
+ class: className,
36
+ children,
37
+ onkeydown,
38
+ ...rest
39
+ }: CommandRootProps = $props();
40
+
41
+ let root = $state<ReturnType<typeof Command.Root> | null>(null);
42
+
43
+ /** The items that can be highlighted, in order: not disabled, and not filtered out. */
44
+ export function getValidItems(): HTMLElement[] {
45
+ return root?.getValidItems() ?? [];
46
+ }
47
+
48
+ /** Highlights the item at `index` among the valid items. */
49
+ export function highlight(index: number): void {
50
+ root?.updateSelectedToIndex(index);
51
+ }
52
+
53
+ /** Moves the highlight by `step` items, stopping at the ends. */
54
+ export function move(step: number): void {
55
+ const items = getValidItems();
56
+ if (!items.length) return;
57
+ const at = items.findIndex((item) => item.hasAttribute('data-selected'));
58
+ highlight(Math.min(Math.max((at === -1 ? 0 : at) + step, 0), items.length - 1));
59
+ }
19
60
  </script>
20
61
 
21
- <Command.Root {loop} data-slot="command" {...rest} {...attrs([command.root, sx], className)}>
62
+ <Command.Root
63
+ bind:this={root}
64
+ bind:value
65
+ {loop}
66
+ data-slot="command"
67
+ {...rest}
68
+ onkeydown={(event: KeyboardEvent & { currentTarget: HTMLDivElement }) => {
69
+ onkeydown?.(event);
70
+ if (event.defaultPrevented || event.isComposing) return;
71
+ if (event.key !== 'PageDown' && event.key !== 'PageUp') return;
72
+ event.preventDefault();
73
+ move(event.key === 'PageDown' ? PAGE : -PAGE);
74
+ }}
75
+ {...attrs([command.root, sx], className)}
76
+ >
22
77
  {@render children?.()}
23
78
  </Command.Root>
@@ -3,14 +3,38 @@ import { Command } from 'bits-ui';
3
3
  import type { Snippet } from 'svelte';
4
4
  import { type Sx } from '../../internal/sx.js';
5
5
  export type CommandRootProps = Loose<Omit<Command.RootProps, 'children' | 'child'> & {
6
+ /**
7
+ * The value of the highlighted item. Bindable: set it to move the highlight from
8
+ * outside, such as back to the first result as the app's own results change.
9
+ */
10
+ value?: string | undefined;
11
+ /**
12
+ * Whether it filters and sorts the items itself, as the search changes. Pass `false`
13
+ * to rank them yourself and list only those that match: results from a server, say.
14
+ */
15
+ shouldFilter?: boolean | undefined;
6
16
  sx?: Sx | undefined;
7
17
  /** A Command.Input, a Command.List and, if you like, a Command.Footer. */
8
18
  children?: Snippet | undefined;
9
19
  }>;
10
20
  declare const CommandRoot: import("svelte").Component<Loose<Omit<Command.RootProps, "children" | "child"> & {
21
+ /**
22
+ * The value of the highlighted item. Bindable: set it to move the highlight from
23
+ * outside, such as back to the first result as the app's own results change.
24
+ */
25
+ value?: string | undefined;
26
+ /**
27
+ * Whether it filters and sorts the items itself, as the search changes. Pass `false`
28
+ * to rank them yourself and list only those that match: results from a server, say.
29
+ */
30
+ shouldFilter?: boolean | undefined;
11
31
  sx?: Sx | undefined;
12
32
  /** A Command.Input, a Command.List and, if you like, a Command.Footer. */
13
33
  children?: Snippet | undefined;
14
- }>, {}, "">;
34
+ }>, {
35
+ getValidItems: () => HTMLElement[];
36
+ highlight: (index: number) => void;
37
+ move: (step: number) => void;
38
+ }, "value">;
15
39
  type CommandRoot = ReturnType<typeof CommandRoot>;
16
40
  export default CommandRoot;