storybook-addon-sdc 0.12.3 → 0.14.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.
Files changed (3) hide show
  1. package/README.md +47 -4
  2. package/dist/preset.js +56 -24
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -17,7 +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
+ - [Story Configuration via thirdPartySettings.sdcStorybook](#story-configuration-via-thirdpartysettingssdcstorybook)
21
21
  - [Why Choose SDC Storybook Over Alternatives?](#why-choose-sdc-storybook-over-alternatives)
22
22
  - [Dependencies](#dependencies)
23
23
  - [Known Issues](#known-issues)
@@ -255,6 +255,10 @@ sdcStorybookOptions: {
255
255
  }
256
256
  ```
257
257
 
258
+ ## Docs addon?
259
+
260
+ The storybook docs addon works out of the box.
261
+
258
262
  ## Support for Single Story Files (`*.story.yml`)
259
263
 
260
264
  In addition to stories defined inside `*.component.yml` files, the addon now supports standalone story files with the `.story.yml` extension.
@@ -554,17 +558,56 @@ Refer to:
554
558
  - [Drupal SDC Documentation](https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/annotated-example-componentyml)
555
559
  - [JSON Schema Documentation](https://json-schema.org/understanding-json-schema/reference/annotations)
556
560
 
557
- ## Disable Basic Story
561
+ ## Story Configuration via `thirdPartySettings.sdcStorybook`
562
+
563
+ The **sdc-addon** now supports configuring how stories are generated from your `component.yml` file.
564
+ You can use the new key `thirdPartySettings.sdcStorybook` to define Storybook-specific settings and control which stories are created.
558
565
 
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:
566
+ ### Example
561
567
 
562
568
  ```yaml
563
569
  thirdPartySettings:
564
570
  sdcStorybook:
571
+ tags:
572
+ - autodocs
565
573
  disableBasicStory: true
574
+ parameters:
575
+ layout: 'centered'
576
+ stories:
577
+ preview:
578
+ args:
579
+ variant: 'default'
580
+ parameters:
581
+ backgrounds:
582
+ default: 'light'
566
583
  ```
567
584
 
585
+ ### Option Reference
586
+
587
+ Below is a list of all available configuration options for `thirdPartySettings.sdcStorybook`:
588
+
589
+ | Key | Type | Description |
590
+ | ------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
591
+ | `tags` | `array` | Optional. Adds additional [Storybook tags](https://storybook.js.org/docs/writing-docs/autodocs) (e.g. `autodocs`). |
592
+ | `disableBasicStory` | `boolean` | When set to `true`, disables the automatically generated “Basic” story. |
593
+ | `parameters` | `object` | Global [Storybook parameters](https://storybook.js.org/docs/writing-stories/parameters) applied to all generated stories. |
594
+ | `stories` | `object` | Allows defining or overriding specific stories. Each key (e.g. `preview`) represents a story. Within each story, you can define `args`, `parameters`, and other Storybook options. |
595
+
596
+ ### Example: Custom Preview Story
597
+
598
+ ```yaml
599
+ thirdPartySettings:
600
+ sdcStorybook:
601
+ stories:
602
+ preview:
603
+ args:
604
+ variant: 'compact'
605
+ parameters:
606
+ layout: 'padded'
607
+ ```
608
+
609
+ This configuration creates an additional story named **Preview**, rendered using the provided `args` and `parameters`.
610
+
568
611
  ## Why Choose SDC Storybook Over Alternatives?
569
612
 
570
613
  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 dirname2, extname } from "path";
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");
@@ -143,8 +154,19 @@ storyNodeRenderer.register(defaultStoryNodes);
143
154
 
144
155
  // src/storiesGenerator.ts
145
156
  var storiesGenerator_default = (stories) => Object.entries(stories).map(
146
- ([storyKey, { props = {}, slots = {}, variants = {} }]) => `
157
+ ([
158
+ storyKey,
159
+ {
160
+ props = {},
161
+ slots = {},
162
+ variants = {},
163
+ description = "",
164
+ name = void 0
165
+ }
166
+ ]) => `
147
167
  export const ${capitalize(storyKey)} = {
168
+ parameters: {docs: {description: {story: ${JSON.stringify(description, null, 2)}}}},
169
+ name: ${JSON.stringify(name ?? capitalize(storyKey), null, 2)},
148
170
  args: {
149
171
  ...Basic.baseArgs,
150
172
  ${generateArgs2(props, false)}
@@ -164,15 +186,15 @@ var generateVariants = (variants) => {
164
186
  };
165
187
 
166
188
  // src/componentMetadata.ts
167
- import { dirname, relative } from "path";
189
+ import { dirname as dirname2, relative } from "path";
168
190
  import { cwd } from "process";
169
191
  var componentMetadata_default = (id, content) => {
170
192
  return {
171
- path: relative(cwd(), dirname(id)),
193
+ path: relative(cwd(), dirname2(id)),
172
194
  machineName: id,
173
195
  status: content.status || "stable",
174
196
  name: content.name,
175
- group: content.group || "All Components"
197
+ group: content.group
176
198
  };
177
199
  };
178
200
 
@@ -218,13 +240,13 @@ ${validationResult.errors.map((error) => error.stack).join("\n")}
218
240
 
219
241
  // src/namespaces.ts
220
242
  import { readdirSync, existsSync } from "fs";
221
- import { basename, join, resolve, sep, relative as relative2 } from "path";
243
+ import { basename, join, resolve, sep as sep2, relative as relative2 } from "path";
222
244
  import { cwd as cwd2 } from "process";
223
245
  import { normalizePath } from "vite";
224
246
  var getSubdirectories = (baseDir) => readdirSync(baseDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(baseDir, entry.name));
225
247
  var getProjectName = (p) => {
226
248
  const fullPath = resolve(p);
227
- const parts = fullPath.split(sep);
249
+ const parts = fullPath.split(sep2);
228
250
  const i = parts.lastIndexOf("components");
229
251
  if (i < 1) {
230
252
  throw new Error(`Could not find 'components' folder in path: ${fullPath}`);
@@ -318,15 +340,15 @@ var Namespaces = class {
318
340
  const componentsDir = join(rootPath, "components");
319
341
  if (existsSync(componentsDir)) {
320
342
  const isExact = fsPath === componentsDir;
321
- const isChild = fsPath.startsWith(componentsDir + sep);
343
+ const isChild = fsPath.startsWith(componentsDir + sep2);
322
344
  if (!isExact && !isChild) {
323
345
  throw new Error(
324
346
  `Could not find 'components' folder in path: ${fsPath}, namespace: ${ns} (${Object.values(this.namespaces).join(", ")})`
325
347
  );
326
348
  }
327
349
  let rel = relative2(componentsDir, fsPath);
328
- if (sep !== "/") {
329
- rel = rel.split(sep).join("/");
350
+ if (sep2 !== "/") {
351
+ rel = rel.split(sep2).join("/");
330
352
  }
331
353
  if (makeComponentIdFormat) {
332
354
  return `${ns}:${rel}`;
@@ -395,14 +417,15 @@ var dynamicImports = (stories, namespaces) => {
395
417
  );
396
418
  return Array.from(imports).join("\n");
397
419
  };
398
- var createStoryIndex = (fileName, baseTitle, stories, disableBasicStory) => {
420
+ var createStoryIndex = (fileName, baseTitle, stories, disableBasicStory, tags) => {
399
421
  const storiesIndex = [];
400
422
  if (disableBasicStory === false) {
401
423
  storiesIndex.push({
402
424
  type: "story",
403
425
  importPath: fileName,
404
426
  exportName: "Basic",
405
- title: baseTitle
427
+ title: baseTitle,
428
+ tags
406
429
  });
407
430
  }
408
431
  if (stories) {
@@ -411,7 +434,8 @@ var createStoryIndex = (fileName, baseTitle, stories, disableBasicStory) => {
411
434
  type: "story",
412
435
  importPath: fileName,
413
436
  exportName: storyKey,
414
- title: baseTitle
437
+ title: baseTitle,
438
+ tags
415
439
  });
416
440
  });
417
441
  }
@@ -431,7 +455,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
431
455
  if (!id.endsWith("component.yml")) return;
432
456
  try {
433
457
  const content = readSDC(id, globalDefs, sdcStorybookOptions.validate);
434
- const imports = generateImports(dirname2(id), namespaces);
458
+ const imports = generateImports(dirname3(id), namespaces);
435
459
  const previewsStories = {
436
460
  ...content.thirdPartySettings?.sdcStorybook?.stories || {},
437
461
  ...loadStoryFilesSync(id)
@@ -454,18 +478,16 @@ var vite_plugin_storybook_yaml_stories_default = ({
454
478
  defaultAttributes: [
455
479
  [
456
480
  "data-component-id",
457
- `${namespaces.pathToNamespace(dirname2(id), true)}`
481
+ `${namespaces.pathToNamespace(dirname3(id), true)}`
458
482
  ]
459
483
  ],
460
484
  componentMetadata: metadata,
461
485
  ...content.variants && {
462
486
  variant: Object.keys(content.variants)[0]
463
- }
464
- };
465
- const basicArgs = {
466
- ...args,
487
+ },
467
488
  ...generateStorybookArgs(content, jsonSchemaFakerOptions)
468
489
  };
490
+ const basicArgs = { ...args };
469
491
  const stories = previewsStories ? storiesGenerator_default(previewsStories) : "";
470
492
  return `
471
493
  ${imports}
@@ -479,13 +501,15 @@ class TwigSafeArray extends Array {
479
501
 
480
502
  export default {
481
503
  component: COMPONENT,
504
+ parameters: {...${JSON.stringify(content?.thirdPartySettings?.sdcStorybook?.parameters ?? {}, null, 2)}, ...{docs: {description: {component: ${JSON.stringify(content.description, null, 2)}}}}},
482
505
  argTypes: ${JSON.stringify(argTypes, null, 2)},
483
506
  args: ${JSON.stringify(args, null, 2)},
484
507
  };
485
508
 
486
509
  export const Basic = {
487
- baseArgs: ${JSON.stringify(args, null, 2)},
510
+
488
511
  args: ${JSON.stringify(basicArgs, null, 2)},
512
+ baseArgs: ${JSON.stringify(args, null, 2)},
489
513
  play: async ({ canvasElement }) => {
490
514
  Drupal.attachBehaviors(canvasElement, window.drupalSettings);
491
515
  },
@@ -503,16 +527,21 @@ var yamlStoriesIndexer = {
503
527
  createIndex: async (fileName, { makeTitle }) => {
504
528
  try {
505
529
  const content = readSDC(fileName);
506
- const baseTitle = makeTitle(`${getProjectName(fileName)}/${content.name}`);
530
+ const group = content.group || deriveGroupFromPath(fileName);
531
+ const baseTitle = makeTitle(
532
+ `${getProjectName(fileName)}/${capitalize(group)}/${content.name}`
533
+ );
507
534
  const stories = content.thirdPartySettings?.sdcStorybook?.stories;
508
- const disableBasicStory = content.thirdPartySettings?.sdcStorybook?.disableBasicStory ?? false;
509
535
  const storiesContent = loadStoryFilesSync(fileName);
510
536
  const mergedStories = { ...stories, ...storiesContent };
537
+ const tags = content?.thirdPartySettings?.sdcStorybook?.tags ?? [];
538
+ const disableBasicStory = content.thirdPartySettings?.sdcStorybook?.disableBasicStory ?? false;
511
539
  return createStoryIndex(
512
540
  fileName,
513
541
  baseTitle,
514
542
  mergedStories,
515
- disableBasicStory
543
+ disableBasicStory,
544
+ tags
516
545
  );
517
546
  } catch (error) {
518
547
  logger.error(`Error creating index for YAML file: ${fileName}, ${error}`);
@@ -521,7 +550,7 @@ var yamlStoriesIndexer = {
521
550
  }
522
551
  };
523
552
  var loadStoryFilesSync = (fileName) => {
524
- const folderPath = dirname2(fileName);
553
+ const folderPath = dirname3(fileName);
525
554
  const storyFiles = globSync(join2(folderPath, "*.story.yml"));
526
555
  return storyFiles.reduce(
527
556
  (acc, file) => {
@@ -645,6 +674,9 @@ async function viteFinal(config, options) {
645
674
  ...twigPlugin ? [twigPlugin] : [],
646
675
  vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces })
647
676
  ],
677
+ optimizeDeps: {
678
+ exclude: ["vite-plugin-twig-drupal", "vite-plugin-twing-drupal"]
679
+ },
648
680
  resolve: {
649
681
  alias: [...namespaces.toViteAlias()]
650
682
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-sdc",
3
- "version": "0.12.3",
3
+ "version": "0.14.1",
4
4
  "description": "Drupal Single Directory Components as stories",
5
5
  "keywords": [
6
6
  "SDC",
@@ -49,6 +49,7 @@
49
49
  },
50
50
  "devDependencies": {
51
51
  "@storybook/addon-links": "^9.1.2",
52
+ "@storybook/addon-docs": "^9.1.2",
52
53
  "@storybook/html-vite": "^9.1.2",
53
54
  "@types/json-schema": "^7.0.15",
54
55
  "@types/node": "^24.2.1",