-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mui/pigment-css into pigm…
…ent/extend-sx-prop
- Loading branch information
Showing
17 changed files
with
741 additions
and
332 deletions.
There are no files selected for viewing
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
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/globalCss').GlobalCssProcessor; |
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,22 @@ | ||
import type { CSSObjectNoCallback } from './base'; | ||
import type { ThemeArgs } from './theme'; | ||
|
||
type Primitve = string | null | undefined | boolean | number; | ||
|
||
type CssArg = ((themeArgs: ThemeArgs) => CSSObjectNoCallback) | CSSObjectNoCallback; | ||
type CssFn = (themeArgs: ThemeArgs) => string | number; | ||
|
||
interface GlobalCss { | ||
/** | ||
* @returns {string} The generated css class name to be referenced. | ||
*/ | ||
(arg: TemplateStringsArray, ...templateArgs: (Primitve | CssFn)[]): string; | ||
/** | ||
* @returns {string} The generated css class name to be referenced. | ||
*/ | ||
(...arg: CssArg[]): string; | ||
} | ||
|
||
declare const globalCss: GlobalCss; | ||
|
||
export default globalCss; |
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 @@ | ||
export default function globalCss() { | ||
throw new Error( | ||
`${process.env.PACKAGE_NAME}: You were trying to call "globalCss" function without configuring your bundler. Make sure to install the bundler specific plugin and use it. @pigment-css/vite-plugin for Vite integration or @pigment-css/nextjs-plugin for Next.js integration.`, | ||
); | ||
} |
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,167 @@ | ||
import type { Expression } from '@babel/types'; | ||
import type { | ||
CallParam, | ||
TemplateParam, | ||
Params, | ||
TailProcessorParams, | ||
ValueCache, | ||
} from '@wyw-in-js/processor-utils'; | ||
import { serializeStyles, Interpolation } from '@emotion/serialize'; | ||
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'; | ||
import type { IOptions } from './styled'; | ||
import { cache } from '../utils/emotion'; | ||
import { getGlobalSelector } from '../utils/preprocessor'; | ||
|
||
export type Primitive = string | number | boolean | null | undefined; | ||
|
||
export type TemplateCallback = (params: Record<string, unknown> | undefined) => string | number; | ||
|
||
export class GlobalCssProcessor extends BaseProcessor { | ||
callParam: CallParam | TemplateParam; | ||
|
||
constructor(params: Params, ...args: TailProcessorParams) { | ||
super([params[0]], ...args); | ||
if (params.length < 2) { | ||
throw BaseProcessor.SKIP; | ||
} | ||
validateParams( | ||
params, | ||
['callee', ['call', 'template']], | ||
`Invalid use of ${this.tagSource.imported} tag.`, | ||
); | ||
|
||
const [, callParams] = params; | ||
if (callParams[0] === 'call') { | ||
this.dependencies.push(callParams[1]); | ||
} else if (callParams[0] === 'template') { | ||
callParams[1].forEach((element) => { | ||
if ('kind' in element && element.kind !== ValueType.CONST) { | ||
this.dependencies.push(element); | ||
} | ||
}); | ||
} | ||
this.callParam = callParams; | ||
} | ||
|
||
build(values: ValueCache) { | ||
if (this.artifacts.length > 0) { | ||
throw new Error(`MUI: "${this.tagSource.imported}" is already built`); | ||
} | ||
|
||
const [callType] = this.callParam; | ||
|
||
if (callType === 'template') { | ||
this.handleTemplate(this.callParam, values); | ||
} else { | ||
this.handleCall(this.callParam, values); | ||
} | ||
} | ||
|
||
private handleTemplate([, callArgs]: TemplateParam, values: ValueCache) { | ||
const templateStrs: string[] = []; | ||
// @ts-ignore @TODO - Fix this. No idea how to initialize a Tagged String array. | ||
templateStrs.raw = []; | ||
const templateExpressions: Primitive[] = []; | ||
const { themeArgs } = this.options as IOptions; | ||
|
||
callArgs.forEach((item) => { | ||
if ('kind' in item) { | ||
switch (item.kind) { | ||
case ValueType.FUNCTION: { | ||
const value = values.get(item.ex.name) as TemplateCallback; | ||
templateExpressions.push(value(themeArgs)); | ||
break; | ||
} | ||
case ValueType.CONST: | ||
templateExpressions.push(item.value); | ||
break; | ||
case ValueType.LAZY: { | ||
const evaluatedValue = values.get(item.ex.name); | ||
if (typeof evaluatedValue === 'function') { | ||
templateExpressions.push(evaluatedValue(themeArgs)); | ||
} else { | ||
templateExpressions.push(evaluatedValue as Primitive); | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} else if (item.type === 'TemplateElement') { | ||
templateStrs.push(item.value.cooked as string); | ||
// @ts-ignore | ||
templateStrs.raw.push(item.value.raw); | ||
} | ||
}); | ||
this.generateArtifacts(templateStrs, ...templateExpressions); | ||
} | ||
|
||
generateArtifacts(styleObjOrTaggged: CSSInterpolation | string[], ...args: Primitive[]) { | ||
const { styles: cssText } = serializeStyles( | ||
args.length > 0 | ||
? [styleObjOrTaggged as Interpolation<{}>, ...args] | ||
: [styleObjOrTaggged as Interpolation<{}>], | ||
cache.registered, | ||
); | ||
|
||
const rules: Rules = { | ||
[this.asSelector]: { | ||
className: this.className, | ||
cssText, | ||
displayName: this.displayName, | ||
start: this.location?.start ?? null, | ||
}, | ||
}; | ||
const sourceMapReplacements: Replacements = [ | ||
{ | ||
length: cssText.length, | ||
original: { | ||
start: { | ||
column: this.location?.start.column ?? 0, | ||
line: this.location?.start.line ?? 0, | ||
}, | ||
end: { | ||
column: this.location?.end.column ?? 0, | ||
line: this.location?.end.line ?? 0, | ||
}, | ||
}, | ||
}, | ||
]; | ||
this.artifacts.push(['css', [rules, sourceMapReplacements]]); | ||
} | ||
|
||
private handleCall([, callArg]: CallParam, values: ValueCache) { | ||
let styleObj: CSSInterpolation; | ||
if (callArg.kind === ValueType.LAZY) { | ||
styleObj = values.get(callArg.ex.name) as CSSInterpolation; | ||
} else if (callArg.kind === ValueType.FUNCTION) { | ||
const { themeArgs } = this.options as IOptions; | ||
const value = values.get(callArg.ex.name) as ( | ||
args: Record<string, unknown> | undefined, | ||
) => CSSInterpolation; | ||
styleObj = value(themeArgs); | ||
} | ||
if (styleObj) { | ||
this.generateArtifacts(styleObj); | ||
} | ||
} | ||
|
||
doEvaltimeReplacement() { | ||
this.replacer(this.value, false); | ||
} | ||
|
||
doRuntimeReplacement() { | ||
this.doEvaltimeReplacement(); | ||
} | ||
|
||
get asSelector() { | ||
return getGlobalSelector(this.className); | ||
} | ||
|
||
get value(): Expression { | ||
return this.astService.nullLiteral(); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
packages/pigment-css-react/tests/globalCss/fixtures/globalCss-theme.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,30 @@ | ||
import { globalCss } from '@pigment-css/react'; | ||
|
||
globalCss` | ||
* { | ||
box-sizing: border-box; | ||
} | ||
@font-face { | ||
font-family: 'Patrick Hand SC'; | ||
font-style: normal; | ||
font-weight: 400; | ||
color: ${({ theme }) => theme.palette.primary.main}; | ||
src: local('Patrick Hand SC'), | ||
local('PatrickHandSC-Regular'), | ||
url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2) | ||
format('woff2'); | ||
unicode-range: U+0100-024f, U+1-1eff, | ||
U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f, | ||
U+A720-A7FF; | ||
} | ||
`; | ||
|
||
let inputGlobalStyles = globalCss(({ theme }) => ({ | ||
'@keyframes mui-auto-fill': { from: { display: 'block', color: 'transparent' } }, | ||
'@keyframes mui-auto-fill-cancel': { | ||
from: { display: 'block', color: theme.palette.primary.main }, | ||
}, | ||
})); | ||
if (typeof inputGlobalStyles === 'function') { | ||
inputGlobalStyles = inputGlobalStyles(); | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/pigment-css-react/tests/globalCss/fixtures/globalCss-theme.output.css
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,27 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
@font-face { | ||
font-family: 'Patrick Hand SC'; | ||
font-style: normal; | ||
font-weight: 400; | ||
color: red; | ||
src: | ||
local('Patrick Hand SC'), | ||
local('PatrickHandSC-Regular'), | ||
url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2) | ||
format('woff2'); | ||
unicode-range: U+0100-024f, U+1-1eff, U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f, U+A720-A7FF; | ||
} | ||
@keyframes mui-auto-fill { | ||
from { | ||
display: block; | ||
color: transparent; | ||
} | ||
} | ||
@keyframes mui-auto-fill-cancel { | ||
from { | ||
display: block; | ||
color: red; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/pigment-css-react/tests/globalCss/fixtures/globalCss-theme.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,5 @@ | ||
null; | ||
let inputGlobalStyles = null; | ||
if (typeof inputGlobalStyles === 'function') { | ||
inputGlobalStyles = inputGlobalStyles(); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/pigment-css-react/tests/globalCss/fixtures/globalCss.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,30 @@ | ||
import { globalCss } from '@pigment-css/react'; | ||
|
||
const green = 'green'; | ||
|
||
globalCss` | ||
* { | ||
box-sizing: border-box; | ||
} | ||
@font-face { | ||
font-family: 'Patrick Hand SC'; | ||
font-style: normal; | ||
font-weight: 400; | ||
color: ${green}; | ||
src: local('Patrick Hand SC'), | ||
local('PatrickHandSC-Regular'), | ||
url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2) | ||
format('woff2'); | ||
unicode-range: U+0100-024f, U+1-1eff, | ||
U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f, | ||
U+A720-A7FF; | ||
} | ||
`; | ||
|
||
let inputGlobalStyles = globalCss({ | ||
'@keyframes mui-auto-fill': { from: { display: 'block' } }, | ||
'@keyframes mui-auto-fill-cancel': { from: { display: 'block' } }, | ||
}); | ||
if (typeof inputGlobalStyles === 'function') { | ||
inputGlobalStyles = inputGlobalStyles(); | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/pigment-css-react/tests/globalCss/fixtures/globalCss.output.css
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,25 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
@font-face { | ||
font-family: 'Patrick Hand SC'; | ||
font-style: normal; | ||
font-weight: 400; | ||
color: green; | ||
src: | ||
local('Patrick Hand SC'), | ||
local('PatrickHandSC-Regular'), | ||
url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2) | ||
format('woff2'); | ||
unicode-range: U+0100-024f, U+1-1eff, U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f, U+A720-A7FF; | ||
} | ||
@keyframes mui-auto-fill { | ||
from { | ||
display: block; | ||
} | ||
} | ||
@keyframes mui-auto-fill-cancel { | ||
from { | ||
display: block; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/pigment-css-react/tests/globalCss/fixtures/globalCss.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,5 @@ | ||
null; | ||
let inputGlobalStyles = null; | ||
if (typeof inputGlobalStyles === 'function') { | ||
inputGlobalStyles = inputGlobalStyles(); | ||
} |
Oops, something went wrong.