refactor(module)!: implement design system with CSS variables (#2298)

This commit is contained in:
Benjamin Canac
2024-10-07 14:48:02 +02:00
committed by GitHub
parent 3cf5535b2f
commit 9368c6a639
279 changed files with 6533 additions and 6994 deletions

View File

@@ -39,9 +39,7 @@ export default defineNuxtConfig({
})
```
3. Import Tailwind and Nuxt UI in your `app.vue`{lang="ts-type"} or in your [CSS](https://nuxt.com/docs/getting-started/styling#the-css-property):
::code-group
3. Import Tailwind CSS and Nuxt UI in your `app.vue`{lang="ts-type"} or [CSS](https://nuxt.com/docs/getting-started/styling#the-css-property):
```vue [app.vue]
<style>
@@ -50,13 +48,6 @@ export default defineNuxtConfig({
</style>
```
```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";
```
::
## Options
You can customize Nuxt UI by providing options in your `nuxt.config.ts`:
@@ -91,25 +82,40 @@ export default defineNuxtConfig({
})
```
### `colorMode`
Use the `colorMode` option to enable or disable the `@nuxt/color-mode` module.
- Default: `true`{lang="ts-type"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxt/ui'],
ui: {
colorMode: false
}
})
```
### `theme.colors`
Use the `theme.colors` option to choose which Tailwind CSS colors are used to generate classes for components.
- Default: `['red', 'orange', 'amber', 'yellow', 'lime', 'green', 'emerald', 'teal', 'cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia', 'pink', 'rose']`{lang="ts-type" class="inline"}
- Default: `['primary', 'secondary', 'success', 'info', 'warning', 'error']`{lang="ts-type" class="inline"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxt/ui'],
ui: {
theme: {
colors: ['blue', 'green', 'red']
colors: ['primary', 'error']
}
}
})
```
::note{to="/getting-started/colors#build-colors"}
This can help reduce the number of CSS classes generated in your bundle.
::tip{to="/getting-started/theme#colors"}
You can use this option to remove some colors and reduce your CSS bundle or add new dynamic colors.
::
### `theme.transitions`

View File

@@ -1,6 +1,6 @@
---
title: Theme
description: 'Learn to customize Nuxt UI components using Tailwind CSS v4 and the Tailwind Variants API.'
description: 'Learn how to customize Nuxt UI components using Tailwind CSS v4, CSS variables and the Tailwind Variants API for powerful and flexible theming.'
---
## Tailwind CSS
@@ -11,7 +11,8 @@ Nuxt UI v3 uses Tailwind CSS v4 alpha which doesn't have a documentation yet, le
Tailwind CSS v4 takes a CSS-first configuration approach, you now customize your theme with CSS variables inside a `@theme` directive:
```css [main.css]
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
@@ -32,6 +33,7 @@ Tailwind CSS v4 takes a CSS-first configuration approach, you now customize your
--color-green-900: #0A5331;
--color-green-950: #052E16;
}
</style>
```
The `@theme` directive tells Tailwind to make new utilities and variants available based on those variables. It's the equivalent of the `theme.extend` key in Tailwind CSS v3 `tailwind.config.ts` file.
@@ -40,19 +42,19 @@ The `@theme` directive tells Tailwind to make new utilities and variants availab
You can learn more about this on [https://tailwindcss.com/blog/tailwindcss-v4-alpha](https://tailwindcss.com/blog/tailwindcss-v4-alpha#css-first-configuration).
::
This is exactly what the [`@import "@nuxt/ui";`](https://github.com/nuxt/ui/blob/v3/src/runtime/index.css) is all about, it extends the default Tailwind CSS theme and declares the `primary`, `error` and `gray` colors to be configurable through the [App Config](https://nuxt.com/docs/guide/directory-structure/app-config#app-config-file) but we'll talk more about that in the [Colors](/getting-started/colors) section.
### `@source`
You can use the `@source` directive to add explicit content glob patterns if you want to look for Tailwind classes in other files that are not automatically detected.
This can be useful when writing Tailwind classes in markdown files with `@nuxt/content`:
```css [main.css]
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
@source "../content/**/*.md";
</style>
```
::note{to="https://github.com/tailwindlabs/tailwindcss/pull/14078"}
@@ -63,18 +65,264 @@ You can learn more about the `@source` directive in this pull request.
You can use the `@plugin` directive to import Tailwind CSS plugins.
```css [main.css]
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
@plugin "@tailwindcss/typography";
</style>
```
::note{to="https://github.com/tailwindlabs/tailwindcss/pull/14264"}
You can learn more about the `@plugin` directive in this pull request.
::
## Tailwind Variants API
## Design system
Nuxt UI extends Tailwind CSS's theming capabilities, providing a flexible design system with pre-configured color aliases and CSS variables. This allows for easy customization and quick adaptation of the UI to your brand's aesthetic.
### Colors
Nuxt UI leverages Nuxt [App Config](https://nuxt.com/docs/guide/directory-structure/app-config#app-config-file) to provide customizable color aliases based on [Tailwind CSS colors](https://tailwindcss.com/docs/customizing-colors#color-palette-reference):
| Color | Default | Description |
| --- | --- | --- |
| `primary`{color="primary"} | `green` | Main brand color, used as the default color for components. |
| `secondary`{color="secondary"} | `blue` | Secondary color to complement the primary color. |
| `success`{color="success"} | `green` | Used for success states. |
| `info`{color="info"} | `blue` | Used for informational states. |
| `warning`{color="warning"} | `yellow` | Used for warning states. |
| `error`{color="error"} | `red` | Used for form error validation states. |
| `neutral` | `slate` | Neutral color for backgrounds, text, etc. |
You can configure these color aliases at runtime in your `app.config.ts` file under the `ui.colors` key, allowing for dynamic theme customization without requiring an application rebuild:
```ts [app.config.ts]
export default defineAppConfig({
ui: {
colors: {
primary: 'blue',
neutral: 'zinc'
}
}
})
```
::note
Try the :prose-icon{name="i-heroicons-swatch-20-solid" class="text-[--ui-primary]"} picker in the header above to change `primary` and `neutral` colors.
::
These colors are used to style the components but also to generate the `color` variants:
::component-code{slug="button"}
---
props:
color: primary
slots:
default: Button
---
::
::tip
You can add you own dynamic color aliases in your `app.config.ts`, you just have to make sure to define them in the [`ui.theme.colors`](/getting-started/installation#themecolors) option in your `nuxt.config.ts` file.
```ts [app.config.ts]
export default defineAppConfig({
ui: {
colors: {
tertiary: 'indigo'
}
}
})
```
```ts [nuxt.config.ts]
export default defineNuxtConfig({
ui: {
theme: {
colors: ['primary', 'secondary', 'tertiary', 'info', 'success', 'warning', 'error']
}
}
})
```
::
::note
These color aliases are not automatically defined as Tailwind CSS colors, so classes like `text-primary-500 dark:text-primary-400` won't be available by default as in Nuxt UI v2. This approach provides more flexibility and prevents overwriting of user-defined Tailwind CSS colors.<br><br>
However, you can generate these classes using Tailwind's `@theme` directive, allowing you to use custom color utility classes while maintaining dynamic color aliases:
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
@theme {
--color-primary-50: var(--ui-color-primary-50);
--color-primary-100: var(--ui-color-primary-100);
--color-primary-200: var(--ui-color-primary-200);
--color-primary-300: var(--ui-color-primary-300);
--color-primary-400: var(--ui-color-primary-400);
--color-primary-500: var(--ui-color-primary-500);
--color-primary-600: var(--ui-color-primary-600);
--color-primary-700: var(--ui-color-primary-700);
--color-primary-800: var(--ui-color-primary-800);
--color-primary-900: var(--ui-color-primary-900);
--color-primary-950: var(--ui-color-primary-950);
}
</style>
```
::
### Tokens
Nuxt UI generates CSS variables as design tokens for component styling. These tokens enable consistent theming and support both light and dark modes. You can use them in Tailwind classes like `text-[--ui-primary]`, which automatically adapts to the current color scheme.
::code-group
```css [Light]
:root {
--ui-primary: var(--ui-color-primary-500);
--ui-secondary: var(--ui-color-secondary-500);
--ui-success: var(--ui-color-success-500);
--ui-info: var(--ui-color-info-500);
--ui-warning: var(--ui-color-warning-500);
--ui-error: var(--ui-color-error-500);
}
```
```css [Dark]
.dark {
--ui-primary: var(--ui-color-primary-400);
--ui-secondary: var(--ui-color-secondary-400);
--ui-success: var(--ui-color-success-400);
--ui-info: var(--ui-color-info-400);
--ui-warning: var(--ui-color-warning-400);
--ui-error: var(--ui-color-error-400);
}
```
::
::tip
You can change which shade is used for each color on light and dark mode:
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
:root {
--ui-primary: var(--ui-color-primary-700);
}
.dark {
--ui-primary: var(--ui-color-primary-200);
}
</style>
```
::
Nuxt UI provides a comprehensive set of design tokens for the `neutral` color palette, ensuring consistent and accessible UI styling across both light and dark modes. These tokens offer fine-grained control over text, background, and border colors:
::code-group
```css [Light]
:root {
/* Least prominent text */
--ui-text-dimmed: var(--ui-color-neutral-400);
/* Slightly muted text */
--ui-text-muted: var(--ui-color-neutral-500);
/* Moderately prominent text */
--ui-text-toned: var(--ui-color-neutral-600);
/* Default text color */
--ui-text: var(--ui-color-neutral-700);
/* Most prominent text */
--ui-text-highlighted: var(--ui-color-neutral-900);
/* Main background color */
--ui-bg: var(--color-white);
/* Slightly elevated background */
--ui-bg-elevated: var(--ui-color-neutral-100);
/* More prominent background */
--ui-bg-accented: var(--ui-color-neutral-200);
/* Inverted background color */
--ui-bg-inverted: var(--ui-color-neutral-900);
/* Default border color */
--ui-border: var(--ui-color-neutral-200);
/* More prominent border */
--ui-border-accented: var(--ui-color-neutral-300);
/* Inverted border color */
--ui-border-inverted: var(--ui-color-neutral-900);
}
```
```css [Dark]
.dark {
/* Least prominent text */
--ui-text-dimmed: var(--ui-color-neutral-500);
/* Slightly muted text */
--ui-text-muted: var(--ui-color-neutral-400);
/* Moderately prominent text */
--ui-text-toned: var(--ui-color-neutral-300);
/* Default text color */
--ui-text: var(--ui-color-neutral-200);
/* Most prominent text */
--ui-text-highlighted: var(--color-white);
/* Main background color */
--ui-bg: var(--ui-color-neutral-900);
/* Slightly elevated background */
--ui-bg-elevated: var(--ui-color-neutral-800);
/* More prominent background */
--ui-bg-accented: var(--ui-color-neutral-700);
/* Inverted background color */
--ui-bg-inverted: var(--color-white);
/* Default border color */
--ui-border: var(--ui-color-neutral-800);
/* More prominent border */
--ui-border-accented: var(--ui-color-neutral-700);
/* Inverted border color */
--ui-border-inverted: var(--color-white);
}
```
::
You can easily customize these CSS variables in your `app.vue`{lang="ts-type"} or [CSS](https://nuxt.com/docs/getting-started/styling#the-css-property) to tailor the appearance of your application:
```vue [app.vue]
<style>
@import "tailwindcss";
@import "@nuxt/ui";
:root {
--ui-bg: var(--ui-color-neutral-50);
--ui-text: var(--ui-color-neutral-900);
}
.dark {
--ui-bg: var(--ui-color-neutral-950);
--ui-border: var(--ui-color-neutral-900);
}
</style>
```
::note
Nuxt UI automatically applies a text and background color on the `<body>` element of your app:
```css
body {
@apply antialiased font-sans text-[--ui-text] bg-[--ui-bg];
}
```
::
## Components theme
Nuxt UI components are styled using the [Tailwind Variants](https://www.tailwind-variants.org/) API, which provides a powerful way to create variants and manage component styles. Let's explore the key features of this API:
@@ -87,7 +335,7 @@ Components in Nuxt UI can have multiple `slots`, each representing a distinct HT
```ts [src/theme/card.ts]
export default {
slots: {
root: 'bg-white dark:bg-gray-900 ring ring-gray-200 dark:ring-gray-800 divide-y divide-gray-200 dark:divide-gray-800 rounded-lg shadow',
root: 'bg-[--ui-bg] ring ring-[--ui-border] divide-y divide-[--ui-border] rounded-lg shadow',
header: 'p-4 sm:px-6',
body: 'p-4 sm:p-6',
footer: 'p-4 sm:px-6'
@@ -135,7 +383,7 @@ export default {
::
::caution
::note
Components without slots don't have a [`ui` prop](#ui-prop), only the [`class` prop](#class-prop) is available to override styles.
::
@@ -146,18 +394,18 @@ Nuxt UI components use `variants` to change the `slots` styles based on props. H
```ts [src/theme/avatar.ts]
export default {
slots: {
root: 'inline-flex items-center justify-center shrink-0 select-none overflow-hidden rounded-full align-middle bg-gray-100 dark:bg-gray-800',
root: 'inline-flex items-center justify-center shrink-0 select-none overflow-hidden rounded-full align-middle bg-[--ui-bg-elevated]',
image: 'h-full w-full rounded-[inherit] object-cover'
},
variants: {
size: {
'sm': {
sm: {
root: 'size-7 text-sm'
},
'md': {
md: {
root: 'size-8 text-base'
},
'lg': {
lg: {
root: 'size-9 text-lg'
}
}
@@ -182,7 +430,7 @@ props:
The `defaultVariants` property specifies the default values for each variant. It determines how a component looks and behaves when no prop is provided. These default values can be customized in your [`app.config.ts`](#appconfigts) to adjust the standard appearance of components throughout your application.
## Customize components
## Customize theme
You have multiple ways to customize the appearance of Nuxt UI components, you can do it for all components at once or on a per-component basis.
@@ -225,18 +473,18 @@ You can also override a component's **slots** using the `ui` prop. This has prio
---
prettier: true
ignore:
- ui.leadingIcon
- ui.trailingIcon
- color
- variant
- size
- icon
props:
icon: i-heroicons-magnifying-glass
trailingIcon: i-heroicons-chevron-right
size: md
color: gray
color: neutral
variant: outline
ui:
leadingIcon: 'text-primary-500 dark:text-primary-400 size-3'
trailingIcon: 'rotate-90 size-3'
slots:
default: |
@@ -244,7 +492,7 @@ slots:
---
::
In this example, the `leadingIcon` slot is overwritten even though the `md` size variant would apply a `size-5` class by default.
In this example, the `trailingIcon` slot is overwritten with `size-3` even though the `md` size variant would apply a `size-5` class to it.
### `class` prop

View File

@@ -1,168 +0,0 @@
---
title: Colors
description: 'Learn to customize Nuxt UI component colors, enhancing your application visual theme.'
---
## Build colors
Nuxt UI components provide dynamic `color` variants. By default, these variants classes are generated based on the default Tailwind CSS colors. Let's take the [Button](/components/button) component as an example:
::component-code{slug="button"}
---
props:
color: 'green'
slots:
default: Button
---
::
You can change these colors with the [`theme.colors`](/getting-started/installation#themecolors) option in your `nuxt.config.ts` to select only the colors you're actually using.
For example, if you added a custom `cerise` color and only use the default `blue` and `green` colors in your application, you can configure the `colors` option like this:
::code-group
```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxt/ui'],
ui: {
theme: {
colors: ['cerise', 'blue', 'green']
}
}
})
```
```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";
@theme {
--color-cerise-50: #FEF2F4;
--color-cerise-100: #FDE6E9;
--color-cerise-200: #FBD0D9;
--color-cerise-300: #F7AAB9;
--color-cerise-400: #F27A93;
--color-cerise-500: #E63F66;
--color-cerise-600: #D42A5B;
--color-cerise-700: #B21E4B;
--color-cerise-800: #951C45;
--color-cerise-900: #801B40;
--color-cerise-950: #470A1F;
}
```
::
::caution
Make sure to use color ranges from `50` to `950`. You can use tools like [UI Colors](https://uicolors.app/) to generate your palette.
::
This configuration will ensure that only classes for those three colors are generated in your final CSS bundle. When you use the `color` prop, it will be typed and provide autocompletion in your editor with those three colors.
```vue
<template>
<UButton color="cerise">Button</UButton>
</template>
```
## Runtime colors
### Default aliases
Nuxt UI introduces three key color aliases used to style components:
- `primary`: Main brand color, used as the default color for components.
- Default: `green`{color="green"}
- `error`: For form error validation states.
- Default: `red`{color="red"}
- `gray`: Neutral color for backgrounds, text, etc.
- Default: `slate`
- `slate | cool | zinc | neutral | stone`
::warning{to="https://tailwindcss.com/docs/customizing-colors#default-color-palette" target="_blank"}
The Tailwind CSS `gray` color is renamed to `cool` in Nuxt UI to avoid conflicts with the `gray` alias.
::
You can configure these aliases in your `app.config.ts` file under the `ui.colors` key:
```ts [app.config.ts]
export default defineAppConfig({
ui: {
colors: {
primary: 'blue',
error: 'orange',
gray: 'zinc'
}
}
})
```
This powerful feature leverages Nuxt [App Config](https://nuxt.com/docs/guide/directory-structure/app-config#app-config-file), enabling dynamic styling of all components at runtime. It allows for real-time theme customization without requiring an application rebuild.
::tip
We recommend using these colors in your application whenever possible with classes like `text-primary-500 dark:text-primary-400`, `border-gray-200 dark:border-gray-800` or `bg-white dark:bg-gray-900` for example.
::
::important
These alias colors don't need to be explicitly listed in the `colors` option of your `nuxt.config.ts`. Also, if you've set `primary` to a custom color (e.g., `cerise`), you don't need to list `cerise` in the `colors` array.
::
::note
You can try this out by using the :prose-icon{name="i-heroicons-swatch-20-solid" class="text-primary-500 dark:text-primary-400"} menu in the header of this documentation.
::
### Custom aliases
You can also add your own color aliases to be configurable at runtime in your `app.config.ts` file:
1. Define the alias color by using CSS variables to let Tailwind know about it:
```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";
@theme {
--color-secondary-50: var(--color-secondary-50);
--color-secondary-100: var(--color-secondary-100);
--color-secondary-200: var(--color-secondary-200);
--color-secondary-300: var(--color-secondary-300);
--color-secondary-400: var(--color-secondary-400);
--color-secondary-500: var(--color-secondary-500);
--color-secondary-600: var(--color-secondary-600);
--color-secondary-700: var(--color-secondary-700);
--color-secondary-800: var(--color-secondary-800);
--color-secondary-900: var(--color-secondary-900);
--color-secondary-950: var(--color-secondary-950);
}
```
2. Set a default value for the color alias in your `app.config.ts` file:
```ts [app.config.ts]
export default defineAppConfig({
ui: {
colors: {
secondary: 'indigo'
}
}
})
```
3. Add this color to the `colors` option of your `nuxt.config.ts` file to generate classes:
```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxt/ui'],
ui: {
colors: ['secondary']
}
})
```
4. You can use the `secondary` color alias in your application and use classes like `text-secondary-500 dark:text-secondary-400`:
```vue
<template>
<UButton color="secondary">Button</UButton>
</template>
```

View File

@@ -12,8 +12,6 @@ links:
Nuxt UI automatically registers the `@nuxt/fonts` module for you, so there's no additional setup required. To use a font in your Nuxt UI application, you can simply declare it in your CSS:
::code-group
```vue [app.vue]
<style>
@import "tailwindcss";
@@ -25,17 +23,6 @@ Nuxt UI automatically registers the `@nuxt/fonts` module for you, so there's no
</style>
```
```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";
@theme {
--font-family-sans: 'Public Sans', sans-serif;
}
```
::
That's it! Nuxt Fonts will detect this and you should immediately see the web font loaded in your browser.
::tip{to="https://fonts.nuxt.com/advanced" target="_blank"}