@carbon/storybook-addon-theme
Version:
Carbon theme switcher for Storybook
35 lines (32 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withCarbonTheme = void 0;
var _previewApi = require("@storybook/preview-api");
var _constants = require("../constants");
/**
* Copyright IBM Corp. 2023, 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.
*/
const withCarbonTheme = (StoryFn, context) => {
const {
globals,
parameters
} = context;
const globalCarbonTheme = globals[_constants.PARAM_KEY];
const storyCarbonTheme = parameters[_constants.PARAM_KEY];
// eslint-disable-next-line react-hooks/rules-of-hooks
(0, _previewApi.useEffect)(() => {
const selector = context.viewMode === 'docs' ? `.sbdocs-preview .docs-story` : '.sb-show-main';
const rootElements = document.querySelectorAll(selector);
rootElements.forEach(rootElement => {
rootElement.setAttribute('data-carbon-theme', globalCarbonTheme ?? storyCarbonTheme);
});
}, [context.id, context.viewMode, globalCarbonTheme, storyCarbonTheme]);
return StoryFn();
};
exports.withCarbonTheme = withCarbonTheme;
//# sourceMappingURL=withCarbonTheme.js.map