Skip to content

Commit

Permalink
chore: all token-free raw mode to storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
castastrophe committed Jul 9, 2024
1 parent 8142be9 commit cff4722
Show file tree
Hide file tree
Showing 104 changed files with 2,124 additions and 1,637 deletions.
2 changes: 1 addition & 1 deletion .github/actions/file-diff/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2024 Adobe. All rights reserved.
* Copyright 2023 Adobe. All rights reserved.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/file-diff/utilities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2024 Adobe. All rights reserved.
* Copyright 2023 Adobe. All rights reserved.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
Expand Down
14 changes: 7 additions & 7 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"tabWidth": 2,
"useTabs": true,
"overrides": [
{
"files": "*.css",
"options": {
"printWidth": 500
}
}
]
{
"files": "*.css",
"options": {
"printWidth": 500
}
}
]
}
2 changes: 1 addition & 1 deletion .storybook/assets/index.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2024 Adobe. All rights reserved.
* Copyright 2023 Adobe. All rights reserved.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
Expand Down
11 changes: 6 additions & 5 deletions .storybook/blocks/component-details.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React from "react";
import { useOf } from '@storybook/blocks';
import React from "react";

import "./component-details.css";

/**
* Displays the current version number of the component. The version is read from
* the component's parameters, where it was sourced from the package.json file.
*
*
* Also displays a component status of "deprecated" if it is set in the story's
* parameters.
*
* Usage of this doc block within MDX template(s):
*
* Usage of this doc block within MDX template(s):
* <ComponentDetails />
*/
const ComponentDetails = () => {
Expand All @@ -21,7 +22,7 @@ const ComponentDetails = () => {
<dt>Current version:</dt>
<dd>{versionNumber}</dd>

{ storyMeta?.csfFile?.meta?.parameters?.status?.type == "deprecated"
{ storyMeta?.csfFile?.meta?.parameters?.status?.type == "deprecated"
? <>
<dt>Component status:</dt>
<dd>Deprecated</dd>
Expand Down
18 changes: 18 additions & 0 deletions .storybook/decorators/arg-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { makeDecorator, useArgs } from "@storybook/preview-api";

/**
* @type import('@storybook/csf').DecoratorFunction<import('@storybook/web-components').WebComponentsFramework>
* @description Global properties added to each component; determines what stylesheets are loaded
**/
export const withArgEvents = makeDecorator({
name: "withArgEvents",
parameterName: "argEvents",
wrapper: (StoryFn, context) => {
const [, updateArgs] = useArgs();

// Bind the updateArgs function for use in nested templates
context.updateArgs = updateArgs;

return StoryFn(context);
},
});
42 changes: 21 additions & 21 deletions .storybook/decorators/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export const withContextWrapper = makeDecorator({
};

useEffect(() => {
const isRaw = Boolean(context === "raw");
const isDefault = Boolean(context === "spectrum");
const isExpress = Boolean(context === "express");
const isLegacy = Boolean(context !== "spectrum");
const isLegacy = Boolean(context === "legacy");

// viewMode is either "docs" or "story"
if (viewMode === "docs") {
if (viewMode === "docs" && !isRaw) {
// add the default classes to the body to ensure labels, headings, and borders are styled correctly
document.body.classList.add("spectrum", "spectrum--light", "spectrum--medium");
}
Expand All @@ -70,19 +72,18 @@ export const withContextWrapper = makeDecorator({
}

// Every container gets the spectrum class
container.classList.toggle("spectrum", true);
container.classList.toggle("spectrum", !isRaw);

// S1 and S1 Express get the legacy class
container.classList.toggle("spectrum--legacy", isLegacy);
container.classList.toggle("spectrum--legacy", isLegacy || isExpress);
// Express only gets the express class
container.classList.toggle("spectrum--express", isExpress);

// Add/remove the base styles for the global, spectrum, and express contexts
toggleStyles(globalContainer, "vars-base", !isLegacy ? tokens?.base : legacy?.global?.base, true);
if (isLegacy) {
toggleStyles(contextContainer, "vars-base-spectrum", legacy?.spectrum?.base, true); // @todo this probably becomes "legacy" in the updated system
toggleStyles(contextContainer, "vars-base-express", legacy?.express?.base, isExpress);
}
toggleStyles(globalContainer, "vars-shared", tokens?.base, isDefault);
toggleStyles(globalContainer, "vars-shared-legacy-express", legacy?.global?.base, isLegacy || isExpress);
toggleStyles(contextContainer, "vars-shared-legacy", legacy?.spectrum?.base, isLegacy);
toggleStyles(contextContainer, "vars-shared-express", legacy?.express?.base, isExpress);

// Darkest is deprecated in Spectrum 2
if (!isLegacy && color === "darkest") color = "dark";
Expand All @@ -91,23 +92,22 @@ export const withContextWrapper = makeDecorator({
// Force light or dark mode if the static color is set
const isColor = c === staticColorSettings[staticKey]?.color || !staticKey && c === color;

container.classList.toggle(`spectrum--${c}`, isColor);
container.classList.toggle(`spectrum--${c}`, isColor && !isRaw);

toggleStyles(colorsContainer, `vars-${c}`, !isLegacy ? tokens?.[c] : legacy?.global?.[c], isColor);
if (isLegacy) {
toggleStyles(colorsContainer, `vars-${c}-spectrum`, legacy?.spectrum?.[c], isColor);
toggleStyles(colorsContainer, `vars-${c}-express`, legacy?.express?.[c], isExpress && isColor);
}
toggleStyles(colorsContainer, `vars-${c}`, tokens?.[c], isColor && isDefault);
toggleStyles(colorsContainer, `vars-${c}-legacy-express`, legacy?.global?.[c], isColor && (isLegacy || isExpress));
toggleStyles(colorsContainer, `vars-${c}-legacy`, legacy?.spectrum?.[c], isColor && isLegacy);
toggleStyles(colorsContainer, `vars-${c}-express`, legacy?.express?.[c], isColor && isExpress);
}

for (const s of ["medium", "large"]) {
container.classList.toggle(`spectrum--${s}`, s === scale);
const isScale = s === scale;
container.classList.toggle(`spectrum--${s}`, isScale && !isRaw);

toggleStyles(scalesContainer, `vars-${s}`, !isLegacy ? tokens?.[s] : legacy?.global?.[s], s === scale);
if (isLegacy) {
toggleStyles(scalesContainer, `vars-${s}-spectrum`, legacy?.spectrum?.[s], s === scale);
toggleStyles(scalesContainer, `vars-${s}-express`, legacy?.express?.[s], isExpress && s === scale);
}
toggleStyles(scalesContainer, `vars-${s}`, tokens?.[s], isScale && isDefault);
toggleStyles(scalesContainer, `vars-${s}-legacy-express`, legacy?.global?.[s], isScale && (isLegacy || isExpress));
toggleStyles(scalesContainer, `vars-${s}-legacy`, legacy?.spectrum?.[s], isScale && isLegacy);
toggleStyles(scalesContainer, `vars-${s}-express`, legacy?.express?.[s], isScale && isExpress);
}

// Start by removing the background color from the container and then add it back if needed
Expand Down
7 changes: 7 additions & 0 deletions .storybook/decorators/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export { withArgEvents } from "./arg-events.js";
export { withContextWrapper } from "./context.js";
export { withLanguageWrapper } from "./language.js";
export { withReducedMotionWrapper } from "./reduce-motion.js";
export { withTestingPreviewWrapper } from "./testing-preview.js";
export { withTextDirectionWrapper } from "./text-direction.js";

/* This is exported but must be opted-into on a component-by-component basis */
export { withOverlayWrapper } from "./overlay.js";
export { withSizingWrapper } from "./sizing.js";

/* External decorators exported for use in stories */
export { withActions } from "@storybook/addon-actions/decorator";

export * from "./utilities.js";
26 changes: 26 additions & 0 deletions .storybook/decorators/overlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Template as Underlay } from "@spectrum-css/underlay/stories/template.js";
import { makeDecorator } from "@storybook/preview-api";
import { html } from "lit";

/**
* @type import('@storybook/csf').DecoratorFunction<import('@storybook/web-components').WebComponentsFramework>
* @description Ensures the Underlay component is rendered only once.
**/
export const withOverlayWrapper = makeDecorator({
name: "withOverlayWrapper",
parameterName: "withOverlay",
// @todo: should this be opt-in?
// skipIfNoParametersOrOptions: true,
wrapper: (StoryFn, context) => {
const {
args: {
isOpen = false,
} = {},
} = context;

return html`
${Underlay({ isOpen }, context)}
${StoryFn(context)}
`;
},
});
60 changes: 60 additions & 0 deletions .storybook/decorators/sizing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { makeDecorator } from "@storybook/preview-api";
import { html } from "lit";
import { styleMap } from "lit/directives/style-map.js";

/**
* @type import('@storybook/csf').DecoratorFunction<import('@storybook/web-components').WebComponentsFramework>
**/
export const withSizingWrapper = makeDecorator({
name: "withSizingWrapper",
parameterName: "sizing",
wrapper: (StoryFn, context) => {
const { argTypes = {} } = context;
const sizes = argTypes.size?.options ?? [];
const sizeMap = {
xxs: "Extra-extra-small",
xs: "Extra-small",
s: "Small",
m: "Medium",
l: "Large",
xl: "Extra-large",
xxl: "Extra-extra-large",
};

const Typography = import("@spectrum-css/typography/stories/template")?.Template ?? null;

return html`
<div style=${styleMap({ "display": sizes.length === 0 || !window.isTestEnv() ? undefined : "none" })}>
${StoryFn(context)}
</div>
<div data-size-container style=${styleMap({
"display": sizes.length === 0 || !window.isTestEnv() ? "none" : "flex",
"flex-direction": "column",
"align-items": "flex-start",
"gap": "24px",
"margin": "12px",
})}>
${sizes.map((size) => {
context.args.size = size;
return html`
<div>
${Typography({
semantics: "heading",
size: "xs",
content: [sizeMap[size]],
})}
<div
style=${styleMap({
"border": "1px solid var(--spectrum-gray-600)",
"border-radius": "4px",
"overflow": "auto",
})}
>
${StoryFn(context)}
</div>
</div>
`;
})}
</div>`;
},
});
29 changes: 17 additions & 12 deletions .storybook/decorators/testing-preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { makeDecorator, useEffect } from "@storybook/preview-api";
import { FORCE_RE_RENDER } from '@storybook/core-events';
import { addons, makeDecorator, useCallback } from "@storybook/preview-api";
import isChromatic from "chromatic/isChromatic";

/**
Expand All @@ -12,21 +13,25 @@ export const withTestingPreviewWrapper = makeDecorator({
const {
globals: {
testingPreview = false,
} = {}
} = {},
viewMode,
} = context;

function init(isTestingPreview) {
// Prevents the "isChromatic" function from being over written
if (typeof window.isChromatic !== "function") {
// If we're not in Chromatic and we want to show the testing preview, we need to override the isChromatic function
// Otherwise, we need to reset it to the original function (in case it was overridden previously)
window.isChromatic = typeof isChromatic === "function" && isChromatic() ? isChromatic : () => isTestingPreview;
}
}
const init = () => {
window.isChromatic = typeof isChromatic === "function" && isChromatic() === true ? isChromatic : () => testingPreview && viewMode !== "docs";
};

init(testingPreview);
// Function that will update the global value and trigger a UI refresh.
const refreshAndUpdateGlobal = () => {
init();

useEffect(() => init(testingPreview), [testingPreview]);
// Invokes Storybook's addon API method (with the FORCE_RE_RENDER) event to trigger a UI refresh
addons.getChannel().emit(FORCE_RE_RENDER);
};

init();

useCallback(() => refreshAndUpdateGlobal(), [testingPreview]);

return StoryFn(context);
},
Expand Down
Loading

0 comments on commit cff4722

Please sign in to comment.