Skip to Content
文档

安装

如何在你的项目中安装和设置 Chakra UI

框架指南

¥Framework Guide

Chakra UI 可与你喜爱的框架兼容。我们已为这些框架整理了分步指南。

¥Chakra UI works in your favorite framework. We've put together step-by-step guides for these frameworks

Next.js

使用 Next.js 应用轻松添加 Chakra UI

¥Easily add Chakra UI with Next.js app

Vite

将 Chakra UI 与 Vite 结合使用

¥Use Chakra UI with Vite

Stackblitz

在 Stackblitz 沙盒中试用 Chakra UI

¥Try Chakra UI in Stackblitz sandbox

所需的最低 Node 版本为 Node.20.x

安装

¥Installation

要在项目中手动设置 Chakra UI,请按照以下步骤操作。

¥To manually set up Chakra UI in your project, follow the steps below.

1

安装 @chakra-ui/react

¥Install @chakra-ui/react

npm i @chakra-ui/react @emotion/react
2

添加代码片段

¥Add snippets

代码片段是预先构建的组件,你可以使用它们更快地构建 UI。使用 @chakra-ui/cli 属性,你可以将代码片段添加到你的项目中。

¥Snippets are pre-built components that you can use to build your UI faster. Using the @chakra-ui/cli you can add snippets to your project.

npx @chakra-ui/cli snippet add
3

设置提供者

¥Setup provider

使用应用根目录下 components/ui/provider 组件中生成的 Provider 组件封装你的应用。

¥Wrap your application with the Provider component generated in the components/ui/provider component at the root of your application.

此提供程序包含以下内容:

¥This provider composes the following:

  • ChakraProvider 来自 @chakra-ui/react,用于样式系统

  • ThemeProvider 来自 next-themes,用于颜色模式

import { Provider } from "@/components/ui/provider"

function App({ Component, pageProps }) {
  return (
    <Provider>
      <Component {...pageProps} />
    </Provider>
  )
}
4

更新 tsconfig

¥Update tsconfig

如果你使用的是 TypeScript,则需要更新 tsconfig 文件中的 compilerOptions,使其包含以下选项:

¥If you're using TypeScript, you need to update the compilerOptions in the tsconfig file to include the following options:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "skipLibCheck": true,
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

如果你使用 JavaScript,请创建一个 jsconfig.json 文件并将上述代码添加到该文件中。

5

尽情享受吧!

¥Enjoy!

借助 Chakra UI 的代码片段和原始组件的强大功能,你可以更快地构建 UI。

¥With the power of the snippets and the primitive components from Chakra UI, you can build your UI faster.

import { Button, HStack } from "@chakra-ui/react"

const Demo = () => {
  return (
    <HStack>
      <Button>Click me</Button>
      <Button>Click me</Button>
    </HStack>
  )
}

学习教程

¥Learn

观看我们的官方课程,深入了解数十个视频,这些视频将教你有关 Chakra UI 的所有知识,从基础知识到高级概念。

¥Watch our official courses and dive into dozens of videos that will teach you everything you need to know about Chakra UI, from basics to advanced concepts.

贡献

¥Contribute

无论你是 Chakra UI 初学者还是高级用户,加入我们的社区都是与志同道合、使用该库构建优秀产品的人士建立联系的最佳方式。

¥Whether you're a beginner or advanced Chakra UI user, joining our community is the best way to connect with like-minded people who build great products with the library.