Skip to content

Commit

Permalink
[mixins] Memoize typography hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Feb 23, 2024
1 parent 33e8448 commit 04da2e6
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/global_styling/mixins/_typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ import {
euiFontSizeFromScale,
_FontScaleOptions,
} from '../functions/typography';
import { useEuiTheme, UseEuiTheme } from '../../services/theme/hooks';
import { _EuiThemeFontScale } from '../variables/typography';
import {
useEuiMemoizedStyles,
useEuiTheme,
UseEuiTheme,
} from '../../services/theme';
import {
_EuiThemeFontScale,
EuiThemeFontScales,
} from '../variables/typography';
import { logicalCSS } from '../functions';

export type EuiThemeFontSize = {
Expand All @@ -39,7 +46,22 @@ export const useEuiFontSize = (
options?: _FontScaleOptions
): EuiThemeFontSize => {
const euiTheme = useEuiTheme();
return euiFontSize(euiTheme, scale, options);
const memoizedFontSizes = useEuiMemoizedStyles(euiFontSizes);

return !options
? memoizedFontSizes[scale]
: euiFontSize(euiTheme, scale, options);
};
// Memomize a basic set of font sizes. We unfortunately can't
// memoize all possible options, there's too many permutations
const euiFontSizes = (euiThemeContext: UseEuiTheme) => {
return EuiThemeFontScales.reduce(
(map, scale) => ({
...map,
[scale]: euiFontSize(euiThemeContext, scale),
}),
{} as Record<_EuiThemeFontScale, EuiThemeFontSize>
);
};

/**
Expand Down Expand Up @@ -71,7 +93,5 @@ export const euiTextTruncate = (
export const euiNumberFormat = ({ euiTheme }: UseEuiTheme) => `
font-feature-settings: ${euiTheme.font.featureSettings}, 'tnum' 1;
`;
export const useEuiNumberFormat = (): string => {
const euiTheme = useEuiTheme();
return euiNumberFormat(euiTheme);
};
export const useEuiNumberFormat = (): string =>
useEuiMemoizedStyles<any>(euiNumberFormat);

0 comments on commit 04da2e6

Please sign in to comment.