storybook-addon-sdc 0.0.18 → 0.1.2
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 +45 -32
- package/dist/preset.js +53 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Storybook SDC Addon Documentation
|
|
2
|
-
Drupal Single Directory Components
|
|
2
|
+
This Storybook addon streamlines the integration of Drupal Single Directory Components(SDC) defined in YAML files (specifically *.component.yml) as stories.
|
|
3
3
|
|
|
4
|
-
[](https://gyazo.com/2dcfa650c961679430d45eef24a82ab8)
|
|
5
5
|
|
|
6
6
|
## Overview:
|
|
7
7
|
|
|
@@ -37,7 +37,7 @@ Here’s why:
|
|
|
37
37
|
- Support for `drupalSettings` and `once.js` within Storybook ensures components behave identically during testing as they will on the Drupal site.
|
|
38
38
|
|
|
39
39
|
#### 7. Twig.js vs Drupal Twig
|
|
40
|
-
While using
|
|
40
|
+
While using Drupal to render components offers tighter integration, there are strong reasons to continue using Twig.js in many scenarios:
|
|
41
41
|
|
|
42
42
|
- Many Components __Don’t Need Full Drupal Logic__. Basic components (buttons, cards, lists) rely on simple HTML and CSS, not on complex template logic. For such components, Twig.js provides sufficient rendering without the need for full Drupal preprocessing.
|
|
43
43
|
- Twig.js Works Well for Frontend-Focused Use Cases.
|
|
@@ -52,24 +52,23 @@ While using __Drupal to render components__ offers tighter integration, there ar
|
|
|
52
52
|
|
|
53
53
|
## This addon uses:
|
|
54
54
|
|
|
55
|
-
- https://github.com/larowlan/vite-plugin-twig-drupal for
|
|
55
|
+
- https://github.com/larowlan/vite-plugin-twig-drupal for loads Twig with Drupal functions.
|
|
56
56
|
- https://github.com/json-schema-faker/json-schema-faker for generation missed props and slots.
|
|
57
57
|
|
|
58
58
|
## Installation:
|
|
59
59
|
|
|
60
|
+
In `@storybook/html-vite`:
|
|
61
|
+
https://storybook.js.org/docs/builders/vite
|
|
62
|
+
|
|
60
63
|
```
|
|
61
64
|
npm i storybook-addon-sdc
|
|
62
65
|
```
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
In vite-html storybook https://storybook.js.org/docs/builders/vite
|
|
67
|
+
### Configuration:
|
|
67
68
|
|
|
68
|
-
main.
|
|
69
|
+
.storybook/main.js
|
|
69
70
|
```
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const config: StorybookConfig = {
|
|
71
|
+
const config = {
|
|
73
72
|
stories: ["../components/**/*.component.yml"], // Your components directory.
|
|
74
73
|
addons: [
|
|
75
74
|
{
|
|
@@ -92,6 +91,31 @@ const config: StorybookConfig = {
|
|
|
92
91
|
export default config;
|
|
93
92
|
```
|
|
94
93
|
|
|
94
|
+
### Default values
|
|
95
|
+
|
|
96
|
+
json-schema-faker not always generate good data. For optimal results, use `default` or `examples` for SDC schema:
|
|
97
|
+
```
|
|
98
|
+
props:
|
|
99
|
+
type: object
|
|
100
|
+
properties:
|
|
101
|
+
html_tag:
|
|
102
|
+
type: string
|
|
103
|
+
enum:
|
|
104
|
+
- article
|
|
105
|
+
- div
|
|
106
|
+
default: article
|
|
107
|
+
slots:
|
|
108
|
+
content:
|
|
109
|
+
title: Content
|
|
110
|
+
examples:
|
|
111
|
+
- Hello! I'm card content
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/annotated-example-componentyml
|
|
115
|
+
- https://json-schema.org/understanding-json-schema/reference/annotations
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
95
119
|
## Experimental stories:
|
|
96
120
|
|
|
97
121
|
By default addon generates only Basic story for every component.
|
|
@@ -127,36 +151,28 @@ thirdPartySettings:
|
|
|
127
151
|
stories:
|
|
128
152
|
preview:
|
|
129
153
|
title: Preview
|
|
130
|
-
|
|
154
|
+
props:
|
|
131
155
|
html_tag: "div"
|
|
156
|
+
slots:
|
|
132
157
|
content:
|
|
133
158
|
- type: component
|
|
134
159
|
component: "umami:title"
|
|
135
|
-
|
|
160
|
+
props:
|
|
136
161
|
label: test
|
|
137
162
|
- type: component
|
|
138
163
|
component: "umami:read-more"
|
|
139
|
-
|
|
164
|
+
slots:
|
|
140
165
|
text: test
|
|
141
|
-
url: '#'
|
|
142
166
|
preview2:
|
|
143
|
-
title:
|
|
144
|
-
|
|
167
|
+
title: Preview2
|
|
168
|
+
props:
|
|
145
169
|
html_tag: "div"
|
|
170
|
+
slots:
|
|
146
171
|
content:
|
|
147
172
|
- type: component
|
|
148
173
|
component: "umami:title"
|
|
149
|
-
|
|
150
|
-
label:
|
|
151
|
-
extra_classes:
|
|
152
|
-
- 'card__title'
|
|
153
|
-
- type: component
|
|
154
|
-
component: "umami:read-more"
|
|
155
|
-
args:
|
|
156
|
-
text: test
|
|
157
|
-
url: '#'
|
|
158
|
-
extra_classes:
|
|
159
|
-
- 'card__read-more'
|
|
174
|
+
props:
|
|
175
|
+
label: test2
|
|
160
176
|
```
|
|
161
177
|
|
|
162
178
|
|
|
@@ -164,7 +180,7 @@ Or you can import yaml in regular storybook *.stories.js
|
|
|
164
180
|
|
|
165
181
|
### Why stories experimental?
|
|
166
182
|
|
|
167
|
-
The community will have to decide what format the
|
|
183
|
+
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.
|
|
168
184
|
|
|
169
185
|
|
|
170
186
|
## Known issues:
|
|
@@ -172,7 +188,4 @@ The community will have to decide what format the yaml stories should be.
|
|
|
172
188
|
- UI Patterns stories format not yet [supported](https://www.drupal.org/project/ui_patterns/issues/3480464).
|
|
173
189
|
- Same problem will be with `$ref: json-schema-definitions://` for SDC from Experience Builder.
|
|
174
190
|
- Plugin uses [Experimental indexers](https://storybook.js.org/docs/api/main-config/main-config-indexers)
|
|
175
|
-
- json-schema-faker not always generate good data. Better to use `default` or `examples` for SDC schema
|
|
176
|
-
https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/annotated-example-componentyml
|
|
177
|
-
https://json-schema.org/understanding-json-schema/reference/annotations
|
|
178
191
|
|
package/dist/preset.js
CHANGED
|
@@ -5478,7 +5478,7 @@ init_esm_shims();
|
|
|
5478
5478
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
5479
5479
|
init_esm_shims();
|
|
5480
5480
|
var import_yaml = __toESM(require_yaml(), 1);
|
|
5481
|
-
import { readdirSync, readFileSync } from "fs";
|
|
5481
|
+
import { readdirSync, readFileSync, existsSync } from "fs";
|
|
5482
5482
|
import { dirname, extname, relative, resolve, join } from "path";
|
|
5483
5483
|
|
|
5484
5484
|
// src/argsGenerator.ts
|
|
@@ -5529,59 +5529,55 @@ var argTypesGenerator_default = (content) => {
|
|
|
5529
5529
|
init_esm_shims();
|
|
5530
5530
|
var storiesGenerator_default = (stories) => {
|
|
5531
5531
|
const generatedStories = [];
|
|
5532
|
-
const
|
|
5532
|
+
const extractArgsAndSlots = (input) => {
|
|
5533
|
+
const args = {};
|
|
5533
5534
|
const slots = {};
|
|
5534
|
-
for (const key in
|
|
5535
|
-
|
|
5536
|
-
|
|
5535
|
+
for (const key in input) {
|
|
5536
|
+
const value = input[key];
|
|
5537
|
+
if (Array.isArray(value)) {
|
|
5538
|
+
value.forEach((item) => {
|
|
5537
5539
|
if (item.type === "component") {
|
|
5538
5540
|
const componentName = item.component.split(":").pop();
|
|
5539
|
-
const kebabCaseName = componentName.replace(
|
|
5540
|
-
const
|
|
5541
|
-
item.
|
|
5542
|
-
|
|
5543
|
-
|
|
5544
|
-
|
|
5545
|
-
|
|
5546
|
-
slots[key] += componentContent;
|
|
5547
|
-
}
|
|
5541
|
+
const kebabCaseName = componentName.replace(/-/g, "");
|
|
5542
|
+
const componentProps = {
|
|
5543
|
+
...item.props || {},
|
|
5544
|
+
...item.slots || {}
|
|
5545
|
+
};
|
|
5546
|
+
const componentContent = `\${${kebabCaseName}.component(${JSON.stringify(componentProps)})}`;
|
|
5547
|
+
slots[key] = (slots[key] || "") + componentContent;
|
|
5548
5548
|
} else {
|
|
5549
|
-
|
|
5550
|
-
if (!slots[key]) {
|
|
5551
|
-
slots[key] = `${slotContent}`;
|
|
5552
|
-
} else {
|
|
5553
|
-
slots[key] += `${slotContent}`;
|
|
5554
|
-
}
|
|
5549
|
+
slots[key] = (slots[key] || "") + `${item}`;
|
|
5555
5550
|
}
|
|
5556
5551
|
});
|
|
5557
|
-
} else if (typeof
|
|
5558
|
-
const
|
|
5559
|
-
Object.
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
} else {
|
|
5563
|
-
slots[nestedSlot] += nestedContent;
|
|
5564
|
-
}
|
|
5552
|
+
} else if (typeof value === "object" && value !== null) {
|
|
5553
|
+
const nested = extractArgsAndSlots(value);
|
|
5554
|
+
Object.assign(args, nested.args);
|
|
5555
|
+
Object.entries(nested.slots).forEach(([nestedKey, nestedContent]) => {
|
|
5556
|
+
slots[nestedKey] = (slots[nestedKey] || "") + nestedContent;
|
|
5565
5557
|
});
|
|
5566
5558
|
} else {
|
|
5567
|
-
|
|
5559
|
+
args[key] = value;
|
|
5568
5560
|
}
|
|
5569
5561
|
}
|
|
5570
|
-
return slots;
|
|
5562
|
+
return { args, slots };
|
|
5571
5563
|
};
|
|
5572
5564
|
Object.entries(stories).forEach(([storyKey, story]) => {
|
|
5573
|
-
const slots =
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
|
|
5565
|
+
const { args, slots } = extractArgsAndSlots(story);
|
|
5566
|
+
Object.entries(slots).forEach(([slotName, content]) => {
|
|
5567
|
+
args[slotName] = `${content}`;
|
|
5568
|
+
});
|
|
5569
|
+
generatedStories.push(`
|
|
5570
|
+
export const ${storyKey} = {
|
|
5577
5571
|
args: {
|
|
5578
|
-
${
|
|
5572
|
+
${Object.entries(args).map(
|
|
5573
|
+
([key, value]) => key === "content" ? `"${key}": \`${value}\`` : `"${key}": ${JSON.stringify(value)}`
|
|
5574
|
+
).join(",\n")}
|
|
5579
5575
|
},
|
|
5580
5576
|
play: async ({ canvasElement }) => {
|
|
5581
5577
|
Drupal.attachBehaviors(canvasElement, window.drupalSettings);
|
|
5582
5578
|
},
|
|
5583
|
-
}
|
|
5584
|
-
);
|
|
5579
|
+
};
|
|
5580
|
+
`);
|
|
5585
5581
|
});
|
|
5586
5582
|
return generatedStories.join("\n");
|
|
5587
5583
|
};
|
|
@@ -5598,19 +5594,22 @@ var resolveComponentPath = (namespace, component) => {
|
|
|
5598
5594
|
...getSubdirectories(baseComponentDir)
|
|
5599
5595
|
];
|
|
5600
5596
|
const possiblePaths = componentDirectories.map(
|
|
5601
|
-
(dir) =>
|
|
5597
|
+
(dir) => join(dir, component, `${component}.component.yml`)
|
|
5602
5598
|
);
|
|
5603
|
-
return possiblePaths.find((path) =>
|
|
5599
|
+
return possiblePaths.find((path) => existsSync(path));
|
|
5604
5600
|
};
|
|
5605
5601
|
var generateImports = (directory) => {
|
|
5606
5602
|
const assets = readdirSync(directory);
|
|
5607
5603
|
return assets.reduce((imports, asset) => {
|
|
5608
5604
|
const assetPath = `./${asset}`;
|
|
5609
|
-
|
|
5610
|
-
|
|
5605
|
+
const extension = extname(asset);
|
|
5606
|
+
if ([".css", ".js"].includes(extension)) {
|
|
5607
|
+
return `${imports} import '${assetPath}';
|
|
5608
|
+
`;
|
|
5611
5609
|
}
|
|
5612
|
-
if (
|
|
5613
|
-
return `${imports} import COMPONENT from '${assetPath}'
|
|
5610
|
+
if (extension === ".twig") {
|
|
5611
|
+
return `${imports} import COMPONENT from '${assetPath}';
|
|
5612
|
+
`;
|
|
5614
5613
|
}
|
|
5615
5614
|
return imports;
|
|
5616
5615
|
}, "");
|
|
@@ -5619,24 +5618,25 @@ var dynamicImports = (stories) => {
|
|
|
5619
5618
|
const imports = /* @__PURE__ */ new Set();
|
|
5620
5619
|
const findComponentArgs = (args) => {
|
|
5621
5620
|
for (const key in args) {
|
|
5622
|
-
|
|
5623
|
-
|
|
5621
|
+
const value = args[key];
|
|
5622
|
+
if (Array.isArray(value)) {
|
|
5623
|
+
value.forEach((item) => {
|
|
5624
5624
|
if (item.type === "component") {
|
|
5625
5625
|
const [namespace, componentName] = item.component.split(":");
|
|
5626
5626
|
const resolvedPath = resolveComponentPath(namespace, componentName);
|
|
5627
5627
|
if (resolvedPath) {
|
|
5628
|
-
const kebabCaseName = componentName.replace(/
|
|
5628
|
+
const kebabCaseName = componentName.replace(/[-]/g, "");
|
|
5629
5629
|
imports.add(`import ${kebabCaseName} from '${resolvedPath}';`);
|
|
5630
5630
|
}
|
|
5631
5631
|
}
|
|
5632
5632
|
});
|
|
5633
|
-
} else if (typeof
|
|
5634
|
-
findComponentArgs(
|
|
5633
|
+
} else if (typeof value === "object" && value !== null) {
|
|
5634
|
+
findComponentArgs(value);
|
|
5635
5635
|
}
|
|
5636
5636
|
}
|
|
5637
5637
|
};
|
|
5638
5638
|
Object.entries(stories).forEach(([_, story]) => {
|
|
5639
|
-
findComponentArgs(story.
|
|
5639
|
+
findComponentArgs(story.slots || {});
|
|
5640
5640
|
});
|
|
5641
5641
|
return Array.from(imports).join("\n");
|
|
5642
5642
|
};
|
|
@@ -5645,7 +5645,7 @@ var vite_plugin_storybook_yaml_stories_default = async ({ jsonSchemaFakerOptions
|
|
|
5645
5645
|
name: "vite-plugin-storybook-yaml-stories",
|
|
5646
5646
|
async load(id) {
|
|
5647
5647
|
if (!id.endsWith("component.yml")) return;
|
|
5648
|
-
const content =
|
|
5648
|
+
const content = readCDC(id);
|
|
5649
5649
|
const imports = generateImports(dirname(id));
|
|
5650
5650
|
const storiesImports = dynamicImports(
|
|
5651
5651
|
content.thirdPartySettings?.sdcStorybook?.stories || {}
|
|
@@ -5664,7 +5664,7 @@ var vite_plugin_storybook_yaml_stories_default = async ({ jsonSchemaFakerOptions
|
|
|
5664
5664
|
},
|
|
5665
5665
|
...argsGenerator_default(content, jsonSchemaFakerOptions)
|
|
5666
5666
|
};
|
|
5667
|
-
const stories = content.thirdPartySettings?.sdcStorybook
|
|
5667
|
+
const stories = content.thirdPartySettings?.sdcStorybook?.stories ? storiesGenerator_default(content.thirdPartySettings.sdcStorybook.stories) : "";
|
|
5668
5668
|
return `
|
|
5669
5669
|
${imports}
|
|
5670
5670
|
${storiesImports}
|
|
@@ -5689,7 +5689,7 @@ ${stories}
|
|
|
5689
5689
|
var yamlStoriesIndexer = {
|
|
5690
5690
|
test: /component\.yml$/,
|
|
5691
5691
|
createIndex: async (fileName, { makeTitle }) => {
|
|
5692
|
-
const content =
|
|
5692
|
+
const content = readCDC(fileName);
|
|
5693
5693
|
const baseTitle = makeTitle(`SDC/${content.name}`);
|
|
5694
5694
|
const storiesIndex = [
|
|
5695
5695
|
{
|
|
@@ -5701,7 +5701,7 @@ var yamlStoriesIndexer = {
|
|
|
5701
5701
|
];
|
|
5702
5702
|
const stories = content.thirdPartySettings?.sdcStorybook?.stories;
|
|
5703
5703
|
if (stories) {
|
|
5704
|
-
Object.entries(stories).forEach(([storyKey,
|
|
5704
|
+
Object.entries(stories).forEach(([storyKey, _]) => {
|
|
5705
5705
|
storiesIndex.push({
|
|
5706
5706
|
type: "story",
|
|
5707
5707
|
importPath: fileName,
|