Skip to content

Commit 3190331

Browse files
committed
feat(theme): add new option 'unimportant' to gen classes without important
1 parent a0d03be commit 3190331

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

packages/api-generator/src/locale/en/useTheme.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"name": "Name of current theme.",
1111
"styles": "**FOR INTERNAL USE ONLY**",
1212
"themeClasses": "**FOR INTERNAL USE ONLY**",
13-
"themes": "Raw theme definitions."
13+
"themes": "Raw theme definitions.",
14+
"unimportant": "Generate utility classes without the !important declaration."
1415
}
1516
}

packages/docs/src/data/new-in.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"exposed": {
44
"change": "3.9.0",
55
"toggle": "3.9.0",
6-
"cycle": "3.9.0"
6+
"cycle": "3.9.0",
7+
"unimportant": "3.9.0"
78
}
89
},
910
"VAppBar": {

packages/vuetify/src/composables/__tests__/theme.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,16 @@ describe('createTheme', () => {
274274
expect(consoleMock).toHaveBeenCalledWith('[Vue warn]: Vuetify: Theme "nonexistent" not found on the Vuetify theme instance')
275275
consoleMock.mockReset()
276276
})
277+
278+
it('should generate utility classes without !important', async () => {
279+
const theme = createTheme({ unimportant: true })
280+
281+
theme.install(app)
282+
283+
const stylesheet = document.getElementById('vuetify-theme-stylesheet')
284+
285+
const css = stylesheet!.innerHTML
286+
287+
expect(css).not.toContain('!important')
288+
})
277289
})

packages/vuetify/src/composables/theme.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type ThemeOptions = false | {
3838
themes?: Record<string, ThemeDefinition>
3939
stylesheetId?: string
4040
scope?: string
41+
unimportant?: boolean
4142
}
4243
export type ThemeDefinition = DeepPartial<InternalThemeDefinition>
4344

@@ -49,6 +50,7 @@ interface InternalThemeOptions {
4950
themes: Record<string, InternalThemeDefinition>
5051
stylesheetId: string
5152
scope?: string
53+
unimportant: boolean
5254
}
5355

5456
interface VariationsOptions {
@@ -197,6 +199,7 @@ function genDefaults () {
197199
},
198200
},
199201
stylesheetId: 'vuetify-theme-stylesheet',
202+
unimportant: false,
200203
}
201204
}
202205

@@ -357,6 +360,7 @@ export function createTheme (options?: ThemeOptions): ThemeInstance & { install:
357360

358361
const styles = computed(() => {
359362
const lines: string[] = []
363+
const important = parsedOptions.unimportant ? '' : ' !important'
360364

361365
if (current.value?.dark) {
362366
createCssClass(lines, ':root', ['color-scheme: dark'], parsedOptions.scope)
@@ -377,14 +381,14 @@ export function createTheme (options?: ThemeOptions): ThemeInstance & { install:
377381
const colors = new Set(Object.values(computedThemes.value).flatMap(theme => Object.keys(theme.colors)))
378382
for (const key of colors) {
379383
if (key.startsWith('on-')) {
380-
createCssClass(fgLines, `.${key}`, [`color: rgb(var(--v-theme-${key})) !important`], parsedOptions.scope)
384+
createCssClass(fgLines, `.${key}`, [`color: rgb(var(--v-theme-${key}))${important}`], parsedOptions.scope)
381385
} else {
382386
createCssClass(bgLines, `.bg-${key}`, [
383387
`--v-theme-overlay-multiplier: var(--v-theme-${key}-overlay-multiplier)`,
384-
`background-color: rgb(var(--v-theme-${key})) !important`,
385-
`color: rgb(var(--v-theme-on-${key})) !important`,
388+
`background-color: rgb(var(--v-theme-${key}))${important}`,
389+
`color: rgb(var(--v-theme-on-${key}))${important}`,
386390
], parsedOptions.scope)
387-
createCssClass(fgLines, `.text-${key}`, [`color: rgb(var(--v-theme-${key})) !important`], parsedOptions.scope)
391+
createCssClass(fgLines, `.text-${key}`, [`color: rgb(var(--v-theme-${key}))${important}`], parsedOptions.scope)
388392
createCssClass(fgLines, `.border-${key}`, [`--v-border-color: var(--v-theme-${key})`], parsedOptions.scope)
389393
}
390394
}

0 commit comments

Comments
 (0)