Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix keyframes and split tests
  • Loading branch information
siriwatknp committed Mar 8, 2024
commit 453a101e275204a6d16d7d97f9a2f4b00364fb5c
6 changes: 6 additions & 0 deletions packages/pigment-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
"build"
]
},
"test:update": {
"cache": false,
"dependsOn": [
"build"
]
},
"test:ci": {
"cache": false,
"dependsOn": [
Expand Down
36 changes: 12 additions & 24 deletions packages/pigment-react/src/processors/keyframes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Expression, TemplateElement } from '@babel/types';
import type { Expression } from '@babel/types';
import type {
CallParam,
TemplateParam,
Expand All @@ -7,7 +7,7 @@ import type {
ValueCache,
} from '@wyw-in-js/processor-utils';
import { serializeStyles, Interpolation } from '@emotion/serialize';
import { type Replacements, type Rules, ValueType, type ExpressionValue } from '@wyw-in-js/shared';
import { type Replacements, type Rules, ValueType } from '@wyw-in-js/shared';
import type { CSSInterpolation } from '@emotion/css';
import { validateParams } from '@wyw-in-js/processor-utils';
import BaseProcessor from './base-processor';
Expand All @@ -34,27 +34,13 @@ export class KeyframesProcessor extends BaseProcessor {
);

const [, callParams] = params;
if (callParams[0] === 'call') {
this.dependencies.push(callParams[1]);

const [firstArg, ...restArgs] = callParams.slice(1).flat() as (
| ExpressionValue
| TemplateElement
)[];
if ('kind' in firstArg && firstArg.kind === ValueType.LAZY) {
restArgs.forEach((arg) => {
if ('kind' in arg) {
this.dependencies.push(arg);
}
});
const [, ...callParamsRest] = callParams;

callParamsRest.flat().forEach((item) => {
if ('kind' in item) {
this.dependencies.push(item);
}
} else if (callParams[0] === 'template') {
callParams[1].forEach((element) => {
if ('kind' in element && element.kind !== ValueType.CONST) {
this.dependencies.push(element);
}
});
}
});
this.callParam = callParams;
}

Expand All @@ -69,7 +55,7 @@ export class KeyframesProcessor extends BaseProcessor {
this.handleTemplate(this.callParam, values);
} else if (isTaggedTemplateCall(callArgs, values)) {
const { themeArgs } = this.options as IOptions;
this.generateArtifacts(`${resolveTaggedTemplate(callArgs, values, themeArgs)}`);
this.generateArtifacts([resolveTaggedTemplate(callArgs, values, themeArgs)]);
} else {
this.handleCall(this.callParam, values);
}
Expand Down Expand Up @@ -116,7 +102,9 @@ export class KeyframesProcessor extends BaseProcessor {

generateArtifacts(styleObjOrTaggged: CSSInterpolation | string[], ...args: Primitive[]) {
const { styles } = serializeStyles(
[styleObjOrTaggged as Interpolation<{}>, args],
args.length > 0
? [styleObjOrTaggged as Interpolation<{}>, ...args]
: [styleObjOrTaggged as Interpolation<{}>],
Comment on lines +103 to +105
Copy link
Member Author

@siriwatknp siriwatknp Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is required to prevent ILLEGAL_ESCAPE_SEQUENCE_ERROR from emotion because serializeStyles assumes that [] is an expression.

cache.registered,
);
const cssText = `@keyframes {${styles}}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { keyframes } from '@pigment-css/react';

const green = 'green';

const gradientKeyframe = keyframes(({ theme }) => ({
'0%': {
background: theme.palette.primary.main,
},
'50%': {
background: green,
},
'100%': {
background: theme.palette.secondary.main,
},
}));

const gradientKeyframe2 = keyframes`
0% {
background: ${({ theme }) => theme.palette.primary.main};
}

50% {
background: ${green};
}

100% {
background: ${({ theme }) => theme.palette.secondary.main};
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@keyframes g2c7x3u {
0% {
background: red;
}
50% {
background: green;
}
100% {
background: blue;
}
}
@keyframes gb35t65 {
0% {
background: red;
}
50% {
background: green;
}
100% {
background: blue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const gradientKeyframe = 'g2c7x3u';
const gradientKeyframe2 = 'gb35t65';
10 changes: 10 additions & 0 deletions packages/pigment-react/tests/keyframes/fixtures/keyframes.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ const rotateKeyframe = keyframes({
transform: 'rotate(0deg)',
},
});

const rotateKeyframe2 = keyframes`
from {
transform: rotate(360deg);
}

to {
transform: rotate(0deg);
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
transform: rotate(0deg);
}
}
@keyframes r3amm75 {
from {
transform: rotate(360deg);
}
to {
transform: rotate(0deg);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const rotateKeyframe = 'r14c1bqo';
const rotateKeyframe2 = 'r3amm75';
23 changes: 23 additions & 0 deletions packages/pigment-react/tests/keyframes/keyframes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,27 @@ describe('Pigment CSS - keyframes', () => {
expect(output.js).to.equal(fixture.js);
expect(output.css).to.equal(fixture.css);
});

it('should transform correctly with theme', async () => {
const { output, fixture } = await runTransformation(
path.join(__dirname, 'fixtures/keyframes-theme.input.js'),
{
themeArgs: {
theme: {
palette: {
primary: {
main: 'red',
},
secondary: {
main: 'blue',
},
},
},
},
},
);

expect(output.js).to.equal(fixture.js);
expect(output.css).to.equal(fixture.css);
});
});
33 changes: 33 additions & 0 deletions packages/pigment-react/tests/styled/fixtures/styled-theme.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { styled, keyframes } from '@pigment-css/react';

const rotateKeyframe = keyframes({
from: {
transform: 'rotate(360deg)',
},
to: {
transform: 'rotate(0deg)',
},
});

const Component = styled.div(({ theme }) => ({
color: theme.palette.primary.main,
animation: `${rotateKeyframe} 2s ease-out 0s infinite`,
}));

const SliderRail = styled('span', {
name: 'MuiSlider',
slot: 'Rail',
})`
display: none;
position: absolute;
border-radius: inherit;
background-color: currentColor;
opacity: 0.38;
font-size: ${({ theme }) => theme.size.font.h1};
`;

const SliderRail2 = styled.span`
display: block;
opacity: 0.38;
font-size: ${({ theme }) => theme.size.font.h1};
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@keyframes r3sp8jf {
from {
transform: rotate(360deg);
}
to {
transform: rotate(0deg);
}
}
.clqufod {
color: red;
animation: r3sp8jf 2s ease-out 0s infinite;
}
.s1fopuc2 {
display: none;
position: absolute;
border-radius: inherit;
background-color: currentColor;
opacity: 0.38;
font-size: 3rem;
}
.s1fopuc2-1 {
font-size: 1.5rem;
}
.s1tggtaa {
display: block;
opacity: 0.38;
font-size: 3rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { styled as _styled3 } from '@pigment-css/react';
import { styled as _styled2 } from '@pigment-css/react';
import { styled as _styled } from '@pigment-css/react';
import _theme from '@pigment-css/react/theme';
const Component = /*#__PURE__*/ _styled('div')({
classes: ['clqufod'],
});
const SliderRail = /*#__PURE__*/ _styled2('span', {
name: 'MuiSlider',
slot: 'Rail',
})({
classes: ['s1fopuc2', 's1fopuc2-1'],
});
const SliderRail2 = /*#__PURE__*/ _styled3('span')({
classes: ['s1tggtaa'],
});
30 changes: 13 additions & 17 deletions packages/pigment-react/tests/styled/fixtures/styled.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ const rotateKeyframe = keyframes({
},
});

const Component = styled.div(({ theme }) => ({
color: theme.palette.primary.main,
const Component = styled.div({
color: '#ff5252',
animation: `${rotateKeyframe} 2s ease-out 0s infinite`,
}));
});

const SliderRail = styled('span', {
name: 'MuiSlider',
slot: 'Rail',
})`
display: none;
position: absolute;
border-radius: inherit;
background-color: currentColor;
opacity: 0.38;
font-size: ${({ theme }) => theme.size.font.h1};
const Component2 = styled.div`
color: red;
&:has(.foo) {
color: blue;
}
`;

const SliderRail2 = styled.span`
display: block;
opacity: 0.38;
font-size: ${({ theme }) => theme.size.font.h1};
const Component3 = styled('div')`
color: red;
&:has(.foo) {
color: blue;
}
`;
24 changes: 10 additions & 14 deletions packages/pigment-react/tests/styled/fixtures/styled.output.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
}
}
.c1vtarpi {
color: red;
color: #ff5252;
animation: r1419f2q 2s ease-out 0s infinite;
}
.s1sjy0ja {
display: none;
position: absolute;
border-radius: inherit;
background-color: currentColor;
opacity: 0.38;
font-size: 3rem;
.c1sjy0ja {
color: red;
}
.c1sjy0ja:has(.foo) {
color: blue;
}
.s1sjy0ja-1 {
font-size: 3rem;
.c6hrafw {
color: red;
}
.s6hrafw {
display: block;
opacity: 0.38;
font-size: 3rem;
.c6hrafw:has(.foo) {
color: blue;
}
11 changes: 4 additions & 7 deletions packages/pigment-react/tests/styled/fixtures/styled.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import _theme from '@pigment-css/react/theme';
const Component = /*#__PURE__*/ _styled('div')({
classes: ['c1vtarpi'],
});
const SliderRail = /*#__PURE__*/ _styled2('span', {
name: 'MuiSlider',
slot: 'Rail',
})({
classes: ['s1sjy0ja', 's1sjy0ja-1'],
const Component2 = /*#__PURE__*/ _styled2('div')({
classes: ['c1sjy0ja'],
});
const SliderRail2 = /*#__PURE__*/ _styled3('span')({
classes: ['s6hrafw'],
const Component3 = /*#__PURE__*/ _styled3('div')({
classes: ['c6hrafw'],
});
Loading