Communitygithub.com

0xAlchemist-cs/personal-ui-system

審查、轉換和改善使用 Chakra UI v3 的 UI 程式碼。每當使用者想要審查 Chakra UI 程式碼是否有問題、將純 HTML/CSS、Tailwind、CSS Modules 或 styled-components 轉換為 Chakra UI、清理雜亂的 Chakra 元件、修正佈局結構或 token 使用,或詢問類似“這樣對嗎”、“這有什麼問題”、“審查我的元件”、“重構這個”、“清理”、“轉換”或“把這個變成 Chakra”等內容時,使用此技能。

personal-ui-system 是什麼?

這項技能是一個針對使用 Chakra UI v3 的 UI 程式碼進行審查、重構和轉換的專用代理。它主要用於分析使用者在開發環境中貼出的原始碼,並根據其意圖(純審查、純轉換,或兩者兼具)提供結構化的批評分析或完整的重寫程式碼。此技能的優勢在於其深度與精確度;它能檢查無障礙性、響應式設計、Chakra API 的正確性(例如標記已棄用的 v2 prop)、token 使用情況、元件結構和可維護性。它支援從多種來源轉換為 Chakra UI,包括純 HTML/CSS、Tailwind CSS、CSS Modules 和 styled-components,並附有詳盡的等價表。此技能與平台無關,但會優先考慮 Next.js App Router,並包含針對 Next.js Link 和 Image 元件的 `asChild` 使用模式。它被設計為務實且無損的,確保保留所有事件處理、狀態變數和無障礙屬性,並明白一個部分轉換但能正常運作的元件勝過一個完全轉換卻不能用的。輸出格式結構化,包括重寫的程式碼、清晰的變更摘要以及可選的改善建議。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/0xAlchemist-cs/personal-ui-system/tree/HEAD/.agents/skills/chakra-ui-refactor

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Chakra UI 重構與審查

你正在使用 Chakra UI v3 審查和改善 UI 程式碼。根據開發者的需求,你將產生結構化的批評、重寫的程式碼,或兩者兼具。在產生任何輸出之前,請完整閱讀程式碼和專案上下文。


步驟 1 — 定位並確定意圖

在產生任何輸出之前,請先確定:

  • Chakra UI 版本 — 檢查 package.json;預設使用 v3
  • 框架 — Next.js App Router、Pages Router、Vite、純 React
  • 來源類型 — 現有 Chakra、純 HTML/CSS、Tailwind、CSS Modules、styled-components
  • 使用者的要求:
    • 僅審查 — "檢查這個"、"這樣正確/慣用嗎"、"有什麼問題"、"審查我的程式碼" → 產生帶有目標性修正的批評,不需要完整重寫
    • 重構 / 轉換 — "重構這個"、"從 Tailwind 轉換"、"清理這個" → 產生重寫後的程式碼
    • 兩者兼具 — "審查並修正" → 先批評,再提供重寫後的程式碼

在回應的開頭說明你的判斷(例如:「正在審查現有的 Chakra v3 程式碼。假設為 Next.js App Router。」)。

如果尚未分享程式碼,請要求提供。不要審查對程式碼的描述。


步驟 2 — 分析程式碼

無論輸出模式為何,都從以下面向來處理程式碼。對於審查,它們會成為發現的問題。對於重構,它們是修正項目的清單。

無障礙性

  • 所有互動元素是否都有可存取的標籤?(圖示按鈕上的 aria-label、標籤+輸入配對的 htmlFor/id、圖片的 alt)
  • 是否支援鍵盤導航?(焦點環未被抑制,互動元素實際上可以被聚焦)
  • 標題層級結構有意義嗎?(沒有跳過層級,沒有將 h1 用作樣式捷徑)
  • 表單欄位是否包裝在附有 Field.Label 和 Field.ErrorText 的 Field.Root 中?
  • 元件是否能在不以顏色為唯一訊號的情況下運作?

響應式設計

  • 佈局元件是否指定了多個中斷點的行為?
  • 是否有硬編碼的像素值,而使用響應式 token 會更好?
  • 這會在手機上壞掉嗎?(固定寬度、在小視埠上使用 overflow: hidden)

Chakra API 正確性

  • 是否使用了 v3 的 prop 名稱?(disabled 而非 isDisabled、colorPalette 而非 colorScheme、gap 而非 spacing、open 而非 isOpen)
  • 複合元件是否使用正確?(Field.Root/Field.Label、Dialog.Root/Dialog.Content 等)
  • 在 Next.js App Router 中,"use client" 是否正確放置?
  • 是否仍存在 v2 模式?(extendTheme、ColorModeScript、useColorModeValue、sx prop)

