Skip to content

Commit 30b422b

Browse files
committed
docs(storybook): Grid support for stories
1 parent ab70886 commit 30b422b

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

.storybook/addons/withGrid.jsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React, { useEffect, useRef } from "react";
2+
3+
const GRID = [
4+
{
5+
value: false,
6+
title: "Grid: Off",
7+
},
8+
{
9+
value: true,
10+
title: "Grid: On",
11+
},
12+
];
13+
14+
export const gridType = {
15+
grid: {
16+
name: "Grid",
17+
description: "Global grid for components",
18+
defaultValue: GRID[0].value,
19+
toolbar: {
20+
title: "Grid",
21+
items: GRID,
22+
dynamicTitle: true,
23+
},
24+
},
25+
};
26+
27+
const removeGridFromDocs = () => {
28+
document.querySelectorAll(".docs-story").forEach((docStory) => {
29+
docStory.classList.remove("u-baseline-grid");
30+
});
31+
};
32+
33+
const removeGridFromStory = () => {
34+
document.body.classList.remove("u-baseline-grid");
35+
};
36+
37+
const clearGrid = () => {
38+
removeGridFromStory();
39+
removeGridFromDocs();
40+
};
41+
42+
const addGridToDocs = () => {
43+
removeGridFromStory();
44+
document.querySelectorAll(".docs-story").forEach((docStory) => {
45+
docStory.classList.add("u-baseline-grid");
46+
});
47+
};
48+
49+
const addGridToStory = () => {
50+
removeGridFromDocs();
51+
document.body.classList.add("u-baseline-grid");
52+
};
53+
54+
export const WithGridProvider = (Story, context) => {
55+
const {
56+
viewMode,
57+
globals: { grid },
58+
} = context;
59+
const isDocs = viewMode === "docs";
60+
const gridRef = useRef(false);
61+
62+
useEffect(() => {
63+
if (gridRef.current !== grid) {
64+
if (grid) {
65+
if (isDocs) {
66+
addGridToDocs();
67+
} else {
68+
addGridToStory();
69+
}
70+
} else {
71+
clearGrid();
72+
}
73+
gridRef.current = grid;
74+
}
75+
}, [grid, isDocs]);
76+
77+
return <Story {...context} />;
78+
};

.storybook/preview.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { themes } from "@storybook/theming";
22
import "vanilla-framework/scss/build.scss";
33
import { themeType, WithThemeProvider } from "./addons/withTheme";
4+
import { gridType, WithGridProvider } from "./addons/withGrid";
45

56
export const parameters = {
67
docs: {
78
theme: themes.vanillaish,
89
},
910
backgrounds: {
11+
grid: {
12+
disable: true,
13+
},
1014
disable: true,
1115
},
1216
};
1317

14-
export const decorators = [WithThemeProvider];
18+
export const decorators = [WithThemeProvider, WithGridProvider];
1519

16-
export const globalTypes = themeType;
20+
export const globalTypes = { ...themeType, ...gridType };

0 commit comments

Comments
 (0)