Mục lục tham khảo

Thành phần

Toggle

Two-state button on Base UI Toggle; pressed shown by an ink fill and aria-pressed, never colour alone.

Nội dung tham khảo viết bằng tiếng Anh; mục “Vietnamese” có ví dụ tiếng Việt.

Xem trước

Install

bash

npx shadcn add @eloquent/toggle

Usage

tsx

import { BoldIcon } from "lucide-react";
import { Toggle } from "@/components/ui/toggle";

<Toggle variant="outline" aria-label="Bold">
  <BoldIcon aria-hidden="true" />
</Toggle>
<Toggle defaultPressed>Save story</Toggle>

Icon-only toggles need an aria-label. Use pressed and onPressedChange to control the state.

States

StateTreatment
OffInk text; the outline variant adds a 1px ink border
PressedSolid ink fill, paper text, aria-pressed="true"
Press (unpressed)tint-1 fill while the button is held down; there is no hover effect
Focus2px ink outline, 2px offset
DisabledHalftone text and border, cursor-not-allowed

Keyboard

KeyAction
Tab / Shift+TabMove focus to and from the toggle (disabled toggles are skipped)
EnterToggle pressed state
SpaceToggle pressed state

Vietnamese

Ink adaptation

Upstream shadcn's radius, shadow and accent fill are removed. Pressed becomes the brand's selected treatment, an ink fill. Hover styling is dropped (controls change on press or focus). Every size keeps a 44px hit target. Built on Base UI Toggle; toggleVariants is exported for toggle-group.