Yamada UIにスターをあげる

スター
Yamada UIYamada UIv1.7.2

Toggle

Toggleは、オンまたはオフの2つの状態を持つコンポーネントです。

ソース@yamada-ui/toggle

インポート

import { Toggle, ToggleGroup } from "@yamada-ui/react"
Copied!

使い方

編集可能な例

<Wrap gap="md">
  <Toggle icon={<BoldIcon />} aria-label="Toggle bold" />

  <Toggle px="4">
    <ItalicIcon />
    Italic
  </Toggle>
</Wrap>
Copied!

バリアントを変更する

編集可能な例

<Wrap gap="md">
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="primary"
    variant="subtle"
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="secondary"
    variant="solid"
  />
  <Toggle icon={<BoldIcon />} aria-label="Toggle bold" variant="outline" />
  <Toggle icon={<BoldIcon />} aria-label="Toggle bold" variant="unstyled" />
</Wrap>
Copied!

サイズを変更する

編集可能な例

<Wrap gap="md" alignItems="flex-start">
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="primary"
    size="xs"
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="secondary"
    size="sm"
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="warning"
    size="md"
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="danger"
    size="lg"
  />
</Wrap>
Copied!

カラースキーマを変更する

編集可能な例

<VStack>
  <Wrap gap="md">
    <Toggle
      defaultIsSelected
      colorScheme="primary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="secondary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="success"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="warning"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="danger"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="link"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="gray"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="neutral"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="red"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="rose"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="pink"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="orange"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="amber"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="yellow"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="lime"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="green"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="emerald"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="teal"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="cyan"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="sky"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="blue"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="indigo"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="violet"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="purple"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      colorScheme="fuchsia"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
  </Wrap>

  <Wrap gap="md">
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="primary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="secondary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="success"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="warning"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="danger"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="link"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="gray"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="neutral"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="red"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="rose"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="pink"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="orange"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="amber"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="yellow"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="lime"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="green"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="emerald"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="teal"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="cyan"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="sky"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="blue"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="indigo"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="violet"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="purple"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="solid"
      colorScheme="fuchsia"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
  </Wrap>

  <Wrap gap="md">
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="primary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="secondary"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="success"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="warning"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="danger"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="link"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="gray"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="neutral"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="red"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="rose"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="pink"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="orange"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="amber"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="yellow"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="lime"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="green"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="emerald"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="teal"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="cyan"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="sky"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="blue"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="indigo"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="violet"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="purple"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
    <Toggle
      defaultIsSelected
      variant="outline"
      colorScheme="fuchsia"
      aria-label="Toggle bold"
      icon={<BoldIcon />}
    />
  </Wrap>
</VStack>
Copied!

無効にする

無効にする場合は、isDisabledtrueにします。

編集可能な例

<Wrap gap="md">
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="primary"
    variant="subtle"
    defaultIsSelected
    isDisabled
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="secondary"
    variant="solid"
    defaultIsSelected
    isDisabled
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    variant="outline"
    defaultIsSelected
    isDisabled
  />
</Wrap>
Copied!

読み取り専用にする

読み取り専用にする場合は、isReadOnlytrueに設定します。

編集可能な例

<Wrap gap="md">
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="primary"
    variant="subtle"
    defaultIsSelected
    isReadOnly
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    colorScheme="secondary"
    variant="solid"
    defaultIsSelected
    isReadOnly
  />
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    variant="outline"
    defaultIsSelected
    isReadOnly
  />
</Wrap>
Copied!

制御する

編集可能な例

const [isSelected, setIsSelected] = useState<boolean>(false)

return (
  <Toggle
    icon={<BoldIcon />}
    aria-label="Toggle bold"
    isSelected={isSelected}
    onChange={setIsSelected}
  />
)
Copied!

グループ化する

Toggleをグループ化する場合は、ToggleGroupを使用します。ToggleGroupは、子要素(Toggle)のvariant, size, colorScheme, isDisabled, isReadOnlyを一括で設定できます。

編集可能な例

const [value, setValue] = useState<string | undefined>(undefined)

return (
  <ToggleGroup value={value} onChange={setValue}>
    <Toggle value="bold" icon={<BoldIcon />} aria-label="Toggle bold" />
    <Toggle value="italic" icon={<ItalicIcon />} aria-label="Toggle italic" />
    <Toggle
      value="underline"
      icon={<UnderlineIcon />}
      aria-label="Toggle underline"
    />
  </ToggleGroup>
)
Copied!

複数選択の場合は、valueまたはdefaultValueを配列にします。

編集可能な例

const [value, setValue] = useState<string[]>([])

return (
  <ToggleGroup value={value} onChange={setValue}>
    <Toggle value="bold" icon={<BoldIcon />} aria-label="Toggle bold" />
    <Toggle value="italic" icon={<ItalicIcon />} aria-label="Toggle italic" />
    <Toggle
      value="underline"
      icon={<UnderlineIcon />}
      aria-label="Toggle underline"
    />
  </ToggleGroup>
)
Copied!

GitHubでこのページを編集する

SwitchSelect