@canonic2/workbench 0.0.0-stage → 0.23.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/LICENSE +18 -0
- package/README.md +32 -2
- package/api.d.ts +210 -0
- package/api.js +14 -0
- package/package.json +13 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Canonic Proprietary License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026. All rights reserved.
|
|
4
|
+
|
|
5
|
+
The Canonic software, documentation, and assets are proprietary. No permission
|
|
6
|
+
is granted to use, copy, modify, publish, distribute, sublicense, or sell them
|
|
7
|
+
except with the copyright holder's prior written permission or under a separate
|
|
8
|
+
written license agreement.
|
|
9
|
+
|
|
10
|
+
Third-party components remain subject to their own licenses and notices.
|
|
11
|
+
This license applies only to Canonic-owned material; it does not apply to
|
|
12
|
+
project content processed by Canonic or material owned by other parties.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. TO THE MAXIMUM EXTENT
|
|
17
|
+
PERMITTED BY LAW, THE COPYRIGHT HOLDER SHALL NOT BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY ARISING FROM THE SOFTWARE OR ITS USE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @canonic2/workbench
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Types for [Canonic Workbench](https://canonic.sh/workbench/) previews:
|
|
4
|
+
`definePreview`, `defineConfig`, and `defineAdapter`.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install --save-dev @canonic2/workbench
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { definePreview } from '@canonic2/workbench';
|
|
12
|
+
|
|
13
|
+
export default definePreview({
|
|
14
|
+
id: 'components/button',
|
|
15
|
+
title: 'Components/Button',
|
|
16
|
+
adapter: 'react',
|
|
17
|
+
source: { entry: './Button.tsx', export: 'Button' },
|
|
18
|
+
inputs: { label: 'Continue', disabled: false },
|
|
19
|
+
scenarios: {
|
|
20
|
+
default: {},
|
|
21
|
+
disabled: { inputs: { disabled: true } },
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The package lets TypeScript and your editor check preview definitions. It
|
|
27
|
+
checks that each scenario's `inputs` match the preview's. When Workbench compiles
|
|
28
|
+
a preview, it supplies `@canonic2/workbench` itself, so the version installed
|
|
29
|
+
here affects types only.
|
|
30
|
+
|
|
31
|
+
See [Workbench previews](https://canonic.sh/workbench/docs/workbench-previews/).
|
|
32
|
+
|
|
33
|
+
Canonic is proprietary; all rights reserved. See [LICENSE](LICENSE).
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/** A key of the space's sizes: one of the defaults, or one the space declares. */
|
|
2
|
+
export type SizeKey = 'fit' | 'laptop' | 'mobile' | 'resizable' | (string & {});
|
|
3
|
+
export interface PreviewContext<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
4
|
+
id: string;
|
|
5
|
+
/** The independently rendered artboard within this scenario, when declared. */
|
|
6
|
+
artboard?: string;
|
|
7
|
+
scenario: string;
|
|
8
|
+
/** The selected iteration of the artboard, when one is selected. */
|
|
9
|
+
iteration?: string;
|
|
10
|
+
inputs: T;
|
|
11
|
+
fixtures: Record<string, unknown>;
|
|
12
|
+
globals: Record<string, unknown>;
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
/** Report asynchronous rendering failures to the host and capture service. */
|
|
15
|
+
error(error: unknown): void;
|
|
16
|
+
action(name: string, ...values: unknown[]): void;
|
|
17
|
+
/** Open another preview, or another scenario of this one, while actions are on. */
|
|
18
|
+
navigate(to: PreviewLink): void;
|
|
19
|
+
}
|
|
20
|
+
/** A preview ID, or a preview and scenario; a scenario alone stays in this preview. */
|
|
21
|
+
export type PreviewLink = string | { preview: string; scenario?: string } | { preview?: string; scenario: string };
|
|
22
|
+
export type Cleanup = void | (() => void | Promise<void>);
|
|
23
|
+
/** A mocked response. `body` objects are sent as JSON. */
|
|
24
|
+
export interface MockResponse {
|
|
25
|
+
status?: number;
|
|
26
|
+
statusText?: string;
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
body?: unknown;
|
|
29
|
+
/** Milliseconds to wait before answering. */
|
|
30
|
+
delay?: number;
|
|
31
|
+
/** Never answer, to show a loading state. */
|
|
32
|
+
pending?: boolean;
|
|
33
|
+
/** Fail as a network error, as when offline. */
|
|
34
|
+
failed?: boolean;
|
|
35
|
+
/** Send this request to the network after all. */
|
|
36
|
+
passthrough?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** A request as a mock handler receives it, with its body parsed. */
|
|
39
|
+
export interface MockRequest {
|
|
40
|
+
method: string;
|
|
41
|
+
url: string;
|
|
42
|
+
path: string;
|
|
43
|
+
query: Record<string, string>;
|
|
44
|
+
headers: Record<string, string>;
|
|
45
|
+
body: unknown;
|
|
46
|
+
operationName: string | null;
|
|
47
|
+
variables: Record<string, unknown> | null;
|
|
48
|
+
}
|
|
49
|
+
export type MockHandler<T extends Record<string, unknown> = Record<string, unknown>> =
|
|
50
|
+
MockResponse | ((request: MockRequest, context: PreviewContext<T>) => MockResponse | Response | Promise<MockResponse | Response>);
|
|
51
|
+
/** Keys: `'GET /api/items'`, `'/api/items/*'`, `'POST /graphql Operation'`, or a full URL. */
|
|
52
|
+
export type RequestMocks<T extends Record<string, unknown> = Record<string, unknown>> = Record<string, MockHandler<T>>;
|
|
53
|
+
/** Another take on one artboard's design: a scenario's shape without its own artboards or iterations. */
|
|
54
|
+
export interface PreviewIteration<T extends Record<string, unknown>> {
|
|
55
|
+
label?: string;
|
|
56
|
+
inputs?: Partial<T>;
|
|
57
|
+
fixtures?: Record<string, unknown>;
|
|
58
|
+
globals?: Record<string, unknown>;
|
|
59
|
+
source?: { entry: string; export?: string };
|
|
60
|
+
/** Answers for the page's fetch and XMLHttpRequest calls. */
|
|
61
|
+
requests?: RequestMocks<T>;
|
|
62
|
+
setup?: (context: PreviewContext<T>) => Cleanup | Promise<Cleanup>;
|
|
63
|
+
play?: (context: PreviewContext<T> & { canvas: HTMLElement }) => void | Promise<void>;
|
|
64
|
+
ready?: (context: PreviewContext<T>) => void | Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
/** An artboard of a scenario, rendered beside the scenario's other artboards. */
|
|
67
|
+
export interface PreviewArtboard<T extends Record<string, unknown>> {
|
|
68
|
+
label?: string;
|
|
69
|
+
/** Width and height in pixels, from 1 to 8192. */
|
|
70
|
+
width: number;
|
|
71
|
+
height: number;
|
|
72
|
+
inputs?: Partial<T>;
|
|
73
|
+
/** Other takes on this artboard's design, by kebab-case ID. */
|
|
74
|
+
iterations?: Record<string, PreviewIteration<T>>;
|
|
75
|
+
}
|
|
76
|
+
/** A scenario declares either its artboards, each with its own iterations, or the iterations of its one implicit artboard. */
|
|
77
|
+
export type PreviewScenario<T extends Record<string, unknown>> = PreviewIteration<T> & (
|
|
78
|
+
| {
|
|
79
|
+
/** Artboards rendered together in this scenario, by kebab-case ID: 1 to 32. */
|
|
80
|
+
artboards: Record<string, PreviewArtboard<T>>;
|
|
81
|
+
iterations?: never;
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
artboards?: never;
|
|
85
|
+
/** Iterations of the scenario's one implicit artboard. */
|
|
86
|
+
iterations?: Record<string, PreviewIteration<T>>;
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
export interface Control {
|
|
90
|
+
type: 'text' | 'number' | 'boolean' | 'select' | 'json';
|
|
91
|
+
label?: string;
|
|
92
|
+
options?: (string | number)[];
|
|
93
|
+
min?: number;
|
|
94
|
+
max?: number;
|
|
95
|
+
step?: number;
|
|
96
|
+
}
|
|
97
|
+
export interface Preview<T extends Record<string, unknown> = Record<string, unknown>> extends PreviewIteration<T> {
|
|
98
|
+
id: string;
|
|
99
|
+
title?: string;
|
|
100
|
+
icon?: string;
|
|
101
|
+
adapter: string;
|
|
102
|
+
source: { entry: string; export?: string };
|
|
103
|
+
styles?: string[];
|
|
104
|
+
assets?: string[];
|
|
105
|
+
environment?: string;
|
|
106
|
+
sizes?: SizeKey[];
|
|
107
|
+
controls?: Record<string, Control>;
|
|
108
|
+
scenarios?: Record<string, PreviewScenario<NoInfer<T>>>;
|
|
109
|
+
/** Addresses the source links or submits to, mapped to the preview each opens. */
|
|
110
|
+
links?: Record<string, PreviewLink>;
|
|
111
|
+
/**
|
|
112
|
+
* Modules replaced in this preview only, whoever imports them: a package
|
|
113
|
+
* name, or a path relative to this file, mapped to the file that replaces it.
|
|
114
|
+
* Other previews keep the real module. The real module must still build.
|
|
115
|
+
*/
|
|
116
|
+
mocks?: Record<string, string>;
|
|
117
|
+
docs?: string;
|
|
118
|
+
}
|
|
119
|
+
export interface BrowserAdapter {
|
|
120
|
+
mount(canvas: HTMLElement, source: unknown, context: PreviewContext, environment?: unknown): Cleanup | Promise<Cleanup>;
|
|
121
|
+
}
|
|
122
|
+
/** A module made from source text, imported by its ID, such as `virtual:build-info`. */
|
|
123
|
+
export interface VirtualModulePlugin { plugin: 'virtual-module'; id: string; source: string }
|
|
124
|
+
/**
|
|
125
|
+
* An exact import sent to another file: from every importer, or only from
|
|
126
|
+
* importers matching `importer` (globs over the path from the project root,
|
|
127
|
+
* or a regular expression over the absolute path). The first match wins.
|
|
128
|
+
*/
|
|
129
|
+
export interface ResolveImportPlugin { plugin: 'resolve'; request: string; to: string; importer?: string | string[] | RegExp }
|
|
130
|
+
/** `import.meta.glob(...)` expanded at build time in the project's sources, or those `include` matches as a rule condition: a path from the project root, a regular expression, or a list of them. */
|
|
131
|
+
export interface ImportGlobPlugin { plugin: 'import-glob'; include?: string | string[] | RegExp }
|
|
132
|
+
/**
|
|
133
|
+
* An SVG sprite: every icon in `dirs` becomes a `<symbol>`. Importing
|
|
134
|
+
* `register` (`virtual:svg-icons-register`) inserts the sprite into the
|
|
135
|
+
* document; `names` (`virtual:svg-icons-names`) exports the symbol IDs.
|
|
136
|
+
*/
|
|
137
|
+
export interface SvgSpritePlugin {
|
|
138
|
+
plugin: 'svg-sprite';
|
|
139
|
+
dirs: string | string[];
|
|
140
|
+
/** `[dir]` and `[name]` stand for the icon's folder and file name; `icon-[dir]-[name]` by default. */
|
|
141
|
+
symbolId?: string;
|
|
142
|
+
register?: string;
|
|
143
|
+
names?: string;
|
|
144
|
+
inject?: 'body-last' | 'body-first';
|
|
145
|
+
domId?: string;
|
|
146
|
+
}
|
|
147
|
+
/** A capability of Workbench's compiler, made by the helpers below. */
|
|
148
|
+
export type CompilerPlugin = VirtualModulePlugin | ResolveImportPlugin | ImportGlobPlugin | SvgSpritePlugin;
|
|
149
|
+
/** An Rspack module rule, such as `{ test: /\.svg$/, loader: './svg-loader.cjs' }`. Loader paths resolve from the project root; JavaScript loaders run in a Node process. */
|
|
150
|
+
export type CompilerRule = Record<string, unknown>;
|
|
151
|
+
export interface Adapter {
|
|
152
|
+
/** Browser module exporting mount(canvas, source, context, environment). */
|
|
153
|
+
runtime: string;
|
|
154
|
+
/** Compiler plugins for this adapter's previews only. */
|
|
155
|
+
plugins?: CompilerPlugin[];
|
|
156
|
+
/** Rspack module rules for this adapter's previews only. */
|
|
157
|
+
rules?: CompilerRule[];
|
|
158
|
+
}
|
|
159
|
+
export interface WorkbenchConfig {
|
|
160
|
+
adapters?: Record<string, Adapter>;
|
|
161
|
+
/** An environment module around every preview, or one per adapter name, relative to the project root. */
|
|
162
|
+
environment?: string | Record<string, string>;
|
|
163
|
+
/**
|
|
164
|
+
* Modules replaced before they are resolved, whoever imports them: a package
|
|
165
|
+
* name, or a path from the project root (with or without its extension),
|
|
166
|
+
* mapped to the file that replaces it. A nested map applies to one adapter's
|
|
167
|
+
* compiler: `{ 'react-native-web': { './src/NativeAudio': './mocks/audio.ts' } }`.
|
|
168
|
+
*/
|
|
169
|
+
mocks?: Record<string, string | Record<string, string>>;
|
|
170
|
+
/** Compiler plugins, applied after Workbench's own. */
|
|
171
|
+
plugins?: CompilerPlugin[];
|
|
172
|
+
/** Rspack module rules, applied after Workbench's own. */
|
|
173
|
+
rules?: CompilerRule[];
|
|
174
|
+
aliases?: Record<string, string>;
|
|
175
|
+
resolveExtensions?: string[];
|
|
176
|
+
/** Packages resolved from one project installation (React in monorepos). */
|
|
177
|
+
dedupe?: string[];
|
|
178
|
+
define?: Record<string, string>;
|
|
179
|
+
}
|
|
180
|
+
/** One lens of a docs page: what renders its examples. Paths are relative to the definition file. */
|
|
181
|
+
export interface DocsLens {
|
|
182
|
+
label?: string;
|
|
183
|
+
adapter: string;
|
|
184
|
+
/** A folder with one example per file (each file's default export), or a file with one example per named export. */
|
|
185
|
+
examples: string;
|
|
186
|
+
styles?: string[];
|
|
187
|
+
environment?: string;
|
|
188
|
+
}
|
|
189
|
+
/** A docs page: Markdown that places examples with ```example <id>``` blocks. */
|
|
190
|
+
export interface Docs {
|
|
191
|
+
id: string;
|
|
192
|
+
title?: string;
|
|
193
|
+
/** The page's icon in the page list, a kebab-case Lucide name; book-open otherwise. */
|
|
194
|
+
icon?: string;
|
|
195
|
+
/** The Markdown file, relative to the definition file. */
|
|
196
|
+
docs: string;
|
|
197
|
+
lenses?: Record<string, DocsLens>;
|
|
198
|
+
/** The lens the page opens with; the first in `lenses` otherwise. */
|
|
199
|
+
lens?: string;
|
|
200
|
+
/** Scenarios every example receives as `context.scenario`. Prefer showing variations side by side. */
|
|
201
|
+
scenarios?: Record<string, { label?: string }>;
|
|
202
|
+
}
|
|
203
|
+
export function definePreview<T extends Record<string, unknown>>(preview: Preview<T>): Preview<T>;
|
|
204
|
+
export function defineDocs(docs: Docs): Docs & { kind: 'docs' };
|
|
205
|
+
export function defineConfig(config: WorkbenchConfig): WorkbenchConfig;
|
|
206
|
+
export function defineAdapter(adapter: Adapter): Adapter;
|
|
207
|
+
export function virtualModule(options: Omit<VirtualModulePlugin, 'plugin'>): VirtualModulePlugin;
|
|
208
|
+
export function resolveImport(options: Omit<ResolveImportPlugin, 'plugin'>): ResolveImportPlugin;
|
|
209
|
+
export function importGlob(options?: Omit<ImportGlobPlugin, 'plugin'>): ImportGlobPlugin;
|
|
210
|
+
export function svgSprite(options: Omit<SvgSpritePlugin, 'plugin'>): SvgSpritePlugin;
|
package/api.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Definitions are ordinary modules. The compiler supplies this import without
|
|
2
|
+
// installing a package in the consuming project. The same API runs in exports.
|
|
3
|
+
export function definePreview(preview) { return preview; }
|
|
4
|
+
// A docs page: Markdown with examples its lenses render. `kind` tells the
|
|
5
|
+
// compiler's index it from a preview.
|
|
6
|
+
export function defineDocs(docs) { return Object.assign({}, docs, { kind: 'docs' }); }
|
|
7
|
+
export function defineConfig(config) { return config; }
|
|
8
|
+
export function defineAdapter(adapter) { return adapter; }
|
|
9
|
+
// Compiler plugins: plain descriptions of capabilities Workbench's compiler
|
|
10
|
+
// provides. The project's paths and names are their options.
|
|
11
|
+
export function virtualModule(options) { return Object.assign({ plugin: 'virtual-module' }, options); }
|
|
12
|
+
export function resolveImport(options) { return Object.assign({ plugin: 'resolve' }, options); }
|
|
13
|
+
export function importGlob(options) { return Object.assign({ plugin: 'import-glob' }, options); }
|
|
14
|
+
export function svgSprite(options) { return Object.assign({ plugin: 'svg-sprite' }, options); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonic2/workbench",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.23.0",
|
|
4
|
+
"description": "Types and define helpers for Canonic Workbench previews",
|
|
5
|
+
"keywords": ["workbench", "preview", "design system", "storybook"],
|
|
6
|
+
"homepage": "https://canonic.sh",
|
|
7
|
+
"repository": { "type": "git", "url": "git+https://github.com/canonic2/canonic.git", "directory": "packages/workbench/sdk" },
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./api.js",
|
|
10
|
+
"types": "./api.d.ts",
|
|
11
|
+
"exports": { ".": { "types": "./api.d.ts", "default": "./api.js" } },
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"files": ["api.js", "api.d.ts", "LICENSE"],
|
|
14
|
+
"license": "SEE LICENSE IN LICENSE"
|
|
15
|
+
}
|