Skip to content

Commit

Permalink
[mixins] Memoize useEuiButtonFocusCSS
Browse files Browse the repository at this point in the history
+ fix animation relying on existing Sass CSS instead of generating actual keyframes from Emotion
  • Loading branch information
cee-chen committed Feb 22, 2024
1 parent c2dbd57 commit 3a88b12
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`useEuiButtonFocusCSS 1`] = `
Object {
"map": undefined,
"name": "1s8jae7-focusCSS",
"next": Object {
"name": "animation-70pju1",
"next": undefined,
"styles": "@keyframes animation-70pju1{
50% {
transform: translateY(1px);
}
}",
},
"styles": "@media screen and (prefers-reduced-motion: no-preference){transition:transform 250ms ease-in-out,background-color 250ms ease-in-out;&:hover:not(:disabled){transform:translateY(-1px);}&:focus{animation:animation-70pju1 250ms cubic-bezier(.34, 1.61, .7, 1);}&:active:not(:disabled){transform:translateY(1px);}};",
"toString": [Function],
}
`;
16 changes: 16 additions & 0 deletions src/themes/amsterdam/global_styling/mixins/button.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { renderHook } from '@testing-library/react';

import { useEuiButtonFocusCSS } from './button';

test('useEuiButtonFocusCSS', () => {
const { result } = renderHook(() => useEuiButtonFocusCSS());
expect(result.current).toMatchSnapshot();
});
22 changes: 17 additions & 5 deletions src/themes/amsterdam/global_styling/mixins/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { css } from '@emotion/react';
import { css, keyframes } from '@emotion/react';
import { euiBackgroundColor, euiCanAnimate } from '../../../../global_styling';
import {
hexToRgb,
Expand All @@ -17,6 +17,7 @@ import {
transparentize,
useEuiTheme,
UseEuiTheme,
useEuiMemoizedStyles,
} from '../../../../services';

export const BUTTON_COLORS = [
Expand Down Expand Up @@ -219,10 +220,16 @@ export const useEuiButtonColorCSS = (options: _EuiButtonOptions = {}) => {
* Creates the translate animation when button is in focus.
* @returns string
*/
export const useEuiButtonFocusCSS = () => {
const { euiTheme } = useEuiTheme();
export const useEuiButtonFocusCSS = () =>
useEuiMemoizedStyles<any>(euiButtonFocusCSS);

return `
const euiButtonFocusAnimation = keyframes`
50% {
transform: translateY(1px);
}
`;
const euiButtonFocusCSS = ({ euiTheme }: UseEuiTheme) => {
const focusCSS = css`
${euiCanAnimate} {
transition: transform ${euiTheme.animation.normal} ease-in-out,
background-color ${euiTheme.animation.normal} ease-in-out;
Expand All @@ -232,7 +239,7 @@ export const useEuiButtonFocusCSS = () => {
}
&:focus {
animation: euiButtonActive ${euiTheme.animation.normal}
animation: ${euiButtonFocusAnimation} ${euiTheme.animation.normal}
${euiTheme.animation.bounce};
}
Expand All @@ -241,6 +248,11 @@ export const useEuiButtonFocusCSS = () => {
}
}
`;
// Remove the auto-generated label.
// We could typically avoid a label by using a plain string `` instead of css``,
// but we need css`` for keyframes`` to work for the focus animation
focusCSS.styles = focusCSS.styles.replace('label:focusCSS;', '');
return focusCSS;
};

/**
Expand Down

0 comments on commit 3a88b12

Please sign in to comment.