Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigment-css] Fix keyframes serialize styles error #41395

Merged
merged 23 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
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
6 changes: 6 additions & 0 deletions packages/pigment-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
"build"
]
},
"test:update": {
"cache": false,
"dependsOn": [
"build"
]
},
"test:ci": {
"cache": false,
"dependsOn": [
Expand Down
4 changes: 3 additions & 1 deletion packages/pigment-react/src/processors/keyframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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.vars ?? theme).palette.primary.main,
animation: `${rotateKeyframe} 2s ease-out 0s infinite`,
}));

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

const SliderRail2 = styled.span`
display: block;
opacity: 0.38;
font-size: ${({ theme }) => (theme.vars ?? 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: block;
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.vars ?? theme).palette.primary.main,
const Component = styled.div({
color: '#ff5252',
animation: `${rotateKeyframe} 2s ease-out 0s infinite`,
}));
});

export const SliderRail = styled('span', {
name: 'MuiSlider',
slot: 'Rail',
})`
display: block;
position: absolute;
border-radius: inherit;
background-color: currentColor;
opacity: 0.38;
font-size: ${({ theme }) => (theme.vars ?? 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.vars ?? 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: block;
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'],
});
export 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'],
});
53 changes: 30 additions & 23 deletions packages/pigment-react/tests/styled/styled.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import path from 'node:path';
import { runTransformation, expect } from '../testUtils';

const theme = {
palette: {
primary: {
main: 'red',
},
},
size: {
font: {
h1: '3rem',
},
},
components: {
MuiSlider: {
styleOverrides: {
rail: {
fontSize: '3rem',
},
},
},
},
};

describe('Pigment CSS - styled', () => {
it('basics', async () => {
const { output, fixture } = await runTransformation(
path.join(__dirname, 'fixtures/styled.input.js'),
);

expect(output.js).to.equal(fixture.js);
expect(output.css).to.equal(fixture.css);
});

it('should work with theme', async () => {
const { output, fixture } = await runTransformation(
path.join(__dirname, 'fixtures/styled-theme.input.js'),
{
themeArgs: {
theme,
theme: {
palette: {
primary: {
main: 'red',
},
},
size: {
font: {
h1: '3rem',
},
},
components: {
MuiSlider: {
styleOverrides: {
rail: {
fontSize: '1.5rem',
},
},
},
},
},
},
},
);
Expand Down
Loading