From 9d405fe413e37f5793114d5895d21189c571d34e Mon Sep 17 00:00:00 2001 From: Cee Chen Date: Wed, 6 Mar 2024 13:45:14 -0800 Subject: [PATCH] Memoize skeleton text styles + line generation --- src/components/skeleton/skeleton_text.tsx | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/components/skeleton/skeleton_text.tsx b/src/components/skeleton/skeleton_text.tsx index c4f046c34c1..4f3bfc8145d 100644 --- a/src/components/skeleton/skeleton_text.tsx +++ b/src/components/skeleton/skeleton_text.tsx @@ -6,11 +6,11 @@ * Side Public License, v 1. */ -import React, { FunctionComponent, HTMLAttributes } from 'react'; +import React, { FunctionComponent, HTMLAttributes, useMemo } from 'react'; import classNames from 'classnames'; +import { useEuiMemoizedStyles } from '../../services'; import { CommonProps } from '../common'; -import { useEuiTheme } from '../../services'; import { TextSize } from '../text/text'; import { EuiSkeletonLoading, _EuiSkeletonAriaProps } from './skeleton_loading'; @@ -45,14 +45,19 @@ export const EuiSkeletonText: FunctionComponent = ({ children, ...rest }) => { - const euiTheme = useEuiTheme(); - const styles = euiSkeletonTextStyles(euiTheme); - const lineCssStyles = [styles.euiSkeletonText, styles[size]]; + const styles = useEuiMemoizedStyles(euiSkeletonTextStyles); + const cssStyles = useMemo( + () => [styles.euiSkeletonText, styles[size]], + [styles, size] + ); - const lineElements = []; - for (let i = 0; i < lines; i++) { - lineElements.push(); - } + const lineElements = useMemo(() => { + const lineElements = []; + for (let i = 0; i < lines; i++) { + lineElements.push(); + } + return lineElements; + }, [lines, cssStyles]); return (