Skip to content

Commit

Permalink
[react] Add createExtendSxProp for Material UI integration (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Jun 4, 2024
1 parent 3c1de03 commit df6e7ef
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Object.defineProperty(exports, '__esModule', {
value: true,
});

exports.default = require('../processors/createExtendSxProp').CreateExtendSxPropProcessor;
3 changes: 2 additions & 1 deletion packages/pigment-css-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"keyframes": "./exports/keyframes.js",
"generateAtomics": "./exports/generateAtomics.js",
"css": "./exports/css.js",
"createUseThemeProps": "./exports/createUseThemeProps.js"
"createUseThemeProps": "./exports/createUseThemeProps.js",
"internal_createExtendSxProp": "./exports/internal_createExtendSxProp.js"
}
},
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/pigment-css-react/src/createExtendSxProp.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function createExtendSxProp(): <T extends Record<string, any>>(props: T) => T;
12 changes: 12 additions & 0 deletions packages/pigment-css-react/src/createExtendSxProp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Runtime function for creating `extendSxProp`.
*
* This is a placeholder for the runtime function for replacing the `extendSxProp` in Material UI
* to reduce bundle size only. DO NOT use it in your codebase.
*/
function extendSxProp(props) {
return props;
}
export default function createExtendSxProp() {
return extendSxProp;
}
1 change: 1 addition & 0 deletions packages/pigment-css-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { default as keyframes } from './keyframes';
export { generateAtomics, atomics } from './generateAtomics';
export { default as css } from './css';
export { default as createUseThemeProps } from './createUseThemeProps';
export { default as internal_createExtendSxProp } from './createExtendSxProp';
export { default as Box } from './Box';
41 changes: 41 additions & 0 deletions packages/pigment-css-react/src/processors/createExtendSxProp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
validateParams,
type Expression,
type Params,
type TailProcessorParams,
} from '@wyw-in-js/processor-utils';
import BaseProcessor from './base-processor';

export class CreateExtendSxPropProcessor extends BaseProcessor {
constructor(params: Params, ...args: TailProcessorParams) {
super([params[0]], ...args);
validateParams(params, ['callee', ['call']], `Invalid use of ${this.tagSource.imported} tag.`);
}

// eslint-disable-next-line class-methods-use-this
build(): void {}

doEvaltimeReplacement(): void {
this.replacer(this.value, false);
}

get value(): Expression {
return this.astService.nullLiteral();
}

doRuntimeReplacement(): void {
const t = this.astService;

const extendSxPropImportIdentifier = t.addNamedImport(
this.tagSource.imported,
process.env.PACKAGE_NAME as string,
);

this.replacer(t.callExpression(extendSxPropImportIdentifier, []), true);
}

public override get asSelector(): string {
// For completeness, this is not intended to be used.
return `.${this.className}`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import path from 'node:path';
import { expect } from 'chai';
import { runTransformation } from '../testUtils';

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

expect(output.js).to.equal(fixture.js);
expect(output.css).to.equal(fixture.css);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { internal_createExtendSxProp } from '../zero-styled';

const extendSxProp = internal_createExtendSxProp();

export default function Typography(inProps) {
const props = extendSxProp(inProps);
return <div {...props} />;
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { internal_createExtendSxProp as _internal_createExtendSxProp } from '@pigment-css/react';
const extendSxProp = /*#__PURE__*/ _internal_createExtendSxProp();
export default function Typography(inProps) {
const props = extendSxProp(inProps);
return <div {...props} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe } from 'yargs';
import { expect } from 'chai';
import createExtendSxProp from '../../src/createExtendSxProp';

describe('createExtendSxProp', () => {
it('return the new copy of input', () => {
const original = { color: 'red' };
expect(createExtendSxProp()(original)).to.not.equal(original);
expect(createExtendSxProp()(original)).to.deep.equal({ color: 'red' });
});
});
2 changes: 1 addition & 1 deletion packages/pigment-css-react/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function runTransformation(
plugins: ['@babel/plugin-syntax-jsx'],
},
tagResolver(source: string, tag: string) {
if (source !== '@pigment-css/react') {
if (source !== '@pigment-css/react' && !source.endsWith('/zero-styled')) {
return null;
}
return require.resolve(`../exports/${tag}`);
Expand Down
1 change: 1 addition & 0 deletions packages/pigment-css-react/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const processors = [
'generateAtomics',
'css',
'createUseThemeProps',
'createExtendSxProp',
'globalCss',
];
const external = ['react', 'react-is', 'prop-types'];
Expand Down

0 comments on commit df6e7ef

Please sign in to comment.