Skip to content

Commit

Permalink
chore(storybook): upgrade to 7 and using vite
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnacioBecerra committed Dec 14, 2023
1 parent a8d8bfa commit 4e2cfb9
Show file tree
Hide file tree
Showing 54 changed files with 1,108 additions and 25 deletions.
2 changes: 1 addition & 1 deletion web-components/packages/carbon-web-components/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
ecmaVersion: 6,
sourceType: 'script',
},
extends: ['../eslint-config-ibmdotcom'],
extends: ['../eslint-config-ibmdotcom', 'plugin:storybook/recommended'],
env: {
node: true,
es6: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

import { html, TemplateResult } from 'lit';
import containerStyles from './_container.scss'; // eslint-disable-line import/first
import containerStyles from './_container.scss?lit'; // eslint-disable-line import/first
import '../src/components/skip-to-content/skip-to-content';

/**
* @param options The rendering options.
Expand All @@ -29,6 +30,7 @@ const container = ({
<cds-skip-to-content href="#main-content"></cds-skip-to-content>
<div
id="main-content"
style="background: red;"
name="main-content"
data-floating-menu-container
role="${hasMainTag ? 'none' : 'main'}">
Expand Down
39 changes: 39 additions & 0 deletions web-components/packages/carbon-web-components/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { StorybookConfig } from '@storybook/web-components-vite';
import { mergeConfig } from 'vite';
import { litStyleLoader, litTemplateLoader } from '@mordech/vite-lit-loader';

import { join, dirname } from 'path';

/**
* This function is used to resolve the absolute path of a package.
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
*/
function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, 'package.json')));
}
const config: StorybookConfig = {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
getAbsolutePath('@storybook/addon-links'),
getAbsolutePath('@storybook/addon-essentials'),
],
framework: {
name: getAbsolutePath('@storybook/web-components-vite'),
options: {},
},
async viteFinal(config) {
// Merge custom configuration into the default config
return mergeConfig(config, {
// Add dependencies to pre-optimization
plugins: [litStyleLoader(), litTemplateLoader()],
optimizeDeps: {
include: ['storybook-dark-mode', '@storybook/web-components'],
exclude: ['lit', 'lit-html']
},
});
},
docs: {
autodocs: 'tag',
},
};
export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Preview, setCustomElementsManifest } from '@storybook/web-components';
import customElements from '../custom-elements.json';
import container from './container';
import theme from './theme';


setCustomElementsManifest(customElements);

export const preview: Preview = {
parameters: {
controls: { expanded: true },
},
};

