-
Notifications
You must be signed in to change notification settings - Fork 859
[Storybook] Fix infinite rerendering bug introduced in #8003 #8024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added benefits: