Skip to content

Commit

Permalink
[storybook][eui] Add EUIThemeProvider to kbn-storybook (#122094)
Browse files Browse the repository at this point in the history
  • Loading branch information
clintandrewhall authored Dec 28, 2021
1 parent 99cdba3 commit 8890384
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/kbn-storybook/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ module.exports = {
webpackFinal: (config) => {
return webpackConfig({ config });
},
config: (entry) => {
return [...entry, require.resolve('./target_node/lib/decorators')];
},
};
32 changes: 32 additions & 0 deletions packages/kbn-storybook/src/lib/decorators.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { EuiProvider } from '@elastic/eui';
import createCache from '@emotion/cache';
import type { DecoratorFn } from '@storybook/react';

/**
* Storybook decorator using the EUI provider. Uses the value from
* `globals` provided by the Storybook theme switcher.
*/
const EuiProviderDecorator: DecoratorFn = (storyFn, { globals }) => {
const colorMode = globals.euiTheme === 'v8.dark' ? 'dark' : 'light';
const emotionCache = createCache({
key: 'eui-styles',
container: document.querySelector(`meta[name="eui-styles-global"]`) as HTMLElement,
});

return (
<EuiProvider colorMode={colorMode} cache={emotionCache}>
{storyFn()}
</EuiProvider>
);
};

export const decorators = [EuiProviderDecorator];
1 change: 1 addition & 0 deletions packages/kbn-storybook/templates/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<% } %>

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="eui-styles-global" />

<!-- Added for Kibana shared dependencies -->
<script>
Expand Down

0 comments on commit 8890384

Please sign in to comment.