diff --git a/packages/pigment-css-react/src/processors/styled.ts b/packages/pigment-css-react/src/processors/styled.ts index 5b029f68..1168b9ae 100644 --- a/packages/pigment-css-react/src/processors/styled.ts +++ b/packages/pigment-css-react/src/processors/styled.ts @@ -310,8 +310,8 @@ export class StyledProcessor extends BaseProcessor { * which we can use to generate our styles. * Order of processing styles - * 1. CSS directly declared in styled call - * 2. CSS declared in theme object's styledOverrides * 3. Variants declared in styled call + * 2. CSS declared in theme object's styledOverrides * 3. Variants declared in theme object */ build(values: ValueCache): void { @@ -325,11 +325,17 @@ export class StyledProcessor extends BaseProcessor { ); // all the variant definitions are collected here so that we can // apply variant styles after base styles for more specific targetting. - const variantsAccumulator: VariantData[] = []; + let variantsAccumulator: VariantData[] = []; (this.styleArgs as ExpressionValue[]).forEach((styleArg) => { this.processStyle(values, styleArg, variantsAccumulator, themeImportIdentifier.name); }); + // Generate CSS for default variants first + variantsAccumulator.forEach((variant) => { + this.processVariant(variant); + }); + variantsAccumulator = []; this.processOverrides(values, variantsAccumulator); + // Generate CSS for variants declared in `styleOverrides`, if any variantsAccumulator.forEach((variant) => { this.processVariant(variant); }); diff --git a/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.input.js b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.input.js new file mode 100644 index 00000000..6871b998 --- /dev/null +++ b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.input.js @@ -0,0 +1,36 @@ +import { styled } from '@pigment-css/react'; + +const OutlinedInputInput = styled('input', { + name: 'MuiOutlinedInput', + slot: 'Input', +})({ + padding: '16.5px 14px', + variants: [ + { + props: { + size: 'small', + }, + style: { + padding: '8.5px 14px', + }, + }, + { + props: ({ ownerState }) => ownerState.multiline, + style: { + padding: 0, + }, + }, + { + props: ({ ownerState }) => ownerState.startAdornment, + style: { + paddingLeft: 0, + }, + }, + { + props: ({ ownerState }) => ownerState.endAdornment, + style: { + paddingRight: 0, + }, + }, + ], +}); diff --git a/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.css b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.css new file mode 100644 index 00000000..cdd4ea1e --- /dev/null +++ b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.css @@ -0,0 +1,21 @@ +.o1ei225m { + padding: 16.5px 14px; +} +.o1ei225m-1 { + padding: 8.5px 14px; +} +.o1ei225m-2 { + padding: 0; +} +.o1ei225m-3 { + padding-left: 0; +} +.o1ei225m-4 { + padding-right: 0; +} +.o1ei225m-5 { + padding-left: 10px; +} +.o1ei225m-6 { + padding: 9px 14.5px; +} diff --git a/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.js b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.js new file mode 100644 index 00000000..49d4e96d --- /dev/null +++ b/packages/pigment-css-react/tests/styled/fixtures/styled-theme-styleOverrides2.output.js @@ -0,0 +1,34 @@ +import { styled as _styled } from '@pigment-css/react'; +import _theme from '@pigment-css/react/theme'; +const OutlinedInputInput = /*#__PURE__*/ _styled('input', { + name: 'MuiOutlinedInput', + slot: 'Input', +})({ + classes: ['o1ei225m', 'o1ei225m-5'], + variants: [ + { + props: { + size: 'small', + }, + className: 'o1ei225m-1', + }, + { + props: ({ ownerState }) => ownerState.multiline, + className: 'o1ei225m-2', + }, + { + props: ({ ownerState }) => ownerState.startAdornment, + className: 'o1ei225m-3', + }, + { + props: ({ ownerState }) => ownerState.endAdornment, + className: 'o1ei225m-4', + }, + { + props: { + size: 'small', + }, + className: 'o1ei225m-6', + }, + ], +}); diff --git a/packages/pigment-css-react/tests/styled/styled.test.tsx b/packages/pigment-css-react/tests/styled/styled.test.tsx index 01db740d..81eb252e 100644 --- a/packages/pigment-css-react/tests/styled/styled.test.tsx +++ b/packages/pigment-css-react/tests/styled/styled.test.tsx @@ -105,4 +105,38 @@ describe('Pigment CSS - styled', () => { expect(output.js).to.equal(fixture.js); expect(output.css).to.equal(fixture.css); }); + + it('should work with theme styleOverrides and variants', async () => { + const { output, fixture } = await runTransformation( + path.join(__dirname, 'fixtures/styled-theme-styleOverrides2.input.js'), + { + themeArgs: { + theme: { + components: { + MuiOutlinedInput: { + styleOverrides: { + input: { + paddingLeft: 10, + variants: [ + { + props: { + size: 'small', + }, + style: { + padding: '9px 14.5px', + }, + }, + ], + }, + }, + }, + }, + }, + }, + }, + ); + + expect(output.js).to.equal(fixture.js); + expect(output.css).to.equal(fixture.css); + }); });