-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
[Emotion] Create hook utility for memoizing component styles per-theme (
- v98.2.1
- v98.2.0
- v98.1.0
- v98.0.0
- v97.3.1
- v97.3.1-rc.0
- v97.3.0
- v97.2.0
- v97.1.0
- v97.0.0
- v97.0.0-backport.2
- v97.0.0-backport.1
- v97.0.0-backport.0
- v96.1.0
- v96.0.0
- v95.12.0
- v95.12.0-backport.0
- v95.11.0
- v95.10.1
- v95.10.0
- v95.9.0
- v95.8.0
- v95.8.0-backport.1
- v95.8.0-backport.0
- v95.7.0
- v95.6.0
- v95.5.0
- v95.4.0
- v95.3.0
- v95.2.0
- v95.1.0
- v95.1.0-backport.0
- v95.0.0
- v95.0.0-backport.0
- v94.6.0
- v94.5.2
- v94.5.1
- v94.5.0
- v94.5.0-backport.1
- v94.5.0-backport.0
- v94.4.1
- v94.4.0
- v94.3.0
- v94.2.1
- v94.2.1-backport.0
- v94.2.0
- v94.1.0
- v94.1.0-backport.0
- v94.0.0
- v93.6.0
- v93.6.0-backport.0
- v93.5.2
- v93.5.1
- v93.5.1-rc.0
- v93.5.0
- v93.4.0
- v93.3.0
- v93.2.0
Showing
14 changed files
with
267 additions
and
113 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,3 @@ | ||
**CSS-in-JS conversions** | ||
|
||
- Added a new memoization/performance optimization utility for CSS-in-JS styles |
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 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 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 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 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 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 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 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 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 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 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 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,85 @@ | ||
/* | ||
* 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 } from 'react'; | ||
import { css } from '@emotion/react'; | ||
import { fireEvent } from '@testing-library/react'; | ||
import { render, renderHook } from '../../test/rtl'; | ||
import { testOnReactVersion } from '../../test/internal'; | ||
|
||
import type { UseEuiTheme } from './hooks'; | ||
import { EuiThemeProvider } from './provider'; | ||
|
||
import { useEuiMemoizedStyles } from './style_memoization'; | ||
|
||
describe('useEuiMemoizedStyles', () => { | ||
beforeEach(jest.clearAllMocks); | ||
|
||
const componentStyles = jest.fn(({ euiTheme }: UseEuiTheme) => ({ | ||
someComponent: css` | ||
color: ${euiTheme.colors.primaryText}; | ||
`, | ||
})); | ||
const Component = () => { | ||
const [rerender, setRerender] = useState(false); | ||
const styles = useEuiMemoizedStyles(componentStyles); | ||
return ( | ||
<button | ||
css={styles.someComponent} | ||
onClick={() => setRerender(!rerender)} | ||
/> | ||
); | ||
}; | ||
|
||
it('memoizes the passed fn, only computing the styles once regardless of other rerenders', () => { | ||
const { getByRole } = render(<Component />); | ||
|
||
expect(componentStyles).toHaveBeenCalledTimes(1); | ||
expect(getByRole('button')).toHaveStyleRule('color', '#006bb8'); | ||
|
||
fireEvent.click(getByRole('button')); | ||
expect(componentStyles).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('recomputes styles if the upstream theme changes', () => { | ||
const { rerender, getByRole } = render( | ||
<EuiThemeProvider colorMode="light"> | ||
<Component /> | ||
</EuiThemeProvider> | ||
); | ||
expect(componentStyles).toHaveBeenCalledTimes(1); | ||
expect(getByRole('button')).toHaveStyleRule('color', '#006bb8'); | ||
|
||
rerender( | ||
<EuiThemeProvider colorMode="dark"> | ||
<Component /> | ||
</EuiThemeProvider> | ||
); | ||
expect(componentStyles).toHaveBeenCalledTimes(2); | ||
expect(getByRole('button')).toHaveStyleRule('color', '#36a2ef'); | ||
|
||
// Should not recompute styles if no theme changes | ||
rerender( | ||
<EuiThemeProvider colorMode="dark"> | ||
<Component /> | ||
</EuiThemeProvider> | ||
); | ||
expect(componentStyles).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
testOnReactVersion(['18'])( | ||
'throws an error if passed anonymous functions', | ||
() => { | ||
expect(() => | ||
renderHook(() => useEuiMemoizedStyles(() => ({}))) | ||
).toThrowError( | ||
'Styles are memoized per function. Your style functions must be statically defined in order to not create a new map entry every rerender.' | ||
); | ||
} | ||
); | ||
}); |
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,85 @@ | ||
/* | ||
* 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, { | ||
FunctionComponent, | ||
PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useState, | ||
useMemo, | ||
} from 'react'; | ||
import type { SerializedStyles, CSSObject } from '@emotion/react'; | ||
|
||
import { useUpdateEffect } from '../hooks'; | ||
import { useEuiTheme, UseEuiTheme } from './hooks'; | ||
|
||
type Styles = SerializedStyles | CSSObject | string | null; | ||
type StylesMaps = Record<string, Styles | Record<string, Styles>>; | ||
// NOTE: We're specifically using a WeakMap instead of a Map in order to allow | ||
// unmounted components to have their styles garbage-collected by the browser | ||
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap | ||
type MemoizedStylesMap = WeakMap<Function, StylesMaps>; | ||
|
||
export const EuiThemeMemoizedStylesContext = createContext<MemoizedStylesMap>( | ||
new WeakMap() | ||
); | ||
|
||
export const EuiThemeMemoizedStylesProvider: FunctionComponent< | ||
PropsWithChildren | ||
> = ({ children }) => { | ||
// Note: we *should not* use `useMemo` instead of `useState` here, as useMemo is not guaranteed | ||
// and its cache can get thrown away by React (https://react.dev/reference/react/useMemo#caveats) | ||
const [componentStyles, rerenderStyles] = useState<MemoizedStylesMap>( | ||
new WeakMap() | ||
); | ||
|
||
// On theme update, wipe the map, which causes the below hook to recompute all styles | ||
const { euiTheme } = useEuiTheme(); | ||
useUpdateEffect(() => { | ||
rerenderStyles(new WeakMap()); | ||
}, [euiTheme]); | ||
|
||
return ( | ||
<EuiThemeMemoizedStylesContext.Provider value={componentStyles}> | ||
{children} | ||
</EuiThemeMemoizedStylesContext.Provider> | ||
); | ||
}; | ||
|
||
/** | ||
* Hook that memoizes the returned values of components style fns/generators | ||
* per-theme | ||
*/ | ||
export const useEuiMemoizedStyles = < | ||
T extends (theme: UseEuiTheme) => StylesMaps | ||
>( | ||
styleGenerator: T | ||
): ReturnType<T> => { | ||
const memoizedStyles = useContext(EuiThemeMemoizedStylesContext); | ||
const euiThemeContext = useEuiTheme(); | ||
|
||
const memoizedComponentStyles = useMemo(() => { | ||
if (!styleGenerator.name) { | ||
throw new Error( | ||
'Styles are memoized per function. Your style functions must be statically defined in order to not create a new map entry every rerender.' | ||
); | ||
} | ||
const existingStyles = memoizedStyles.get(styleGenerator); | ||
if (existingStyles) { | ||
return existingStyles; | ||
} else { | ||
const generatedStyles = styleGenerator(euiThemeContext); | ||
memoizedStyles.set(styleGenerator, generatedStyles); | ||
|
||
return generatedStyles; | ||
} | ||
}, [styleGenerator, memoizedStyles, euiThemeContext]); | ||
|
||
return memoizedComponentStyles as ReturnType<T>; | ||
}; |