@ng-nomads/storybook-addon-anatomy 0.1.0 → 0.1.1
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/README.md +7 -2
- package/dist/preview.d.ts +1 -1
- package/dist/preview.js +7 -4
- package/docs/demo.png +0 -0
- package/docs/examples.md +590 -0
- package/docs/how-it-works.md +151 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,10 +6,15 @@ A Storybook addon that explains the **anatomy** of a component. A toolbar toggle
|
|
|
6
6
|
- **Slots**: the component row (icons, field, help ...) drawn as flush layout slots with a dashed outline and the measured width. Empty space that a component keeps is hatched.
|
|
7
7
|
- **Box model**: a quiet margin / padding / content overlay of the component, and a DevTools-style overlay for the element under the pointer.
|
|
8
8
|
|
|
9
|
-

|
|
9
|
+

|
|
10
10
|
|
|
11
11
|
The addon never wraps your story. It draws a plain-DOM layer over the page after each render, so it works with any renderer. Notes write args with `useArgs()`, so the notes and the Controls panel stay in sync.
|
|
12
12
|
|
|
13
|
+
## Docs
|
|
14
|
+
|
|
15
|
+
- [How it works](https://github.com/ng-nomads/storybook-addon-anatomy/blob/main/docs/how-it-works.md): the manager and preview parts, and the render lifecycle.
|
|
16
|
+
- [Examples](https://github.com/ng-nomads/storybook-addon-anatomy/blob/main/docs/examples.md): copy-paste stories for Angular, React, Vue, web components and plain HTML.
|
|
17
|
+
|
|
13
18
|
## Install
|
|
14
19
|
|
|
15
20
|
```sh
|
|
@@ -65,7 +70,7 @@ export const Playground: Story = {
|
|
|
65
70
|
| `layout` | `{ [note]: 'left' \| 'right' \| 'top' \| 'bottom' }` | Fixed side of a note. Default: balanced by height. |
|
|
66
71
|
| `exclude` | `string[]` | Args never shown in a note. |
|
|
67
72
|
| `slots` | `AnatomySlot[]` | The component row, start to end. See below. |
|
|
68
|
-
| `backdrop` | selector | Element that gets the box-model backdrop. Default: `root`. |
|
|
73
|
+
| `backdrop` | selector or list | Element that gets the box-model backdrop. Default: `root`. |
|
|
69
74
|
| `font` | `{ notes?, body?, load? }` | See [Fonts](#fonts). |
|
|
70
75
|
|
|
71
76
|
**Part selectors** are CSS selectors. `[data-part="label"]` is the stable choice for your own components. Open shadow roots are searched, and `host::part(name)` finds `[part~=name]` in the shadow root of `host`.
|
package/dist/preview.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ declare const anatomyInitialGlobals: {
|
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* Draws the anatomy view and the box model inspector over the story (Canvas tab only).
|
|
12
|
-
* Registered by the addon
|
|
12
|
+
* Registered by the addon preview annotations. If you add it by hand, put it last in the decorators so it is the outermost, so that the args it
|
|
13
13
|
* removes do not reach the other decorators or the story.
|
|
14
14
|
*/
|
|
15
15
|
declare const withAnatomy: DecoratorFunction<Renderer>;
|
package/dist/preview.js
CHANGED
|
@@ -464,9 +464,10 @@ function renderSlots(holder, marks, componentBottom) {
|
|
|
464
464
|
dropCrowdedCaptions(holder);
|
|
465
465
|
}
|
|
466
466
|
function dropCrowdedCaptions(holder) {
|
|
467
|
+
const captions = Array.from(holder.querySelectorAll(".sba-layout-caption"));
|
|
468
|
+
const byLeft = captions.map((caption) => ({ caption, rect: caption.getBoundingClientRect() })).sort((a, b) => a.rect.left - b.rect.left);
|
|
467
469
|
let lastRight = -Infinity;
|
|
468
|
-
for (const caption of
|
|
469
|
-
const rect = caption.getBoundingClientRect();
|
|
470
|
+
for (const { caption, rect } of byLeft) {
|
|
470
471
|
const crowded = rect.left < lastRight + 6;
|
|
471
472
|
caption.classList.toggle("is-low", crowded);
|
|
472
473
|
lastRight = crowded ? lastRight : rect.right;
|
|
@@ -542,10 +543,12 @@ var ECHO_END_T = 0.86;
|
|
|
542
543
|
var ECHO_DRIFT = 2.4;
|
|
543
544
|
var TIP_GAP = 6;
|
|
544
545
|
var TAIL_GAP = 9;
|
|
546
|
+
var FULL_BEND_LENGTH = 120;
|
|
545
547
|
function buildWire(start, end, groupIndex) {
|
|
546
|
-
const bend = BEND_BY_GROUP[groupIndex % BEND_BY_GROUP.length];
|
|
547
548
|
const dx = end.x - start.x;
|
|
548
549
|
const dy = end.y - start.y;
|
|
550
|
+
const scale = Math.min(1, Math.hypot(dx, dy) / FULL_BEND_LENGTH);
|
|
551
|
+
const bend = BEND_BY_GROUP[groupIndex % BEND_BY_GROUP.length] * scale;
|
|
549
552
|
const first = { x: start.x + dx * 0.32, y: start.y + dy * 0.06 + bend };
|
|
550
553
|
const second = { x: end.x - dx * 0.29, y: end.y - dy * 0.27 + bend };
|
|
551
554
|
const aim = pointOnCurve(start, first, second, end, HEAD_AIM_T);
|
|
@@ -748,7 +751,7 @@ function markActive(state, placed) {
|
|
|
748
751
|
// src/balance.ts
|
|
749
752
|
var SIDES2 = ["left", "right", "top", "bottom"];
|
|
750
753
|
var EMPTY_SIDE_COST = 60;
|
|
751
|
-
var ROW_NOTE_COST =
|
|
754
|
+
var ROW_NOTE_COST = 120;
|
|
752
755
|
var FAR_SIDE_COST = 24;
|
|
753
756
|
var MAX_MOVES = 40;
|
|
754
757
|
function balanceSides(notes, space) {
|
package/docs/demo.png
CHANGED
|
Binary file
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Copy-paste recipes for `@ng-nomads/storybook-addon-anatomy`. Every example uses the real `parameters.anatomy` keys. See the [README](../README.md) for the full key table.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev @ng-nomads/storybook-addon-anatomy
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`.storybook/main.ts`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import type { StorybookConfig } from '@storybook/angular'; // or your framework package
|
|
15
|
+
|
|
16
|
+
const config: StorybookConfig = {
|
|
17
|
+
stories: ['../src/**/*.stories.ts'],
|
|
18
|
+
addons: ['@ng-nomads/storybook-addon-anatomy'],
|
|
19
|
+
framework: '@storybook/angular',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default config;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The addon registers its toolbar toggles and its preview setup by itself. You need no `manager.ts` and no `preview.ts` change.
|
|
26
|
+
|
|
27
|
+
## A minimal example
|
|
28
|
+
|
|
29
|
+
One note, one part. The note shows the arg `label`. The arrow points at the element `[data-part="label"]`.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
export const Playground: Story = {
|
|
33
|
+
args: { label: 'Email' },
|
|
34
|
+
parameters: {
|
|
35
|
+
anatomy: {
|
|
36
|
+
groups: { Label: { note: 'Name the field', args: ['label'] } },
|
|
37
|
+
parts: { Label: '[data-part="label"]' },
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Open the story. Switch on **Anatomy** in the toolbar. Change the label in the note. The component and the Controls panel update.
|
|
44
|
+
|
|
45
|
+
The `root` key is not needed here. The default root is the story container.
|
|
46
|
+
|
|
47
|
+
## Angular
|
|
48
|
+
|
|
49
|
+
Angular is the main target. Use a standalone component. Put a `data-part` attribute on each element that a note points at.
|
|
50
|
+
|
|
51
|
+
The component:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// text-field.component.ts
|
|
55
|
+
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
|
|
56
|
+
|
|
57
|
+
@Component({
|
|
58
|
+
selector: 'app-text-field',
|
|
59
|
+
standalone: true,
|
|
60
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
61
|
+
template: `
|
|
62
|
+
<div class="tf" data-part="field" [class.is-disabled]="disabled()">
|
|
63
|
+
<div class="tf-main">
|
|
64
|
+
@if (label()) {
|
|
65
|
+
<label class="tf-label" data-part="label">{{ label() }}</label>
|
|
66
|
+
}
|
|
67
|
+
<div class="tf-row">
|
|
68
|
+
@if (startIcon()) {
|
|
69
|
+
<span class="tf-icon" data-part="startIcon">{{ startIcon() }}</span>
|
|
70
|
+
}
|
|
71
|
+
<input
|
|
72
|
+
class="tf-input"
|
|
73
|
+
data-part="input"
|
|
74
|
+
[placeholder]="placeholder()"
|
|
75
|
+
[disabled]="disabled()"
|
|
76
|
+
/>
|
|
77
|
+
@if (endIcon()) {
|
|
78
|
+
<span class="tf-icon" data-part="endIcon">{{ endIcon() }}</span>
|
|
79
|
+
}
|
|
80
|
+
</div>
|
|
81
|
+
@if (hint()) {
|
|
82
|
+
<span class="tf-hint" data-part="hint">{{ hint() }}</span>
|
|
83
|
+
}
|
|
84
|
+
</div>
|
|
85
|
+
</div>
|
|
86
|
+
`,
|
|
87
|
+
})
|
|
88
|
+
export class TextFieldComponent {
|
|
89
|
+
readonly label = input('');
|
|
90
|
+
readonly placeholder = input('');
|
|
91
|
+
readonly hint = input('');
|
|
92
|
+
readonly startIcon = input('');
|
|
93
|
+
readonly endIcon = input('');
|
|
94
|
+
readonly disabled = input(false);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The story file:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// text-field.stories.ts
|
|
102
|
+
import type { Meta, StoryObj } from '@storybook/angular';
|
|
103
|
+
import { TextFieldComponent } from './text-field.component';
|
|
104
|
+
|
|
105
|
+
const meta: Meta<TextFieldComponent> = {
|
|
106
|
+
title: 'Components/Text field',
|
|
107
|
+
component: TextFieldComponent,
|
|
108
|
+
argTypes: {
|
|
109
|
+
label: { control: 'text' },
|
|
110
|
+
placeholder: { control: 'text' },
|
|
111
|
+
hint: { control: 'text' },
|
|
112
|
+
startIcon: { control: 'text' },
|
|
113
|
+
endIcon: { control: 'text' },
|
|
114
|
+
disabled: { control: 'boolean' },
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
export default meta;
|
|
118
|
+
|
|
119
|
+
type Story = StoryObj<TextFieldComponent>;
|
|
120
|
+
|
|
121
|
+
export const Playground: Story = {
|
|
122
|
+
args: {
|
|
123
|
+
label: 'Email',
|
|
124
|
+
placeholder: 'name@example.com',
|
|
125
|
+
hint: 'We never share it.',
|
|
126
|
+
startIcon: '@',
|
|
127
|
+
endIcon: '',
|
|
128
|
+
disabled: false,
|
|
129
|
+
},
|
|
130
|
+
parameters: {
|
|
131
|
+
anatomy: {
|
|
132
|
+
root: '[data-part="field"]',
|
|
133
|
+
groups: {
|
|
134
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
135
|
+
Input: { note: 'What people type', args: ['placeholder', 'disabled'] },
|
|
136
|
+
Hint: { note: 'Help under the field', args: ['hint'] },
|
|
137
|
+
Icons: {
|
|
138
|
+
note: 'Add icons',
|
|
139
|
+
args: ['startIcon', 'endIcon', 'reserveStartIconSpace', 'reserveEndIconSpace'],
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
parts: {
|
|
143
|
+
Label: '[data-part="label"]',
|
|
144
|
+
Input: '[data-part="input"]',
|
|
145
|
+
Hint: '[data-part="hint"]',
|
|
146
|
+
Icons: '[data-part="startIcon"]',
|
|
147
|
+
startIcon: '[data-part="startIcon"]',
|
|
148
|
+
endIcon: '[data-part="endIcon"]',
|
|
149
|
+
},
|
|
150
|
+
layout: { Label: 'top', Input: 'bottom' },
|
|
151
|
+
slots: [
|
|
152
|
+
{ name: 'startIcon', part: '[data-part="startIcon"]', reservable: true, width: 24 },
|
|
153
|
+
{ name: 'endIcon', part: '[data-part="endIcon"]', reservable: true, width: 24 },
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Notes:
|
|
161
|
+
|
|
162
|
+
- Angular binds `args` to the signal inputs. The reserve args never reach the component.
|
|
163
|
+
- Put `parameters.anatomy` on the **Playground** story. The **Anatomy** toggle shows for stories that have it.
|
|
164
|
+
- `@if` removes the element from the DOM when the value is empty. The slot then collapses. Turn on `reserveStartIconSpace` to draw the space.
|
|
165
|
+
|
|
166
|
+
## React
|
|
167
|
+
|
|
168
|
+
```tsx
|
|
169
|
+
// TextField.stories.tsx
|
|
170
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
171
|
+
|
|
172
|
+
interface TextFieldProps {
|
|
173
|
+
label: string;
|
|
174
|
+
placeholder: string;
|
|
175
|
+
hint: string;
|
|
176
|
+
startIcon: string;
|
|
177
|
+
disabled: boolean;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function TextField({ label, placeholder, hint, startIcon, disabled }: TextFieldProps) {
|
|
181
|
+
return (
|
|
182
|
+
<div className="tf" data-part="field">
|
|
183
|
+
{label && <label data-part="label">{label}</label>}
|
|
184
|
+
<div className="tf-row">
|
|
185
|
+
{startIcon && <span data-part="startIcon">{startIcon}</span>}
|
|
186
|
+
<input data-part="input" placeholder={placeholder} disabled={disabled} />
|
|
187
|
+
</div>
|
|
188
|
+
{hint && <span data-part="hint">{hint}</span>}
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const meta: Meta<typeof TextField> = {
|
|
194
|
+
title: 'Components/Text field',
|
|
195
|
+
component: TextField,
|
|
196
|
+
};
|
|
197
|
+
export default meta;
|
|
198
|
+
|
|
199
|
+
type Story = StoryObj<typeof TextField>;
|
|
200
|
+
|
|
201
|
+
export const Playground: Story = {
|
|
202
|
+
args: {
|
|
203
|
+
label: 'Email',
|
|
204
|
+
placeholder: 'name@example.com',
|
|
205
|
+
hint: 'We never share it.',
|
|
206
|
+
startIcon: '@',
|
|
207
|
+
disabled: false,
|
|
208
|
+
},
|
|
209
|
+
argTypes: {
|
|
210
|
+
label: { control: 'text' },
|
|
211
|
+
placeholder: { control: 'text' },
|
|
212
|
+
hint: { control: 'text' },
|
|
213
|
+
startIcon: { control: 'text' },
|
|
214
|
+
disabled: { control: 'boolean' },
|
|
215
|
+
},
|
|
216
|
+
parameters: {
|
|
217
|
+
anatomy: {
|
|
218
|
+
root: '[data-part="field"]',
|
|
219
|
+
groups: {
|
|
220
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
221
|
+
Input: { note: 'What people type', args: ['placeholder', 'disabled'] },
|
|
222
|
+
Hint: { note: 'Help under the field', args: ['hint'] },
|
|
223
|
+
Icon: { note: 'Add an icon', args: ['startIcon', 'reserveStartIconSpace'] },
|
|
224
|
+
},
|
|
225
|
+
parts: {
|
|
226
|
+
Label: '[data-part="label"]',
|
|
227
|
+
Input: '[data-part="input"]',
|
|
228
|
+
Hint: '[data-part="hint"]',
|
|
229
|
+
Icon: '[data-part="startIcon"]',
|
|
230
|
+
},
|
|
231
|
+
slots: [{ name: 'startIcon', part: '[data-part="startIcon"]', reservable: true }],
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
React 19 passes unknown props to a function component, so the addon removes `reserveStartIconSpace` before the render. The component never sees it.
|
|
238
|
+
|
|
239
|
+
## Vue
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
// TextField.stories.ts
|
|
243
|
+
import type { Meta, StoryObj } from '@storybook/vue3-vite';
|
|
244
|
+
import { defineComponent } from 'vue';
|
|
245
|
+
|
|
246
|
+
const TextField = defineComponent({
|
|
247
|
+
props: {
|
|
248
|
+
label: { type: String, default: '' },
|
|
249
|
+
placeholder: { type: String, default: '' },
|
|
250
|
+
hint: { type: String, default: '' },
|
|
251
|
+
disabled: { type: Boolean, default: false },
|
|
252
|
+
},
|
|
253
|
+
template: `
|
|
254
|
+
<div class="tf" data-part="field">
|
|
255
|
+
<label v-if="label" data-part="label">{{ label }}</label>
|
|
256
|
+
<input data-part="input" :placeholder="placeholder" :disabled="disabled" />
|
|
257
|
+
<span v-if="hint" data-part="hint">{{ hint }}</span>
|
|
258
|
+
</div>
|
|
259
|
+
`,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const meta: Meta<typeof TextField> = {
|
|
263
|
+
title: 'Components/Text field',
|
|
264
|
+
component: TextField,
|
|
265
|
+
argTypes: {
|
|
266
|
+
label: { control: 'text' },
|
|
267
|
+
placeholder: { control: 'text' },
|
|
268
|
+
hint: { control: 'text' },
|
|
269
|
+
disabled: { control: 'boolean' },
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
export default meta;
|
|
273
|
+
|
|
274
|
+
type Story = StoryObj<typeof TextField>;
|
|
275
|
+
|
|
276
|
+
export const Playground: Story = {
|
|
277
|
+
args: { label: 'Email', placeholder: 'name@example.com', hint: 'We never share it.' },
|
|
278
|
+
parameters: {
|
|
279
|
+
anatomy: {
|
|
280
|
+
root: '[data-part="field"]',
|
|
281
|
+
groups: {
|
|
282
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
283
|
+
Input: { note: 'What people type', args: ['placeholder', 'disabled'] },
|
|
284
|
+
Hint: { note: 'Help under the field', args: ['hint'] },
|
|
285
|
+
},
|
|
286
|
+
parts: {
|
|
287
|
+
Label: '[data-part="label"]',
|
|
288
|
+
Input: '[data-part="input"]',
|
|
289
|
+
Hint: '[data-part="hint"]',
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
The `template` option needs the Vue build that includes the compiler. A single-file component does not need it.
|
|
297
|
+
|
|
298
|
+
## Web component with shadow DOM
|
|
299
|
+
|
|
300
|
+
Give each shadow part a `part` attribute. In `parameters.anatomy`, write the selector as `host::part(name)`. The addon finds `[part~="name"]` in the open shadow root of `host`.
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
// ui-text-field.stories.ts
|
|
304
|
+
import type { Meta, StoryObj } from '@storybook/web-components-vite';
|
|
305
|
+
import { html } from 'lit';
|
|
306
|
+
|
|
307
|
+
class UiTextField extends HTMLElement {
|
|
308
|
+
static observedAttributes = ['label', 'hint'];
|
|
309
|
+
|
|
310
|
+
constructor() {
|
|
311
|
+
super();
|
|
312
|
+
this.attachShadow({ mode: 'open' });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
connectedCallback() {
|
|
316
|
+
this.draw();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
attributeChangedCallback() {
|
|
320
|
+
this.draw();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private draw() {
|
|
324
|
+
const label = this.getAttribute('label') ?? '';
|
|
325
|
+
const hint = this.getAttribute('hint') ?? '';
|
|
326
|
+
this.shadowRoot!.innerHTML = `
|
|
327
|
+
<div part="field">
|
|
328
|
+
${label ? `<label part="label">${label}</label>` : ''}
|
|
329
|
+
<input part="input" />
|
|
330
|
+
${hint ? `<span part="hint">${hint}</span>` : ''}
|
|
331
|
+
</div>`;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (!customElements.get('ui-text-field')) customElements.define('ui-text-field', UiTextField);
|
|
335
|
+
|
|
336
|
+
interface Args {
|
|
337
|
+
label: string;
|
|
338
|
+
hint: string;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const meta: Meta<Args> = {
|
|
342
|
+
title: 'Components/Text field (web component)',
|
|
343
|
+
argTypes: {
|
|
344
|
+
label: { control: 'text' },
|
|
345
|
+
hint: { control: 'text' },
|
|
346
|
+
},
|
|
347
|
+
render: ({ label, hint }) => html`<ui-text-field label=${label} hint=${hint}></ui-text-field>`,
|
|
348
|
+
};
|
|
349
|
+
export default meta;
|
|
350
|
+
|
|
351
|
+
type Story = StoryObj<Args>;
|
|
352
|
+
|
|
353
|
+
export const Playground: Story = {
|
|
354
|
+
args: { label: 'Email', hint: 'We never share it.' },
|
|
355
|
+
parameters: {
|
|
356
|
+
anatomy: {
|
|
357
|
+
root: 'ui-text-field',
|
|
358
|
+
groups: {
|
|
359
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
360
|
+
Hint: { note: 'Help under the field', args: ['hint'] },
|
|
361
|
+
},
|
|
362
|
+
parts: {
|
|
363
|
+
Label: 'ui-text-field::part(label)',
|
|
364
|
+
Hint: 'ui-text-field::part(hint)',
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Always name the host before `::part`. A bare `::part(label)` finds nothing. The addon cannot search a closed shadow root.
|
|
372
|
+
|
|
373
|
+
## Plain HTML
|
|
374
|
+
|
|
375
|
+
This is the setup of the demo in this repository (`@storybook/html-vite`). The `render` function returns a string.
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
// button.stories.ts
|
|
379
|
+
import type { Meta, StoryObj } from '@storybook/html-vite';
|
|
380
|
+
|
|
381
|
+
interface ButtonArgs {
|
|
382
|
+
label: string;
|
|
383
|
+
icon: string;
|
|
384
|
+
disabled: boolean;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const meta: Meta<ButtonArgs> = {
|
|
388
|
+
title: 'Components/Button',
|
|
389
|
+
argTypes: {
|
|
390
|
+
label: { control: 'text' },
|
|
391
|
+
icon: { control: 'text' },
|
|
392
|
+
disabled: { control: 'boolean' },
|
|
393
|
+
},
|
|
394
|
+
render: ({ label, icon, disabled }) => `
|
|
395
|
+
<button class="btn" data-part="button" ${disabled ? 'disabled' : ''}>
|
|
396
|
+
${icon ? `<span data-part="icon">${icon}</span>` : ''}
|
|
397
|
+
<span data-part="label">${label}</span>
|
|
398
|
+
</button>`,
|
|
399
|
+
};
|
|
400
|
+
export default meta;
|
|
401
|
+
|
|
402
|
+
type Story = StoryObj<ButtonArgs>;
|
|
403
|
+
|
|
404
|
+
export const Playground: Story = {
|
|
405
|
+
args: { label: 'Save', icon: '+', disabled: false },
|
|
406
|
+
parameters: {
|
|
407
|
+
anatomy: {
|
|
408
|
+
root: '[data-part="button"]',
|
|
409
|
+
groups: {
|
|
410
|
+
Label: { note: 'Say what it does', args: ['label'] },
|
|
411
|
+
Icon: { note: 'Add an icon', args: ['icon'] },
|
|
412
|
+
State: { note: 'Turn it off', args: ['disabled'] },
|
|
413
|
+
},
|
|
414
|
+
parts: {
|
|
415
|
+
Label: '[data-part="label"]',
|
|
416
|
+
Icon: '[data-part="icon"]',
|
|
417
|
+
State: '[data-part="button"]',
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
## A component you do not own
|
|
425
|
+
|
|
426
|
+
A third-party component has no `data-part` attributes. Target its own class names. Give each part a list of selectors. The first visible match wins, so a list also covers versions that rename a class.
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
export const Playground: Story = {
|
|
430
|
+
args: { label: 'Email', hint: 'We never share it.' },
|
|
431
|
+
parameters: {
|
|
432
|
+
anatomy: {
|
|
433
|
+
root: ['.lib-field', '.lib-form-field', 'lib-text-field'],
|
|
434
|
+
groups: {
|
|
435
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
436
|
+
Hint: { note: 'Help under the field', args: ['hint'] },
|
|
437
|
+
},
|
|
438
|
+
parts: {
|
|
439
|
+
Label: ['.lib-field__label', '.lib-label', 'label'],
|
|
440
|
+
Hint: ['.lib-field__hint', '.lib-hint', '[role="note"]'],
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
};
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Tips:
|
|
448
|
+
|
|
449
|
+
- A selector that matches a hidden element is skipped. A list entry for a state that is off is safe.
|
|
450
|
+
- Put the most specific selector first and a broad fallback last.
|
|
451
|
+
- Prefer a stable class over a position selector such as `:nth-child`.
|
|
452
|
+
- If the library renders outside the story container (a portal or an overlay), the addon cannot find it. Set `storyRoot` only when your framework renders the story somewhere other than `#storybook-root` or `#root`.
|
|
453
|
+
|
|
454
|
+
## Reservable slots
|
|
455
|
+
|
|
456
|
+
A slot is one column of the component row. Mark a slot `reservable: true` when its content can be empty. The addon then adds the arg `reserve<Name>Space`.
|
|
457
|
+
|
|
458
|
+
```ts
|
|
459
|
+
parameters: {
|
|
460
|
+
anatomy: {
|
|
461
|
+
groups: {
|
|
462
|
+
Icons: {
|
|
463
|
+
note: 'Add icons',
|
|
464
|
+
// Reserve args appear in a note only when you list them.
|
|
465
|
+
args: ['startIcon', 'endIcon', 'reserveStartIconSpace', 'reserveEndIconSpace'],
|
|
466
|
+
},
|
|
467
|
+
},
|
|
468
|
+
parts: { Icons: '[data-part="startIcon"]' },
|
|
469
|
+
slots: [
|
|
470
|
+
{ name: 'startIcon', part: '[data-part="startIcon"]', reservable: true, width: 24 },
|
|
471
|
+
{ name: 'endIcon', part: '[data-part="endIcon"]', reservable: true },
|
|
472
|
+
],
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
How the name forms: `startIcon` gives `reserveStartIconSpace`. `endIcon` gives `reserveEndIconSpace`. The package also exports `reserveArgName(slot)` if you need the name in code.
|
|
478
|
+
|
|
479
|
+
What the slot shows:
|
|
480
|
+
|
|
481
|
+
| State | Drawn as |
|
|
482
|
+
| --- | --- |
|
|
483
|
+
| Element present, shows content | Plain slot, for example `start icon · 40`. |
|
|
484
|
+
| Element present, shows nothing | Hatched, `kept empty · 44`. |
|
|
485
|
+
| Element absent, reserve arg on | Hatched, `reserved · 24`. |
|
|
486
|
+
| Element absent, reserve arg off | Not drawn. |
|
|
487
|
+
|
|
488
|
+
Rules:
|
|
489
|
+
|
|
490
|
+
- The reserve arg changes only the overlay. The component layout does not change.
|
|
491
|
+
- `width` sets the width of the reserved slot. If you omit it, the addon uses the last measured width, then 24.
|
|
492
|
+
- Set `caption` to change the words under the slot. The default is the name in words.
|
|
493
|
+
- The arg defaults to `false`. It shows in Controls only while **Anatomy** is on.
|
|
494
|
+
|
|
495
|
+
## Groups, layout and exclude
|
|
496
|
+
|
|
497
|
+
A note shows only the args in its `args` list, in that order. Every other arg stays in Controls.
|
|
498
|
+
|
|
499
|
+
```ts
|
|
500
|
+
parameters: {
|
|
501
|
+
anatomy: {
|
|
502
|
+
groups: {
|
|
503
|
+
// The key is the note id. `note` is the handwritten title.
|
|
504
|
+
Label: { note: 'Name the field', args: ['label'] },
|
|
505
|
+
Input: { note: 'What people type', args: ['placeholder', 'disabled', 'debugId'] },
|
|
506
|
+
Hint: { args: ['hint'] }, // the title is the key: "Hint"
|
|
507
|
+
},
|
|
508
|
+
// Pin a note to a side. Other notes are balanced by the addon.
|
|
509
|
+
layout: { Label: 'top', Input: 'bottom' },
|
|
510
|
+
// Never show these args in a note, even when a group lists them.
|
|
511
|
+
exclude: ['debugId'],
|
|
512
|
+
parts: {
|
|
513
|
+
Label: '[data-part="label"]',
|
|
514
|
+
Input: '[data-part="input"]',
|
|
515
|
+
Hint: '[data-part="hint"]',
|
|
516
|
+
},
|
|
517
|
+
},
|
|
518
|
+
},
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
Details:
|
|
522
|
+
|
|
523
|
+
- A `layout` side is `'left'`, `'right'`, `'top'` or `'bottom'`. Without `layout`, the addon balances the columns.
|
|
524
|
+
- `parts` is keyed by note id or by arg name. An arg key wins while that control has focus. The note id is the fallback.
|
|
525
|
+
- A note with no usable arg is not drawn. An arg without a control (a function, an output) is skipped.
|
|
526
|
+
- The notes are drawn in the order of `groups`.
|
|
527
|
+
|
|
528
|
+
Control kinds in a note:
|
|
529
|
+
|
|
530
|
+
| `control` | Shown as |
|
|
531
|
+
| --- | --- |
|
|
532
|
+
| `boolean` | Switch |
|
|
533
|
+
| `select`, `radio`, `inline-radio` (with `options`) | Select |
|
|
534
|
+
| `text` | Text field |
|
|
535
|
+
| `color` | Colour picker and hex field |
|
|
536
|
+
| `number`, `range` | Number field |
|
|
537
|
+
| `object` | Small JSON field |
|
|
538
|
+
|
|
539
|
+
## Theming and fonts
|
|
540
|
+
|
|
541
|
+
Set the `--anatomy-*` variables in your preview styles. For example, import a CSS file in `.storybook/preview.ts`.
|
|
542
|
+
|
|
543
|
+
```css
|
|
544
|
+
/* .storybook/anatomy-theme.css */
|
|
545
|
+
:root {
|
|
546
|
+
--anatomy-accent: #b3261e;
|
|
547
|
+
--anatomy-wire: #8a8f98;
|
|
548
|
+
--anatomy-paper: #fbf8f4;
|
|
549
|
+
--anatomy-ink: #222222;
|
|
550
|
+
--anatomy-slot-line: rgba(179, 38, 30, 0.35);
|
|
551
|
+
--anatomy-slot-fill: rgba(179, 38, 30, 0.04);
|
|
552
|
+
--anatomy-slot-hatch: rgba(179, 38, 30, 0.22);
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
```ts
|
|
557
|
+
// .storybook/preview.ts
|
|
558
|
+
import './anatomy-theme.css';
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
Fonts: the note titles use Architects Daughter. The addon loads it from Google Fonts. To use your own fonts, or to load nothing, set `font`:
|
|
562
|
+
|
|
563
|
+
```ts
|
|
564
|
+
parameters: {
|
|
565
|
+
anatomy: {
|
|
566
|
+
font: {
|
|
567
|
+
notes: "'Patrick Hand', cursive", // font-family of the titles
|
|
568
|
+
body: 'Inter, sans-serif', // font-family of the small texts and controls
|
|
569
|
+
load: false, // do not load Google Fonts
|
|
570
|
+
},
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
Set `load: false` when the page is offline or a strict Content Security Policy blocks Google Fonts. The titles then use the `notes` font, or a cursive fallback. Load your own font files in the preview.
|
|
576
|
+
|
|
577
|
+
## Checklist for a good anatomy story
|
|
578
|
+
|
|
579
|
+
- [ ] Put `parameters.anatomy` on one **Playground** story, not on every story.
|
|
580
|
+
- [ ] Use four to six notes at most.
|
|
581
|
+
- [ ] Put one to three args in each note. A note with ten controls is a form, not a note.
|
|
582
|
+
- [ ] Point each note at one part.
|
|
583
|
+
- [ ] Group args by what they change on screen, not by their type.
|
|
584
|
+
- [ ] Use `data-part` attributes for your own components. Do not use class names that change.
|
|
585
|
+
- [ ] Keep each `data-part` name unchanged across releases.
|
|
586
|
+
- [ ] Give each arg a `control`. An arg without a control cannot show in a note.
|
|
587
|
+
- [ ] Use short note titles in the imperative: "Name the field", "Add icons".
|
|
588
|
+
- [ ] Mark a slot `reservable` only when its content can be empty.
|
|
589
|
+
- [ ] Set `layout` only for a note whose side matters. Let the addon balance the rest.
|
|
590
|
+
- [ ] Open the story at a narrow width. Check that the picker works.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# How the addon works
|
|
2
|
+
|
|
3
|
+
This page explains what happens inside the addon. You do not need it to use the addon. Read it when a note shows in the wrong place or an arrow points at the wrong part.
|
|
4
|
+
|
|
5
|
+
The addon has two parts: a **manager** part and a **preview** part. Storybook loads them in two different frames.
|
|
6
|
+
|
|
7
|
+
## The manager part
|
|
8
|
+
|
|
9
|
+
The manager is the Storybook UI around the Canvas. It runs in the outer frame.
|
|
10
|
+
|
|
11
|
+
The manager registers one toolbar tool. The tool shows only on the Canvas tab, not in Docs. It has two toggle buttons:
|
|
12
|
+
|
|
13
|
+
| Button | Global | Shows when |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| **Anatomy** | `anatomy` | The story has `parameters.anatomy`. |
|
|
16
|
+
| **Box model** | `anatomyBoxModel` | Always. |
|
|
17
|
+
|
|
18
|
+
A toggle button writes a Storybook global with `updateGlobals`. The manager does nothing else. It does not draw and it does not read your component.
|
|
19
|
+
|
|
20
|
+
Both globals start as `false`. The preview part sets them with `initialGlobals`.
|
|
21
|
+
|
|
22
|
+
## The preview part
|
|
23
|
+
|
|
24
|
+
The preview runs in the iframe that holds your story. The addon exports four preview annotations. Storybook reads them when the addon is in `addons`.
|
|
25
|
+
|
|
26
|
+
| Export | What it does |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `decorators` | Registers `withAnatomy`. It mounts the overlay and the box-model inspector. |
|
|
29
|
+
| `initialGlobals` | Sets `anatomy` and `anatomyBoxModel` to `false`. |
|
|
30
|
+
| `argTypesEnhancers` | Adds one `reserve<Name>Space` argType for each slot with `reservable: true`. |
|
|
31
|
+
| `argsEnhancers` | Gives each reserve arg the default value `false`. |
|
|
32
|
+
|
|
33
|
+
### The reserve args
|
|
34
|
+
|
|
35
|
+
A slot with `reservable: true` gets one boolean arg. Its name is `reserve` + the slot name in PascalCase + `Space`. The slot `startIcon` gives `reserveStartIconSpace`.
|
|
36
|
+
|
|
37
|
+
The arg sits in the **Anatomy** category of the Controls panel. It shows only while the **Anatomy** toggle is on.
|
|
38
|
+
|
|
39
|
+
The arg is for the overlay only. It never reaches your component.
|
|
40
|
+
|
|
41
|
+
### The decorator
|
|
42
|
+
|
|
43
|
+
`withAnatomy` does four things.
|
|
44
|
+
|
|
45
|
+
1. It reads the story `parameters.anatomy` and the two globals.
|
|
46
|
+
2. It removes the reserve args from `context.args`. Then it calls the story with the cleaned args. Your component and your source snippets never see them.
|
|
47
|
+
3. It mounts the anatomy view when the **Anatomy** global is on. It mounts after the story has rendered, because it uses `useEffect`.
|
|
48
|
+
4. It starts the box-model inspector when either toggle is on.
|
|
49
|
+
|
|
50
|
+
The decorator is the outermost decorator. This way the reserve args are gone before any other decorator runs.
|
|
51
|
+
|
|
52
|
+
### How notes write args
|
|
53
|
+
|
|
54
|
+
Each control in a note calls `updateArgs` from `useArgs()`. Storybook then changes the story args. This is the same path the Controls panel uses.
|
|
55
|
+
|
|
56
|
+
So the notes and the Controls panel stay in sync:
|
|
57
|
+
|
|
58
|
+
- You change a control in a note. Storybook updates the args. The Controls panel shows the new value.
|
|
59
|
+
- You change a control in the Controls panel. Storybook re-renders the story. The decorator calls `view.update(args)`. The notes show the new value.
|
|
60
|
+
|
|
61
|
+
A note shows only the args listed in `groups[note].args`. An arg without a control is never shown. Functions and outputs have no control.
|
|
62
|
+
|
|
63
|
+
## The render lifecycle
|
|
64
|
+
|
|
65
|
+
The overlay is a plain DOM layer. It is a child of `document.body`. It does not sit inside your story.
|
|
66
|
+
|
|
67
|
+
```mermaid
|
|
68
|
+
sequenceDiagram
|
|
69
|
+
participant S as Storybook
|
|
70
|
+
participant D as withAnatomy
|
|
71
|
+
participant V as Anatomy view
|
|
72
|
+
participant P as Your story DOM
|
|
73
|
+
|
|
74
|
+
S->>D: Render story (args)
|
|
75
|
+
D->>P: Story renders with cleaned args
|
|
76
|
+
S->>D: Story rendered (effects run)
|
|
77
|
+
D->>V: mountAnatomy (first time)
|
|
78
|
+
D->>V: update(args) on every render
|
|
79
|
+
V->>P: Find the root (storyRoot, root)
|
|
80
|
+
V->>P: Find parts (selectors, lists, shadow roots)
|
|
81
|
+
V->>P: Measure slots
|
|
82
|
+
V->>V: Place notes (balance, layout)
|
|
83
|
+
V->>V: Draw slot boxes and arrows
|
|
84
|
+
Note over V,P: A ResizeObserver and a MutationObserver<br/>schedule the next pass
|
|
85
|
+
P-->>V: Pointer moves with Alt held
|
|
86
|
+
V->>P: Draw the box-model overlay
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The steps in order:
|
|
90
|
+
|
|
91
|
+
1. **The story renders.** Your framework renders the story as usual. The addon does not change the template.
|
|
92
|
+
2. **The view mounts.** After the render, the decorator calls `mountAnatomy`. The view adds a `sba-on` class to `<html>`. It adds a stage class to the story container.
|
|
93
|
+
3. **The addon finds the root.** The story container is `parameters.anatomy.storyRoot`. The default is `#storybook-root`, then `#root`. The component element is `parameters.anatomy.root` inside that container. If `root` finds nothing, the container is the component.
|
|
94
|
+
4. **The addon finds the parts.** For each note, the addon looks up a selector. It tries `parts[arg]` for the arg that has focus. Then it tries `parts[noteId]`.
|
|
95
|
+
- A selector can be a list. The first **visible** match wins.
|
|
96
|
+
- A selector that matches a hidden element or an element with no size is skipped.
|
|
97
|
+
- The search goes into open shadow roots.
|
|
98
|
+
- `host::part(name)` looks for `[part~="name"]` in the shadow root of `host`.
|
|
99
|
+
- A bad selector finds nothing. It does not break the story.
|
|
100
|
+
5. **The addon measures the slots.** Each slot in `slots` gets a state:
|
|
101
|
+
|
|
102
|
+
| Slot element | State |
|
|
103
|
+
| --- | --- |
|
|
104
|
+
| Present and shows content | Plain slot. |
|
|
105
|
+
| Present, keeps its box, shows nothing | Hatched, `kept empty`. |
|
|
106
|
+
| Absent, `reservable`, reserve arg on | Hatched, `reserved`. |
|
|
107
|
+
| Absent otherwise | Not drawn. |
|
|
108
|
+
|
|
109
|
+
6. **The addon places the notes.** The layout puts notes on the left and right of the component. It balances the column heights. It uses the side nearest to each part. A `layout` entry fixes the side of one note and the balance never moves it. The addon grows the space above and below the story so that every note fits. It repeats the measure step up to three times.
|
|
110
|
+
7. **The addon draws the arrows.** Each note gets a hand-drawn arrow to its part. The note with focus gets the accent colour. Focus is a pointer over the note or a focus in one of its controls.
|
|
111
|
+
8. **Hover and Alt.** In the anatomy view, hold **Alt** and move the pointer. The addon draws margin, border, padding and content of the element under the pointer. The **Box model** toggle does the same without Alt.
|
|
112
|
+
|
|
113
|
+
### When the view draws again
|
|
114
|
+
|
|
115
|
+
The view draws again, in one animation frame, when:
|
|
116
|
+
|
|
117
|
+
- the args change,
|
|
118
|
+
- the story container changes size, content or attributes,
|
|
119
|
+
- a transition or animation ends,
|
|
120
|
+
- focus moves in or out of the story,
|
|
121
|
+
- the window resizes,
|
|
122
|
+
- the fonts finish loading.
|
|
123
|
+
|
|
124
|
+
When you switch the toggle off or change the story, the view removes its layer and its classes.
|
|
125
|
+
|
|
126
|
+
### Narrow pages
|
|
127
|
+
|
|
128
|
+
At 620 px and below, the view shows a picker and one note at a time. The component sits under the note.
|
|
129
|
+
|
|
130
|
+
## Why it never wraps your story
|
|
131
|
+
|
|
132
|
+
The addon does not put your story inside a wrapper element. It does not change your template, your component or your styles.
|
|
133
|
+
|
|
134
|
+
This has three effects:
|
|
135
|
+
|
|
136
|
+
- It works with any renderer. The addon uses only the DOM.
|
|
137
|
+
- Your component sees the same DOM, the same args and the same events as without the addon.
|
|
138
|
+
- Source snippets stay clean. The reserve args are removed before the story renders.
|
|
139
|
+
|
|
140
|
+
The overlay is a sibling layer on `document.body`. The addon only adds a few classes and CSS custom properties to the story container. It removes them when the view closes.
|
|
141
|
+
|
|
142
|
+
## Limitations
|
|
143
|
+
|
|
144
|
+
- The toolbar tools show only in the Canvas, not in Docs.
|
|
145
|
+
- A part must exist in the DOM of the story. The addon cannot search closed shadow roots.
|
|
146
|
+
- The anatomy view reads `parameters.anatomy` as it is. It does not read your component source.
|
|
147
|
+
- A reserved slot is an illustration. The component layout does not change.
|
|
148
|
+
- A note can show a control only for these kinds: `boolean`, `select`, `radio`, `inline-radio` (with `options`), `text`, `color`, `number`, `range` and `object`.
|
|
149
|
+
- At 620 px and below, the view shows one note at a time.
|
|
150
|
+
- If you register `withAnatomy` by hand, put it last in `decorators`.
|
|
151
|
+
- The view draws one story at a time.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-nomads/storybook-addon-anatomy",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Explain a component's anatomy in Storybook: handwritten notes with live arg controls around the story, hand-drawn arrows to each part, reserved-space slots and a box-model overlay.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"storybook-addon",
|