From 126dbcf059dd93a4650d346f8f7f59b41f8f3907 Mon Sep 17 00:00:00 2001 From: Cee Chen Date: Wed, 14 Feb 2024 11:41:03 -0800 Subject: [PATCH 01/11] [EuiScreenReaderOnly] Optimize styles - make styles a static obj instead of a function --- .../screen_reader_only.test.tsx.snap | 2 +- .../screen_reader_only.styles.ts | 20 +++++++++---------- .../screen_reader_only/screen_reader_only.tsx | 20 ++++++++++--------- .../resizable_container.test.tsx.snap | 4 ++-- .../resizable_collapse_button.tsx | 5 +---- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/components/accessibility/screen_reader_only/__snapshots__/screen_reader_only.test.tsx.snap b/src/components/accessibility/screen_reader_only/__snapshots__/screen_reader_only.test.tsx.snap index b96c8872fdc..c6f851ca209 100644 --- a/src/components/accessibility/screen_reader_only/__snapshots__/screen_reader_only.test.tsx.snap +++ b/src/components/accessibility/screen_reader_only/__snapshots__/screen_reader_only.test.tsx.snap @@ -18,7 +18,7 @@ exports[`EuiScreenReaderOnly adds an accessibility class to a child element when exports[`EuiScreenReaderOnly will show on focus 1`] = ` Link diff --git a/src/components/accessibility/screen_reader_only/screen_reader_only.styles.ts b/src/components/accessibility/screen_reader_only/screen_reader_only.styles.ts index 5099dea3540..9ef1131029c 100644 --- a/src/components/accessibility/screen_reader_only/screen_reader_only.styles.ts +++ b/src/components/accessibility/screen_reader_only/screen_reader_only.styles.ts @@ -34,13 +34,13 @@ export const euiScreenReaderOnly = () => ` /* * Styles */ -export const euiScreenReaderOnlyStyles = (showOnFocus?: boolean) => ({ - euiScreenReaderOnly: showOnFocus - ? css` - /* The :active selector is necessary for Safari which removes :focus when a button is pressed */ - &:not(:focus, :active, :focus-within) { - ${euiScreenReaderOnly()} - } - ` - : css(euiScreenReaderOnly()), -}); +export const euiScreenReaderOnlyStyles = { + euiScreenReaderOnly: css(euiScreenReaderOnly()), + + 'euiScreenReaderOnly-showOnFocus': css` + /* The :active selector is necessary for Safari which removes :focus when a button is pressed */ + &:not(:focus, :active, :focus-within) { + ${euiScreenReaderOnly()} + } + `, +}; diff --git a/src/components/accessibility/screen_reader_only/screen_reader_only.tsx b/src/components/accessibility/screen_reader_only/screen_reader_only.tsx index e46e5278db6..426e627c2c0 100644 --- a/src/components/accessibility/screen_reader_only/screen_reader_only.tsx +++ b/src/components/accessibility/screen_reader_only/screen_reader_only.tsx @@ -6,11 +6,11 @@ * Side Public License, v 1. */ -import { ReactElement, FunctionComponent } from 'react'; +import { ReactElement, FunctionComponent, useMemo } from 'react'; import classNames from 'classnames'; import { cloneElementWithCss } from '../../../services'; -import { euiScreenReaderOnlyStyles } from './screen_reader_only.styles'; +import { euiScreenReaderOnlyStyles as styles } from './screen_reader_only.styles'; export interface EuiScreenReaderOnlyProps { /** @@ -30,13 +30,15 @@ export const EuiScreenReaderOnly: FunctionComponent< > = ({ children, className, showOnFocus }) => { const classes = classNames(className, children.props.className); - const styles = euiScreenReaderOnlyStyles(showOnFocus); - const cssStyles = [styles.euiScreenReaderOnly]; - - const props = { - className: classes.length ? classes : undefined, - css: cssStyles, - }; + const props = useMemo( + () => ({ + className: classes.length ? classes : undefined, + css: showOnFocus + ? styles['euiScreenReaderOnly-showOnFocus'] + : styles.euiScreenReaderOnly, + }), + [classes, showOnFocus] + ); return cloneElementWithCss(children, props); }; diff --git a/src/components/resizable_container/__snapshots__/resizable_container.test.tsx.snap b/src/components/resizable_container/__snapshots__/resizable_container.test.tsx.snap index ad3e6f48762..65820a3e682 100644 --- a/src/components/resizable_container/__snapshots__/resizable_container.test.tsx.snap +++ b/src/components/resizable_container/__snapshots__/resizable_container.test.tsx.snap @@ -233,7 +233,7 @@ exports[`EuiResizableContainer can have toggleable panels 1`] = ` >