Vite
A guide for installing and using Yamada UI with Vite.js projects
Installation
- 1
Create application
Create Vite application.
pnpm create vite my-app --template react-tsnpm create vite my-app -- --template react-tsyarn create vite my-app --template react-tsbun create vite my-app --template react-ts - 2
Setup
Running the command will create the necessary files and folders in your project.
pnpm dlx @yamada-ui/cli initnpx @yamada-ui/cli inityarn dlx @yamada-ui/cli initbunx @yamada-ui/cli init - 3
Install the package
Install
@workspaces/uito your application.pnpm add "@workspaces/ui@workspace:*"npm install "@workspaces/ui@workspace:*"yarn add "@workspaces/ui@workspace:*"bun add "@workspaces/ui@workspace:*" - 4
Add provider
After installing, add
UIProviderto the root of your application.main.tsx
import { StrictMode } from "react" import { createRoot } from "react-dom/client" import App from "./App.tsx" import { UIProvider } from "@workspaces/ui" createRoot(document.getElementById("root")!).render( <StrictMode> <UIProvider> <App /> </UIProvider> </StrictMode>, ) - 5
Use components
After adding
UIProvider, you can use the components in your application.App.tsx
import { Button } from "@workspaces/ui" function App() { return <Button>Click me!</Button> } export default AppThat's it! You've successfully set up Yamada UI.
Scripts
ColorModeScript
To use Color Mode, you need to add ColorModeScript to the body to ensure it works correctly.
This is because color mode is implemented using localStorage or cookies, and adding the script ensures proper synchronization when the page loads.
vite.config.ts
import type { Plugin } from "vite"
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import { COLOR_MODE_STORAGE_KEY, getStorageScript } from "@workspaces/ui"
function injectColorModeScript(): Plugin {
return {
name: "inject-color-mode-script",
transformIndexHtml(html) {
const content = getStorageScript(
"colorMode",
COLOR_MODE_STORAGE_KEY,
)({ defaultValue: "light" })
return html.replace("<body>", `<body><script>${content}</script>`)
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), injectColorModeScript()],
})
If you change the defaultColorMode in your config, set the defaultValue prop on ColorModeScript.
vite.config.ts
import type { Plugin } from "vite"
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import { COLOR_MODE_STORAGE_KEY, getStorageScript } from "@workspaces/ui"
import { config } from "@workspace/theme"
function injectColorModeScript(): Plugin {
return {
name: "inject-color-mode-script",
transformIndexHtml(html) {
const content = getStorageScript(
"colorMode",
COLOR_MODE_STORAGE_KEY,
)({ defaultValue: config.defaultColorMode })
return html.replace("<body>", `<body><script>${content}</script>`)
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), injectColorModeScript()],
})
ThemeSchemeScript
To use theme switching, you need to add ThemeSchemeScript to the body to ensure it works correctly.
This is because theme switching is implemented using localStorage or cookies, and adding the script ensures proper synchronization when the page loads.
vite.config.ts
import type { Plugin } from "vite"
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import { getStorageScript, THEME_SCHEME_STORAGE_KEY } from "@workspaces/ui"
function injectThemeSchemeScript(): Plugin {
return {
name: "inject-theme-scheme-scripts",
transformIndexHtml(html) {
const content = getStorageScript(
"themeScheme",
THEME_SCHEME_STORAGE_KEY,
)({ defaultValue: "base" })
return html.replace("<body>", `<body><script>${content}</script>`)
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), injectThemeSchemeScript()],
})
If you change the defaultThemeScheme in your config, set the defaultValue prop on ThemeSchemeScript.
vite.config.ts
import type { Plugin } from "vite"
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import { getStorageScript, THEME_SCHEME_STORAGE_KEY } from "@workspaces/ui"
import { config } from "@workspace/theme"
function injectThemeSchemeScript(): Plugin {
return {
name: "inject-theme-scheme-scripts",
transformIndexHtml(html) {
const content = getStorageScript(
"themeScheme",
THEME_SCHEME_STORAGE_KEY,
)({ defaultValue: config.defaultThemeScheme })
return html.replace("<body>", `<body><script>${content}</script>`)
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), injectThemeSchemeScript()],
})