storybook-addon-sdc 0.9.2 → 0.9.4
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 +78 -2
- package/dist/preset.js +93 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -302,6 +302,51 @@ The addon dynamically renders the components and stories as defined:
|
|
|
302
302
|
|
|
303
303
|
[](https://gyazo.com/7212a3f44052ebde34b59a1555d96afe)
|
|
304
304
|
|
|
305
|
+
### Supported Types
|
|
306
|
+
|
|
307
|
+
#### `component`
|
|
308
|
+
|
|
309
|
+
Use `component` to nest other components within your story.
|
|
310
|
+
|
|
311
|
+
```yaml
|
|
312
|
+
type: component
|
|
313
|
+
component: 'umami:badge'
|
|
314
|
+
props:
|
|
315
|
+
icon: serves
|
|
316
|
+
slots:
|
|
317
|
+
text:
|
|
318
|
+
type: component
|
|
319
|
+
component: 'umami:title'
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
#### `html_tag`
|
|
325
|
+
|
|
326
|
+
Use `html_tag` to embed HTML markup within a specific tag.
|
|
327
|
+
|
|
328
|
+
```yaml
|
|
329
|
+
type: html_tag
|
|
330
|
+
value: 'Markup'
|
|
331
|
+
attributes:
|
|
332
|
+
class: class-1
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
#### `image`
|
|
338
|
+
|
|
339
|
+
Use `image` to embed images into your story.
|
|
340
|
+
|
|
341
|
+
```yaml
|
|
342
|
+
type: image
|
|
343
|
+
uri: 'Markup'
|
|
344
|
+
attributes:
|
|
345
|
+
class: class-1
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
305
350
|
## Support for Single Story Files (`*.story.yml`)
|
|
306
351
|
|
|
307
352
|
In addition to stories defined inside `*.component.yml` files, the addon now supports standalone story files with the `.story.yml` extension.
|
|
@@ -320,7 +365,10 @@ props:
|
|
|
320
365
|
props:
|
|
321
366
|
icon: timer
|
|
322
367
|
slots:
|
|
323
|
-
text:
|
|
368
|
+
text:
|
|
369
|
+
type: html_tag
|
|
370
|
+
tag: span
|
|
371
|
+
value: Test
|
|
324
372
|
- type: component
|
|
325
373
|
component: 'umami:badge'
|
|
326
374
|
props:
|
|
@@ -335,6 +383,35 @@ props:
|
|
|
335
383
|
text: Ciao
|
|
336
384
|
```
|
|
337
385
|
|
|
386
|
+
## Extending SDC Story Rendering
|
|
387
|
+
|
|
388
|
+
You can add custom renderers for additional `story` node types.
|
|
389
|
+
For example, to render a custom `icon` type:
|
|
390
|
+
|
|
391
|
+
```yaml
|
|
392
|
+
- type: icon
|
|
393
|
+
icon: arrow
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
Add the following to your `sdcStorybookOptions`:
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
const sdcStorybookOptions: SDCStorybookOptions = {
|
|
400
|
+
...
|
|
401
|
+
storyNodesRenderer: [
|
|
402
|
+
{
|
|
403
|
+
appliesTo: item => item?.type === 'icon',
|
|
404
|
+
render: item =>
|
|
405
|
+
Json.toString(
|
|
406
|
+
`<svg class="icon" aria-hidden="true"><use xlink:href="#${item.icon}"></use></svg>`
|
|
407
|
+
),
|
|
408
|
+
priority: -4,
|
|
409
|
+
},
|
|
410
|
+
],
|
|
411
|
+
...
|
|
412
|
+
}
|
|
413
|
+
```
|
|
414
|
+
|
|
338
415
|
### Why stories experimental?
|
|
339
416
|
|
|
340
417
|
The [community will have to decide](https://docs.google.com/document/d/1wCQLXrK1lrV2gYlqmqD2pybTql6_H1dByWIKB5xQFcQ/edit?tab=t.0#heading=h.3949vjfiqczr) what format the YAML stories should be.
|
|
@@ -458,7 +535,6 @@ const config = {
|
|
|
458
535
|
## Known Issues
|
|
459
536
|
|
|
460
537
|
- The addon relies on [Experimental indexers](https://storybook.js.org/docs/api/main-config/main-config-indexers).
|
|
461
|
-
- Twing works in Node 23+
|
|
462
538
|
|
|
463
539
|
## UI Patterns
|
|
464
540
|
|
package/dist/preset.js
CHANGED
|
@@ -9,6 +9,9 @@ import { JSONSchemaFaker } from "json-schema-faker";
|
|
|
9
9
|
var generateArgs = (schema, defs) => {
|
|
10
10
|
return Object.entries(schema).reduce((acc, [key, property]) => {
|
|
11
11
|
acc[key] = JSONSchemaFaker.generate(property, defs);
|
|
12
|
+
if (acc[key] instanceof Object) {
|
|
13
|
+
acc[key] = Object.values(acc[key]);
|
|
14
|
+
}
|
|
12
15
|
return acc;
|
|
13
16
|
}, {});
|
|
14
17
|
};
|
|
@@ -66,6 +69,81 @@ var resolveComponentPath = (namespace, component) => {
|
|
|
66
69
|
);
|
|
67
70
|
return possiblePaths.find(existsSync);
|
|
68
71
|
};
|
|
72
|
+
var toAttributes = (attrs) => {
|
|
73
|
+
if (!attrs) return "";
|
|
74
|
+
return " " + Object.entries(attrs).map(([key, value]) => {
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
value = value.join(" ");
|
|
77
|
+
}
|
|
78
|
+
return `${key}="${value}"`;
|
|
79
|
+
}).join(" ");
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/storyNodeRender.ts
|
|
83
|
+
var generateArgs2 = (args, isSlot = false) => Object.entries(args).map(([key, value]) => `${key}: ${formatArgValue(value, isSlot)},`).join("\n");
|
|
84
|
+
var formatArgValue = (value, isSlot) => {
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
const arrayContent = value.map((item) => storyNodeRenderer.render(item));
|
|
87
|
+
return `new TwigSafeArray(${arrayContent.join(", ")})`;
|
|
88
|
+
}
|
|
89
|
+
return storyNodeRenderer.render(value);
|
|
90
|
+
};
|
|
91
|
+
var StoryNodeRenderService = class {
|
|
92
|
+
renderer = [];
|
|
93
|
+
register(renderers) {
|
|
94
|
+
renderers.forEach((renderer) => {
|
|
95
|
+
this.renderer.push(renderer);
|
|
96
|
+
this.renderer.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
render(item) {
|
|
100
|
+
const renderer = this.renderer.find((h) => h.appliesTo(item));
|
|
101
|
+
return renderer?.render ? renderer.render(item) : JSON.stringify(item);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var renderComponent = (item) => {
|
|
105
|
+
const kebabCaseName = convertToKebabCase(item.component);
|
|
106
|
+
const componentProps = `...{ ${generateArgs2(item.props ?? {}, false)}}, ...{${generateArgs2(item.slots ?? {}, true)}}`;
|
|
107
|
+
const storyArgs = item.story ? `...${kebabCaseName}.${capitalize(item.story)}.args` : "...{}";
|
|
108
|
+
return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.args, ${storyArgs}, ${componentProps}})`;
|
|
109
|
+
};
|
|
110
|
+
var renderImage = (item) => {
|
|
111
|
+
return JSON.stringify(
|
|
112
|
+
`<img src="${item.uri}"${toAttributes(item.attributes)}>`
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
var renderHtmlTag = (item) => {
|
|
116
|
+
return JSON.stringify(
|
|
117
|
+
`<${item.tag ?? "div"}${toAttributes(item.attributes)}> ${item.value} </${item.tag ?? "div"}>`
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
var renderMarkup = (item) => {
|
|
121
|
+
return JSON.stringify(`${item.markup}`);
|
|
122
|
+
};
|
|
123
|
+
var defaultStoryNodes = [
|
|
124
|
+
{
|
|
125
|
+
appliesTo: (item) => item?.type === "component",
|
|
126
|
+
render: (item) => renderComponent(item),
|
|
127
|
+
priority: -4
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
appliesTo: (item) => item?.theme === "image" || item?.type === "image",
|
|
131
|
+
render: (item) => renderImage(item),
|
|
132
|
+
priority: -1
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
appliesTo: (item) => item?.type === "html_tag" || item?.theme === "html_tag",
|
|
136
|
+
render: (item) => renderHtmlTag(item),
|
|
137
|
+
priority: -2
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
appliesTo: (item) => item?.type === "markup" || item?.theme === "markup",
|
|
141
|
+
render: (item) => renderMarkup(item),
|
|
142
|
+
priority: -2
|
|
143
|
+
}
|
|
144
|
+
];
|
|
145
|
+
var storyNodeRenderer = new StoryNodeRenderService();
|
|
146
|
+
storyNodeRenderer.register(defaultStoryNodes);
|
|
69
147
|
|
|
70
148
|
// src/storiesGenerator.ts
|
|
71
149
|
var storiesGenerator_default = (stories) => Object.entries(stories).map(
|
|
@@ -73,7 +151,7 @@ var storiesGenerator_default = (stories) => Object.entries(stories).map(
|
|
|
73
151
|
export const ${capitalize(storyKey)} = {
|
|
74
152
|
args: {
|
|
75
153
|
...Basic.args,
|
|
76
|
-
${generateArgs2(props)}
|
|
154
|
+
${generateArgs2(props, false)}
|
|
77
155
|
${generateArgs2(slots, true)}
|
|
78
156
|
${generateVariants(variants)}
|
|
79
157
|
},
|
|
@@ -83,22 +161,6 @@ export const ${capitalize(storyKey)} = {
|
|
|
83
161
|
};
|
|
84
162
|
`
|
|
85
163
|
).join("\n");
|
|
86
|
-
var generateArgs2 = (args, isSlot = false) => Object.entries(args).map(([key, value]) => `${key}: ${formatArgValue(value, isSlot)},`).join("\n");
|
|
87
|
-
var formatArgValue = (value, isSlot) => {
|
|
88
|
-
if (Array.isArray(value)) {
|
|
89
|
-
const arrayContent = value.map(
|
|
90
|
-
(item) => item?.type === "component" ? generateComponent(item) : JSON.stringify(item)
|
|
91
|
-
);
|
|
92
|
-
return `new TwigSafeArray(${arrayContent.join(", ")})`;
|
|
93
|
-
}
|
|
94
|
-
return JSON.stringify(value);
|
|
95
|
-
};
|
|
96
|
-
var generateComponent = (item) => {
|
|
97
|
-
const kebabCaseName = convertToKebabCase(item.component);
|
|
98
|
-
const componentProps = { ...item.props, ...item.slots };
|
|
99
|
-
const storyArgs = item.story ? `...${kebabCaseName}.${item.story}.args` : "...{}";
|
|
100
|
-
return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.args, ${storyArgs}, ...${JSON.stringify(componentProps)}})`;
|
|
101
|
-
};
|
|
102
164
|
var generateVariants = (variants) => {
|
|
103
165
|
return Object.entries(variants).map(
|
|
104
166
|
([variantKey, variantValue]) => `${variantKey}: ${JSON.stringify(variantValue.title)},`
|
|
@@ -189,22 +251,27 @@ var generateImports = (directory) => readdirSync2(directory).filter(
|
|
|
189
251
|
}).join("\n");
|
|
190
252
|
var dynamicImports = (stories) => {
|
|
191
253
|
const imports = /* @__PURE__ */ new Set();
|
|
254
|
+
const importComponent = (item) => {
|
|
255
|
+
const [namespace, componentName] = item.component.split(":");
|
|
256
|
+
const resolvedPath = resolveComponentPath(namespace, componentName);
|
|
257
|
+
const kebabCaseName = convertToKebabCase(item.component);
|
|
258
|
+
if (resolvedPath) {
|
|
259
|
+
imports.add(`import * as ${kebabCaseName} from '${resolvedPath}';`);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
192
262
|
const extractComponentImports = (args) => {
|
|
193
263
|
Object.values(args).forEach((value) => {
|
|
194
264
|
if (Array.isArray(value)) {
|
|
195
265
|
value.forEach((item) => {
|
|
196
266
|
if (item.type === "component") {
|
|
197
|
-
|
|
198
|
-
const resolvedPath = resolveComponentPath(namespace, componentName);
|
|
199
|
-
const kebabCaseName = convertToKebabCase(item.component);
|
|
200
|
-
if (resolvedPath) {
|
|
201
|
-
imports.add(
|
|
202
|
-
`import * as ${kebabCaseName} from '${resolvedPath}';`
|
|
203
|
-
);
|
|
204
|
-
}
|
|
267
|
+
importComponent(item);
|
|
205
268
|
}
|
|
269
|
+
extractComponentImports(value);
|
|
206
270
|
});
|
|
207
271
|
} else if (value && typeof value === "object") {
|
|
272
|
+
if (value.type === "component") {
|
|
273
|
+
importComponent(value);
|
|
274
|
+
}
|
|
208
275
|
extractComponentImports(value);
|
|
209
276
|
}
|
|
210
277
|
});
|
|
@@ -253,6 +320,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
253
320
|
...content.thirdPartySettings?.sdcStorybook?.stories || {},
|
|
254
321
|
...loadStoryFilesSync(id)
|
|
255
322
|
};
|
|
323
|
+
storyNodeRenderer.register(sdcStorybookOptions.storyNodesRenderer ?? []);
|
|
256
324
|
const storiesImports = dynamicImports(previewsStories);
|
|
257
325
|
const metadata = componentMetadata_default(id, content);
|
|
258
326
|
const argTypes = {
|
|
@@ -303,7 +371,6 @@ export const Basic = {
|
|
|
303
371
|
Drupal.attachBehaviors(canvasElement, window.drupalSettings);
|
|
304
372
|
},
|
|
305
373
|
};
|
|
306
|
-
|
|
307
374
|
${stories}
|
|
308
375
|
`;
|
|
309
376
|
} catch (error) {
|