@shortround/react 0.0.0-stage → 0.5.0
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/CHANGELOG.md +40 -0
- package/LICENSE +21 -0
- package/README.md +539 -2
- package/dist/react.es.js +647 -0
- package/package.json +69 -3
- package/src/index.d.ts +243 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @shortround/react
|
|
2
|
+
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- First release. The React UI that was in `@shortround/core` up to 0.4.0, unchanged in behaviour: `ShortRoundSidekick`,
|
|
8
|
+
`ShortRoundProvider`, `useShortRound`, `useSidekick`, `useShortRoundToast`, `SidekickStoreProvider`,
|
|
9
|
+
`useSidekickStore`, the palette on cmdk, keyboard, focus and the dialog, and the theme contract's types
|
|
10
|
+
(`SidekickComponents`, `ShortRoundTheme`, `IconRegistry`, `ToastProps`). It draws `@shortround/core`, which it takes
|
|
11
|
+
as a peer (`^0.5.0`; the packages are released at one version). See MIGRATING.md, 0.5.0. Earlier history: `@shortround/core`'s CHANGELOG.
|
|
12
|
+
- `useShortRound` is `@shortround/core`'s `createPaletteStore` as a hook, with the same return value, plus
|
|
13
|
+
`revertIntentions` (the Back and Cancel rows a list asked for). Its `dispatch` chooses a row as `ShortRoundSidekick`
|
|
14
|
+
does: it keeps the Back and Cancel a result asks for, and a completion row sets the input.
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- Accepts React 19 as well as 18: the `react` and `react-dom` peers are `^18.2.0 || ^19.0.0`.
|
|
19
|
+
- The palette shows completion rows (see `@shortround/core`'s 0.5.0): choosing one, by Enter or a click, sets the
|
|
20
|
+
input and keeps the palette open; Tab applies the first enabled one, and otherwise moves focus as usual.
|
|
21
|
+
- The palette renders again when its resolver says its rows changed, and stops listening when the resolver is
|
|
22
|
+
replaced or the palette unmounts. The highlight returns to the first enabled row when the rows change that way; the
|
|
23
|
+
rule is core's palette store's (`highlightedId`), which the palette reads.
|
|
24
|
+
- `ShortRoundSidekick` takes `palette` (a catalog's palette, `buildPalette`'s result), instead of `defaultIntentions`
|
|
25
|
+
and the two resolvers: it opens on `palette.current()` with that side panel, and follows `palette.notify()` while
|
|
26
|
+
open. `defaultIntentions` is now optional.
|
|
27
|
+
- Line hints: with a palette that has `commandLine()`, the input shows each word coloured by its role and the rest of
|
|
28
|
+
the next word as ghost text, and Tab completes through the line wherever the caret is. On by default;
|
|
29
|
+
`disableLineHints` turns them off. Pickers and typed-text steps show the plain input. Each word is a `span` with
|
|
30
|
+
`data-role`, for themes. The hints and the typed-line rows are one reading, the palette store's line: a lookup is
|
|
31
|
+
called once per text, and with the caret mid-line the completion rows are the next steps at the caret.
|
|
32
|
+
|
|
33
|
+
### Patch Changes
|
|
34
|
+
|
|
35
|
+
- In a typed-text step, choosing a row whose result brings no list (only a `message`, say) no longer turns matching
|
|
36
|
+
back on: `disableInputMatching` belongs to the list a result brings (`@shortround/core` 0.5.0).
|
|
37
|
+
- A row whose id starts or ends with a space can be chosen, and shows as highlighted. cmdk trims an item's value, so
|
|
38
|
+
such a row's id came back changed and choosing it did nothing; each row now reports its own id.
|
|
39
|
+
- Uses the app's React JSX runtime (`react/jsx-runtime`) instead of a bundled copy of React 18's, which would break
|
|
40
|
+
under another React. The package ships its `CHANGELOG.md`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Christian Sepulveda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,540 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @shortround/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ShortRound for React: the command palette and sidekick, their hooks, the provider that chooses a theme, and the theme
|
|
4
|
+
contract. It draws [`@shortround/core`](https://github.com/csepulv/shortround/tree/main/packages/shortround-core),
|
|
5
|
+
which holds the palette's state, matching, the catalog engine and the constants, with no UI.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @shortround/core @shortround/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For the MUI theme the examples use: `npm install @shortround/mui`, with its peers (see its README).
|
|
14
|
+
|
|
15
|
+
`@shortround/core` is a peer: your app installs it, and imports the constants and stores from it. So are `react` and
|
|
16
|
+
`react-dom`, 18.2 or 19. The package is ES modules only. The look comes from
|
|
17
|
+
- a theme package like `@shortround/mui`, or
|
|
18
|
+
- your own components (see Custom UI Components).
|
|
19
|
+
|
|
20
|
+
It can be imported outside a browser, so your Node tests can import code that imports it.
|
|
21
|
+
|
|
22
|
+
Upgrading from an earlier version? See the
|
|
23
|
+
[migration guide](https://github.com/csepulv/shortround/blob/main/MIGRATING.md).
|
|
24
|
+
|
|
25
|
+
## Choosing a theme: ShortRoundProvider
|
|
26
|
+
|
|
27
|
+
An app chooses its theme once, at its root. Below the provider, app code depends only on this package and
|
|
28
|
+
`@shortround/core`.
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import { ShortRoundProvider, ShortRoundSidekick, useShortRoundToast } from '@shortround/react';
|
|
32
|
+
import { muiTheme } from '@shortround/mui'; // the root is the only place that names a theme
|
|
33
|
+
|
|
34
|
+
root.render(
|
|
35
|
+
<ShortRoundProvider theme={muiTheme}>
|
|
36
|
+
<App />
|
|
37
|
+
</ShortRoundProvider>
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Anywhere below:
|
|
41
|
+
<ShortRoundSidekick title="Short Round" defaultIntentions={intentions} />;
|
|
42
|
+
const { showToast } = useShortRoundToast(); // shows a toast in the theme's Toast
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
- A theme is `{ components, Toast }`: the `SidekickComponents` slots and a Toast view. The provider holds the
|
|
46
|
+
toast's state; the theme only draws it. Define the theme at module scope (or memoize it): a theme object built
|
|
47
|
+
on every render remounts its Toast.
|
|
48
|
+
- An action's `message` is shown as a toast.
|
|
49
|
+
- A nested `ShortRoundProvider` wins for its subtree.
|
|
50
|
+
- `ShortRoundSidekick`'s `SidekickComponents` and `showToast` props (the 0.2.x way to choose a look) override the provider; each prop
|
|
51
|
+
overrides only its own part (slots or toast). Without a provider, `SidekickComponents` is required. Define the
|
|
52
|
+
slot components at module scope too: a slot component created during render remounts on every render, which
|
|
53
|
+
moves focus back into the palette.
|
|
54
|
+
- `ShortRoundProvider` and `SidekickStoreProvider` are separate: one chooses the theme, the other shares one
|
|
55
|
+
palette's state (see Sidekick Store).
|
|
56
|
+
|
|
57
|
+
## A palette from a catalog
|
|
58
|
+
|
|
59
|
+
An app with a catalog hands the sidekick the palette `buildPalette` returns, instead of `defaultIntentions` and the
|
|
60
|
+
two resolvers:
|
|
61
|
+
|
|
62
|
+
```jsx
|
|
63
|
+
<ShortRoundSidekick title="Short Round" palette={palette} />
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The sidekick opens on `palette.current()` (the commands the app's state allows now) with that state's side panel.
|
|
67
|
+
While it is open, it follows `palette.notify()`: call it when your app's state changes outside the palette, and the
|
|
68
|
+
list re-reads. Build the palette once. See `@shortround/core`'s
|
|
69
|
+
[ADOPTING-A-CATALOG.md](https://github.com/csepulv/shortround/blob/main/packages/shortround-core/ADOPTING-A-CATALOG.md).
|
|
70
|
+
|
|
71
|
+
**Line hints.** With a palette, the input shows how the line is being read: each word marked with its role, and, when the
|
|
72
|
+
caret is at the end of the line, the rest of the next word as ghost text (`@shortround/mui` greys it). Tab completes through the line, wherever the caret is; Enter runs the
|
|
73
|
+
highlighted row as before. Pickers and typed-text steps show the plain input. Turn the hints off with
|
|
74
|
+
`disableLineHints`:
|
|
75
|
+
|
|
76
|
+
```jsx
|
|
77
|
+
<ShortRoundSidekick palette={palette} disableLineHints />
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A theme colours the words: each is a `span` with `data-role` (`verb`, `text`, `hint`, `param`, `value`, `unread`,
|
|
81
|
+
`broken`, and `ghost` for the ghost), inside the element the theme's `IntentionPalette.Input` slot already receives.
|
|
82
|
+
`@shortround/mui` colours them from its theme's palette. The words line up under the input on their own: they copy
|
|
83
|
+
the input's position and text style. A theme's `data-role` rules should change only colour and decoration: the
|
|
84
|
+
words sit under the input's own text, so anything that changes a word's width (weight, size, font, letter spacing)
|
|
85
|
+
moves the words after it away from the caret.
|
|
86
|
+
|
|
87
|
+
## Icons
|
|
88
|
+
|
|
89
|
+
An intention's `icon` is a name. Without anything more, the theme draws its own icon for that name (see the theme's
|
|
90
|
+
README for its names). To use your own icons, from any library, give the provider an icon registry once:
|
|
91
|
+
|
|
92
|
+
```jsx
|
|
93
|
+
const icons = {
|
|
94
|
+
Icon: MaterialSymbol, // draws any name: <Icon name="forum" />
|
|
95
|
+
names: { chat: 'forum' }, // an intention's icon name → the name Icon (or the theme) knows
|
|
96
|
+
intentions: { 'app.send': ChatBubble } // an icon for one intention, by its id
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
<ShortRoundProvider theme={muiTheme} icons={icons}>
|
|
100
|
+
<App />
|
|
101
|
+
</ShortRoundProvider>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Each row's icon is the first that applies:
|
|
105
|
+
|
|
106
|
+
1. the intention's entry in `intentions`, by id (even if the intention has no `icon`);
|
|
107
|
+
2. none, when the intention has no `icon`; the theme keeps the space, so titles line up;
|
|
108
|
+
3. for Back and Cancel, the theme's icons;
|
|
109
|
+
4. otherwise the name, mapped by `names`, drawn by `Icon`;
|
|
110
|
+
5. without `Icon`, the theme's icon for the name;
|
|
111
|
+
6. the theme's placeholder, for a name it doesn't know.
|
|
112
|
+
|
|
113
|
+
- `Icon` draws every named row but Back and Cancel. An icon font can't tell a name it lacks, so with `Icon` the
|
|
114
|
+
placeholder doesn't appear.
|
|
115
|
+
- Back and Cancel keep the theme's icons, since your icon set may have no name for them. To change them, use
|
|
116
|
+
`intentions: { [RevertIntentionIds.BACK]: MyBack }`.
|
|
117
|
+
- Your icons draw at the size you give them.
|
|
118
|
+
- Define the registry at module scope (or memoize it), like the theme.
|
|
119
|
+
|
|
120
|
+
## The side panel (sidecar)
|
|
121
|
+
|
|
122
|
+
The side panel shows content beside the palette. By default, palette navigation drives it:
|
|
123
|
+
|
|
124
|
+
- an action's result opens it (`sideEffects.sidecarRenderer`) or closes it (`sideEffects.closeSidecar`); a result
|
|
125
|
+
with neither leaves it as it is;
|
|
126
|
+
- Back replays the result of the last earlier step that brought a list (a step that only showed a message or
|
|
127
|
+
changed the panel doesn't count);
|
|
128
|
+
- Back to the start list and Cancel show the start list's panel (`defaultSidecarRenderer`), or close it;
|
|
129
|
+
- opening the palette shows the start list's panel, if any;
|
|
130
|
+
- closing the palette, however it closes, clears it. The panel never outlives the palette.
|
|
131
|
+
|
|
132
|
+
To decide changes yourself, give `ShortRoundSidekick` a `sidecarReducer`. Core proposes each change but a close, with
|
|
133
|
+
a reason, and applies what the reducer returns:
|
|
134
|
+
|
|
135
|
+
```jsx
|
|
136
|
+
// Keep the chat across Back and Cancel, and bring it back when the palette opens again.
|
|
137
|
+
// (renderChat and chat.wasShowing are your app's: its chat renderer, and whether the chat showed at the last close.)
|
|
138
|
+
const keepChat = (current, proposed, { reason }) => {
|
|
139
|
+
if (reason === 'open') return chat.wasShowing ? { renderer: renderChat } : proposed;
|
|
140
|
+
if (['start', 'reset', 'back'].includes(reason) && current.renderer === renderChat) return current;
|
|
141
|
+
return proposed;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
<ShortRoundSidekick defaultIntentions={intentions} sidecarReducer={keepChat} />
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
- A change is `{ renderer }` or `{ renderer: null }`; `current` has the same shape. Returning `current` keeps the
|
|
148
|
+
panel as it is, without remounting its content.
|
|
149
|
+
- Reasons: `result`, `back`, `start` (Back to the start list), `reset` (Cancel), `open`, `app`.
|
|
150
|
+
- From app code, inside or outside React: `store.openSidecar(renderer)` and `store.closeSidecar()` (reason `app`), or
|
|
151
|
+
`useSidekick()`'s `setSidecarRenderer` and `closeSidecar`, which do the same. Call them while the palette is open:
|
|
152
|
+
opening the palette replaces a panel set while it was closed, unless your reducer keeps it (returns `current` for
|
|
153
|
+
`open`).
|
|
154
|
+
|
|
155
|
+
## Keyboard, focus and screen readers
|
|
156
|
+
|
|
157
|
+
The sidekick is a non-modal dialog: the page stays usable while it's open, and clicking outside doesn't close it.
|
|
158
|
+
A modal of your own that holds focus (such as MUI's `Drawer` or `Dialog`) pulls focus back from the sidekick's input,
|
|
159
|
+
so it can't be typed in while that modal is open; give the modal `disableEnforceFocus` (MUI:
|
|
160
|
+
`ModalProps={{ disableEnforceFocus: true }}` on a `Drawer`).
|
|
161
|
+
|
|
162
|
+
- **Cmd/Ctrl+K** opens it from anywhere, or brings focus back into it when it is already open.
|
|
163
|
+
`installKeyboardShortcuts={false}` turns off this global shortcut only.
|
|
164
|
+
- On open, focus moves to its input (to the sidekick itself in compact size); on close, focus returns to where it
|
|
165
|
+
was, unless the user has since moved it elsewhere in the page. After a row is clicked, focus goes back to the
|
|
166
|
+
input, so typing carries on.
|
|
167
|
+
- **Escape** closes it when pressed inside it, so Escape in your own dialogs and fields stays yours. Content inside
|
|
168
|
+
the sidekick (a widget in the side panel) sees Escape first and can keep it with `event.preventDefault()`.
|
|
169
|
+
- **Enter** runs the highlighted row. After every new list the highlight is on the list's first enabled row of your
|
|
170
|
+
own, so the first Enter in a picker or a typed-text prompt runs it rather than Back. On a completion row (see Typed
|
|
171
|
+
input), Enter completes the input instead of running anything.
|
|
172
|
+
- **Tab**, with a catalog palette, first completes through the line (see Line hints). Otherwise it completes the input
|
|
173
|
+
with the first enabled completion row, when the list has one; otherwise Tab moves focus as usual.
|
|
174
|
+
- **Arrow keys** move through the rows and wrap around at either end. Back and Cancel, when a list shows them, are
|
|
175
|
+
drawn at the top under **Navigate** but come last for the keyboard: ArrowUp from the first row reaches them.
|
|
176
|
+
- It is announced as a dialog named by `title` (or "ShortRound"), and its controls carry `ControlLabels`.
|
|
177
|
+
- `ShortRoundSidekick`'s `onClose` is called after the sidekick closes, whatever closed it: the close control,
|
|
178
|
+
Escape, or the app through the store.
|
|
179
|
+
|
|
180
|
+
## Hooks
|
|
181
|
+
|
|
182
|
+
### useShortRound
|
|
183
|
+
|
|
184
|
+
Manages the list of intentions and fuzzy matching of user input, for a palette you draw yourself. It is
|
|
185
|
+
`@shortround/core`'s palette store (`createPaletteStore`) as a hook.
|
|
186
|
+
|
|
187
|
+
```jsx
|
|
188
|
+
import { useShortRound } from '@shortround/react';
|
|
189
|
+
|
|
190
|
+
const {
|
|
191
|
+
inputValue,
|
|
192
|
+
onInputChange,
|
|
193
|
+
intentions,
|
|
194
|
+
revertIntentions,
|
|
195
|
+
dispatch,
|
|
196
|
+
reset,
|
|
197
|
+
back,
|
|
198
|
+
inputMessage,
|
|
199
|
+
dispatchedStack
|
|
200
|
+
} = useShortRound({
|
|
201
|
+
defaultIntentions
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Parameters:**
|
|
206
|
+
- `defaultIntentions: Intention[]` - Initial set of intentions to display
|
|
207
|
+
- `resolveDefaultInput?: (text: string) => Intention[]` - Turns typed text into extra rows for the default list
|
|
208
|
+
(see Typed input). `ShortRoundSidekick` takes the same prop
|
|
209
|
+
- `resolveDefaultFallbackInput?: ResolveFallbackInput` - Adds rows after the default list's matches (see Typed input).
|
|
210
|
+
`ShortRoundSidekick` takes the same prop
|
|
211
|
+
|
|
212
|
+
**Returns:**
|
|
213
|
+
- `inputValue: string` - Current input text
|
|
214
|
+
- `onInputChange: (value: string) => void` - Update input and trigger fuzzy matching
|
|
215
|
+
- `intentions: Intention[]` - Filtered intentions based on input
|
|
216
|
+
- `revertIntentions: Intention[]` - The Back and Cancel rows the current list asked for; draw them under Navigate
|
|
217
|
+
(see System Intentions)
|
|
218
|
+
- `dispatch: (intentionId: string) => Promise<IntentionActionResult>` - Choose a row, as `ShortRoundSidekick` does: a
|
|
219
|
+
completion row sets the input; any other runs, and keeps the Back and Cancel its result asks for
|
|
220
|
+
- `reset: () => Promise<IntentionActionResult>` - Reset to default intentions
|
|
221
|
+
- `back: () => Promise<IntentionActionResult>` - Go back to previous intention set
|
|
222
|
+
- `inputMessage?: { type: string; text: string }` - Validation message to display
|
|
223
|
+
- `dispatchedStack: { intention: Intention; result: IntentionActionResult }[]` - History of executed intentions,
|
|
224
|
+
each with the result its action returned. Back drops the step that brought the current list, and the steps after
|
|
225
|
+
it, and returns to the list of the last earlier step that brought one; a step whose result brings no list
|
|
226
|
+
(`intentions`) leaves the list, and its Back and Cancel, as they are
|
|
227
|
+
|
|
228
|
+
### useSidekick
|
|
229
|
+
|
|
230
|
+
UI controller that manages the popover (overlay) and sidekick drawer.
|
|
231
|
+
|
|
232
|
+
```jsx
|
|
233
|
+
import { useSidekick } from '@shortround/react';
|
|
234
|
+
|
|
235
|
+
const {
|
|
236
|
+
isOpen,
|
|
237
|
+
isSidecarOpen,
|
|
238
|
+
onOpen,
|
|
239
|
+
onClose,
|
|
240
|
+
setSidecarRenderer,
|
|
241
|
+
closeSidecar,
|
|
242
|
+
height,
|
|
243
|
+
anchorOrigin,
|
|
244
|
+
cycleAnchorOrigin,
|
|
245
|
+
// ... other properties
|
|
246
|
+
} = useSidekick({
|
|
247
|
+
store, // optional: external store
|
|
248
|
+
initial: { size: 'medium' } // optional: initial state
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
**Parameters:**
|
|
253
|
+
- `store?: SidekickStore` - External store instance
|
|
254
|
+
- `initial?: Partial<SidekickStoreState>` - Initial state overrides, used only when there is no `store` and no
|
|
255
|
+
`SidekickStoreProvider` above
|
|
256
|
+
|
|
257
|
+
**Returns:** Full sidekick state and control methods (see TypeScript definitions for complete interface). Among
|
|
258
|
+
them, `anchorPosition` is the CSS position for the current `anchorOrigin`; it is worked out, not stored.
|
|
259
|
+
|
|
260
|
+
## Intention Object
|
|
261
|
+
|
|
262
|
+
```jsx
|
|
263
|
+
{
|
|
264
|
+
id: 'unique-identifier',
|
|
265
|
+
title: 'Display Name',
|
|
266
|
+
group: 'Category Name', // optional: rows without one share one unnamed group
|
|
267
|
+
aliases: ['search', 'terms'], // optional
|
|
268
|
+
icon: 'icon-name', // optional: a name (see Icons)
|
|
269
|
+
disabled: false, // optional
|
|
270
|
+
completion: 'search grid with ', // optional: a completion row sets the input to this; action isn't called
|
|
271
|
+
validate: (input) => ({ valid: input.length > 0, message: 'Type a name first' }), // optional: a returned message shows
|
|
272
|
+
action: async (input) => {
|
|
273
|
+
// Your logic here
|
|
274
|
+
return {
|
|
275
|
+
intentions: newIntentions, // optional: new intention set
|
|
276
|
+
shouldReset: false, // optional: close palette
|
|
277
|
+
disableInputMatching: false, // optional, with intentions: that list ignores typed text (a typed-text step)
|
|
278
|
+
resolveInput: (text) => [], // optional: rows from typed text, for the new list (see Typed input)
|
|
279
|
+
includeRevertIntentions: [RevertIntentionIds.BACK], // optional: add back/cancel
|
|
280
|
+
message: 'Success!', // optional: toast notification
|
|
281
|
+
sideEffects: {
|
|
282
|
+
sidecarRenderer: () => <MyComponent /> // optional: show sidekick content
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## Typed input
|
|
290
|
+
|
|
291
|
+
A resolver turns what the user types into rows of its own, so a line such as `sort created newest` can become one
|
|
292
|
+
row that runs directly. The default list's resolver is `resolveDefaultInput`; a list an action returns brings its
|
|
293
|
+
own as `resolveInput`, or none.
|
|
294
|
+
|
|
295
|
+
- Resolved rows show first, then the fuzzy matches; a resolved row with a listed row's id replaces it.
|
|
296
|
+
- Back restores the previous list's resolver.
|
|
297
|
+
- A resolver runs on every render while text is typed, so keep it quick and synchronous. One that throws adds no
|
|
298
|
+
rows and logs why. It adds nothing during a typed-text step (`disableInputMatching`).
|
|
299
|
+
- Memoize it, like `defaultIntentions`: a new resolver resets the list and the input.
|
|
300
|
+
- A resolver whose rows can change while the input doesn't (an async lookup landed) may carry
|
|
301
|
+
`subscribe(listener)`, returning a function that stops the listening. The palette subscribes to the current
|
|
302
|
+
resolver, renders again each time the listener is called, and unsubscribes when the resolver is replaced or the
|
|
303
|
+
palette unmounts.
|
|
304
|
+
|
|
305
|
+
```js
|
|
306
|
+
const resolveInput = (text) => engine.rowsFor(text); // (engine is your app's)
|
|
307
|
+
resolveInput.subscribe = (listener) => engine.onChange(listener); // returns an unsubscribe function
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
A row with `completion` is a completion row: choosing it (Enter or a click) sets the input to `completion` and keeps
|
|
311
|
+
the palette open; its `action` isn't called and Back doesn't step back over it. Tab applies the first enabled
|
|
312
|
+
completion row. A resolver can offer completions beside runnable rows, so a user builds a line step by step: `search`
|
|
313
|
+
→ `search grid with ` → `search grid with tags react`.
|
|
314
|
+
|
|
315
|
+
A fallback adds rows after the matches, such as "Send as chat: <text>" for a question no command matches. The default
|
|
316
|
+
list's is `resolveDefaultFallbackInput`; a list an action returns brings its own as `resolveFallbackInput`, or none (a
|
|
317
|
+
picker, say).
|
|
318
|
+
|
|
319
|
+
```jsx
|
|
320
|
+
// (chat.send is your app's.)
|
|
321
|
+
const sendAsChat = (text, { matches }) =>
|
|
322
|
+
matches.length
|
|
323
|
+
? []
|
|
324
|
+
: [{ id: 'send-as-chat', title: `Send as chat: ${text}`, group: 'Chat', action: () => chat.send(text) }];
|
|
325
|
+
|
|
326
|
+
<ShortRoundSidekick defaultIntentions={intentions} resolveDefaultFallbackInput={sendAsChat} />
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
- It's called while text is typed (not on a blank input, not during a typed-text step) with `matches`: what the list
|
|
330
|
+
shows before it, typed-line rows then fuzzy matches. Return rows only when `matches` is empty for an "unmatched"
|
|
331
|
+
row, or always to offer them after any matches.
|
|
332
|
+
- Its rows can be chosen; a row whose id is already listed is left out. They come last when their `group` is one no
|
|
333
|
+
match uses (`Chat` above). A row in a match's group, or with no group while ungrouped matches show, is drawn at the
|
|
334
|
+
end of that group, before later groups.
|
|
335
|
+
- Start and Cancel restore the default list's; Back restores the earlier list's.
|
|
336
|
+
- Keep it quick and synchronous, and memoize it: a new function resets the list and the input. So read app state that
|
|
337
|
+
changes (such as whether the chat shows) when the fallback runs, through a ref or a store, rather than closing over
|
|
338
|
+
it. One that throws adds no rows and logs why.
|
|
339
|
+
|
|
340
|
+
`@shortround/core`'s catalog engine (`loadCatalog`, `buildPalette`) builds the intentions, the resolver and (given
|
|
341
|
+
one) the fallback from a YAML catalog: see its
|
|
342
|
+
[CATALOG.md](https://github.com/csepulv/shortround/tree/main/packages/shortround-core/CATALOG.md).
|
|
343
|
+
|
|
344
|
+
## System Intentions
|
|
345
|
+
|
|
346
|
+
ShortRound provides built-in system intentions for navigation, from `@shortround/core`:
|
|
347
|
+
|
|
348
|
+
```jsx
|
|
349
|
+
import { RevertIntentionIds, makeRevertIntentionsFor } from '@shortround/core';
|
|
350
|
+
|
|
351
|
+
// Available system intentions
|
|
352
|
+
RevertIntentionIds.BACK // Go back to previous intention set
|
|
353
|
+
RevertIntentionIds.RESET // Reset to default intentions
|
|
354
|
+
|
|
355
|
+
// Include them in your action results
|
|
356
|
+
return {
|
|
357
|
+
intentions: myIntentions,
|
|
358
|
+
includeRevertIntentions: [RevertIntentionIds.BACK, RevertIntentionIds.RESET]
|
|
359
|
+
};
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
They form a group of their own, `Navigate`, separate from any group of yours (even one with the same name), drawn
|
|
363
|
+
at the top of the list and last for the keyboard (see Keyboard, focus and screen readers).
|
|
364
|
+
|
|
365
|
+
`ShortRoundSidekick` draws them. A palette of your own on `useShortRound` draws the hook's `revertIntentions`, and
|
|
366
|
+
chooses one with `dispatch` (for example `dispatch(RevertIntentionIds.BACK)`).
|
|
367
|
+
|
|
368
|
+
## Sidekick Store
|
|
369
|
+
|
|
370
|
+
For advanced usage, you can create and manage the sidekick store manually. The store is `@shortround/core`'s; it works
|
|
371
|
+
outside React too.
|
|
372
|
+
|
|
373
|
+
```jsx
|
|
374
|
+
import { createSidekickStore } from '@shortround/core';
|
|
375
|
+
import { SidekickStoreProvider } from '@shortround/react';
|
|
376
|
+
|
|
377
|
+
const store = createSidekickStore({
|
|
378
|
+
size: 'full',
|
|
379
|
+
anchorOrigin: 'center',
|
|
380
|
+
commandWidth: '50vw'
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
function App() {
|
|
384
|
+
return (
|
|
385
|
+
<SidekickStoreProvider store={store}>
|
|
386
|
+
{/* Your components */}
|
|
387
|
+
</SidekickStoreProvider>
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
To open the palette somewhere else, name the place: `createSidekickStore({ anchorOrigin: 'bottom-left' })`, or the
|
|
393
|
+
same in `ShortRoundSidekick`'s `initialSidekickState`. Each of the nine `AnchorPositions` is placed with CSS alone,
|
|
394
|
+
so it stays in place when the window is resized.
|
|
395
|
+
|
|
396
|
+
## Constants
|
|
397
|
+
|
|
398
|
+
From `@shortround/core`:
|
|
399
|
+
|
|
400
|
+
```jsx
|
|
401
|
+
import { AnchorPositions, ControlLabels, Sizes } from '@shortround/core';
|
|
402
|
+
|
|
403
|
+
// Available anchor positions
|
|
404
|
+
AnchorPositions.CENTER, AnchorPositions.TOP_LEFT, etc.
|
|
405
|
+
|
|
406
|
+
// Available sizes
|
|
407
|
+
Sizes.COMPACT, Sizes.MEDIUM, Sizes.FULL
|
|
408
|
+
|
|
409
|
+
// The control bar's accessible names, passed to a theme's ControlBar as `labels`
|
|
410
|
+
ControlLabels.close, ControlLabels.anchor, ControlLabels.size, ControlLabels.sizes.compact
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
## TypeScript Support
|
|
414
|
+
|
|
415
|
+
This package includes full TypeScript definitions. Key types:
|
|
416
|
+
|
|
417
|
+
- `UseShortRoundResult` - Hook return type
|
|
418
|
+
- `UseSidekickResult` - Hook return type
|
|
419
|
+
- `AnchorPositionStyle` - The CSS position `useSidekick` returns as `anchorPosition`
|
|
420
|
+
- `SidekickComponents` - Interface for custom UI implementations
|
|
421
|
+
- `ShortRoundTheme` - What `ShortRoundProvider` takes: `{ components, Toast }`
|
|
422
|
+
- `IconRegistry` - The app's own icons: `{ Icon, names, intentions }` (see Icons)
|
|
423
|
+
- `ToastProps` - The Toast view's props
|
|
424
|
+
|
|
425
|
+
The headless types are `@shortround/core`'s: `Intention`, `IntentionActionResult`, `SidekickStore`, `SidecarReducer`,
|
|
426
|
+
`SidecarChange`, `SidecarReason` (see The side panel), and `ResolveInput`, `ResolveFallbackInput` (see Typed input).
|
|
427
|
+
|
|
428
|
+
## Custom UI Components
|
|
429
|
+
|
|
430
|
+
While we provide pre-built UI packages like `@shortround/mui`, you can create your own theme. Slots only draw:
|
|
431
|
+
ShortRound renders cmdk (the input, each row, keyboard and selection), moves focus and announces the sidekick, so a
|
|
432
|
+
theme imports no cmdk. Row styling goes in the palette frame, through cmdk's attributes (`[cmdk-item]`,
|
|
433
|
+
`[data-selected="true"]`). The test hooks (`data-testid`) below are the ones the conformance suite looks for.
|
|
434
|
+
|
|
435
|
+
```jsx
|
|
436
|
+
const SIZES = ['compact', 'medium', 'full'];
|
|
437
|
+
|
|
438
|
+
const MySidekickComponents = {
|
|
439
|
+
IntentionPalette: {
|
|
440
|
+
Frame: ({ height, children }) => <div style={{ height }}>{children}</div>,
|
|
441
|
+
// `input` is ShortRound's input element; place it.
|
|
442
|
+
Input: ({ input, inputMessage }) => (
|
|
443
|
+
<div>
|
|
444
|
+
{input}
|
|
445
|
+
{inputMessage && <p>{inputMessage.text}</p>}
|
|
446
|
+
</div>
|
|
447
|
+
),
|
|
448
|
+
// Drawn inside ShortRound's row, which handles click, keyboard and disabled. ShortRound chooses `icon`;
|
|
449
|
+
// the space stays when there is none, so titles line up.
|
|
450
|
+
Item: ({ intention, icon }) => (
|
|
451
|
+
<div>
|
|
452
|
+
<span style={{ display: 'inline-block', width: 18 }}>{icon}</span>
|
|
453
|
+
<span>{intention.title}</span>
|
|
454
|
+
{intention.subtitle && <small>{intention.subtitle}</small>}
|
|
455
|
+
</div>
|
|
456
|
+
),
|
|
457
|
+
Group: ({ name }) => <div className="group-header">{name}</div>,
|
|
458
|
+
NoMatches: ({ inputValue }) => (
|
|
459
|
+
<div data-testid="shortround-no-matches">No matches for "{inputValue}"</div>
|
|
460
|
+
)
|
|
461
|
+
},
|
|
462
|
+
Sidekick: {
|
|
463
|
+
Frame: ({ height, commandWidth, children }) => (
|
|
464
|
+
<div style={{ height, width: commandWidth }}>{children}</div>
|
|
465
|
+
),
|
|
466
|
+
// `labels` are ShortRound's accessible names (ControlLabels); apply them.
|
|
467
|
+
ControlBar: ({ title, labels, onClose, setSize, cycleAnchorOrigin }) => (
|
|
468
|
+
<div>
|
|
469
|
+
<span>{title}</span>
|
|
470
|
+
<button aria-label={labels.anchor} data-testid="shortround-anchor" onClick={cycleAnchorOrigin}>
|
|
471
|
+
⌖
|
|
472
|
+
</button>
|
|
473
|
+
<div aria-label={labels.size} role="group">
|
|
474
|
+
{SIZES.map((size) => (
|
|
475
|
+
<button
|
|
476
|
+
aria-label={labels.sizes[size]}
|
|
477
|
+
data-testid={`shortround-size-${size}`}
|
|
478
|
+
key={size}
|
|
479
|
+
onClick={() => setSize(size)}
|
|
480
|
+
>
|
|
481
|
+
{size[0].toUpperCase()}
|
|
482
|
+
</button>
|
|
483
|
+
))}
|
|
484
|
+
</div>
|
|
485
|
+
<button aria-label={labels.close} data-testid="shortround-close" onClick={onClose}>
|
|
486
|
+
×
|
|
487
|
+
</button>
|
|
488
|
+
</div>
|
|
489
|
+
),
|
|
490
|
+
Popover: ({ anchorPosition, height, totalWidth, children }) => (
|
|
491
|
+
<div style={{ position: 'fixed', ...anchorPosition, height, width: totalWidth, display: 'flex' }}>
|
|
492
|
+
{children}
|
|
493
|
+
</div>
|
|
494
|
+
)
|
|
495
|
+
},
|
|
496
|
+
SidecarContent: ({ isOpen, renderSidecar, height }) => (
|
|
497
|
+
<div style={{ height, display: isOpen ? 'block' : 'none' }}>
|
|
498
|
+
{renderSidecar?.()}
|
|
499
|
+
</div>
|
|
500
|
+
),
|
|
501
|
+
// The theme's icons by name; `back` and `cancel` for Back and Cancel, `unknown` for a name it doesn't know.
|
|
502
|
+
// Kept with the slots, so apps passing SidekickComponents without a provider get them too.
|
|
503
|
+
// (SearchIcon and the others stand for your own icon components.)
|
|
504
|
+
icons: { search: SearchIcon, back: BackIcon, cancel: CancelIcon, unknown: UnknownIcon }
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// A theme is the slots plus a Toast view; ShortRoundProvider holds the toast's state.
|
|
508
|
+
const myTheme = {
|
|
509
|
+
components: MySidekickComponents,
|
|
510
|
+
Toast: ({ isOpen, message, onClose }) =>
|
|
511
|
+
isOpen ? (
|
|
512
|
+
<div data-testid="shortround-toast" role="status" onClick={onClose}>
|
|
513
|
+
{message}
|
|
514
|
+
</div>
|
|
515
|
+
) : null
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// Use your theme at the app's root
|
|
519
|
+
<ShortRoundProvider theme={myTheme}>
|
|
520
|
+
<App />
|
|
521
|
+
</ShortRoundProvider>
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
What each slot must do, beyond its props, is written in the JSDoc on `SidekickComponents` in this package's types.
|
|
525
|
+
To check a theme against those obligations, run the conformance suite from your theme's tests: see
|
|
526
|
+
[`@shortround/conformance`](https://github.com/csepulv/shortround/tree/main/packages/shortround-conformance)
|
|
527
|
+
(GitHub only, not on npm).
|
|
528
|
+
|
|
529
|
+
**For a complete implementation example**, see the [@shortround/mui package source code](https://github.com/csepulv/shortround/tree/main/packages/shortround-mui/src) which demonstrates proper component integration with Material-UI.
|
|
530
|
+
|
|
531
|
+
## Other Packages
|
|
532
|
+
|
|
533
|
+
- **[@shortround/core](https://github.com/csepulv/shortround/tree/main/packages/shortround-core)** - The palette
|
|
534
|
+
without a UI: its state, matching, the catalog engine and the constants
|
|
535
|
+
- **[@shortround/mui](https://github.com/csepulv/shortround/tree/main/packages/shortround-mui)** - Material-UI components
|
|
536
|
+
- See the [GitHub repo](https://github.com/csepulv/shortround) for other package details and examples
|
|
537
|
+
|
|
538
|
+
## License
|
|
539
|
+
|
|
540
|
+
MIT
|