Skip to content

Commit 1415f76

Browse files
committed
chore(storybook): standardize down-state decorator
1 parent 6e299d4 commit 1415f76

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

.storybook/decorators/down-state.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
export const withDownStateDimensionCapture = (selector) => (Story, context) => {
2-
const captureDownStateDimensions = () => {
3-
const components = document.querySelectorAll(selector);
4-
components.forEach((component) => {
5-
const { width, height } = component.getBoundingClientRect();
6-
component.style.setProperty('--spectrum-downstate-width', `${width}px`);
7-
component.style.setProperty('--spectrum-downstate-height', `${height}px`);
8-
});
9-
};
1+
import { makeDecorator, useEffect } from "@storybook/preview-api";
2+
import { fetchContainers } from "./helpers.js";
103

11-
document.addEventListener("DOMContentLoaded", () => {
12-
// Wait to make sure the story is fully rendered (otherwise width/height can be wrong)
13-
setTimeout(() => {
14-
captureDownStateDimensions();
15-
}, 100);
16-
});
4+
export const withDownStateDimensionCapture = makeDecorator({
5+
name: "withDownStateDimensionCapture",
6+
parameterName: "downState",
7+
wrapper: (StoryFn, context) => {
8+
const { args = {}, parameters = {}, viewMode, id } = context;
9+
10+
/* Selectors are defined in the downState parameter */
11+
const {
12+
// Fall back to the rootClass if no selectors are provided
13+
selectors = args.rootClass ? [ args.rootClass ] : []
14+
} = parameters.downState ?? {};
1715

18-
return Story(context);
19-
};
16+
/**
17+
* This effect will run after the component is rendered and will capture the dimensions of the
18+
* components that are specified in the selectors array. It will then set the custom properties
19+
* --spectrum-downstate-width and --spectrum-downstate-height on the component to the width and
20+
* height of the component respectively. This will allow the downstate to be calculated correctly
21+
* in the CSS.
22+
*/
23+
useEffect(() => {
24+
if (selectors.length === 0) return;
25+
26+
for (const container of fetchContainers(id, viewMode === "docs")) {
27+
for (const selector of selectors) {
28+
const components = [...container.querySelectorAll(selector)];
29+
for (const component of components) {
30+
const { width, height } = component.getBoundingClientRect();
31+
component.style.setProperty("--spectrum-downstate-width", `${width}px`);
32+
component.style.setProperty("--spectrum-downstate-height", `${height}px`);
33+
}
34+
}
35+
}
36+
}, [selectors]);
37+
38+
return StoryFn(context);
39+
},
40+
});

components/button/stories/button.stories.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ export default {
8989
type: "figma",
9090
url: "https://www.figma.com/design/Mngz9H7WZLbrCvGQf3GnsY/S2-%2F-Desktop?node-id=707-2774",
9191
},
92+
html: {
93+
root: "#render-root"
94+
},
95+
downState: {
96+
selectors: [".spectrum-Button:not(:disabled)"],
97+
}
9298
packageJson,
9399
metadata,
94100
},
95101
decorators: [
96-
withDownStateDimensionCapture('.spectrum-Button:not(:disabled)')
102+
withDownStateDimensionCapture,
97103
],
98104
tags: ["!autodocs"],
99105
};

components/picker/stories/picker.stories.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ export default {
142142
},
143143
packageJson,
144144
metadata,
145+
downState: {
146+
selectors: [".spectrum-Picker:not(:disabled, .is-disabled, .is-loading)"],
147+
},
145148
},
146149
decorators: [
147-
withDownStateDimensionCapture(".spectrum-Picker:not(:disabled, .is-disabled, .is-loading)"),
150+
withDownStateDimensionCapture,
148151
],
149152
};
150153

0 commit comments

Comments
 (0)