export const decorators = [
function decoratorContainer(story, context) {
const result = story();
const { hasMainTag } = result;
const { theme } = context.globals;

document.documentElement.setAttribute('storybook-carbon-theme', theme);

return container({ hasMainTag, children: result });
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// @license
//
// Copyright IBM Corp. 2019, 2023
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@use '@carbon/styles/scss/config' as *;
@use '@carbon/themes/scss/themes';
@use '@carbon/styles/scss/theme';
@use '@carbon/grid';

@use '@carbon/styles/scss/components/button/tokens' as button-tokens;
@use '@carbon/styles/scss/components/notification/tokens' as notification-tokens;
@use '@carbon/styles/scss/components/tag/tokens' as tag-tokens;
@include theme.add-component-tokens(button-tokens.$button-tokens);
@include theme.add-component-tokens(notification-tokens.$notification-tokens);
@include theme.add-component-tokens(tag-tokens.$tag-tokens);

// Emit the flex-grid styles
@include grid.flex-grid();

*,
*::before,
*::after {
box-sizing: border-box;
}

// The default theme is "white" (White)
:root {
@include theme.theme(themes.$white);
}
// Set the <html> theme attribute to "g10" to use the Gray 10 theme
// <html theme="g10">
:root[storybook-carbon-theme='g10'] {
@include theme.theme(themes.$g10);
}

// Set the <html> theme attribute to "g90" to use the Gray 90 theme
// <html theme="g90">
:root[storybook-carbon-theme='g90'] {
@include theme.theme(themes.$g90);
}

// Set the <html> theme attribute to "g100" to use the Gray 100 theme
// <html theme="g100">
:root[storybook-carbon-theme='g100'] {
@include theme.theme(themes.$g100);
}

body {
background-color: theme.$background;
color: theme.$text-primary;
}

.sb-show-main.sb-main-padded {
padding: 0;
}

#main-content {
padding: 42px;
position: relative;
}

.#{$prefix}--content.#{$prefix}-ce-demo-devenv--ui-shell-content {
margin: 0;
height: 100vh;
width: 100%;
padding: 2rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { StoryContext } from '@storybook/addons';

/**
* The list of knobs factories set in story parameters.
*/
type KnobParameters = {
[componentName: string]: () => { [propName: string]: any };
};

export const decorators = [
/**
* A Storybook decorator that takes list of knobs factories set in story parameters,
* generates knob values from those, and sets them to story args.
* @param story The original story factory.
* @param context The story context.
*/
function decoratorKnobs(
story,
{ args, parameters: { knobs } }: StoryContext
) {
if (Object(knobs) === knobs) {
Object.keys(knobs).forEach((name) => {
const { [name]: knobsForComponent } = knobs as KnobParameters;
if (typeof knobsForComponent === 'function') {
args[name] = knobsForComponent();
}
}, {});
}
return story();
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

function config(entry = []) {
return [...entry, require.resolve('./decorators.ts')];
}

module.exports = {
config,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, TemplateResult } from 'lit';
import containerStyles from './_container.scss'; // eslint-disable-line import/first

/**
* @param options The rendering options.
* @param [options.hasMainTag] `true` if the story itself has `<main>` tag.
* @param [options.children] The story content.
* @returns The content that wraps the story.
*/
const container = ({
hasMainTag,
children,
}: {
hasMainTag?: boolean;
children: TemplateResult;
}) => html`
<style>
${containerStyles}
</style>
<cds-skip-to-content href="#main-content"></cds-skip-to-content>
<div
id="main-content"
name="main-content"
data-floating-menu-container
role="${hasMainTag ? 'none' : 'main'}">
${children}
</div>
`;

export default container;
21 changes: 21 additions & 0 deletions web-components/packages/carbon-web-components/.storybook2/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { create } from '@storybook/theming';
import { version } from '../package.json';

export default create({
// Typography
fontBase: "'IBM Plex Sans', 'Helvetica Neue', Arial, sans-serif",
fontCode:
"'IBM Plex Mono', Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace",
brandTitle: `@carbon/web-components ${version}`,
brandUrl:
'https://github.com/carbon-design-system/carbon-for-ibm-dotcom/tree/main/packages/carbon-web-components',
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ As Shadow DOM (one of the Web Components specs that `@carbon/web-components` use
However, in cases where your application or a Carbon-derived style guide wants
to change the styles of our components, there are a few options.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Using CSS Custom Properties](#using-css-custom-properties)
- [Dependency injection](#dependency-injection)
- [Creating derived components with different style](#creating-derived-components-with-different-style)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Using CSS Custom Properties

Changes to CSS Custom Properties of the Carbon theme are reflected in the color
Expand Down
23 changes: 12 additions & 11 deletions web-components/packages/carbon-web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"build": "gulp build && yarn wca",
"build:dist": "gulp build:dist",
"build:dist:dev": "gulp build:dist:dev",
"build-storybook": "cross-env NODE_OPTIONS=--openssl-legacy-provider BABEL_ENV=es build-storybook",
"build-storybook": "storybook build",
"ci-check": "yarn wca && yarn typecheck && yarn build && yarn test:unit",
"clean": "gulp clean",
"reset": "yarn cache clean && yarn clean && rimraf node_modules && yarn install && yarn build",
"start": "yarn storybook",
"storybook": "cross-env NODE_OPTIONS=--openssl-legacy-provider start-storybook -p 9000",
"storybook": "storybook dev -p 6006",
"test": "gulp test && yarn test:integration",
"test:integration": "yarn test:integration:build && yarn test:integration:ui",
"test:integration:build": "jest -c tests/integration/build/jest.config.js --runInBand",
Expand Down Expand Up @@ -94,6 +94,7 @@
"@babel/traverse": "~7.23.0",
"@carbon/icon-helpers": "^10.45.1",
"@carbon/icons": "^11.31.0",
"@mordech/vite-lit-loader": "^0.31.3",
"@open-wc/semantic-dom-diff": "~0.18.0",
"@percy-io/in-percy": "^0.1.11",
"@percy/cli": "^1.8.1",
Expand All @@ -105,13 +106,13 @@
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/pluginutils": "^4.2.0",
"@storybook/addon-a11y": "^6.5.15",
"@storybook/addon-actions": "^6.5.15",
"@storybook/addon-docs": "^6.5.15",
"@storybook/addon-essentials": "^6.5.15",
"@storybook/addon-knobs": "^6.4.0",
"@storybook/addon-storysource": "^6.5.15",
"@storybook/web-components": "^6.5.15",
"@storybook/addon-essentials": "^7.6.4",
"@storybook/addon-knobs": "^7.0.2",
"@storybook/addon-links": "^7.6.4",
"@storybook/addon-storysource": "^7.6.4",
"@storybook/blocks": "^7.6.4",
"@storybook/web-components": "^7.6.4",
"@storybook/web-components-vite": "^7.6.4",
"@types/bluebird": "^3.5.36",
"@types/jasmine": "^3.10.2",
"@types/jest": "^29.2.3",
Expand Down Expand Up @@ -139,6 +140,7 @@
"custom-event": "^1.0.0",
"del": "^6.0.0",
"es6-promise": "^4.2.8",
"eslint-plugin-storybook": "^0.6.15",
"execa": "^5.1.1",
"expect-playwright": "~0.8.0",
"expect-puppeteer": "^6.0.0",
Expand Down Expand Up @@ -197,6 +199,7 @@
"rtlcss": "^3.0.0",
"sass": "~1.69.0",
"sass-loader": "^10.0.0",
"storybook": "^7.6.4",
"strip-comments": "^1.0.0",
"style-loader": "^2.0.0",
"temp": "^0.9.0",
Expand All @@ -207,8 +210,6 @@
"typescript": "^5.2.2",
"use-debounce": "^3.2.0",
"web-component-analyzer": "1.2.0-next.0",
"webpack": "^4.46.0",
"webpack-dev-server": "^4.0.0",
"zone.js": "~0.14.0"
},
"typings": "es/index.d.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
BUTTON_TOOLTIP_ALIGNMENT,
BUTTON_TOOLTIP_POSITION,
} from './defs';
import styles from './button.scss';
import styles from './button.scss?lit';
import HostListener from '../../globals/decorators/host-listener';
import HostListenerMixin from '../../globals/mixins/host-listener';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';
Expand Down Expand Up @@ -299,6 +299,7 @@ class CDSButton extends HostListenerMixin(FocusMixin(LitElement)) {
</p>
`
: html`
<p>a</p>
<a
id="button"
part="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/

import './button';
import './button-set';
import './button-skeleton';
// import './button-set';
// import './button-skeleton';
Loading

0 comments on commit 4e2cfb9

Please sign in to comment.