@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/package.json CHANGED
@@ -1,6 +1,72 @@
1
1
  {
2
2
  "name": "@shortround/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.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.5.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.5.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,243 @@
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
+ ControlLabels,
6
+ Intention,
7
+ IntentionActionResult,
8
+ PaletteSource,
9
+ ResolveFallbackInput,
10
+ ResolveInput,
11
+ SidecarReducer,
12
+ SidekickStore,
13
+ SidekickStoreState
14
+ } from '@shortround/core';
15
+ import { JSX, ReactNode } from 'react';
16
+
17
+ /** Where the sidekick sits for an anchor origin, as CSS position properties. */
18
+ export interface AnchorPositionStyle {
19
+ top?: number | string;
20
+ left?: number | string;
21
+ right?: number | string;
22
+ bottom?: number | string;
23
+ transform?: string;
24
+ }
25
+
26
+ export interface UseSidekickResult extends SidekickStoreState {
27
+ /** The position for the current `anchorOrigin`. */
28
+ anchorPosition: AnchorPositionStyle;
29
+ onOpen: () => void;
30
+ setIsOpen: (val: boolean) => void;
31
+ onClose: () => void;
32
+ height: string;
33
+ setSize: (val: string) => void;
34
+ setAnchorOrigin: (origin: AnchorPosition) => void;
35
+ cycleAnchorOrigin: () => void;
36
+ /** The store's `openSidecar`: through the sidekick's `sidecarReducer` (reason `app`). */
37
+ setSidecarRenderer: (renderer: () => ReactNode) => void;
38
+ commandWidth: string;
39
+ sidecarWidth: string;
40
+ /** The store's `closeSidecar`: through the sidekick's `sidecarReducer` (reason `app`). */
41
+ closeSidecar: () => void;
42
+ renderSidecar: () => ReactNode | undefined;
43
+ }
44
+
45
+ /**
46
+ * A theme's slots (`ShortRoundTheme.components`): views only. The palette renders cmdk (the input, each row, keyboard
47
+ * and selection), focus and the dialog; a theme imports no cmdk. Each slot's obligations are checked by
48
+ * `@shortround/conformance` (`describeThemeContract`), which finds a theme's controls by the `data-testid`s named below.
49
+ */
50
+ export interface SidekickComponents {
51
+ IntentionPalette: {
52
+ /** Renders its children. Row styling goes here, through cmdk's attributes (`[cmdk-item]`, `[data-selected]`). */
53
+ Frame: React.ComponentType<{ height: string; children: ReactNode }>;
54
+ /** Places `input` (the palette's cmdk input, `data-testid="shortround-input"`) and shows `inputMessage.text`. */
55
+ Input: React.ComponentType<{
56
+ input: ReactNode;
57
+ inputMessage?: { type: string; text: string };
58
+ }>;
59
+ /**
60
+ * Draws one row inside the palette's cmdk item (`data-testid="shortround-item-<id>"`): shows the title and
61
+ * subtitle, and shows `icon` when given (the palette chooses it; see `IconRegistry`). A row without an icon should
62
+ * keep the icon's space, so titles line up. `isSelected` says whether the row is highlighted, for a theme that
63
+ * doesn't style through `[data-selected]`.
64
+ */
65
+ Item: React.ComponentType<{
66
+ intention: Intention;
67
+ icon?: ReactNode;
68
+ isSelected: boolean;
69
+ }>;
70
+ /** Shows the group's name. */
71
+ Group: React.ComponentType<{ name: string }>;
72
+ /** Says nothing matched, naming `inputValue` (`data-testid="shortround-no-matches"`). */
73
+ NoMatches: React.ComponentType<{ inputValue: string }>;
74
+ };
75
+ Sidekick: {
76
+ /** Renders its children. */
77
+ Frame: React.ComponentType<{
78
+ height: string;
79
+ commandWidth: string;
80
+ children: ReactNode;
81
+ }>;
82
+ /**
83
+ * Shows the title; a close control calls `onClose` (`shortround-close`); size controls call `setSize` with
84
+ * compact, medium or full (`shortround-size-<size>`); a position control calls `cycleAnchorOrigin`
85
+ * (`shortround-anchor`). Each control, and the group of size controls, takes its accessible name from `labels`.
86
+ */
87
+ ControlBar: React.ComponentType<{
88
+ title?: string;
89
+ labels: typeof ControlLabels;
90
+ anchorOrigin: string;
91
+ size: string;
92
+ onClose: () => void;
93
+ setSize: (size: string) => void;
94
+ cycleAnchorOrigin: () => void;
95
+ }>;
96
+ /** Renders its children, in a portal or not. */
97
+ Popover: React.ComponentType<{
98
+ anchorPosition: object;
99
+ height: string;
100
+ totalWidth: string;
101
+ anchorOrigin: string;
102
+ children: ReactNode;
103
+ }>;
104
+ };
105
+ /** Renders `renderSidecar()` when the side panel opens (`isOpen`). */
106
+ SidecarContent: React.ComponentType<{
107
+ isOpen: boolean;
108
+ renderSidecar: () => ReactNode | undefined;
109
+ height: string;
110
+ }>;
111
+ /**
112
+ * The theme's icons by name; `unknown` is the placeholder for a name the theme doesn't know. They travel with the
113
+ * slots, so an app passing `SidekickComponents` without a provider keeps them. Back and Cancel use `back` and
114
+ * `cancel` from here unless the app overrides them by id.
115
+ */
116
+ icons?: Record<string, React.ComponentType>;
117
+ }
118
+
119
+ /**
120
+ * The app's own icons, given once to `ShortRoundProvider`. The palette looks up each row's icon in this order: the
121
+ * intention's override in `intentions`; none when the intention has no `icon`; for Back and Cancel, the theme's; the
122
+ * name mapped by `names`, drawn by `Icon`; the theme's icon for the name; the theme's placeholder.
123
+ */
124
+ export interface IconRegistry {
125
+ /** Draws any name, as an icon font or a by-name icon component does. When given, it draws every named row. */
126
+ Icon?: React.ComponentType<{ name: string }>;
127
+ /** An intention's icon name → the name `Icon` (or the theme) knows: `{ chat: 'forum' }`. */
128
+ names?: Record<string, string>;
129
+ /** An icon for one intention, by its id, whatever its name. */
130
+ intentions?: Record<string, React.ComponentType>;
131
+ }
132
+
133
+ /** The theme's Toast: a view. `ShortRoundProvider` holds the state; the theme calls `onClose` to dismiss it. */
134
+ export interface ToastProps {
135
+ isOpen: boolean;
136
+ message: string | null;
137
+ onClose: () => void;
138
+ }
139
+
140
+ /**
141
+ * What an app hands to `ShortRoundProvider`, once, at its root (defined at module scope, or memoized): the slots and
142
+ * the Toast view. Toast's obligation: while `isOpen`, show `message` (`data-testid="shortround-toast"`); otherwise
143
+ * render nothing.
144
+ */
145
+ export interface ShortRoundTheme {
146
+ components: SidekickComponents;
147
+ Toast: React.ComponentType<ToastProps>;
148
+ }
149
+
150
+ /**
151
+ * Chooses the theme for everything below it. `ShortRoundSidekick` takes its slots from the nearest provider
152
+ * (a nested provider wins for its subtree); its own `SidekickComponents` and `showToast` props override it, each
153
+ * for its own part.
154
+ */
155
+ export function ShortRoundProvider(props: {
156
+ theme: ShortRoundTheme;
157
+ /** The app's own icons. Define it at module scope (or memoize it), like the theme. */
158
+ icons?: IconRegistry;
159
+ children: ReactNode;
160
+ }): JSX.Element;
161
+
162
+ /** Shows a toast in the nearest provider's theme. Throws outside a `ShortRoundProvider`. */
163
+ export function useShortRoundToast(): { showToast: (args: { message: string }) => void };
164
+
165
+ export interface UseShortRoundResult {
166
+ inputValue: string;
167
+ inputMessage?: { type: string; text: string };
168
+ onInputChange: (val: string) => void;
169
+ intentions: Intention[];
170
+ /** The Back and Cancel rows the current list asked for (`includeRevertIntentions`); draw them under Navigate. */
171
+ revertIntentions: Intention[];
172
+ /**
173
+ * Chooses a row, as ShortRoundSidekick does: a completion row sets the input; any other runs, and keeps the Back
174
+ * and Cancel its result asks for.
175
+ */
176
+ dispatch: (id: string) => Promise<IntentionActionResult>;
177
+ reset: () => Promise<IntentionActionResult>;
178
+ back: () => Promise<IntentionActionResult>;
179
+ dispatchedStack: { intention: Intention; result: IntentionActionResult }[];
180
+ }
181
+
182
+ export function useShortRound(opts: {
183
+ defaultIntentions: Intention[];
184
+ /** Resolver for the default list. Memoize it, like `defaultIntentions`: a new one resets the list. */
185
+ resolveDefaultInput?: ResolveInput;
186
+ /** Fallback for the default list; start and Cancel restore it. Memoize it too. */
187
+ resolveDefaultFallbackInput?: ResolveFallbackInput;
188
+ }): UseShortRoundResult;
189
+
190
+ export function useSidekick(opts?: {
191
+ store?: SidekickStore;
192
+ initial?: Partial<SidekickStoreState>;
193
+ }): UseSidekickResult;
194
+
195
+ export function SidekickStoreProvider(props: {
196
+ store?: SidekickStore;
197
+ initial?: Partial<SidekickStoreState>;
198
+ children: ReactNode;
199
+ }): JSX.Element;
200
+
201
+ export function useSidekickStore(): SidekickStore | null;
202
+
203
+ export function ShortRoundSidekick(props: {
204
+ title?: string;
205
+ /**
206
+ * A palette that follows the app (`buildPalette`'s result, or any `PaletteSource`): the sidekick opens on its
207
+ * `current()`, with that side panel, and follows its `notify()` while open. Instead of `defaultIntentions` and the
208
+ * two resolvers. Build it once.
209
+ */
210
+ palette?: PaletteSource;
211
+ /** The start list, when there is no `palette`. */
212
+ defaultIntentions?: Intention[];
213
+ /** Resolver for the default list. Memoize it, like `defaultIntentions`: a new one resets the list. */
214
+ resolveDefaultInput?: ResolveInput;
215
+ /** Fallback for the default list (see `ResolveFallbackInput`); start and Cancel restore it. Memoize it too. */
216
+ resolveDefaultFallbackInput?: ResolveFallbackInput;
217
+ /** Overrides the nearest `ShortRoundProvider`'s slots; required without a provider. */
218
+ SidekickComponents?: SidekickComponents;
219
+ /** Overrides the nearest `ShortRoundProvider`'s toast. */
220
+ showToast?: (args: { message: string }) => void;
221
+ /**
222
+ * The global Cmd/Ctrl+K shortcut (default `true`). Escape inside the sidekick closes it either way; content inside
223
+ * can keep Escape with `event.preventDefault()`.
224
+ */
225
+ installKeyboardShortcuts?: boolean;
226
+ sidekickStore?: SidekickStore;
227
+ initialSidekickState?: Partial<SidekickStoreState>;
228
+ /** Called after the sidekick closes, whatever closed it. */
229
+ onClose?: () => void;
230
+ /**
231
+ * Decides every side panel change but a close (see `SidecarReducer`); without one, the palette's proposals apply. A
232
+ * close always clears the panel, whatever closed it.
233
+ */
234
+ sidecarReducer?: SidecarReducer;
235
+ /** The start list's panel: proposed when the palette opens, on Back to the start and on Cancel. */
236
+ defaultSidecarRenderer?: () => ReactNode;
237
+ /**
238
+ * Turns off the input's line hints (default `false`): with a `palette` that has `commandLine()`, the input shows
239
+ * each word coloured by its role (`data-role` on the word, for the theme) and the rest of the next word as ghost
240
+ * text, and Tab completes through the line. Pickers and typed-text steps never show them.
241
+ */
242
+ disableLineHints?: boolean;
243
+ }): JSX.Element;