diff --git a/.storybook/drupal-attribute.js b/.storybook/drupal-attribute.js new file mode 100644 index 000000000..d1207dd10 --- /dev/null +++ b/.storybook/drupal-attribute.js @@ -0,0 +1,56 @@ +const { DrupalAttribute } = require("drupal-attribute"); + +/** + * Storybook-specific DrupalAttribute variant. + * + * Aligns the JS implementation with Drupal's PHP Attribute behavior by keeping + * empty-string attribute values (e.g. `hidden=""`) instead of dropping them. + */ +class StorybookDrupalAttribute extends DrupalAttribute { + toString() { + const components = []; + + for (const key of this.keys()) { + let value = this.get(key); + + if (value === null || value === undefined || value === false) { + continue; + } + + if (value === true) { + components.push(key); + continue; + } + + if (Array.isArray(value)) { + const filtered = value + .filter((v) => v !== null && v !== undefined && v !== "") + .map(String); + + if (filtered.length === 0) { + continue; + } + + value = filtered.join(" "); + } + + if (value === key) { + components.push(key); + continue; + } + + if (value === "") { + components.push(`${key}=""`); + continue; + } + + components.push(`${key}="${String(value)}"`); + } + + return components.length ? ` ${components.join(" ")}` : ""; + } +} + +module.exports = { + StorybookDrupalAttribute, +}; diff --git a/.storybook/environment.js b/.storybook/environment.js index 251c59710..af5e304f0 100644 --- a/.storybook/environment.js +++ b/.storybook/environment.js @@ -6,7 +6,7 @@ const { createFilesystemLoader, createFunction, } = require("twing"); -const { DrupalAttribute } = require("drupal-attribute"); +const { StorybookDrupalAttribute } = require("./drupal-attribute"); const projComponentsAbsPath = path.resolve(__dirname, "../src/components"); const projCompositionsAbsPath = path.resolve(__dirname, "../src/compositions"); @@ -23,7 +23,7 @@ try { const createAttribute = createFunction( "create_attribute", - () => Promise.resolve(new DrupalAttribute()), + () => Promise.resolve(new StorybookDrupalAttribute()), [], ); diff --git a/src/components/bcl-card-layout/__snapshots__/card-layout.test.js.snap b/src/components/bcl-card-layout/__snapshots__/card-layout.test.js.snap index a8f94913b..d184b1936 100644 --- a/src/components/bcl-card-layout/__snapshots__/card-layout.test.js.snap +++ b/src/components/bcl-card-layout/__snapshots__/card-layout.test.js.snap @@ -585,7 +585,7 @@ exports[`OE - Card grid renders correctly with cards having equal height 1`] = ` class="col" >