Token 與樣式使用

  • 是否在可以使用語意 token 的地方使用了硬編碼顏色?(bg="#f9fafb" → bg="bg.subtle")
  • 是否使用了原始十六進制或調色板值而非語意 token?這些在暗色模式下不會生效。
  • 是否存在應使用 Chakra 樣式 prop 的內聯 style={{}} prop?

元件結構

  • 是否有不必要的巢狀結構?(Box > Box > Box 而一個就夠了)
  • 是否在每個子元素上使用手動間距(mt={4}),而使用 Stack gap={4} 會更簡潔?
  • 是否使用了正確的佈局原語?(Flex vs Stack vs Grid vs SimpleGrid)

可維護性

  • 相同的視覺模式是否重複了 3 次以上?(應考慮提取為元件或 recipe)
  • 是否有一次性樣式覆寫,暗示需要一個 recipe 或 slot recipe?
  • prop 型別 / TypeScript 型別是否存在且正確?

步驟 3a — 審查輸出

當使用者想要批評而非重寫時使用此方式。

按影響程度對發現的問題進行分類:

嚴重 — 破壞行為或排除使用者的錯誤和無障礙性失敗

  • 純圖示按鈕缺少 aria-label
  • 表單欄位沒有關聯的標籤
  • 帶有 onClick 但無法鍵盤存取的互動式 Box
  • 錯誤的 v3 prop 名稱,靜默無效(isDisabled 在 v3 中不會停用)
  • 在 App Router 中使用 hook 的元件缺少 "use client"

改進 — 不緊急的正確性和品質問題

  • 破壞暗色模式的硬編碼顏色
  • 缺少響應式中斷點
  • 不必要的巢狀或錯誤的佈局原語
  • 仍可運作但應更新的 v2 模式

可選建議 — 非阻斷性的想法

  • 將重複的模式提取為一個元件
  • 用 recipe 變體取代一次性樣式
  • 添加缺少的 TypeScript 型別

對於每個嚴重問題和重要的改進,顯示最小的修正:

**關閉按鈕缺少 aria-label**(嚴重)
用於關閉對話框的 IconButton 沒有可存取的標籤。

// 修正前
<IconButton icon={<CloseIcon />} onClick={onClose} />
// 修正後
<IconButton aria-label="關閉對話框" icon={<CloseIcon />} onClick={onClose} />

跳過沒有內容可報告的章節。根據程式碼調整篇幅 — 一個 20 行程式碼的元件需要精要的審查,而非詳盡的審查。


步驟 3b — 重構 / 轉換輸出

當使用者想要重寫程式碼時使用此方式。

按來源的轉換策略

從純 HTML / CSS

HTMLChakra 等價
<div> 佈局包裝器Box、Flex、Stack、Grid
<section>, <article>Box as="section", Box as="article"
<nav>Box as="nav"
<ul> / <li>Box as="ul" / Box as="li",或 Stack
<button>Button 或 IconButton
<a>Link
<img>Image(保留 alt)
<input>, <select>Input、NativeSelect 置於 Field.Root 內
CSSChakra 樣式 prop
display: flexFlex 或 display="flex"
flex-direction: columnStack 或 flexDir="column"
gap: 16pxgap={4}(1 單位 = 4px)
padding: 16px 24pxpy={4} px={6}
border-radius: 8pxrounded="md"
color: #6b7280color="fg.muted"
background: #f9fafbbg="bg.subtle"

從 Tailwind CSS

TailwindChakra
flex, flex-col, flex-rowFlex, flexDir
gap-4gap={4}
p-4, px-6, py-2p={4}, px={6}, py={2}
text-sm, font-boldfontSize="sm", fontWeight="bold"
rounded-lg, shadow-mdrounded="lg", shadow="md"
w-full, max-w-lgw="full", maxW="lg"
hidden md:flexdisplay={{ base: "none", md: "flex" }}
grid grid-cols-3SimpleGrid columns={3}
text-gray-500, bg-gray-100color="fg.muted", bg="bg.subtle"
hover:bg-gray-100_hover={{ bg: "bg.subtle" }}

用 Chakra 的中斷點物件語法取代 Tailwind 的響應式前綴:

// Tailwind: text-sm md:base lg:text-lg
<Text fontSize={{ base: "sm", md: "md", lg: "lg" }} />

從 CSS Modules — 將可以轉換的部分轉換為 Chakra prop;對於無法用 prop 表達的樣式(動畫、複雜選擇器),保留 className:

// 修正前
<div className={styles.card}>...</div>
// 修正後 — 已轉換為 prop;className 僅保留給無法轉換的樣式
<Box p={4} rounded="lg" shadow="sm" className={styles.fadeIn}>...</Box>

一旦所有 class 名稱都被消除,就完全移除該 import。

從 styled-components / @emotion/styled — 映射到具有等效樣式 prop 的 Box,然後移除 import 並在不再使用時解除安裝:

