Skip to content

Commit d52c701

Browse files
authored
feat(bundle): bundled css for dev/demos (#3477)
Resurrecting the CSS bundle for development use. Excellent resource for generating quick demo pages by loading all the CSS components in the library in a pre-bundled package with tokens pre-loaded and minified to remove unused tokens.
1 parent c7ffbd8 commit d52c701

24 files changed

+801
-121
lines changed

.changeset/honest-bulldogs-flow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@spectrum-css/preview": minor
3+
---
4+
5+
Update Storybook to leverage the new CSS bundled assets.

.changeset/stupid-rice-compare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@spectrum-css/bundle": major
3+
---
4+
5+
Resurrecting the CSS bundle for development use. Excellent resource for generating quick demo pages by loading all the CSS components in the library in a pre-bundled package with tokens pre-loaded and minified to remove unused tokens.

.storybook/blocks/ComponentDetails.jsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { useOf } from '@storybook/blocks';
22
import { ResetWrapper } from "@storybook/components";
33
import { styled } from "@storybook/theming";
44
import React, { useEffect, useState } from "react";
5+
6+
import { Body, Code, Heading } from "./Typography.jsx";
7+
import { fetchToken } from "./utilities.js";
8+
59
import AdobeSVG from "../assets/images/adobe_logo.svg?raw";
610
import GitHubSVG from "../assets/images/github_logo.svg?raw";
711
import NpmSVG from "../assets/images/npm_logo.svg?raw";
812
import WCSVG from "../assets/images/wc_logo.svg?raw";
9-
import { Body, Code, Heading } from "./Typography.jsx";
10-
import { fetchToken } from "./utilities.js";
1113

1214
export const DList = styled.dl`
1315
display: grid;

.storybook/blocks/PropertiesTable.jsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import "@spectrum-css/table/dist/index.css";
2-
31
import { DocsContext, useOf } from "@storybook/blocks";
42
import { NAVIGATE_URL } from "@storybook/core-events";
53
import { styled } from "@storybook/theming";
64
import React, { useContext } from 'react';
5+
76
import { ThemeContainer } from "./ThemeContainer.jsx";
87
import { Body, Code, LinkableHeading } from "./Typography.jsx";
98

.storybook/blocks/utilities.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ import { useTheme } from "@storybook/theming";
1414
* @returns {string|void} - The value of the token
1515
*/
1616
function parseData(data, { key, color, platform }) {
17-
// If nothing exists for that key, report that the key is missing
18-
if (!data) {
19-
console.log(`⚠️ Token ${key} can't be found in the spectrum token data`);
20-
return;
21-
}
17+
// If nothing exists for that key, report that the key is missing
18+
if (!data) {
19+
console.log(`⚠️ Token ${key} can't be found in the spectrum token data`);
20+
return;
21+
}
2222

23-
// Check if the key has a value
24-
if (data.value) return data.value;
23+
// Check if the key has a value
24+
if (data.value) return data.value;
2525

26-
if (Object.keys(data.sets).length === 0) {
27-
console.log(`⚠️ Token ${key} has no value or sets`);
28-
return;
29-
}
26+
if (Object.keys(data.sets).length === 0) {
27+
console.log(`⚠️ Token ${key} has no value or sets`);
28+
return;
29+
}
3030

31-
// Check if one of the contexts is a key in the sets
32-
if (color in data.sets) {
33-
return parseData(data.sets[color], { key, color, platform });
34-
}
31+
// Check if one of the contexts is a key in the sets
32+
if (color in data.sets) {
33+
return parseData(data.sets[color], { key, color, platform });
34+
}
3535

36-
if (platform in data.sets) {
37-
return parseData(data.sets[platform], { key, color, platform });
38-
}
36+
if (platform in data.sets) {
37+
return parseData(data.sets[platform], { key, color, platform });
38+
}
3939

40-
return;
40+
return;
4141
}
4242

4343
/**
@@ -49,23 +49,25 @@ function parseData(data, { key, color, platform }) {
4949
* @returns {string|undefined} - The value of the token or a fallback value
5050
*/
5151
export function fetchToken(key, fallback = undefined, { color, scale } = {}) {
52-
if (typeof key !== "string") return fallback;
52+
if (typeof key !== "string") return fallback;
5353

54-
// Fetch the theme if it exists; this data exists if wrapped in a ThemeProvider
54+
// Fetch the theme if it exists; this data exists if wrapped in a ThemeProvider
5555
const theme = useTheme() ?? {};
5656

57-
// If the color or scale is not provided, use the theme values or a fallback
58-
if (typeof color !== "string" && typeof theme.color == "string") color = theme.color;
59-
else if (!color) color = "light";
57+
// If the color or scale is not provided, use the theme values or a fallback
58+
if (typeof color !== "string" && typeof theme.color == "string")
59+
color = theme.color;
60+
else if (!color) color = "light";
6061

61-
if (typeof scale !== "string" && typeof theme.scale == "string") scale = theme.scale;
62-
else if (!scale) scale = "medium";
62+
if (typeof scale !== "string" && typeof theme.scale == "string")
63+
scale = theme.scale;
64+
else if (!scale) scale = "medium";
6365

64-
// Create a platform context based on the scale (platform used in the token data)
65-
const platform = scale === "medium" ? "desktop" : "mobile";
66+
// Create a platform context based on the scale (platform used in the token data)
67+
const platform = scale === "medium" ? "desktop" : "mobile";
6668

67-
// Check if the spectrum data is available
68-
if (!spectrum || typeof spectrum !== "object") return fallback;
69+
// Check if the spectrum data is available
70+
if (!spectrum || typeof spectrum !== "object") return fallback;
6971

70-
return parseData(spectrum[key], { color, platform }) ?? fallback;
72+
return parseData(spectrum[key], { color, platform }) ?? fallback;
7173
}

.storybook/manager.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import "@spectrum-css/tokens/dist/index.css";
22
import { addons } from "@storybook/manager-api";
33
import { create } from "@storybook/theming";
4+
5+
// Import the CSS bundle
6+
import "@spectrum-css/bundle/dist/index.min.css";
7+
48
import "./assets/index.css";
9+
510
import logo from "./assets/logo.svg";
611
import pkg from "./package.json";
712

.storybook/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
},
3030
"dependencies": {
3131
"@adobe/spectrum-css-workflow-icons": "^1.5.4",
32-
"@spectrum-css/table": "workspace:^",
32+
"@spectrum-css/bundle": "workspace:^",
3333
"@spectrum-css/tokens": "workspace:^",
34-
"@spectrum-css/typography": "workspace:^",
3534
"@spectrum-css/ui-icons": "workspace:^"
3635
},
3736
"devDependencies": {

.storybook/preview.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import {
2020
globalTypes
2121
} from "./types";
2222

23+
// Load the Spectrum CSS bundle to style the docs elements
24+
import "@spectrum-css/bundle/dist/index.min.css";
25+
26+
// Import the custom base styles
2327
import "./assets/base.css";
2428

2529
window.global = window;

.storybook/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"{projectRoot}/modes",
1212
"{projectRoot}/templates",
1313
"{projectRoot}/types",
14-
"{projectRoot}/*.{js,html,mdx}"
14+
"{projectRoot}/*.{js,html}"
1515
]
1616
},
1717
"targets": {

.stylelintignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ generator
66
dist
77
.storybook/storybook-static
88
*-generated.css
9+
tools/bundle/src/*.css
910

1011
node_modules/**/*.css
1112

0 commit comments

Comments
 (0)