Từng bước thiết kế hệ thống component có thể tái sử dụng cao: tận dụng Tailwind CSS v4 CSS-first token, Class Variance Authority (CVA) quản lý biến thể linh hoạt và Headless UI chuẩn Accessible.
Nguyễn Duy Noa
Frontend Developer
Một trong những bài toán lớn nhất của các nhóm phát triển Frontend là làm sao để duy trì tính nhất quán về mặt giao diện (Visual Consistency) khi dự án phình to. Việc viết CSS ad-hoc hay copy paste class Tailwind lặp đi lặp lại sẽ dẫn đến sự hỗn loạn về mã nguồn và rất khó bảo trì.
Giải pháp chuẩn mực hiện nay là kết hợp sức mạnh của Tailwind CSS v4 (với kiến trúc @theme inline), Class Variance Authority (CVA) để định nghĩa component variants, và các thư viện Headless UI Primitives (@base-ui/react, Radix UI) để đảm bảo chuẩn Accessibility (a11y).
Khác với các phiên bản trước phụ thuộc vào file cấu hình tailwind.config.js, Tailwind CSS v4 chuyển toàn bộ cấu hình token về file CSS thông qua chỉ thị @theme:
@import "tailwindcss";
@theme inline {
--color-brand-primary: #10b981;
--color-brand-accent: #06b6d4;
--color-surface-card: #18181b;
--radius-custom: 0.75rem;
--font-display: "Outfit", sans-serif;
}
/* Các biến theme tối/sáng */
:root {
--background: #09090b;
--foreground: #f4f4f5;
}Đối với các component có trạng thái phức tạp như Modal/Dialog, Menu, Tooltip, bạn không nên tự viết logic bắt phím Escape hay khóa scroll. Hãy sử dụng @base-ui/react hoặc Radix UI:
"use client";
import { Dialog } from "@base-ui/react";
import { X } from "@phosphor-icons/react";
export function CustomModal({ isOpen, onClose, title, children }: any) {
return (
<Dialog.Root open={isOpen} onOpenChange={onClose}>
<Dialog.Portal>
<Dialog.Backdrop className="fixed inset-0 bg-black/70 backdrop-blur-sm z-50 animate-fade-in" />
<Dialog.Popup className="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-full max-w-lg bg-zinc-900 border border-zinc-800 rounded-2xl p-6 shadow-2xl z-50">
<div className="flex items-center justify-between mb-4">
<Dialog.Title className="text-lg font-bold text-zinc-100">{title}</Dialog.Title>
<Dialog.Close className="text-zinc-500 hover:text-zinc-200">
<X size={20} />
</Dialog.Close>
</div>
<Dialog.Description className="text-zinc-400 text-sm">{children}</Dialog.Description>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
);
}Facing similar challenges?
I can help you optimize your website — reach out for a free consultation.
Kết hợp Tailwind CSS v4, CVA và Base UI tạo nên một nền tảng vững chắc để phát triển sản phẩm. Codebase của bạn sẽ vừa chuẩn mực về mặt thẩm mỹ, vừa dễ mở rộng và thân thiện với tất cả người dùng.
If you have specific questions about web performance, React, or want to collaborate on a project — reach out directly.
Explore more technical deep-dives and frontend optimization guides.
Khám phá kiến trúc thư mục chuẩn Feature-Based kết hợp cùng Next.js App Router giúp phân tách rõ ràng trách nhiệm, giải quyết triệt để vấn đề phình to mã nguồn, tối ưu hóa ranh giới Server/Client Components và nâng cao tốc độ phát triển cho team từ nhỏ đến lớn.
Hướng dẫn thực chiến áp dụng tư duy Clean Code vào hệ sinh thái Front-End hiện đại (React 19, Next.js App Router, TypeScript): Quy chuẩn đặt tên có ngữ nghĩa, SOLID cho Component, xóa sổ Boolean Props Hell, loại bỏ lạm dụng useEffect, làm sạch câu lệnh điều kiện JSX và thiết lập Type-Safety tuyệt đối.
Xây dựng hệ thống đa ngôn ngữ hoàn chỉnh trong Next.js App Router với next-intl: từ kiến trúc routing as-needed, SSR translations trong React Server Components, Type-Safe navigation, cho tới cấu hình hreflang và canonical chuẩn SEO quốc tế.