mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
feat(module): add theme.defaultVariants option (#4400)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,14 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record<string, an
|
||||
const template = (theme as any)[component]
|
||||
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 || {})
|
||||
.filter(([_, values]) => {
|
||||
const keys = Object.keys(values as Record<string, unknown>)
|
||||
@@ -56,7 +64,10 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record<string, an
|
||||
return [
|
||||
`import template from ${JSON.stringify(templatePath)}`,
|
||||
...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}`,
|
||||
`export default result as typeof theme`
|
||||
].join('\n\n')
|
||||
|
||||
Reference in New Issue
Block a user