Skip to content

Commit 162210d

Browse files
authoredFeb 18, 2025··
Improve the types to the keyframes() function to handle CSS variables. (#1803)
1 parent 75b34ed commit 162210d

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed
 

‎.changeset/long-foxes-pay.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/react': patch
3+
---
4+
5+
Improve the types to the `keyframes()` function to handle CSS variables.

‎packages/react/src/keyframes/__tests__/index.test.tsx

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @jsxImportSource @compiled/react */
22
// eslint-disable-next-line import/no-extraneous-dependencies
3-
import { keyframes, styled } from '@compiled/react';
3+
import { keyframes, styled, css } from '@compiled/react';
44
import { render } from '@testing-library/react';
55

66
import defaultFadeOut, { namedFadeOut, fadeOut as shadowedFadeOut } from '../__fixtures__';
@@ -180,6 +180,32 @@ describe('keyframes', () => {
180180
`"@keyframes korwhog{0%{opacity:1}to{opacity:0}}"`
181181
);
182182
});
183+
184+
it('containing css variables', () => {
185+
const variable = '--opacity';
186+
const fadeOut = keyframes({
187+
from: {
188+
[variable]: 1,
189+
},
190+
to: {
191+
'--opacity': 0,
192+
},
193+
});
194+
const styles = css({
195+
animationName: fadeOut,
196+
opacity: `var(${variable})`,
197+
});
198+
199+
const { getByText } = render(<div css={styles}>hello world</div>);
200+
201+
expect(getByText('hello world')).toHaveCompiledCss({
202+
animationName: 'k1sm7npi',
203+
opacity: 'var(--opacity)',
204+
});
205+
expect(getKeyframe('k1sm7npi')).toMatchInlineSnapshot(
206+
`"@keyframes k1sm7npi{0%{--opacity:1px}to{--opacity:0}}"`
207+
);
208+
});
183209
});
184210
});
185211

‎packages/react/src/keyframes/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import type { BasicTemplateInterpolations, CSSProps } from '../types';
22
import { createSetupError } from '../utils/error';
33

4-
export type KeyframeSteps = string | Record<string, CSSProps<void>>;
4+
export type KeyframeSteps =
5+
// `string` would just be an arbitrary CSS-like string such as `keyframes('from{opacity:1;}to{opacity:0;}')`
6+
| string
7+
| Record<
8+
'from' | 'to' | string,
9+
// We allow basically all CSSProperties here and CSS Variables mapping to their values.
10+
// eg. `{ display: 'block', '--var': 'block' }` — but likely it just becomes `'--var': any`
11+
CSSProps<void> | { [key: `--${string}`]: CSSProps<void>[keyof CSSProps<void>] }
12+
>;
513

614
/**
715
* ## Keyframes

0 commit comments

Comments
 (0)
Please sign in to comment.