-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] move EuiProvider decorator + globalTypes to separate file
- cleaner, contain concerns in a single file
- Loading branch information
Showing
3 changed files
with
129 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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, { useState, useMemo, FunctionComponent } from 'react'; | ||
import { css } from '@emotion/react'; | ||
import type { Preview } from '@storybook/react'; | ||
|
||
import { EuiThemeColorMode } from '../src/services'; | ||
import { EuiProvider, EuiProviderProps } from '../src/components/provider'; | ||
|
||
/** | ||
* Primary EuiProvider decorator to wrap around all stories | ||
* @see https://storybook.js.org/docs/writing-stories/decorators | ||
*/ | ||
export const EuiProviderDecorator: FunctionComponent< | ||
EuiProviderProps<{}> & { | ||
writingMode: WritingModes; | ||
} | ||
> = ({ children, writingMode, ...euiProviderProps }) => { | ||
// Append portals into Storybook's root div (rather than <body>) | ||
// so that loki correctly captures them for VRT screenshots | ||
const [sibling, setPortalSibling] = useState<HTMLElement | null>(null); | ||
const portalInsert = useMemo(() => { | ||
if (sibling) { | ||
return { | ||
EuiPortal: { insert: { sibling, position: 'after' as const } }, | ||
}; | ||
} | ||
}, [sibling]); | ||
|
||
// Set CSS writing mode/direction on story-wrapper | ||
const writingModeCss = useMemo( | ||
() => [{ label: 'writingMode' }, writingModeStyles[writingMode]], | ||
[writingMode] | ||
); | ||
|
||
return ( | ||
<EuiProvider componentDefaults={portalInsert} {...euiProviderProps}> | ||
<div id="story-wrapper" ref={setPortalSibling} css={writingModeCss}> | ||
{portalInsert && children} | ||
</div> | ||
</EuiProvider> | ||
); | ||
}; | ||
|
||
/** | ||
* Styles used for testing CSS logical properties | ||
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_writing_modes | ||
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties | ||
*/ | ||
const writingModeStyles = { | ||
ltr: css` | ||
direction: ltr; | ||
`, | ||
rtl: css` | ||
direction: rtl; | ||
`, | ||
'vertical-lr': css` | ||
writing-mode: vertical-lr; | ||
`, | ||
'vertical-rl': css` | ||
writing-mode: vertical-rl; | ||
`, | ||
// Sideways RL is the same as vertical RL | ||
sideways: css` | ||
writing-mode: sideways-lr; | ||
`, | ||
}; | ||
type WritingModes = keyof typeof writingModeStyles; | ||
|
||
/** | ||
* Storybook toolbar types - define these separately so that we can ensure | ||
* their values match ones that EuiProviderDecorator expects | ||
*/ | ||
type ToolbarDisplay = { title: string; icon: string }; | ||
|
||
const storybookToolbarColorModes: Array< | ||
ToolbarDisplay & { value: EuiThemeColorMode } | ||
> = [ | ||
{ value: 'light', title: 'Light mode', icon: 'circlehollow' }, | ||
{ value: 'dark', title: 'Dark mode', icon: 'circle' }, | ||
]; | ||
|
||
const storybookToolbarWritingModes: Array< | ||
ToolbarDisplay & { value: WritingModes } | ||
> = [ | ||
{ value: 'ltr', title: 'LTR', icon: 'arrowleft' }, | ||
{ value: 'rtl', title: 'RTL', icon: 'arrowright' }, | ||
{ value: 'vertical-lr', title: 'Vertical LTR', icon: 'arrowup' }, | ||
{ value: 'vertical-rl', title: 'Vertical RTL', icon: 'arrowdown' }, | ||
{ value: 'sideways', title: 'Sideways LTR', icon: 'collapse' }, | ||
]; | ||
|
||
/** | ||
* Export Storybook toolbar globals/context that affect our EuiProvider decorator | ||
* @see https://storybook.js.org/docs/essentials/toolbars-and-globals | ||
*/ | ||
export const euiProviderDecoratorGlobals: Preview['globalTypes'] = { | ||
colorMode: { | ||
description: 'Color mode for EuiProvider theme', | ||
defaultValue: 'light', | ||
toolbar: { | ||
title: 'Color mode', | ||
items: storybookToolbarColorModes, | ||
dynamicTitle: true, | ||
}, | ||
}, | ||
writingMode: { | ||
description: 'Writing mode for testing logical property directions', | ||
defaultValue: 'ltr', | ||
toolbar: { | ||
title: 'Writing mode', | ||
items: storybookToolbarWritingModes, | ||
dynamicTitle: true, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.