From d15c9354343f73eca3e014b581ee4dfeb6a60e3c Mon Sep 17 00:00:00 2001 From: Kevin Qualters Date: Tue, 21 Nov 2023 18:49:14 +0000 Subject: [PATCH] Fix skeleton text performance --- src/components/skeleton/skeleton_text.tsx | 80 ++++++++++++----------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/components/skeleton/skeleton_text.tsx b/src/components/skeleton/skeleton_text.tsx index c4f046c34c1..1e9f08c12a9 100644 --- a/src/components/skeleton/skeleton_text.tsx +++ b/src/components/skeleton/skeleton_text.tsx @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import React, { FunctionComponent, HTMLAttributes } from 'react'; +import React, { FunctionComponent, HTMLAttributes, useMemo, memo } from 'react'; import classNames from 'classnames'; import { CommonProps } from '../common'; @@ -32,42 +32,48 @@ export type EuiSkeletonTextProps = CommonProps & size?: TextSize; }; -export const EuiSkeletonText: FunctionComponent = ({ - isLoading = true, - lines = 3, - size = 'm', - className, - contentAriaLabel, - announceLoadingStatus, - announceLoadedStatus, - ariaLiveProps, - ariaWrapperProps, - children, - ...rest -}) => { - const euiTheme = useEuiTheme(); - const styles = euiSkeletonTextStyles(euiTheme); - const lineCssStyles = [styles.euiSkeletonText, styles[size]]; +export const EuiSkeletonText: FunctionComponent = memo( + ({ + isLoading = true, + lines = 3, + size = 'm', + className, + contentAriaLabel, + announceLoadingStatus, + announceLoadedStatus, + ariaLiveProps, + ariaWrapperProps, + children, + ...rest + }) => { + const euiTheme = useEuiTheme(); - const lineElements = []; - for (let i = 0; i < lines; i++) { - lineElements.push(); - } + const lineElements = useMemo(() => { + const styles = euiSkeletonTextStyles(euiTheme); + const lineCssStyles = [styles.euiSkeletonText, styles[size]]; - return ( - - {lineElements} - + const lineElements = []; + for (let i = 0; i < lines; i++) { + lineElements.push(); } - loadedContent={children || ''} - contentAriaLabel={contentAriaLabel} - announceLoadingStatus={announceLoadingStatus} - announceLoadedStatus={announceLoadedStatus} - ariaLiveProps={ariaLiveProps} - {...ariaWrapperProps} - /> - ); -}; + return lineElements; + }, [lines, size, euiTheme]); + + return ( + + {lineElements} + + } + loadedContent={children || ''} + contentAriaLabel={contentAriaLabel} + announceLoadingStatus={announceLoadingStatus} + announceLoadedStatus={announceLoadedStatus} + ariaLiveProps={ariaLiveProps} + {...ariaWrapperProps} + /> + ); + } +);