Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow marking utility declarations as !important
  • Loading branch information
thecrypticace committed Sep 25, 2024
commit d5ba6ed5c5f1eb8f034a3eef75e6ff00c3962fd1
2 changes: 1 addition & 1 deletion packages/tailwindcss/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function compileAstNodes(candidate: Candidate, designSystem: DesignSystem
for (let nodes of asts) {
let propertySort = getPropertySort(nodes)

if (candidate.important) {
if (candidate.important || designSystem.important === true) {
applyImportant(nodes)
}

Expand Down
1 change: 1 addition & 0 deletions packages/tailwindcss/src/design-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function buildDesignSystem(theme: Theme): DesignSystem {

// How to mark important utilities
// - wrap with a selector (any string)
// - add an !important (true)
// - do nothing (false)
important: false,

Expand Down
28 changes: 28 additions & 0 deletions packages/tailwindcss/src/important.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,31 @@ test('Utilities can be wrapped in a selector', async () => {
"
`)
})

test('Utilities can be marked with important', async () => {
// This is the v4 equivalent of `important: true` from v3
let input = css`
@import 'tailwindcss/utilities' important;
`

let compiler = await compile(input, {
loadStylesheet: async (id: string, base: string) => ({
base,
content: '@tailwind utilities;',
}),
})

expect(compiler.build(['underline', 'hover:line-through'])).toMatchInlineSnapshot(`
".underline {
text-decoration-line: underline!important;
}
.hover\\:line-through {
&:hover {
@media (hover: hover) {
text-decoration-line: line-through!important;
}
}
}
"
`)
})
12 changes: 12 additions & 0 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ async function parseCss(
return WalkAction.Skip
}

// Drop instances of `@media important`
//
// We support `@import "tailwindcss" important` to mark all declarations
// in generated utilities as `!important`.
if (node.selector.startsWith('@media important')) {
important = true

replaceWith(node.nodes)

return WalkAction.Skip
}

if (node.selector !== '@theme' && !node.selector.startsWith('@theme ')) return

let [themeOptions, themePrefix] = parseThemeOptions(node.selector)
Expand Down
30 changes: 30 additions & 0 deletions packages/tailwindcss/src/intellisense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,33 @@ test('Utilities show when nested in a selector in intellisense', async () => {
]
`)
})

test('Utilities, when marked as important, show as important in intellisense', async () => {
let input = css`
@import 'tailwindcss/utilities' important;
`

let design = await __unstable__loadDesignSystem(input, {
loadStylesheet: async (_, base) => ({
base,
content: '@tailwind utilities;',
}),
})

expect(design.candidatesToCss(['underline', 'hover:line-through'])).toMatchInlineSnapshot(`
[
".underline {
text-decoration-line: underline!important;
}
",
".hover\\:line-through {
&:hover {
@media (hover: hover) {
text-decoration-line: line-through!important;
}
}
}
",
]
`)
})