-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
keyframes
function that lazily injects the keyframes into the s…
…heet and return a unique name
- Loading branch information
Showing
11 changed files
with
346 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@twind/website': patch | ||
'@twind/cdn': patch | ||
'twind': patch | ||
--- | ||
|
||
add `keyframes` function that lazily injects the keyframes into the sheet and return a unique name |
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
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,73 @@ | ||
import type { CSSObject, CSSValue, StringLike } from './types' | ||
|
||
import { escape, hash } from './utils' | ||
import { tw as tw$ } from './runtime' | ||
import { astish } from './internal/astish' | ||
import { css } from './css' | ||
|
||
export interface KeyframesFunction { | ||
(style: CSSObject | string): StringLike | ||
|
||
(strings: TemplateStringsArray, ...interpolations: readonly CSSValue[]): StringLike | ||
|
||
bind(thisArg?: ((tokens: string) => string) | undefined | void): Keyframes & { | ||
[label: string]: KeyframesFunction | ||
} | ||
} | ||
|
||
export type Keyframes = KeyframesFunction & { | ||
[label: string]: KeyframesFunction | ||
} | ||
|
||
export const keyframes = /* @__PURE__ */ bind() | ||
|
||
function bind(thisArg: ((tokens: string) => string) | undefined | void): Keyframes { | ||
return new Proxy( | ||
function keyframes( | ||
strings: CSSObject | string | TemplateStringsArray, | ||
...interpolations: readonly CSSValue[] | ||
): StringLike { | ||
return keyframes$(thisArg, '', strings, interpolations) | ||
} as Keyframes, | ||
{ | ||
get(target, name) { | ||
if (name === 'bind') { | ||
return bind | ||
} | ||
|
||
return function namedKeyframes( | ||
strings: CSSObject | string | TemplateStringsArray, | ||
...interpolations: readonly CSSValue[] | ||
): StringLike { | ||
return keyframes$(thisArg, name as string, strings, interpolations) | ||
} | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
function keyframes$( | ||
thisArg: ((tokens: string) => string) | undefined | void, | ||
name: string, | ||
strings: CSSObject | string | TemplateStringsArray, | ||
interpolations: readonly CSSValue[], | ||
): StringLike { | ||
const ast = astish(strings, interpolations) | ||
|
||
const keyframeName = escape(name + hash(JSON.stringify([name, ast]))) | ||
|
||
const tw = typeof thisArg == 'function' ? thisArg : tw$ | ||
|
||
// lazy inject keyframes | ||
return { | ||
toString() { | ||
tw( | ||
css({ | ||
[`@keyframes ${keyframeName}`]: astish(strings, interpolations), | ||
} as unknown as CSSObject), | ||
) | ||
|
||
return keyframeName | ||
}, | ||
} as StringLike | ||
} |
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
Oops, something went wrong.