-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support keyframes in JS theme config #14594
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions
54
packages/tailwindcss/src/compat/apply-keyframes-to-ast.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,54 @@ | ||
import { expect, test } from 'vitest' | ||
import { toCss, type AstNode } from '../ast' | ||
import { buildDesignSystem } from '../design-system' | ||
import { Theme } from '../theme' | ||
import { applyKeyframesToAst } from './apply-keyframes-to-ast' | ||
import { resolveConfig } from './config/resolve-config' | ||
|
||
test('Config values can be merged into the theme', () => { | ||
let theme = new Theme() | ||
let design = buildDesignSystem(theme) | ||
|
||
let ast: AstNode[] = [] | ||
|
||
let resolvedUserConfig = resolveConfig(design, [ | ||
{ | ||
config: { | ||
theme: { | ||
keyframes: { | ||
'fade-in': { | ||
'0%': { opacity: '0' }, | ||
'100%': { opacity: '1' }, | ||
}, | ||
'fade-out': { | ||
'0%': { opacity: '1' }, | ||
'100%': { opacity: '0' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
base: '/root', | ||
}, | ||
]) | ||
applyKeyframesToAst(ast, resolvedUserConfig) | ||
|
||
expect(toCss(ast)).toMatchInlineSnapshot(` | ||
"@keyframes fade-in { | ||
0% { | ||
opacity: 0; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
@keyframes fade-out { | ||
0% { | ||
opacity: 1; | ||
} | ||
100% { | ||
opacity: 0; | ||
} | ||
} | ||
" | ||
`) | ||
}) |
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 { rule, type AstNode } from '../ast' | ||
import type { ResolvedConfig } from './config/types' | ||
import { objectToAst } from './plugin-api' | ||
|
||
export function applyKeyframesToAst(ast: AstNode[], { theme }: ResolvedConfig) { | ||
if ('keyframes' in theme) { | ||
for (let [name, keyframe] of Object.entries(theme.keyframes)) { | ||
ast.push(rule(`@keyframes ${name}`, objectToAst(keyframe as any))) | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh wait what.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting, in v3, plugins require an
addBase
to add the keyframes but if it was part of the JS config theme, it would be added automatically. That's lovely:https://play.tailwindcss.com/YQLQchsWWW?file=config
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In v3 the way this worked was keyframes were not emitted until used. So you had to do
@keyframes …
: theme('keyframes.whatever`) in your utility function. And we handle that already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we do in addUtilities at least. I don't think we do in matchUtilities which we should I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least I'm like 95% sure it works that way. Maybe we just de-duped them…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh gotch you're right they're only added when used! If you use
addBase
and use it with ananimate-*
variable, it is emitted twice in v3 (at the end of the stylesheet).I guess it's fine if we always emit keyframes for compat now (we do this for v4 keyframes too!). However unsure if we should then guard against this case. Maybe it's ok if the keyframes are emitted twice here since it does no harm? WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it's probably fine — would be nice to dedupe them but that can be an optimization done later. They should still be de-duped when minifying with lightingcss.