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

ensure css prop gets handled properly in storybook #35

Merged
merged 4 commits into from
May 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion web-components/src/components/chart/chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class Chart extends BaseElement {

protected render() {
return html`
<div class="chart-container"></div>
<div class="chart-container" data-chromatic="ignore"></div>
`;
}

Expand Down
9 changes: 5 additions & 4 deletions web-components/src/components/spinner/spinner.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { Meta, StoryFn, StoryObj } from '@storybook/web-components';
import docs from './spinner.md?raw';
import { ifDefined } from 'lit-html/directives/if-defined.js';

const meta: Meta<Spinner> = {
type SpinnerStory = Spinner & { '--spinner-background-color': string };
const meta: Meta<SpinnerStory> = {
title: 'Components/Spinner',
component: 'dss-spinner',
argTypes: {
type: { control: 'select', options: spinnerTypes },
size: { control: 'select', options: spinnerSizes },
thickness: { control: 'select', options: spinnerThickness },
'--spinner-background-color': { control: 'color' },
},
parameters: {
docs: {
Expand All @@ -25,12 +27,11 @@ const meta: Meta<Spinner> = {
};
export default meta;

export type SpinnerStory = Spinner & { backgroundColor: string };
const Template: StoryFn<SpinnerStory> = ({
type,
size,
thickness,
backgroundColor,
'--spinner-background-color': backgroundColor,
}) => html`
<div style="height: 4rem; width: 4rem">
<dss-spinner
Expand Down Expand Up @@ -64,7 +65,7 @@ export const OverwriteBackground: StoryObj<SpinnerStory> = {
render: Template,
args: {
type: 'secondary',
backgroundColor: 'papayawhip',
'--spinner-background-color': 'papayawhip',
},
};

Expand Down
3 changes: 2 additions & 1 deletion web-components/src/components/table/table.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export const CustomRender: StoryObj<Table> = {

}).format(wealth.amount),
header: () => html`
<marquee>Wealth</marquee>`,
<span style="text-transform: uppercase">Wealth</span>
`,
cell: ({ row: { original } }) => {
const { amount, currency } = original.wealth;
const formattedParts = new Intl.NumberFormat(navigator.language, {
Expand Down
5 changes: 4 additions & 1 deletion web-components/src/internals/baseElement/baseElement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { LitElement, unsafeCSS } from 'lit';
import global from './global.css?inline';
import disableAnimations from './disable-animations.css?inline';

export const ActionKeystrokes = [' ', 'Enter'];

export default class BaseElement<EventsPayloadMap = Record<string, never>> extends LitElement {

protected static globalStyles = unsafeCSS(global);
protected static globalStyles = import.meta.env.VITE_IS_CHROMATIC
Copy link
Collaborator

@Cybertron01Z Cybertron01Z May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea:
i am not the biggest fan of doing this in the baseElement.
Could we do something like this in the .stories files to disable the animations?

var sheet = new CSSStyleSheet()
sheet.replaceSync( `.color { color: pink }`)
host.shadowRoot.adoptedStyleSheets.push(sheet) 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it too much either, but we need to be inside each components Shadow DOM. There is not a nice way to do this, if we have to place CSSStyleSheets on every story trying to reach into the Shadow DOM, it's going to be messy and we are going to miss some leading to unexpected errors.

In this way here we have every shadow DOM but only if at build time the env var IS_CHROMATIC is present. So all our prod builds do not have these CSS styles included. Which I think is a nice compromise that is very maintainable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough 👍🏻 could we do something similar also with reduced motion? I don't think we have any big animations but could this also be something that we want to support? Or is this more on application level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point! For the reset.css we used this as a base: https://dev.to/hankchizljaw/a-modern-css-reset-6p3
As you can see, in the end it disables all animations for people that have reduced motion set. With our client we had the problem though, that they used virtual desktops which always had reduced motion on, so we never got any animations which led us to remove this CSS.

We then ended up setting it for the bigger animations on a per component base. You can see the loadingPlaceholder for example: https://github.com/Zuehlke/design-system-starter/blob/main/web-components/src/components/loadingPlaceholder/loadingPlaceholder.css

? unsafeCSS(disableAnimations + global)
: unsafeCSS(global);

/**
* Change events are relevant for many form libraries and general form handling. According to the spec they bubble
Expand Down
14 changes: 14 additions & 0 deletions web-components/src/internals/baseElement/disable-animations.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* We have to disable all animations inside our web components for Chromatic Snapshot tests */

html:focus-within {
scroll-behavior: auto;
}

*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
Loading