Skip to content

Commit

Permalink
Memoize skeleton text styles + line generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Mar 7, 2024
1 parent 67adfe0 commit 9d405fe
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/components/skeleton/skeleton_text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,14 +45,19 @@ export const EuiSkeletonText: FunctionComponent<EuiSkeletonTextProps> = ({
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(<span key={i} css={lineCssStyles} />);
}
const lineElements = useMemo(() => {
const lineElements = [];
for (let i = 0; i < lines; i++) {
lineElements.push(<span key={i} css={cssStyles} />);
}
return lineElements;
}, [lines, cssStyles]);

return (
<EuiSkeletonLoading
Expand Down

0 comments on commit 9d405fe

Please sign in to comment.