storybook-addon-sdc 0.12.2 → 0.13.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 +29 -0
- package/dist/preset.js +27 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ This addon streamlines the integration of Drupal Single Directory Components (SD
|
|
|
17
17
|
- [Regular Storybook](#regular-storybook)
|
|
18
18
|
- [Configuration Options](#configuration-options)
|
|
19
19
|
- [Setting Default Values](#setting-default-values)
|
|
20
|
+
- [Disable Basic Story](#disable-basic-story)
|
|
20
21
|
- [Why Choose SDC Storybook Over Alternatives?](#why-choose-sdc-storybook-over-alternatives)
|
|
21
22
|
- [Dependencies](#dependencies)
|
|
22
23
|
- [Known Issues](#known-issues)
|
|
@@ -346,6 +347,23 @@ And in story definitions within YAML files:
|
|
|
346
347
|
],
|
|
347
348
|
```
|
|
348
349
|
|
|
350
|
+
- **Non Component Namespaces**: If you want to use components from a directory that does not follow the SDC structure (i.e., does not have a `components` directory), you can define a namespace that points directly to that directory. For example, if you have an `assets` directory with SVG icons:
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
sdcStorybookOptions: {
|
|
354
|
+
namespaces: {
|
|
355
|
+
'assets': join(cwd(), './assets'),
|
|
356
|
+
},
|
|
357
|
+
},
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
In this case, the addon will look for components directly in the specified directory.
|
|
361
|
+
And you can use them in twig and stories as usual:
|
|
362
|
+
|
|
363
|
+
```twig
|
|
364
|
+
{% include '@assets/icons/example.svg' %}
|
|
365
|
+
```
|
|
366
|
+
|
|
349
367
|
## Regular storybook
|
|
350
368
|
|
|
351
369
|
All Storybook functions work as usual, and you can import SDC YAML into `.stories.js` files.
|
|
@@ -536,6 +554,17 @@ Refer to:
|
|
|
536
554
|
- [Drupal SDC Documentation](https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/annotated-example-componentyml)
|
|
537
555
|
- [JSON Schema Documentation](https://json-schema.org/understanding-json-schema/reference/annotations)
|
|
538
556
|
|
|
557
|
+
## Disable Basic Story
|
|
558
|
+
|
|
559
|
+
By default the addon generates a `Basic` story for each component using default props and slots. This is useful for quickly previewing components, but if you prefer to create your own stories exclusively, you can disable this feature.
|
|
560
|
+
To disable the automatic generation of the `Basic` story, set `disableBasicStory: true` in your component YAML:
|
|
561
|
+
|
|
562
|
+
```yaml
|
|
563
|
+
thirdPartySettings:
|
|
564
|
+
sdcStorybook:
|
|
565
|
+
disableBasicStory: true
|
|
566
|
+
```
|
|
567
|
+
|
|
539
568
|
## Why Choose SDC Storybook Over Alternatives?
|
|
540
569
|
|
|
541
570
|
While solutions like [SDC Styleguide](https://www.drupal.org/project/sdc_styleguide) and [Drupal Storybook](https://www.drupal.org/project/storybook) are valuable, the SDC Storybook addon offers distinct advantages:
|
package/dist/preset.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
2
2
|
import { readdirSync as readdirSync2, readFileSync } from "fs";
|
|
3
3
|
import { parse as parseYaml } from "yaml";
|
|
4
|
-
import { join as join2, basename as basename2, dirname as
|
|
4
|
+
import { join as join2, basename as basename2, dirname as dirname3, extname } from "path";
|
|
5
5
|
import { globSync } from "glob";
|
|
6
6
|
|
|
7
7
|
// src/logger.ts
|
|
@@ -63,6 +63,7 @@ var argTypesGenerator_default = (content) => {
|
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
// src/utils.ts
|
|
66
|
+
import { dirname, sep } from "path";
|
|
66
67
|
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
67
68
|
var convertToKebabCase = (str) => str.replace(/[-:]/g, "");
|
|
68
69
|
var toAttributes = (attrs) => {
|
|
@@ -74,6 +75,16 @@ var toAttributes = (attrs) => {
|
|
|
74
75
|
return `${key}="${value}"`;
|
|
75
76
|
}).join(" ");
|
|
76
77
|
};
|
|
78
|
+
var deriveGroupFromPath = (fileName) => {
|
|
79
|
+
const dir = dirname(fileName);
|
|
80
|
+
const parts = dir.split(sep).filter(Boolean);
|
|
81
|
+
const lower = parts.map((p) => p.toLowerCase());
|
|
82
|
+
const index = lower.lastIndexOf("components");
|
|
83
|
+
if (index !== -1 && parts[index + 2]) {
|
|
84
|
+
return parts[index + 1];
|
|
85
|
+
}
|
|
86
|
+
return "SDC";
|
|
87
|
+
};
|
|
77
88
|
|
|
78
89
|
// src/storyNodeRender.ts
|
|
79
90
|
var generateArgs2 = (args, isSlot = false) => Object.entries(args).map(([key, value]) => `${key}: ${formatArgValue(value, isSlot)},`).join("\n");
|
|
@@ -164,15 +175,15 @@ var generateVariants = (variants) => {
|
|
|
164
175
|
};
|
|
165
176
|
|
|
166
177
|
// src/componentMetadata.ts
|
|
167
|
-
import { dirname, relative } from "path";
|
|
178
|
+
import { dirname as dirname2, relative } from "path";
|
|
168
179
|
import { cwd } from "process";
|
|
169
180
|
var componentMetadata_default = (id, content) => {
|
|
170
181
|
return {
|
|
171
|
-
path: relative(cwd(),
|
|
182
|
+
path: relative(cwd(), dirname2(id)),
|
|
172
183
|
machineName: id,
|
|
173
184
|
status: content.status || "stable",
|
|
174
185
|
name: content.name,
|
|
175
|
-
group: content.group
|
|
186
|
+
group: content.group
|
|
176
187
|
};
|
|
177
188
|
};
|
|
178
189
|
|
|
@@ -218,13 +229,13 @@ ${validationResult.errors.map((error) => error.stack).join("\n")}
|
|
|
218
229
|
|
|
219
230
|
// src/namespaces.ts
|
|
220
231
|
import { readdirSync, existsSync } from "fs";
|
|
221
|
-
import { basename, join, resolve, sep, relative as relative2 } from "path";
|
|
232
|
+
import { basename, join, resolve, sep as sep2, relative as relative2 } from "path";
|
|
222
233
|
import { cwd as cwd2 } from "process";
|
|
223
234
|
import { normalizePath } from "vite";
|
|
224
235
|
var getSubdirectories = (baseDir) => readdirSync(baseDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(baseDir, entry.name));
|
|
225
236
|
var getProjectName = (p) => {
|
|
226
237
|
const fullPath = resolve(p);
|
|
227
|
-
const parts = fullPath.split(
|
|
238
|
+
const parts = fullPath.split(sep2);
|
|
228
239
|
const i = parts.lastIndexOf("components");
|
|
229
240
|
if (i < 1) {
|
|
230
241
|
throw new Error(`Could not find 'components' folder in path: ${fullPath}`);
|
|
@@ -318,15 +329,15 @@ var Namespaces = class {
|
|
|
318
329
|
const componentsDir = join(rootPath, "components");
|
|
319
330
|
if (existsSync(componentsDir)) {
|
|
320
331
|
const isExact = fsPath === componentsDir;
|
|
321
|
-
const isChild = fsPath.startsWith(componentsDir +
|
|
332
|
+
const isChild = fsPath.startsWith(componentsDir + sep2);
|
|
322
333
|
if (!isExact && !isChild) {
|
|
323
334
|
throw new Error(
|
|
324
335
|
`Could not find 'components' folder in path: ${fsPath}, namespace: ${ns} (${Object.values(this.namespaces).join(", ")})`
|
|
325
336
|
);
|
|
326
337
|
}
|
|
327
338
|
let rel = relative2(componentsDir, fsPath);
|
|
328
|
-
if (
|
|
329
|
-
rel = rel.split(
|
|
339
|
+
if (sep2 !== "/") {
|
|
340
|
+
rel = rel.split(sep2).join("/");
|
|
330
341
|
}
|
|
331
342
|
if (makeComponentIdFormat) {
|
|
332
343
|
return `${ns}:${rel}`;
|
|
@@ -431,7 +442,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
431
442
|
if (!id.endsWith("component.yml")) return;
|
|
432
443
|
try {
|
|
433
444
|
const content = readSDC(id, globalDefs, sdcStorybookOptions.validate);
|
|
434
|
-
const imports = generateImports(
|
|
445
|
+
const imports = generateImports(dirname3(id), namespaces);
|
|
435
446
|
const previewsStories = {
|
|
436
447
|
...content.thirdPartySettings?.sdcStorybook?.stories || {},
|
|
437
448
|
...loadStoryFilesSync(id)
|
|
@@ -454,7 +465,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
454
465
|
defaultAttributes: [
|
|
455
466
|
[
|
|
456
467
|
"data-component-id",
|
|
457
|
-
`${namespaces.pathToNamespace(
|
|
468
|
+
`${namespaces.pathToNamespace(dirname3(id), true)}`
|
|
458
469
|
]
|
|
459
470
|
],
|
|
460
471
|
componentMetadata: metadata,
|
|
@@ -503,7 +514,10 @@ var yamlStoriesIndexer = {
|
|
|
503
514
|
createIndex: async (fileName, { makeTitle }) => {
|
|
504
515
|
try {
|
|
505
516
|
const content = readSDC(fileName);
|
|
506
|
-
const
|
|
517
|
+
const group = content.group || deriveGroupFromPath(fileName);
|
|
518
|
+
const baseTitle = makeTitle(
|
|
519
|
+
`${getProjectName(fileName)}/${capitalize(group)}/${content.name}`
|
|
520
|
+
);
|
|
507
521
|
const stories = content.thirdPartySettings?.sdcStorybook?.stories;
|
|
508
522
|
const disableBasicStory = content.thirdPartySettings?.sdcStorybook?.disableBasicStory ?? false;
|
|
509
523
|
const storiesContent = loadStoryFilesSync(fileName);
|
|
@@ -521,7 +535,7 @@ var yamlStoriesIndexer = {
|
|
|
521
535
|
}
|
|
522
536
|
};
|
|
523
537
|
var loadStoryFilesSync = (fileName) => {
|
|
524
|
-
const folderPath =
|
|
538
|
+
const folderPath = dirname3(fileName);
|
|
525
539
|
const storyFiles = globSync(join2(folderPath, "*.story.yml"));
|
|
526
540
|
return storyFiles.reduce(
|
|
527
541
|
(acc, file) => {
|