# Toggle

> A button with a pressed state, composed with Button or IconButton.

```tsx title="hero.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Toggle } from '@langgenius/dify-ui/toggle'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'

const formats = [
  { format: 'bold', label: 'Bold', icon: 'i-ri-bold', defaultPressed: true },
  { format: 'italic', label: 'Italic', icon: 'i-ri-italic', defaultPressed: false },
  {
    format: 'strikethrough',
    label: 'Strikethrough',
    icon: 'i-ri-strikethrough',
    defaultPressed: false,
  },
] as const

export function ToggleHeroDemo() {
  return (
    <div className="group flex w-full max-w-80 flex-col gap-2.5">
      <TooltipProvider>
        <div role="group" aria-label="Text formatting" className="flex items-center gap-1">
          {formats.map((item) => (
            <Tooltip key={item.format}>
              <TooltipTrigger
                render={
                  <Toggle
                    defaultPressed={item.defaultPressed}
                    data-format={item.format}
                    className="data-pressed:bg-state-accent-active data-pressed:text-text-accent data-pressed:hover:bg-state-accent-active-alt"
                    render={
                      <IconButton size="lg" aria-label={item.label}>
                        <span aria-hidden="true" className={`${item.icon} size-4`} />
                      </IconButton>
                    }
                  />
                }
              />
              <TooltipContent>{item.label}</TooltipContent>
            </Tooltip>
          ))}
        </div>
      </TooltipProvider>
      <p className="system-sm-regular text-text-secondary group-has-[[data-format=bold][data-pressed]]:font-semibold group-has-[[data-format=italic][data-pressed]]:italic group-has-[[data-format=strikethrough][data-pressed]]:line-through">
        Retry the HTTP node up to three times before the workflow fails.
      </p>
    </div>
  )
}
```

## Usage guidelines

- Use `Toggle` for the pressed state of a button, such as toolbar formatting or view options. It is not a form field: use `Checkbox` for a boolean value saved with a form and `Switch` for a setting that takes effect immediately.
- Compose it with `Button` or `IconButton` through `render`. `Toggle` carries only the focus ring and renders one `<button>` with `aria-pressed`.
- Give an icon-only toggle one accessible name through `aria-label` on `IconButton`, and hide the glyph with `aria-hidden`. Keep the name the same in both states; `aria-pressed` reports the state.
- Pair an icon-only toggle with a `Tooltip` that shows the same words as its name, with `Toggle` as the `render` of `TooltipTrigger`.
- Style the pressed look at the call site with `data-pressed:` and Dify state tokens. Switching to a filled glyph for the pressed state adds a cue that does not rely on color.

## Anatomy

```tsx
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Toggle } from '@langgenius/dify-ui/toggle'
```

```tsx
<Toggle
  render={
    <IconButton aria-label="Label">
      <span aria-hidden="true" />
    </IconButton>
  }
/>
```

## Examples

### Text toggle

Render through `Button` when the toggle has a visible label.

```tsx title="text.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Toggle } from '@langgenius/dify-ui/toggle'

export function ToggleTextDemo() {
  return (
    <Toggle
      defaultPressed
      className="group data-pressed:bg-state-accent-active data-pressed:text-text-accent data-pressed:hover:bg-state-accent-active-alt"
      render={<Button size="small" variant="tertiary" />}
    >
      <span
        aria-hidden="true"
        className="group-not-data-pressed:i-ri-pushpin-line group-not-data-pressed:size-3.5 group-data-pressed:i-ri-pushpin-fill group-data-pressed:size-3.5"
      />
      Pin to sidebar
    </Toggle>
  )
}
```

### Controlled pressed state

Control `pressed` with `onPressedChange` when the owner coordinates several toggles. Here pressing one rating clears the other, and the pressed rating switches to a filled icon.

```tsx title="feedback.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Toggle } from '@langgenius/dify-ui/toggle'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'
import * as React from 'react'

type Rating = 'like' | 'dislike' | null

export function ToggleFeedbackDemo() {
  const [rating, setRating] = React.useState<Rating>(null)

  return (
    <div className="flex w-full max-w-80 flex-col gap-2">
      <p className="rounded-2xl bg-background-section-burn px-4 py-3 body-md-regular text-text-primary">
        Your workspace keeps workflow run logs for 30 days. Export them from the Logs page to keep
        them longer.
      </p>
      <TooltipProvider>
        <div role="group" aria-label="Rate this answer" className="flex items-center gap-0.5">
          <Tooltip>
            <TooltipTrigger
              render={
                <Toggle
                  pressed={rating === 'like'}
                  onPressedChange={(pressed) => setRating(pressed ? 'like' : null)}
                  className="data-pressed:bg-state-accent-active data-pressed:text-text-accent data-pressed:hover:bg-state-accent-active-alt"
                  render={
                    <IconButton aria-label="Good answer">
                      <span
                        aria-hidden="true"
                        className={
                          rating === 'like'
                            ? 'i-ri-thumb-up-fill size-4'
                            : 'i-ri-thumb-up-line size-4'
                        }
                      />
                    </IconButton>
                  }
                />
              }
            />
            <TooltipContent side="bottom">Good answer</TooltipContent>
          </Tooltip>
          <Tooltip>
            <TooltipTrigger
              render={
                <Toggle
                  pressed={rating === 'dislike'}
                  onPressedChange={(pressed) => setRating(pressed ? 'dislike' : null)}
                  className="data-pressed:bg-state-destructive-hover data-pressed:text-text-destructive data-pressed:hover:bg-state-destructive-hover-alt data-pressed:hover:text-text-destructive"
                  render={
                    <IconButton aria-label="Bad answer">
                      <span
                        aria-hidden="true"
                        className={
                          rating === 'dislike'
                            ? 'i-ri-thumb-down-fill size-4'
                            : 'i-ri-thumb-down-line size-4'
                        }
                      />
                    </IconButton>
                  }
                />
              }
            />
            <TooltipContent side="bottom">Bad answer</TooltipContent>
          </Tooltip>
        </div>
      </TooltipProvider>
    </div>
  )
}
```

## Keyboard

| Key     | Action                     |
| ------- | -------------------------- |
| `Enter` | Toggles the pressed state. |
| `Space` | Toggles the pressed state. |

## API reference

### Toggle

A two-state toggle button that exposes `data-pressed` for styling. Renders a `<button>` element.

Carries only focus-ring styles; compose with `Button` or `IconButton` via `render`.

Inherits `className`, `pressed`, `defaultPressed`, `disabled`, `onPressedChange`, `value`, `render`, `style`, `nativeButton` from [Base UI Toggle](https://base-ui.com/react/components/toggle).

**Data attributes**

| Attribute       | Description                                 |
| --------------- | ------------------------------------------- |
| `data-pressed`  | Present when the toggle button is pressed.  |
| `data-disabled` | Present when the toggle button is disabled. |
