From 35f90b9920c16acf6540ef57366a9e0ac8f9c478 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Wed, 9 Jul 2025 14:37:49 +0200 Subject: [PATCH] feat(module): add `theme.defaultVariants` option (#4400) --- .../2.installation/1.nuxt.md | 21 +++++++++++++++ .../1.getting-started/2.installation/2.vue.md | 26 +++++++++++++++++++ src/module.ts | 19 +++++++++++++- src/templates.ts | 13 +++++++++- 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/docs/content/1.getting-started/2.installation/1.nuxt.md b/docs/content/1.getting-started/2.installation/1.nuxt.md index 0e08853f..18b7155e 100644 --- a/docs/content/1.getting-started/2.installation/1.nuxt.md +++ b/docs/content/1.getting-started/2.installation/1.nuxt.md @@ -225,6 +225,27 @@ export default defineNuxtConfig({ This option adds the `transition-colors` class on components with hover or active states. :: +### `theme.defaultVariants` :badge{label="Soon" class="align-text-top"} + +Use the `theme.defaultVariants` option to override the default `color` and `size` variants for components. + +- Default: `{ color: 'primary', size: 'md' }`{lang="ts-type"} + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + modules: ['@nuxt/ui'], + css: ['~/assets/css/main.css'], + ui: { + theme: { + defaultVariants: { + color: 'neutral', + size: 'sm' + } + } + } +}) +``` + ## Continuous Releases Nuxt UI uses [pkg.pr.new](https://github.com/stackblitz-labs/pkg.pr.new) for continuous preview releases, providing developers with instant access to the latest features and bug fixes without waiting for official releases. diff --git a/docs/content/1.getting-started/2.installation/2.vue.md b/docs/content/1.getting-started/2.installation/2.vue.md index cdab65af..1b992624 100644 --- a/docs/content/1.getting-started/2.installation/2.vue.md +++ b/docs/content/1.getting-started/2.installation/2.vue.md @@ -333,6 +333,32 @@ export default defineConfig({ This option adds the `transition-colors` class on components with hover or active states. :: +### `theme.defaultVariants` :badge{label="Soon" class="align-text-top"} + +Use the `theme.defaultVariants` option to override the default `color` and `size` variants for components. + +- Default: `{ color: 'primary', size: 'md' }`{lang="ts-type"} + +```ts [vite.config.ts] +import { defineConfig } from 'vite' +import vue from '@vitejs/plugin-vue' +import ui from '@nuxt/ui/vite' + +export default defineConfig({ + plugins: [ + vue(), + ui({ + theme: { + defaultVariants: { + color: 'neutral', + size: 'sm' + } + } + }) + ] +}) +``` + ### `inertia` Use the `inertia` option to enable compatibility with [Inertia.js](https://inertiajs.com/). diff --git a/src/module.ts b/src/module.ts index 34f0c654..bc8fc12f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,6 +6,9 @@ import { name, version } from '../package.json' export type * from './runtime/types' +type Color = 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'error' | (string & {}) +type Size = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | (string & {}) + export interface ModuleOptions { /** * Prefix for components @@ -38,7 +41,7 @@ export interface ModuleOptions { * @defaultValue `['primary', 'secondary', 'success', 'info', 'warning', 'error']` * @link https://ui.nuxt.com/getting-started/installation/nuxt#themecolors */ - colors?: string[] + colors?: Color[] /** * Enable or disable transitions on components @@ -46,6 +49,20 @@ export interface ModuleOptions { * @link https://ui.nuxt.com/getting-started/installation/nuxt#themetransitions */ transitions?: boolean + + defaultVariants?: { + /** + * The default color variant to use for components + * @defaultValue `'primary'` + */ + color?: Color + + /** + * The default size variant to use for components + * @defaultValue `'md'` + */ + size?: Size + } } } diff --git a/src/templates.ts b/src/templates.ts index d4c17e65..a34c9902 100644 --- a/src/templates.ts +++ b/src/templates.ts @@ -26,6 +26,14 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record { const keys = Object.keys(values as Record) @@ -56,7 +64,10 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record