// 修正前
const Card = styled.div`padding: 1rem; &:hover { background: #f3f4f6; }`
// 修正後
<Box p={4} _hover={{ bg: "bg.muted" }} />

從現有的 Chakra UI(清理)

  • 扁平化 Box > Box > Box 巢狀結構;用 Stack gap={N} 取代每個子元素上的 mt/mb
  • 將 sx 替換為 css(v3 重新命名);更新內部的巢狀 &:hover 選擇器
  • 將 bg="gray.50" 替換為 bg="bg.subtle" 和其他語意 token 等價物
  • 更新 v2 prop 名稱:isDisabled→disabled、colorScheme→colorPalette、spacing→gap
  • 更新複合元件:FormControl→Field.Root、Modal→Dialog、Select→NativeSelect

保留行為 — 不容妥協

  • 完全按照原樣保留所有事件處理程式、狀態變數和條件式渲染
  • 保留 aria-* 屬性;在明顯需要的地方添加遺漏的屬性
  • 保留語意 HTML 意圖(<nav> → Box as="nav",而不僅僅是 Box)
  • 保留已有的 "use client";僅在元件確實需要時才添加
  • 不要僅僅因為元件使用 Chakra UI 就添加 "use client"
  • 一個部分轉換但能正常運作的元件,勝過一個完全轉換卻不能用的元件

用於 Next.js Link 和 Image 的 asChild

import NextLink from "next/link"
<ChakraLink asChild><NextLink href="/about">關於</NextLink></ChakraLink>

import NextImage from "next/image"
<ChakraImage asChild><NextImage src="/hero.png" alt="英雄圖片" width={800} height={400} /></ChakraImage>

重構輸出格式

1. 重構後的程式碼 — 完整、可執行,且包含正確的 import

2. 變更摘要 — 精簡清單,專注於非顯而易見的決策:

- 將 <div className="flex gap-4"> 替換為 <Flex gap={4}>
- 將 bg="#f9fafb" 替換為 bg="bg.subtle" 以支援暗色模式
- 保持表單提交處理程式和驗證邏輯不變
- 為關閉圖示按鈕添加 aria-label(之前遺漏)

3. 建議(可選)— 本次重構之外的實質改善:提取可重複使用的元件、採用 recipe、遷移 v2 模式。


何時詢問 vs 繼續進行

如果程式碼和意圖清晰,則繼續進行。在以下情況時詢問:

  • 尚未分享程式碼
  • 元件非常大且範圍不明確
  • 存在你看不見但會改變審查結果的上下文或狀態
  • 存在可能有多種解釋的模糊行為

當你在沒有詢問的情況下繼續進行時,請在回應的開頭說明你的假設。

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

0xAlchemist-cs/personal-ui-system

Use when the user's task involves Ant Design (antd) — writing antd components, debugging antd issues, querying antd APIs/props/tokens/demos, migrating between antd versions, or analyzing antd usage in a project. Triggers on antd-related code, imports from 'antd', or explicit antd questions.

0xAlchemist-cs/personal-ui-system

Build responsive, accessible UI components and layouts using Chakra UI v3, install or configure Chakra UI in new and existing projects, and design scalable themes using tokens, semantic tokens, recipes, and slot recipes. Use this skill whenever a user asks to build, create, or generate any UI component, page, form, dashboard, navbar, card, landing section, pricing table, or layout using Chakra UI; wants to add Chakra UI to a project, set up ChakraProvider, run CLI snippets, configure color mode, or fix provider wrapping; or asks about theming — defining brand colors, design tokens, semantic tokens, dark mode values, component recipes, slot recipes, typegen, or ejecting the default theme. Trigger on any Chakra UI building, setup, or theming or charts request, however casually phrased — "add my brand colors", "make a reusable card style", "build a bar chart", "show me a line chart", "make me a login form", "build a sidebar", "add Chakra to my app".

0xAlchemist-cs/personal-ui-system

Migrate Chakra UI projects from v2 to v3, covering package changes, codemods, provider setup, color mode, prop renaming, compound components, theming, recipes, and Next.js updates. Use this skill whenever a user is upgrading Chakra UI versions, encountering breaking changes after an upgrade, converting old v2 patterns (ColorModeScript, useColorModeValue, styleConfig, extendTheme, isDisabled, colorScheme, @chakra-ui/icons, framer-motion dependency), fixing compound component patterns, or asking about differences between Chakra UI v2 and v3 — even if they don't say "migrate" or "upgrade" explicitly.

0xAlchemist-cs/personal-ui-system

Integrates Material UI with Next.js App and Pages routers using @mui/material-nextjs, Emotion cache providers, next/font, CSS layers with Tailwind/CSS Modules, Link component prop patterns, CSS theme variables SSR notes, and App Router useSearchParams + Suspense. Use when setting up or debugging MUI in a Next.js app.

相關技能