Skip to content

Commit

Permalink
feat(storybook-config): add Carbon Theme & Type Panel (#7569)
Browse files Browse the repository at this point in the history
* feat(storybook-config): add custom panel

* storybook config custom preset with create-react-app package

* storybook config custom presets - need to transpile

* storybook-config-custom-presets: added babel cli & babel core

* stuck on panel rendering with custom properties variable

* stuck on panel rendering with custom properties variable

* testing file path

* panel is showing up - just not fully functioning

* feat(storybook-config): add theme and type addon panels

* chore: remove theme addon files

* feat(storybook-config): add theme and type addon panels - added copyright headers

* feat(storybook-config): ran yarn dedupe

* feat(storybook-config): make custom panel conditionally appear

* chore(storybook-config): remove merge messages

Co-authored-by: Andrea N. Cardona <[email protected]>
  • Loading branch information
Alessandra Davila and andreancardona authored Feb 1, 2021
1 parent 7cb28b4 commit 0bdea69
Show file tree
Hide file tree
Showing 54 changed files with 324 additions and 209 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions config/storybook-preset-carbon/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright IBM Corp. 2021, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2018
* Copyright IBM Corp. 2021, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -36,10 +36,7 @@ const typeTokenDefaults = {
'productive-heading-04': '28-36',
};

/**
* Storybook add-on panel for Carbon theme switcher.
*/
export const CarbonThemesPanel = ({ api, active }) => {
export const CarbonThemePanel = ({ api, active }) => {
const [currentTheme, setCurrentTheme] = useState('white');
const handleChange = useCallback(
(event) => {
Expand Down Expand Up @@ -77,29 +74,31 @@ export const CarbonThemesPanel = ({ api, active }) => {
);
};

CarbonThemesPanel.propTypes = {
CarbonThemePanel.propTypes = {
/**
* `true` if this Storybook add-on panel is active.
*/
active: PropTypes.bool.isRequired,

/**
* The Storybook API object.
*/
api: PropTypes.shape({
getChannel: PropTypes.func,
}).isRequired,

/**
* `true` if this Storybook add-on panel is active.
*/
active: PropTypes.bool.isRequired,
};

/**
* Storybook add-on panel for Carbon type token switcher.
*/
export const CarbonTypePanel = ({ api, active }) => {
const [currentTypeTokens, setCurrentTypeTokens] = useState(typeTokenDefaults);
const handleTokenChange = useCallback(
(event) => {
const { name: tokenName, value: tokenValue } = event.target;
setCurrentTypeTokens({ ...currentTypeTokens, [tokenName]: tokenValue });
setCurrentTypeTokens((currentTypeTokens) => {
return {
...currentTypeTokens,
[tokenName]: tokenValue,
};
});
api.getChannel().emit(CARBON_TYPE_TOKEN, { tokenName, tokenValue });
},
[api]
Expand Down Expand Up @@ -131,15 +130,15 @@ export const CarbonTypePanel = ({ api, active }) => {
};

CarbonTypePanel.propTypes = {
/**
* `true` if this Storybook add-on panel is active.
*/
active: PropTypes.bool.isRequired,

/**
* The Storybook API object.
*/
api: PropTypes.shape({
getChannel: PropTypes.func,
}).isRequired,

/**
* `true` if this Storybook add-on panel is active.
*/
active: PropTypes.bool.isRequired,
};
14 changes: 14 additions & 0 deletions config/storybook-preset-carbon/carbon-theme-addon/src/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright IBM Corp. 2021, 2021
*
* 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 strict';

module.exports = {
managerEntries(entry = []) {
return [...entry, require.resolve('./register')];
},
};
36 changes: 36 additions & 0 deletions config/storybook-preset-carbon/carbon-theme-addon/src/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright IBM Corp. 2021, 2021
*
* 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 React from 'react';
import { addons } from '@storybook/addons';
import { CarbonThemePanel, CarbonTypePanel } from './components/Panel';
import {
CARBON_THEMES_ADDON_ID,
CARBON_THEME_PANEL_ID,
CARBON_TYPE_ADDON_ID,
CARBON_TYPE_PANEL_ID,
} from './shared';

// Disabling because storybook addons doesn't provide proptypes or display names for these panels
/* eslint-disable react/display-name, react/prop-types */
addons.register(CARBON_THEMES_ADDON_ID, (api) => {
addons.addPanel(CARBON_THEME_PANEL_ID, {
title: 'Carbon Theme',
render: ({ active, key }) => (
<CarbonThemePanel api={api} key={key} active={active} />
),
});
});

addons.register(CARBON_TYPE_ADDON_ID, (api) => {
addons.addPanel(CARBON_TYPE_PANEL_ID, {
title: 'Carbon Type',
render: ({ active, key }) => (
<CarbonTypePanel api={api} key={key} active={active} />
),
});
});
13 changes: 13 additions & 0 deletions config/storybook-preset-carbon/carbon-theme-addon/src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright IBM Corp. 2021, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

export const CARBON_THEMES_ADDON_ID = 'carbon-theme';
export const CARBON_CURRENT_THEME = `${CARBON_THEMES_ADDON_ID}/current`;
export const CARBON_THEME_PANEL_ID = `${CARBON_THEMES_ADDON_ID}/panel`;
export const CARBON_TYPE_ADDON_ID = 'carbon-type';
export const CARBON_TYPE_TOKEN = `${CARBON_TYPE_ADDON_ID}/type`;
export const CARBON_TYPE_PANEL_ID = `${CARBON_TYPE_ADDON_ID}/panel`;
6 changes: 3 additions & 3 deletions config/storybook-preset-carbon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ module.exports = {
'@storybook/addon-storysource',
'@storybook/addon-knobs',
'@storybook/addon-actions',
'@storybook/addon-links',
'@storybook/addon-docs',
'@storybook/addon-notes/register',
'storybook-readme/register',

// Phase 3: port over custom panels/add-ons
'@storybook/addon-links',
CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES === 'true' &&
require.resolve('./dist/preset.js'),
],

webpack(config) {
Expand Down
17 changes: 15 additions & 2 deletions config/storybook-preset-carbon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
"components",
"react"
],
"scripts": {
"build": "babel ./carbon-theme-addon/src --out-dir ./dist",
"clean": "rimraf ./dist"
},
"peerDependencies": {
"@storybook/react": "^5.3.19"
"@storybook/react": "^5.3.19",
"react": "^16.8.6 || ^17.0.1",
"react-dom": "^16.8.6 || ^17.0.1"
},
"dependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-docs": "^5.3.19",
"@storybook/addon-knobs": "^5.3.19",
Expand All @@ -29,9 +37,14 @@
"@storybook/addon-storysource": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/source-loader": "^5.3.19",
"react-is": "^16.8.6",
"storybook-readme": "^5.0.8"
},
"devDependencies": {
"@storybook/react": "^5.3.19"
"@storybook/react": "^5.3.19",
"babel-loader": "^8.2.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"webpack": "^4.41.5"
}
}
36 changes: 0 additions & 36 deletions packages/react/.storybook/addon-carbon-theme/register.js

This file was deleted.

5 changes: 1 addition & 4 deletions packages/react/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
'use strict';

module.exports = {
addons: [
'storybook-preset-carbon',
require.resolve('./addon-carbon-theme/register'),
],
addons: ['storybook-preset-carbon'],
stories: ['../src/**/*-story.js', '../src/**/*.stories.mdx'],
};
5 changes: 1 addition & 4 deletions packages/react/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { addDecorator, addParameters } from '@storybook/react';
import addons from '@storybook/addons';
import { themes } from '@storybook/theming';
import { configureActions } from '@storybook/addon-actions';
import {
CARBON_CURRENT_THEME,
CARBON_TYPE_TOKEN,
} from './addon-carbon-theme/shared';
import { CARBON_CURRENT_THEME, CARBON_TYPE_TOKEN } from './shared';
import Container from './Container';
import PackageInfo from './../package.json';

Expand Down
File renamed without changes.
Loading

0 comments on commit 0bdea69

Please sign in to comment.