Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(storybook): Grid support for stories #1155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .storybook/addons/withGrid.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useEffect, useRef } from "react";

const GRID = [
{
value: false,
title: "Grid: Off",
},
{
value: true,
title: "Grid: On",
},
];

export const gridType = {
grid: {
name: "Grid",
description: "Global grid for components",
defaultValue: GRID[0].value,
toolbar: {
title: "Grid",
items: GRID,
dynamicTitle: true,
},
},
};

const removeGridFromDocs = () => {
document.querySelectorAll(".docs-story").forEach((docStory) => {
docStory.classList.remove("u-baseline-grid");
});
};

const removeGridFromStory = () => {
document.body.classList.remove("u-baseline-grid");
};

const clearGrid = () => {
removeGridFromStory();
removeGridFromDocs();
};

const addGridToDocs = () => {
removeGridFromStory();
document.querySelectorAll(".docs-story").forEach((docStory) => {
docStory.classList.add("u-baseline-grid");
});
};

const addGridToStory = () => {
removeGridFromDocs();
document.body.classList.add("u-baseline-grid");
};

export const WithGridProvider = (Story, context) => {
const {
viewMode,
globals: { grid },
} = context;
const isDocs = viewMode === "docs";
const gridRef = useRef(false);

useEffect(() => {
if (gridRef.current !== grid) {
if (grid) {
if (isDocs) {
addGridToDocs();
} else {
addGridToStory();
}
} else {
clearGrid();
}
gridRef.current = grid;
}
}, [grid, isDocs]);

return <Story {...context} />;
};
8 changes: 6 additions & 2 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { themes } from "@storybook/theming";
import "vanilla-framework/scss/build.scss";
import { themeType, WithThemeProvider } from "./addons/withTheme";
import { gridType, WithGridProvider } from "./addons/withGrid";

export const parameters = {
docs: {
theme: themes.vanillaish,
},
backgrounds: {
grid: {
disable: true,
},
disable: true,
},
};

export const decorators = [WithThemeProvider];
export const decorators = [WithThemeProvider, WithGridProvider];

export const globalTypes = themeType;
export const globalTypes = { ...themeType, ...gridType };
Loading