-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[zero][system] Add support for keyframes (#39155)
- Loading branch information
1 parent
6c70f79
commit 0124b79
Showing
17 changed files
with
325 additions
and
79 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import styled from './styled'; | ||
import sx from './sx'; | ||
import keyframes from './keyframes'; | ||
|
||
export { styled, sx }; | ||
export { styled, sx, keyframes }; | ||
export default styled; |
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,4 @@ | ||
// @TODO - Implement correct style definitions | ||
type Primitve = string | null | undefined | boolean | number; | ||
export default function keyframes(arg: Record<string, any>): string; | ||
export default function keyframes(arg: TemplateStringsArray, ...templateArgs: Primitve[]): string; |
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 keyframes() { | ||
throw new Error( | ||
'MUI: You were trying to call "keyframes" function without configuring your bundler. Make sure to install the bundler specific plugin and use it. @mui/zero-vite-plugin for Vite integration or @mui/zero-next-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
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,44 @@ | ||
import { | ||
BaseProcessor as LinariaBaseProcessor, | ||
toValidCSSIdentifier, | ||
buildSlug, | ||
} from '@linaria/tags'; | ||
import { slugify, type IVariableContext } from '@linaria/utils'; | ||
|
||
export default abstract class BaseProcessor extends LinariaBaseProcessor { | ||
variableIdx = 0; | ||
|
||
// Implementation taken from Linaria - https://github.com/callstack/linaria/blob/master/packages/react/src/processors/styled.ts#L284 | ||
protected getCustomVariableId(cssKey: string, source: string, hasUnit: boolean) { | ||
const context = this.getVariableContext(cssKey, source, hasUnit); | ||
const customSlugFn = this.options.variableNameSlug; | ||
if (!customSlugFn) { | ||
return toValidCSSIdentifier(`${this.slug}-${context.index}`); | ||
} | ||
|
||
return typeof customSlugFn === 'function' | ||
? customSlugFn(context) | ||
: buildSlug(customSlugFn, { ...context }); | ||
} | ||
|
||
// Implementation taken from Linaria - https://github.com/callstack/linaria/blob/master/packages/react/src/processors/styled.ts#L362 | ||
protected getVariableContext(cssKey: string, source: string, hasUnit: boolean): IVariableContext { | ||
const getIndex = () => { | ||
// eslint-disable-next-line no-plusplus | ||
return this.variableIdx++; | ||
}; | ||
|
||
return { | ||
componentName: this.displayName, | ||
componentSlug: this.slug, | ||
get index() { | ||
return getIndex(); | ||
}, | ||
precedingCss: cssKey, | ||
processor: this.constructor.name, | ||
source: '', | ||
unit: '', | ||
valueSlug: slugify(`${source}${hasUnit}`), | ||
}; | ||
} | ||
} |
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,154 @@ | ||
import type { Expression } from '@babel/types'; | ||
import { validateParams } from '@linaria/tags'; | ||
import type { | ||
CallParam, | ||
TemplateParam, | ||
Params, | ||
TailProcessorParams, | ||
ValueCache, | ||
} from '@linaria/tags'; | ||
import type { Replacements, Rules } from '@linaria/utils'; | ||
import { ValueType } from '@linaria/utils'; | ||
import type { CSSInterpolation } from '@emotion/css'; | ||
import BaseProcessor from './base-processor'; | ||
import type { IOptions } from './styled'; | ||
import { cache, keyframes } from './utils/emotion'; | ||
|
||
type Primitive = string | number | boolean | null | undefined; | ||
|
||
export default class KeyframesProcessor extends BaseProcessor { | ||
callParam: CallParam | TemplateParam; | ||
|
||
constructor(params: Params, ...args: TailProcessorParams) { | ||
super(params, ...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('Tag 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[] = []; | ||
const templateExpressions: Primitive[] = []; | ||
callArgs.forEach((item) => { | ||
if ('kind' in item) { | ||
switch (item.kind) { | ||
case ValueType.FUNCTION: | ||
throw item.buildCodeFrameError( | ||
'Functions are not allowed to be interpolated in keyframes tag.', | ||
); | ||
case ValueType.CONST: | ||
templateExpressions.push(item.value); | ||
break; | ||
case ValueType.LAZY: { | ||
const evaluatedValue = values.get(item.ex.name); | ||
if (typeof evaluatedValue === 'function') { | ||
throw item.buildCodeFrameError( | ||
'Functions are not allowed to be interpolated in keyframes tag.', | ||
); | ||
} else { | ||
templateExpressions.push(evaluatedValue as Primitive); | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} else if (item.type === 'TemplateElement') { | ||
templateStrs.push(item.value.cooked as string); | ||
} | ||
}); | ||
this.generateArtifacts(templateStrs, ...templateExpressions); | ||
} | ||
|
||
generateArtifacts(styleObjOrTaggged: CSSInterpolation | string[], ...args: Primitive[]) { | ||
const keyframeName = keyframes(styleObjOrTaggged, ...args); | ||
const cacheCssText = cache.inserted[keyframeName.replace('animation-', '')] as string; | ||
const cssText = cacheCssText.replaceAll(keyframeName, ''); | ||
|
||
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 Function; | ||
styleObj = value(themeArgs) as CSSInterpolation; | ||
} | ||
if (styleObj) { | ||
this.generateArtifacts(styleObj); | ||
} | ||
} | ||
|
||
doEvaltimeReplacement() { | ||
this.replacer(this.value, false); | ||
} | ||
|
||
doRuntimeReplacement() { | ||
this.doEvaltimeReplacement(); | ||
} | ||
|
||
get asSelector() { | ||
return this.className; | ||
} | ||
|
||
get value(): Expression { | ||
return this.astService.stringLiteral(this.className); | ||
} | ||
} |
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,29 @@ | ||
import type { Element } from 'stylis'; | ||
import { serialize, compile, stringify, middleware } from 'stylis'; | ||
|
||
function globalSelector(element: Element) { | ||
switch (element.type) { | ||
case 'rule': | ||
element.props = (element.props as string[]).map((value: any) => { | ||
if (value.match(/(:where|:is)\(/)) { | ||
value = value.replace(/\.[^:]+(:where|:is)/, '$1'); | ||
return value; | ||
} | ||
return value; | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
const serializer = middleware([globalSelector, stringify]); | ||
|
||
const stylis = (css: string) => serialize(compile(css), serializer); | ||
|
||
export function preprocessor(selector: string, cssText: string) { | ||
if (cssText.startsWith('@keyframes')) { | ||
return stylis(cssText.replace('@keyframes', `@keyframes ${selector}`)); | ||
} | ||
return stylis(`${selector}{${cssText}}`); | ||
} |
Oops, something went wrong.