Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ae7f4d9

Browse files
committed
feat(keyframes*): Added @Keyframes at global
1 parent b0358e5 commit ae7f4d9

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import type { CustomCSSProperties } from './custom-css-properties';
22

33
type JSXType = keyof JSX.IntrinsicElements | '*';
4-
54
type HTMLType = {
65
[K in JSXType]?: CustomCSSProperties;
76
};
87

98
type ClassName = `.${string}`;
10-
119
type ClassNameType = {
1210
[K in ClassName]?: CustomCSSProperties;
1311
};
1412

1513
type Attribute = `${string}[${string}]${string}`;
16-
1714
type AttributeType = {
1815
[K in Attribute]?: CustomCSSProperties;
1916
};
2017

2118
type Consecutive = `${JSXType} ${string}`;
22-
2319
type ConsecutiveType = {
2420
[K in Consecutive]?: CustomCSSProperties;
2521
};
2622

2723
type Pseudo = `${JSXType}:${string}`;
28-
2924
type PseudoType = {
3025
[K in Pseudo]?: CustomCSSProperties;
3126
};
3227

33-
export type CustomHTMLType = HTMLType | PseudoType | ClassNameType | AttributeType | ConsecutiveType;
28+
type KeyframeSelector = 'from' | 'to' | `${number}%`;
29+
type KeyframesDefinition = {
30+
[K in KeyframeSelector]?: CustomCSSProperties;
31+
};
32+
type AtKeyframes = {
33+
[K in `@keyframes ${string}`]: KeyframesDefinition;
34+
};
35+
36+
export type CustomHTMLType = HTMLType | ClassNameType | AttributeType | ConsecutiveType | PseudoType | AtKeyframes;

src/_internal/utils/sheet-compiler.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,26 @@ export function sheetCompiler(object: ClassesObjectType | CustomHTMLType, base62
3838
const isPseudoOrMediaClass = property.startsWith('@') ? isClassInc || isElementInc : classIndex || elementIndex;
3939
let colon = '';
4040

41-
if (typeof value === 'string' || typeof value === 'number') {
41+
if (property.startsWith('@keyframes')) {
42+
let keyframesRule = `${property} {\n`;
43+
for (const keyframe in value as PropertyType) {
44+
if (Object.prototype.hasOwnProperty.call(value, keyframe)) {
45+
const keyframeValue = (value as PropertyType)[keyframe];
46+
keyframesRule += `${innerIndent}${keyframe} {\n`;
47+
if (typeof keyframeValue === 'object' && keyframeValue !== null) {
48+
for (const prop in keyframeValue) {
49+
if (Object.prototype.hasOwnProperty.call(keyframeValue, prop)) {
50+
const propValue = keyframeValue[prop];
51+
keyframesRule += `${innerIndent} ${camelToKebabCase(prop)}: ${propValue};\n`;
52+
}
53+
}
54+
}
55+
keyframesRule += `${innerIndent}}\n`;
56+
}
57+
}
58+
keyframesRule += `}\n`;
59+
cssRule += keyframesRule;
60+
} else if (typeof value === 'string' || typeof value === 'number') {
4261
const CSSProp = camelToKebabCase(property);
4362
const applyValue = applyCssValue(value, CSSProp);
4463
cssRule += `${bigIndent ? ' ' : ' '}${CSSProp}: ${applyValue};\n`;
@@ -106,7 +125,10 @@ export function sheetCompiler(object: ClassesObjectType | CustomHTMLType, base62
106125
if (Object.prototype.hasOwnProperty.call(styles, property)) {
107126
const value = (styles as PropertyType)[property] as unknown as PropertyType;
108127
if (isClassesObjectType(value)) {
109-
if (property.startsWith('@media')) {
128+
if (property.startsWith('@keyframes')) {
129+
const keyframesStyles = stringConverter('', { [property]: value }, currentIndentLevel);
130+
styleSheet += '\n' + Object.values(keyframesStyles)[0];
131+
} else if (property.startsWith('@media')) {
110132
bigIndent = true;
111133
const mediaStyles = createStyles(value, currentIndentLevel + 1);
112134
styleSheet += `\n${indent}${property} {${mediaStyles}${indent}}\n`;

0 commit comments

Comments
 (0)