storybook-addon-sdc 0.10.1 → 0.12.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.
Files changed (3) hide show
  1. package/README.md +57 -30
  2. package/dist/preset.js +53 -29
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -11,9 +11,9 @@ This addon streamlines the integration of Drupal Single Directory Components (SD
11
11
  - [Features of the Addon](#features-of-the-addon)
12
12
  - [Quickstart Guide](#quickstart-guide)
13
13
  - [Configuration](#configuration)
14
- - [Namespaces](#namespaces)
15
14
  - [Creating Experimental Stories](#creating-experimental-stories)
16
15
  - [Support for Single Story Files (\*.story.yml)](#support-for-single-story-files-storyyml)
16
+ - [Namespaces](#namespaces)
17
17
  - [Regular Storybook](#regular-storybook)
18
18
  - [Configuration Options](#configuration-options)
19
19
  - [Setting Default Values](#setting-default-values)
@@ -94,7 +94,7 @@ The SDC Storybook Addon simplifies the integration of Drupal Single Directory Co
94
94
  ## Configuration
95
95
 
96
96
  To configure the addon, update `.storybook/main.js` as shown below:
97
- You can use this plugin either with [Twig.js](https://github.com/twigjs/twig.js) or [Twing.js](https://twing.nightlycommit.com/) by setting the `twigLib` option to `'twig'` or `'twing'` in your configuration. Only one of vitePluginTwigDrupalOptions or vitePluginTwingDrupalOptions should be active, depending on your twigLib setting.
97
+ You can use this plugin either with [Twig.js](https://github.com/twigjs/twig.js) or [Twing.js](https://twing.nightlycommit.com/) by setting the `twigLib` option to `'twig'` or `'twing'` in your configuration.
98
98
 
99
99
  ```js
100
100
  import { join } from 'node:path' // 1. Add dependencies.
@@ -122,34 +122,6 @@ const config = {
122
122
  export default config
123
123
  ```
124
124
 
125
- ## Namespaces
126
-
127
- - **Default Namespace**: The addon works out of the box without requiring namespace configuration and will use the current directory name as the default namespace, mirroring the behavior of a Drupal theme or module.
128
- For example, if your `package.json` is in the `my-theme` directory, the default namespace will be `my-theme`, and this namespace will be linked to the `./components` directory.
129
-
130
- - **Custom Namespace**: If you want to use a custom namespace or explicitly set the namespace, you can specify it in the plugin configuration:
131
-
132
- ```js
133
- sdcStorybookOptions: {
134
- ...
135
- namespace: 'umami',
136
- },
137
- ```
138
-
139
- - **Multiple Namespaces**: The most useful feature is the ability to use multiple namespaces.
140
- This allows you to use components from other directories (parent or sibling).
141
- These directories must also contain a `components` directory with SDC components.
142
-
143
- ```js
144
- import { resolve } from 'node:path'
145
- sdcStorybookOptions: {
146
- namespaces: {
147
- 'parent-namespace': resolve('../parent-namespace'),
148
- 'grandparent-namespace': resolve('../../grandparent-namespace'),
149
- },
150
- },
151
- ```
152
-
153
125
  ## Creating Experimental Stories
154
126
 
155
127
  The `sdcStorybook` configuration in the SDC YAML file provides a flexible way to define custom stories for your components. This feature allows you to use predefined props, custom slots, or even reuse stories defined elsewhere. Here's how it works:
@@ -319,6 +291,61 @@ slots:
319
291
 
320
292
  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.
321
293
 
294
+ ## Namespaces
295
+
296
+ Namespace logic mirrors the behavior of Drupal themes and modules. This means your namespaces can be used:
297
+
298
+ In Twig as `namespace:component`:
299
+
300
+ ```twig
301
+ {% include 'umami:title' %}
302
+ ```
303
+
304
+ And in story definitions within YAML files:
305
+
306
+ ```yaml
307
+ - type: component
308
+ component: 'umami:title'
309
+ ```
310
+
311
+ ### Namespace definitions:
312
+
313
+ - **Default Namespace**: The addon works out of the box without requiring namespace configuration and will use the current directory name as the default namespace, mirroring the behavior of a Drupal theme or module.
314
+ For example, if your `package.json` is in the `my-theme` directory, the default namespace will be `my-theme`, and this namespace will be linked to the `./components` directory.
315
+
316
+ - **Custom Namespace**: If you want to use a custom namespace or explicitly set the namespace, you can specify it in the plugin configuration:
317
+
318
+ ```js
319
+ sdcStorybookOptions: {
320
+ ...
321
+ namespace: 'umami',
322
+ },
323
+ ```
324
+
325
+ - **Multiple Namespaces**: The most powerful feature is the ability to use multiple namespaces.
326
+ This allows you to use components from other directories (parent or sibling).
327
+ These directories must also contain a `/components` directory with SDC components.
328
+
329
+ ```js
330
+ import { resolve } from 'node:path'
331
+ sdcStorybookOptions: {
332
+ namespaces: {
333
+ 'parent-namespace': resolve('../parent-namespace'),
334
+ 'grandparent-namespace': resolve('../../grandparent-namespace'),
335
+ },
336
+ },
337
+ ```
338
+
339
+ Due to the specifics of the indexer implementation, you also need to duplicate the component paths in the `stories` array:
340
+
341
+ ```js
342
+ stories: [
343
+ '../components/**/*.component.yml',
344
+ '../../parent-namespace/components/**/*.component.yml',
345
+ '../../../grandparent-namespace/components/**/*.component.yml',
346
+ ],
347
+ ```
348
+
322
349
  ## Regular storybook
323
350
 
324
351
  All Storybook functions work as usual, and you can import SDC YAML into `.stories.js` files.
package/dist/preset.js CHANGED
@@ -101,7 +101,7 @@ var renderComponent = (item) => {
101
101
  const kebabCaseName = convertToKebabCase(item.component);
102
102
  const componentProps = `...{ ${generateArgs2(item.props ?? {}, false)}}, ...{${generateArgs2(item.slots ?? {}, true)}}`;
103
103
  const storyArgs = item.story ? `...${kebabCaseName}.${capitalize(item.story)}.args` : "...{}";
104
- return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.args, ${storyArgs}, ${componentProps}})`;
104
+ return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.baseArgs, ${storyArgs}, ${componentProps}})`;
105
105
  };
106
106
  var renderImage = (item) => {
107
107
  return JSON.stringify(
@@ -146,7 +146,7 @@ var storiesGenerator_default = (stories) => Object.entries(stories).map(
146
146
  ([storyKey, { props = {}, slots = {}, variants = {} }]) => `
147
147
  export const ${capitalize(storyKey)} = {
148
148
  args: {
149
- ...Basic.args,
149
+ ...Basic.baseArgs,
150
150
  ${generateArgs2(props, false)}
151
151
  ${generateArgs2(slots, true)}
152
152
  ${generateVariants(variants)}
@@ -253,11 +253,12 @@ var Namespaces = class {
253
253
  }
254
254
  toViteAlias() {
255
255
  const aliases = [];
256
- Object.keys(this.namespaces).forEach((namespace) => {
256
+ Object.entries(this.namespaces).forEach(([namespace, path]) => {
257
+ const hasComponents = existsSync(join(path, "components"));
257
258
  aliases.push({
258
259
  find: "@" + namespace,
259
260
  replacement: normalizePath(
260
- resolve(this.namespaces[namespace], "components")
261
+ hasComponents ? join(path, "components") : path
261
262
  )
262
263
  });
263
264
  });
@@ -267,14 +268,24 @@ var Namespaces = class {
267
268
  toTwigJsNamespaces() {
268
269
  let namespaces = {};
269
270
  for (const [ns, path] of Object.entries(this.namespaces)) {
270
- namespaces[ns] = join(path, "components");
271
+ const componentsPath = join(path, "components");
272
+ if (existsSync(componentsPath)) {
273
+ namespaces[ns] = componentsPath;
274
+ } else {
275
+ namespaces[ns] = path;
276
+ }
271
277
  }
272
278
  return namespaces;
273
279
  }
274
280
  toTwingNamespaces() {
275
281
  let namespaces = {};
276
282
  for (const [ns, path] of Object.entries(this.namespaces)) {
277
- namespaces[ns] = [join(path, "components")];
283
+ const componentsPath = join(path, "components");
284
+ if (existsSync(componentsPath)) {
285
+ namespaces[ns] = [componentsPath];
286
+ } else {
287
+ namespaces[ns] = [path];
288
+ }
278
289
  }
279
290
  return namespaces;
280
291
  }
@@ -305,21 +316,24 @@ var Namespaces = class {
305
316
  }
306
317
  const rootPath = resolve(this.namespaces[ns]);
307
318
  const componentsDir = join(rootPath, "components");
308
- const isExact = fsPath === componentsDir;
309
- const isChild = fsPath.startsWith(componentsDir + sep);
310
- if (!isExact && !isChild) {
311
- throw new Error(
312
- `Could not find 'components' folder in path: ${fsPath}, namespace: ${ns} (${Object.values(this.namespaces).join(", ")})`
313
- );
314
- }
315
- let rel = relative2(componentsDir, fsPath);
316
- if (sep !== "/") {
317
- rel = rel.split(sep).join("/");
318
- }
319
- if (makeComponentIdFormat) {
320
- return `${ns}:${rel}`;
319
+ if (existsSync(componentsDir)) {
320
+ const isExact = fsPath === componentsDir;
321
+ const isChild = fsPath.startsWith(componentsDir + sep);
322
+ if (!isExact && !isChild) {
323
+ throw new Error(
324
+ `Could not find 'components' folder in path: ${fsPath}, namespace: ${ns} (${Object.values(this.namespaces).join(", ")})`
325
+ );
326
+ }
327
+ let rel = relative2(componentsDir, fsPath);
328
+ if (sep !== "/") {
329
+ rel = rel.split(sep).join("/");
330
+ }
331
+ if (makeComponentIdFormat) {
332
+ return `${ns}:${rel}`;
333
+ }
334
+ return `@${ns}/${rel}`;
321
335
  }
322
- return `@${ns}/${rel}`;
336
+ return `@${ns}`;
323
337
  }
324
338
  };
325
339
  var toNamespaces = (namespaceDefinition) => {
@@ -381,15 +395,16 @@ var dynamicImports = (stories, namespaces) => {
381
395
  );
382
396
  return Array.from(imports).join("\n");
383
397
  };
384
- var createStoryIndex = (fileName, baseTitle, stories) => {
385
- const storiesIndex = [
386
- {
398
+ var createStoryIndex = (fileName, baseTitle, stories, disableBasicStory) => {
399
+ const storiesIndex = [];
400
+ if (disableBasicStory === false) {
401
+ storiesIndex.push({
387
402
  type: "story",
388
403
  importPath: fileName,
389
404
  exportName: "Basic",
390
405
  title: baseTitle
391
- }
392
- ];
406
+ });
407
+ }
393
408
  if (stories) {
394
409
  Object.keys(stories).forEach((storyKey) => {
395
410
  storiesIndex.push({
@@ -445,10 +460,12 @@ var vite_plugin_storybook_yaml_stories_default = ({
445
460
  componentMetadata: metadata,
446
461
  ...content.variants && {
447
462
  variant: Object.keys(content.variants)[0]
448
- },
463
+ }
464
+ };
465
+ const basicArgs = {
466
+ ...args,
449
467
  ...generateStorybookArgs(content, jsonSchemaFakerOptions)
450
468
  };
451
- const basicArgs = { ...args };
452
469
  const stories = previewsStories ? storiesGenerator_default(previewsStories) : "";
453
470
  return `
454
471
  ${imports}
@@ -467,6 +484,7 @@ export default {
467
484
  };
468
485
 
469
486
  export const Basic = {
487
+ baseArgs: ${JSON.stringify(args, null, 2)},
470
488
  args: ${JSON.stringify(basicArgs, null, 2)},
471
489
  play: async ({ canvasElement }) => {
472
490
  Drupal.attachBehaviors(canvasElement, window.drupalSettings);
@@ -487,9 +505,15 @@ var yamlStoriesIndexer = {
487
505
  const content = readSDC(fileName);
488
506
  const baseTitle = makeTitle(`${getProjectName(fileName)}/${content.name}`);
489
507
  const stories = content.thirdPartySettings?.sdcStorybook?.stories;
508
+ const disableBasicStory = content.thirdPartySettings?.sdcStorybook?.disableBasicStory ?? false;
490
509
  const storiesContent = loadStoryFilesSync(fileName);
491
510
  const mergedStories = { ...stories, ...storiesContent };
492
- return createStoryIndex(fileName, baseTitle, mergedStories);
511
+ return createStoryIndex(
512
+ fileName,
513
+ baseTitle,
514
+ mergedStories,
515
+ disableBasicStory
516
+ );
493
517
  } catch (error) {
494
518
  logger.error(`Error creating index for YAML file: ${fileName}, ${error}`);
495
519
  throw error;
@@ -571,7 +595,7 @@ var DEFAULT_ADDON_OPTIONS = {
571
595
  // 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
572
596
  },
573
597
  vitePluginTwingDrupalOptions: {
574
- include: "/.twig(?.*)?$/"
598
+ include: /\.twig(\?.*)?$/
575
599
  },
576
600
  vitePluginTwigDrupalOptions: {},
577
601
  jsonSchemaFakerOptions: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-sdc",
3
- "version": "0.10.1",
3
+ "version": "0.12.2",
4
4
  "description": "Drupal Single Directory Components as stories",
5
5
  "keywords": [
6
6
  "SDC",