@shortround/react 0.0.0-stage → 0.6.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 +58 -0
- package/LICENSE +21 -0
- package/README.md +578 -2
- package/dist/react.es.js +652 -0
- package/package.json +69 -3
- package/src/index.d.ts +266 -0
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shortround/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "ShortRound for React: the command palette, the sidekick, the provider and the theme contract, on @shortround/core.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"shortround",
|
|
7
|
+
"command palette",
|
|
8
|
+
"intentional ui",
|
|
9
|
+
"react",
|
|
10
|
+
"hooks"
|
|
11
|
+
],
|
|
12
|
+
"author": "Chris Sepulveda <csepulv@gmail.com>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/csepulv/shortround.git",
|
|
17
|
+
"directory": "packages/shortround-react"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/csepulv/shortround/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/csepulv/shortround#readme",
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/react.es.js",
|
|
28
|
+
"module": "./dist/react.es.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./src/index.d.ts",
|
|
32
|
+
"import": "./dist/react.es.js",
|
|
33
|
+
"default": "./dist/react.es.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src/index.d.ts",
|
|
39
|
+
"CHANGELOG.md"
|
|
40
|
+
],
|
|
41
|
+
"types": "./src/index.d.ts",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@shortround/core": "0.6.0",
|
|
44
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
45
|
+
"@testing-library/react": "^16.3.0",
|
|
46
|
+
"@types/react": "19.1.8",
|
|
47
|
+
"@vitejs/plugin-react": "^4.6.0",
|
|
48
|
+
"jest-extended": "^6.0.0",
|
|
49
|
+
"jsdom": "^26.1.0",
|
|
50
|
+
"react": "^18.2.0",
|
|
51
|
+
"vite": "^7.3.6",
|
|
52
|
+
"typescript": "5.8.3",
|
|
53
|
+
"vitest": "^3.2.4"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@shortround/core": "^0.6.0",
|
|
57
|
+
"react": "^18.2.0 || ^19.0.0",
|
|
58
|
+
"react-dom": "^18.2.0 || ^19.0.0"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"cmdk": "^1.1.1",
|
|
62
|
+
"framer-motion": "^12.19.1",
|
|
63
|
+
"tiny-invariant": "^1.3.3"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "vite build",
|
|
67
|
+
"dev": "vite build --watch",
|
|
68
|
+
"test": "vitest run",
|
|
69
|
+
"types:check": "tsc -p types-check",
|
|
70
|
+
"test:node-import": "node scripts/check-node-import.js"
|
|
71
|
+
}
|
|
6
72
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// @shortround/react's types: the UI. The headless types (intentions, results, resolvers, the sidekick store, the
|
|
2
|
+
// constants) are @shortround/core's; this file imports them and doesn't re-export them.
|
|
3
|
+
import type {
|
|
4
|
+
AnchorPosition,
|
|
5
|
+
CommandLineState,
|
|
6
|
+
ControlLabels,
|
|
7
|
+
Intention,
|
|
8
|
+
IntentionActionResult,
|
|
9
|
+
PaletteSource,
|
|
10
|
+
ResolveFallbackInput,
|
|
11
|
+
ResolveInput,
|
|
12
|
+
SidecarReducer,
|
|
13
|
+
SidekickStore,
|
|
14
|
+
SidekickStoreState
|
|
15
|
+
} from '@shortround/core';
|
|
16
|
+
import { JSX, ReactNode, RefObject } from 'react';
|
|
17
|
+
|
|
18
|
+
/** Where the sidekick sits for an anchor origin, as CSS position properties. */
|
|
19
|
+
export interface AnchorPositionStyle {
|
|
20
|
+
top?: number | string;
|
|
21
|
+
left?: number | string;
|
|
22
|
+
right?: number | string;
|
|
23
|
+
bottom?: number | string;
|
|
24
|
+
transform?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface UseSidekickResult extends SidekickStoreState {
|
|
28
|
+
/** The position for the current `anchorOrigin`. */
|
|
29
|
+
anchorPosition: AnchorPositionStyle;
|
|
30
|
+
onOpen: () => void;
|
|
31
|
+
setIsOpen: (val: boolean) => void;
|
|
32
|
+
onClose: () => void;
|
|
33
|
+
height: string;
|
|
34
|
+
setSize: (val: string) => void;
|
|
35
|
+
setAnchorOrigin: (origin: AnchorPosition) => void;
|
|
36
|
+
cycleAnchorOrigin: () => void;
|
|
37
|
+
/** The store's `openSidecar`: through the sidekick's `sidecarReducer` (reason `app`). */
|
|
38
|
+
setSidecarRenderer: (renderer: () => ReactNode) => void;
|
|
39
|
+
commandWidth: string;
|
|
40
|
+
sidecarWidth: string;
|
|
41
|
+
/** The store's `closeSidecar`: through the sidekick's `sidecarReducer` (reason `app`). */
|
|
42
|
+
closeSidecar: () => void;
|
|
43
|
+
renderSidecar: () => ReactNode | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A theme's slots (`ShortRoundTheme.components`): views only. The palette renders cmdk (the input, each row, keyboard
|
|
48
|
+
* and selection), focus and the dialog; a theme imports no cmdk. Each slot's obligations are checked by
|
|
49
|
+
* `@shortround/conformance` (`describeThemeContract`), which finds a theme's controls by the `data-testid`s named below.
|
|
50
|
+
*/
|
|
51
|
+
export interface SidekickComponents {
|
|
52
|
+
IntentionPalette: {
|
|
53
|
+
/** Renders its children. Row styling goes here, through cmdk's attributes (`[cmdk-item]`, `[data-selected]`). */
|
|
54
|
+
Frame: React.ComponentType<{ height: string; children: ReactNode }>;
|
|
55
|
+
/** Places `input` (the palette's cmdk input, `data-testid="shortround-input"`) and shows `inputMessage.text`. */
|
|
56
|
+
Input: React.ComponentType<{
|
|
57
|
+
input: ReactNode;
|
|
58
|
+
inputMessage?: { type: string; text: string };
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* Draws one row inside the palette's cmdk item (`data-testid="shortround-item-<id>"`): shows the title and
|
|
62
|
+
* subtitle, and shows `icon` when given (the palette chooses it; see `IconRegistry`). A row without an icon should
|
|
63
|
+
* keep the icon's space, so titles line up. `isSelected` says whether the row is highlighted, for a theme that
|
|
64
|
+
* doesn't style through `[data-selected]`.
|
|
65
|
+
*/
|
|
66
|
+
Item: React.ComponentType<{
|
|
67
|
+
intention: Intention;
|
|
68
|
+
icon?: ReactNode;
|
|
69
|
+
isSelected: boolean;
|
|
70
|
+
}>;
|
|
71
|
+
/** Shows the group's name. */
|
|
72
|
+
Group: React.ComponentType<{ name: string }>;
|
|
73
|
+
/** Says nothing matched, naming `inputValue` (`data-testid="shortround-no-matches"`). */
|
|
74
|
+
NoMatches: React.ComponentType<{ inputValue: string }>;
|
|
75
|
+
};
|
|
76
|
+
Sidekick: {
|
|
77
|
+
/** Renders its children. */
|
|
78
|
+
Frame: React.ComponentType<{
|
|
79
|
+
height: string;
|
|
80
|
+
commandWidth: string;
|
|
81
|
+
children: ReactNode;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* Shows the title; a close control calls `onClose` (`shortround-close`); size controls call `setSize` with
|
|
85
|
+
* compact, medium or full (`shortround-size-<size>`); a position control calls `cycleAnchorOrigin`
|
|
86
|
+
* (`shortround-anchor`). Each control, and the group of size controls, takes its accessible name from `labels`.
|
|
87
|
+
*/
|
|
88
|
+
ControlBar: React.ComponentType<{
|
|
89
|
+
title?: string;
|
|
90
|
+
labels: typeof ControlLabels;
|
|
91
|
+
anchorOrigin: string;
|
|
92
|
+
size: string;
|
|
93
|
+
onClose: () => void;
|
|
94
|
+
setSize: (size: string) => void;
|
|
95
|
+
cycleAnchorOrigin: () => void;
|
|
96
|
+
}>;
|
|
97
|
+
/** Renders its children, in a portal or not. */
|
|
98
|
+
Popover: React.ComponentType<{
|
|
99
|
+
anchorPosition: object;
|
|
100
|
+
height: string;
|
|
101
|
+
totalWidth: string;
|
|
102
|
+
anchorOrigin: string;
|
|
103
|
+
children: ReactNode;
|
|
104
|
+
}>;
|
|
105
|
+
};
|
|
106
|
+
/** Renders `renderSidecar()` when the side panel opens (`isOpen`). */
|
|
107
|
+
SidecarContent: React.ComponentType<{
|
|
108
|
+
isOpen: boolean;
|
|
109
|
+
renderSidecar: () => ReactNode | undefined;
|
|
110
|
+
height: string;
|
|
111
|
+
}>;
|
|
112
|
+
/**
|
|
113
|
+
* The theme's icons by name; `unknown` is the placeholder for a name the theme doesn't know. They travel with the
|
|
114
|
+
* slots, so an app passing `SidekickComponents` without a provider keeps them. Back and Cancel use `back` and
|
|
115
|
+
* `cancel` from here unless the app overrides them by id.
|
|
116
|
+
*/
|
|
117
|
+
icons?: Record<string, React.ComponentType>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The app's own icons, given once to `ShortRoundProvider`. The palette looks up each row's icon in this order: the
|
|
122
|
+
* intention's override in `intentions`; none when the intention has no `icon`; for Back and Cancel, the theme's; the
|
|
123
|
+
* name mapped by `names`, drawn by `Icon`; the theme's icon for the name; the theme's placeholder.
|
|
124
|
+
*/
|
|
125
|
+
export interface IconRegistry {
|
|
126
|
+
/** Draws any name, as an icon font or a by-name icon component does. When given, it draws every named row. */
|
|
127
|
+
Icon?: React.ComponentType<{ name: string }>;
|
|
128
|
+
/** An intention's icon name → the name `Icon` (or the theme) knows: `{ chat: 'forum' }`. */
|
|
129
|
+
names?: Record<string, string>;
|
|
130
|
+
/** An icon for one intention, by its id, whatever its name. */
|
|
131
|
+
intentions?: Record<string, React.ComponentType>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The theme's Toast: a view. `ShortRoundProvider` holds the state; the theme calls `onClose` to dismiss it. */
|
|
135
|
+
export interface ToastProps {
|
|
136
|
+
isOpen: boolean;
|
|
137
|
+
message: string | null;
|
|
138
|
+
onClose: () => void;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* What an app hands to `ShortRoundProvider`, once, at its root (defined at module scope, or memoized): the slots and
|
|
143
|
+
* the Toast view. Each slot group is needed only by what draws it: `ShortRoundSidekick` throws, naming the groups it
|
|
144
|
+
* misses, when it mounts; another package's group (such as `@shortround/chat`'s `Chat`) is checked by its own
|
|
145
|
+
* component. Toast's obligation: while `isOpen`, show `message` (`data-testid="shortround-toast"`); otherwise render
|
|
146
|
+
* nothing.
|
|
147
|
+
*/
|
|
148
|
+
export interface ShortRoundTheme {
|
|
149
|
+
components: Partial<SidekickComponents> & { [group: string]: any };
|
|
150
|
+
Toast: React.ComponentType<ToastProps>;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Chooses the theme for everything below it. `ShortRoundSidekick` takes its slots from the nearest provider
|
|
155
|
+
* (a nested provider wins for its subtree); its own `SidekickComponents` and `showToast` props override it, each
|
|
156
|
+
* for its own part.
|
|
157
|
+
*/
|
|
158
|
+
export function ShortRoundProvider(props: {
|
|
159
|
+
theme: ShortRoundTheme;
|
|
160
|
+
/** The app's own icons. Define it at module scope (or memoize it), like the theme. */
|
|
161
|
+
icons?: IconRegistry;
|
|
162
|
+
children: ReactNode;
|
|
163
|
+
}): JSX.Element;
|
|
164
|
+
|
|
165
|
+
/** Shows a toast in the nearest provider's theme. Throws outside a `ShortRoundProvider`. */
|
|
166
|
+
export function useShortRoundToast(): { showToast: (args: { message: string }) => void };
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The nearest `ShortRoundProvider`'s theme slots (`theme.components`), or `null` with no provider. For a package that
|
|
170
|
+
* draws through a slot group of its own, such as `@shortround/chat`'s `Chat`, which the provider doesn't require.
|
|
171
|
+
*/
|
|
172
|
+
export function useShortRoundComponents(): ShortRoundTheme['components'] | null;
|
|
173
|
+
|
|
174
|
+
export interface UseShortRoundResult {
|
|
175
|
+
inputValue: string;
|
|
176
|
+
inputMessage?: { type: string; text: string };
|
|
177
|
+
onInputChange: (val: string) => void;
|
|
178
|
+
intentions: Intention[];
|
|
179
|
+
/** The Back and Cancel rows the current list asked for (`includeRevertIntentions`); draw them under Navigate. */
|
|
180
|
+
revertIntentions: Intention[];
|
|
181
|
+
/**
|
|
182
|
+
* Chooses a row, as ShortRoundSidekick does: a completion row sets the input; any other runs, and keeps the Back
|
|
183
|
+
* and Cancel its result asks for.
|
|
184
|
+
*/
|
|
185
|
+
dispatch: (id: string) => Promise<IntentionActionResult>;
|
|
186
|
+
reset: () => Promise<IntentionActionResult>;
|
|
187
|
+
back: () => Promise<IntentionActionResult>;
|
|
188
|
+
dispatchedStack: { intention: Intention; result: IntentionActionResult }[];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function useShortRound(opts: {
|
|
192
|
+
defaultIntentions: Intention[];
|
|
193
|
+
/** Resolver for the default list. Memoize it, like `defaultIntentions`: a new one resets the list. */
|
|
194
|
+
resolveDefaultInput?: ResolveInput;
|
|
195
|
+
/** Fallback for the default list; start and Cancel restore it. Memoize it too. */
|
|
196
|
+
resolveDefaultFallbackInput?: ResolveFallbackInput;
|
|
197
|
+
}): UseShortRoundResult;
|
|
198
|
+
|
|
199
|
+
export function useSidekick(opts?: {
|
|
200
|
+
store?: SidekickStore;
|
|
201
|
+
initial?: Partial<SidekickStoreState>;
|
|
202
|
+
}): UseSidekickResult;
|
|
203
|
+
|
|
204
|
+
export function SidekickStoreProvider(props: {
|
|
205
|
+
store?: SidekickStore;
|
|
206
|
+
initial?: Partial<SidekickStoreState>;
|
|
207
|
+
children: ReactNode;
|
|
208
|
+
}): JSX.Element;
|
|
209
|
+
|
|
210
|
+
export function useSidekickStore(): SidekickStore | null;
|
|
211
|
+
|
|
212
|
+
export function ShortRoundSidekick(props: {
|
|
213
|
+
title?: string;
|
|
214
|
+
/**
|
|
215
|
+
* A palette that follows the app (`buildPalette`'s result, or any `PaletteSource`): the sidekick opens on its
|
|
216
|
+
* `current()`, with that side panel, and follows its `notify()` while open. Instead of `defaultIntentions` and the
|
|
217
|
+
* two resolvers. Build it once.
|
|
218
|
+
*/
|
|
219
|
+
palette?: PaletteSource;
|
|
220
|
+
/** The start list, when there is no `palette`. */
|
|
221
|
+
defaultIntentions?: Intention[];
|
|
222
|
+
/** Resolver for the default list. Memoize it, like `defaultIntentions`: a new one resets the list. */
|
|
223
|
+
resolveDefaultInput?: ResolveInput;
|
|
224
|
+
/** Fallback for the default list (see `ResolveFallbackInput`); start and Cancel restore it. Memoize it too. */
|
|
225
|
+
resolveDefaultFallbackInput?: ResolveFallbackInput;
|
|
226
|
+
/** Overrides the nearest `ShortRoundProvider`'s slots; required without a provider. */
|
|
227
|
+
SidekickComponents?: SidekickComponents;
|
|
228
|
+
/** Overrides the nearest `ShortRoundProvider`'s toast. */
|
|
229
|
+
showToast?: (args: { message: string }) => void;
|
|
230
|
+
/**
|
|
231
|
+
* The global Cmd/Ctrl+K shortcut (default `true`). Escape inside the sidekick closes it either way; content inside
|
|
232
|
+
* can keep Escape with `event.preventDefault()`.
|
|
233
|
+
*/
|
|
234
|
+
installKeyboardShortcuts?: boolean;
|
|
235
|
+
sidekickStore?: SidekickStore;
|
|
236
|
+
initialSidekickState?: Partial<SidekickStoreState>;
|
|
237
|
+
/** Called after the sidekick closes, whatever closed it. */
|
|
238
|
+
onClose?: () => void;
|
|
239
|
+
/**
|
|
240
|
+
* Decides every side panel change but a close (see `SidecarReducer`); without one, the palette's proposals apply. A
|
|
241
|
+
* close always clears the panel, whatever closed it.
|
|
242
|
+
*/
|
|
243
|
+
sidecarReducer?: SidecarReducer;
|
|
244
|
+
/** The start list's panel: proposed when the palette opens, on Back to the start and on Cancel. */
|
|
245
|
+
defaultSidecarRenderer?: () => ReactNode;
|
|
246
|
+
/**
|
|
247
|
+
* Turns off the input's line hints (default `false`): with a `palette` that has `commandLine()`, the input shows
|
|
248
|
+
* each word coloured by its role (`data-role` on the word, for the theme) and the rest of the next word as ghost
|
|
249
|
+
* text, and Tab completes through the line. Pickers and typed-text steps never show them.
|
|
250
|
+
*/
|
|
251
|
+
disableLineHints?: boolean;
|
|
252
|
+
}): JSX.Element;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* An input with its typed line drawn under it, for a host's own input (a chat, an editor's command field): each word
|
|
256
|
+
* with its role (`data-role` on the word's span, for a stylesheet to colour) and the ghost text after the last word
|
|
257
|
+
* (`data-role="ghost"`). The input itself (`children`, an `<input>` or `<textarea>` whose element is `inputRef`) stays
|
|
258
|
+
* on top with its text made transparent, so typing, the caret and selection stay native. Without `lineState` the input
|
|
259
|
+
* shows as it is. The line is drawn on one line: pass no `lineState` while the text has a line break.
|
|
260
|
+
*/
|
|
261
|
+
export function LineInput(props: {
|
|
262
|
+
children: ReactNode;
|
|
263
|
+
inputRef: RefObject<HTMLInputElement | HTMLTextAreaElement | null>;
|
|
264
|
+
/** A command line's state (`CommandLine.getState()`, or a chat line's `drawnLineState`), or `null`. */
|
|
265
|
+
lineState?: CommandLineState | null;
|
|
266
|
+
}): JSX.Element;
|