-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react] Add
createExtendSxProp
for Material UI integration (#112)
- Loading branch information
1 parent
3c1de03
commit df6e7ef
Showing
13 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/pigment-css-react/exports/internal_createExtendSxProp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/pigment-css-react/src/processors/createExtendSxProp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/pigment-css-react/tests/createExtendSxProp/createExtendSxProp.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/pigment-css-react/tests/createExtendSxProp/fixtures/extendSxProp.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
6 changes: 6 additions & 0 deletions
6
packages/pigment-css-react/tests/createExtendSxProp/fixtures/extendSxProp.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/pigment-css-react/tests/createExtendSxProp/runtime-createExtendSxProp.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters