テーマ
テーマ設定は、ユーザーインターフェース(UI)を設計する上で重要な要素です。アプリケーション全体で一貫した美観を適用し、ユーザーエクスペリエンスを向上させ、視覚的な統一感を維持することができます。
NextUIでは、TailwindCSSプラグインを使用して、簡単かつ柔軟なテーマのカスタマイズを可能にしています。このプラグインは、tw-colors プラグインをベースにしており、L-Blondy が開発しました。これにより、アプリケーションのさまざまなコンポーネントにわたって、カラースキーム、レイアウト構成などをカスタマイズできます。
テーマとは?
NextUIにおけるテーマとは、アプリケーション全体で一貫して適用できる、あらかじめ定義された色、レイアウト属性、その他のUI要素のセットのことです。テーマは、視覚的な一貫性を確保し、ユーザーエクスペリエンスを向上させ、アプリの外観の管理と更新を簡素化します。
セットアップ
NextUIのテーマ機能を使用するための最初のステップは、nextui
プラグインをtailwind.config.js
ファイルに追加することです。以下にその方法の例を示します。
注: pnpmとモノレポアーキテクチャを使用している場合は、ROOTの
node_modules
を指していることを確認してください。
// tailwind.config.jsconst {nextui} = require("@nextui-org/react");/** @type {import('tailwindcss').Config} */module.exports = {content: [// ...// make sure it's pointing to the ROOT node_module"./node_modules/@nextui-org/theme/dist/**/*.{js,ts,jsx,tsx}",],theme: {extend: {},},darkMode: "class",plugins: [nextui()],};
使い方
プラグインをtailwind.config.js
ファイルに追加した後、デフォルトのテーマ(ライト/ダーク)またはカスタムテーマを使用できます。以下は、これらのテーマをmain.jsx
またはmain.tsx
で適用する方法です。
src ディレクトリに移動し、main.jsx
または main.tsx
内で、ルート要素に以下のクラス名を適用します。
- ライトテーマの場合は
light
。 - ダークテーマの場合は
dark
。 - テキストの色を設定するには
text-foreground
。 - 背景色を設定するには
bg-background
。
// main.tsx or main.jsximport React from "react";import ReactDOM from "react-dom/client";import {NextUIProvider} from "@nextui-org/react";import App from "./App";import "./index.css";ReactDOM.createRoot(document.getElementById("root")).render(<React.StrictMode><NextUIProvider><main className="dark text-foreground bg-background"><App /></main></NextUIProvider></React.StrictMode>,);
注:カラークラスの詳細については、カラー セクションを参照してください。
デフォルトのプラグインオプション
nextui
プラグインはデフォルトの構造を提供します。以下にその概要を示します。
module.exports = {plugins: [nextui({prefix: "nextui", // prefix for themes variablesaddCommonColors: false, // override common colors (e.g. "blue", "green", "pink").defaultTheme: "light", // default theme from the themes objectdefaultExtendTheme: "light", // default theme to extend on custom themeslayout: {}, // common layout tokens (applied to all themes)themes: {light: {layout: {}, // light theme layout tokenscolors: {}, // light theme colors},dark: {layout: {}, // dark theme layout tokenscolors: {}, // dark theme colors},// ... custom themes},}),],};
テーマオプション
これらは、テーマにカスタム設定を適用するために使用できるオプションです。
module.exports = {plugins: [nextui({themes: {light: {layout: {},colors: {}},dark: {layout: {},colors: {}},... // custom themes}})]}
ネストされたテーマ
NextUI はネストされたテーマをサポートしており、アプリケーションの異なるセクションに異なるテーマを適用できます。
<html class="dark">...<div class="light">...</div><div class="purple-dark">...</div></html>
テーマベースのバリアント
NextUI では、現在アクティブなテーマに基づいて TailwindCSS スタイルを適用できます。以下に、その方法の例を示します。
<!-- In dark theme, background will be dark and text will be light.In light theme, background will be light and text will be dark --><div class="dark dark:bg-gray-800 dark:text-white bg-white text-black">...<div>Text color changes based on theme</div></div><div class="light light:bg-gray-100 light:text-black bg-black text-white">...<div>Text color changes based on theme</div></div>
API リファレンス
次の表は、NextUI でテーマを扱う際に使用できるさまざまな属性の概要を示しています。
属性 | 型 | 説明 | デフォルト |
---|---|---|---|
prefix | string | CSS 変数のプレフィックス。 | nextui |
addCommonColors | boolean | true の場合、一般的な nextui カラー (例: "blue"、"green"、"purple") が TailwindCSS のデフォルトカラーを置き換えます。 | false |
defaultTheme | light | dark | 使用するデフォルトのテーマ。 | light |
defaultExtendTheme | light | dark | 拡張するデフォルトのテーマ。 | light |
layout | LayoutTheme | レイアウトの定義。 | - |
themes | ConfigThemes | テーマの定義。 | - |
型
ConfigThemes
type ConfigTheme = {extend?: "light" | "dark"; // base theme to extendlayout?: LayoutTheme; // see LayoutThemecolors?: ThemeColors; // see ThemeColors};type ConfigThemes = Record<string, ConfigTheme>;
LayoutTheme
type BaseThemeUnit = {small?: string;medium?: string;large?: string;};type FontThemeUnit = {small?: string;medium?: string;large?: string;tiny?: string;};interface LayoutTheme {/*** The default font size applied across the components.*/fontSize?: FontThemeUnit;/*** The default line height applied across the components.*/lineHeight?: FontThemeUnit;/*** The default radius applied across the components.* we recommend to use `rem` units.*/radius?: BaseThemeUnit;/*** A number between 0 and 1 that is applied as opacity-[value] when* the component is disabled.*/disabledOpacity?: string | number;/*** The default height applied to the divider component.* we recommend to use `px` units.*/dividerWeight?: string;/*** The border width applied across the components.*/borderWidth?: BaseThemeUnit;/*** The box shadow applied across the components.*/boxShadow?: BaseThemeUnit;}
ThemeColors
type ColorScale = {50: string;100: string;200: string;300: string;400: string;500: string;600: string;700: string;800: string;900: string;foreground: string; // contrast colorDEFAULT: string;};type BaseColors = {background: ColorScale; // the page background colorforeground: ColorScale; // the page text colordivider: ColorScale; // used for divider and single line borderoverlay: ColorScale; // used for modal, popover, etc.focus: ColorScale; // used for focus state outlinecontent1: ColorScale; // used for card, modal, popover, etc.content2: ColorScale;content3: ColorScale;content4: ColorScale;};// brand colorstype ThemeColors = BaseColors & {default: ColorScale;primary: ColorScale;secondary: ColorScale;success: ColorScale;warning: ColorScale;danger: ColorScale;};