mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-30 11:47:55 +01:00
feat(module): add theme.defaultVariants option (#4400)
This commit is contained in:
@@ -225,6 +225,27 @@ export default defineNuxtConfig({
|
|||||||
This option adds the `transition-colors` class on components with hover or active states.
|
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
|
## 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.
|
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.
|
||||||
|
|||||||
@@ -333,6 +333,32 @@ export default defineConfig({
|
|||||||
This option adds the `transition-colors` class on components with hover or active states.
|
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`
|
### `inertia`
|
||||||
|
|
||||||
Use the `inertia` option to enable compatibility with [Inertia.js](https://inertiajs.com/).
|
Use the `inertia` option to enable compatibility with [Inertia.js](https://inertiajs.com/).
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import { name, version } from '../package.json'
|
|||||||
|
|
||||||
export type * from './runtime/types'
|
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 {
|
export interface ModuleOptions {
|
||||||
/**
|
/**
|
||||||
* Prefix for components
|
* Prefix for components
|
||||||
@@ -38,7 +41,7 @@ export interface ModuleOptions {
|
|||||||
* @defaultValue `['primary', 'secondary', 'success', 'info', 'warning', 'error']`
|
* @defaultValue `['primary', 'secondary', 'success', 'info', 'warning', 'error']`
|
||||||
* @link https://ui.nuxt.com/getting-started/installation/nuxt#themecolors
|
* @link https://ui.nuxt.com/getting-started/installation/nuxt#themecolors
|
||||||
*/
|
*/
|
||||||
colors?: string[]
|
colors?: Color[]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable or disable transitions on components
|
* Enable or disable transitions on components
|
||||||
@@ -46,6 +49,20 @@ export interface ModuleOptions {
|
|||||||
* @link https://ui.nuxt.com/getting-started/installation/nuxt#themetransitions
|
* @link https://ui.nuxt.com/getting-started/installation/nuxt#themetransitions
|
||||||
*/
|
*/
|
||||||
transitions?: boolean
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,14 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record<string, an
|
|||||||
const template = (theme as any)[component]
|
const template = (theme as any)[component]
|
||||||
const result = typeof template === 'function' ? template(options) : template
|
const result = typeof template === 'function' ? template(options) : template
|
||||||
|
|
||||||
|
// Override default variants from nuxt.config.ts
|
||||||
|
if (result?.defaultVariants?.color && options.theme?.defaultVariants?.color) {
|
||||||
|
result.defaultVariants.color = options.theme.defaultVariants.color
|
||||||
|
}
|
||||||
|
if (result?.defaultVariants?.size && options.theme?.defaultVariants?.size) {
|
||||||
|
result.defaultVariants.size = options.theme.defaultVariants.size
|
||||||
|
}
|
||||||
|
|
||||||
const variants = Object.entries(result.variants || {})
|
const variants = Object.entries(result.variants || {})
|
||||||
.filter(([_, values]) => {
|
.filter(([_, values]) => {
|
||||||
const keys = Object.keys(values as Record<string, unknown>)
|
const keys = Object.keys(values as Record<string, unknown>)
|
||||||
@@ -56,7 +64,10 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record<string, an
|
|||||||
return [
|
return [
|
||||||
`import template from ${JSON.stringify(templatePath)}`,
|
`import template from ${JSON.stringify(templatePath)}`,
|
||||||
...generateVariantDeclarations(variants),
|
...generateVariantDeclarations(variants),
|
||||||
`const result = typeof template === 'function' ? (template as Function)(${JSON.stringify(options, null, 2)}) : template`,
|
`const options = ${JSON.stringify(options, null, 2)}`,
|
||||||
|
`const result = typeof template === 'function' ? (template as Function)(options) : template`,
|
||||||
|
`if (result?.defaultVariants?.color && options.theme?.defaultVariants?.color) result.defaultVariants.color = options.theme.defaultVariants.color`,
|
||||||
|
`if (result?.defaultVariants?.size && options.theme?.defaultVariants?.size) result.defaultVariants.size = options.theme.defaultVariants.size`,
|
||||||
`const theme = ${json}`,
|
`const theme = ${json}`,
|
||||||
`export default result as typeof theme`
|
`export default result as typeof theme`
|
||||||
].join('\n\n')
|
].join('\n\n')
|
||||||
|
|||||||
Reference in New Issue
Block a user