Skip to content

Commit

Permalink
[EuiButtonIcon] memoize empty hover color styles
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Feb 22, 2024
1 parent bb99273 commit b1e9e8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
27 changes: 15 additions & 12 deletions src/components/button/button_icon/button_icon.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

import { css } from '@emotion/react';
import { css, type SerializedStyles } from '@emotion/react';

import { UseEuiTheme } from '../../../services';
import { logicalSizeCSS } from '../../../global_styling';
import {
BUTTON_COLORS,
_EuiButtonColor,
euiButtonEmptyColor,
euiButtonSizeMap,
Expand Down Expand Up @@ -55,14 +56,16 @@ export const euiButtonIconStyles = (euiThemeContext: UseEuiTheme) => {
};
};

export const _emptyHoverStyles = (
euiThemeContext: UseEuiTheme,
color: _EuiButtonColor
) => {
return css`
&:hover {
background-color: ${euiButtonEmptyColor(euiThemeContext, color)
.backgroundColor};
}
`;
};
export const _emptyHoverStyles = (euiThemeContext: UseEuiTheme) =>
BUTTON_COLORS.reduce(
(styles, color) => ({
...styles,
[color]: css`
&:hover {
background-color: ${euiButtonEmptyColor(euiThemeContext, color)
.backgroundColor};
}
`,
}),
{} as Record<_EuiButtonColor, SerializedStyles>
);
10 changes: 3 additions & 7 deletions src/components/button/button_icon/button_icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import React, {
} from 'react';
import classNames from 'classnames';

import { getSecureRelForTarget, useEuiTheme, useEuiMemoizedStyles } from '../../../services';
import { getSecureRelForTarget, useEuiMemoizedStyles } from '../../../services';
import {
CommonProps,
ExclusiveUnion,
Expand Down Expand Up @@ -118,7 +118,6 @@ export const EuiButtonIcon: FunctionComponent<Props> = ({
isLoading,
...rest
}) => {
const euiThemeContext = useEuiTheme();
const isDisabled = isButtonDisabled({
isDisabled: _isDisabled || disabled,
href,
Expand All @@ -137,18 +136,15 @@ export const EuiButtonIcon: FunctionComponent<Props> = ({

const buttonColorStyles = useEuiButtonColorCSS({ display });
const buttonFocusStyle = useEuiButtonFocusCSS();
const emptyHoverStyles =
display === 'empty' &&
!isDisabled &&
_emptyHoverStyles(euiThemeContext, color);
const emptyHoverStyles = useEuiMemoizedStyles(_emptyHoverStyles);

const styles = useEuiMemoizedStyles(euiButtonIconStyles);
const cssStyles = [
styles.euiButtonIcon,
styles[size],
buttonColorStyles[isDisabled ? 'disabled' : color],
buttonFocusStyle,
emptyHoverStyles,
display === 'empty' && !isDisabled && emptyHoverStyles[color],
isDisabled && styles.isDisabled,
];

Expand Down

0 comments on commit b1e9e8c

Please sign in to comment.