--- title: Tabs description: "`Tabs`は、異なる表示領域を切り替えるコンポーネントです。" links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/tabs/tabs.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/tabs - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-tabs--basic --- ```tsx 孫悟空 ベジータ フリーザ クリリンのことか……クリリンのことかーーーっ!!!!! へっ!きたねぇ花火だ 私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく…… ``` ## 使い方 ```tsx import { Tabs } from "@yamada-ui/react" ``` ```tsx import { Tabs } from "@/components/ui" ``` ```tsx import { Tabs } from "@workspaces/ui" ``` ```tsx ``` ### itemsを使う ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ### バリアントを変更する ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( {(variant) => ( )} ) ``` ### サイズを変更する ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( {(size) => ( )} ) ``` ### デフォルトの項目を選択する ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ### 方向を変更する ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( {(orientation) => ( )} ) ``` ### 配置を変更する ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( {(align) => ( )} ) ``` ### コンテナ幅まで引き伸ばす ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ### 無効にする ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { disabled: true, panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ### アクティブをマニュアル化する デフォルトでは、フォーカスされたタブは自動的にアクティブ化されます。任意のタイミング(`Enter`や`Space`が押されたとき)でタブをアクティブ化する合は、`manual`を`true`に設定します。 ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ### リンクにする ```tsx const items = useMemo( () => [ { asChild: true, panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: ( 孫悟空 ), }, { asChild: true, panel: "へっ!きたねぇ花火だ", tab: ( ベジータ ), }, { asChild: true, panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: ( フリーザ ), }, ], [], ) return ( ) ``` ### 遅延レンダリング ```tsx const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( {(lazyBehavior) => ( )} ) ``` ### 動的にタブを操作する ```tsx const [index, setIndex] = useState(0) const [items, setItems] = useState([ { id: uuid(), panel: "Tab Content", tab: "Tab" }, { id: uuid(), panel: "Tab Content", tab: "Tab" }, { id: uuid(), panel: "Tab Content", tab: "Tab" }, ]) const onAddTab = () => { const nextItems = [...items, { id: uuid(), panel: "Tab Content", tab: "Tab" }] setItems(nextItems) setIndex(nextItems.length - 1) } const onRemoveTab = (id: string) => { const removeIndex = items.findIndex((item) => item.id === id) const nextItems = items.filter((item) => item.id !== id) setItems(nextItems) if (removeIndex < index) { setIndex(index - 1) } else if (removeIndex === index) { setIndex(Math.min(index, nextItems.length - 1)) } } return ( {items.map(({ id, tab }, index) => { const hasRemove = items.length > 1 return ( {tab} {hasRemove ? ( } role="button" onClick={(ev) => { ev.stopPropagation() onRemoveTab(id!) }} /> ) : null} ) })} {items.map(({ id, panel }, index) => ( {panel} {id} index: {index} id: {id} ))} ) ``` ### タブをカスタマイズする ```tsx 孫悟空 ベジータ フリーザ クリリンのことか……クリリンのことかーーーっ!!!!! へっ!きたねぇ花火だ 私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく…… ``` ### 制御する ```tsx const [index, onChange] = useState(0) const items = useMemo( () => [ { panel: "クリリンのことか……クリリンのことかーーーっ!!!!!", tab: "孫悟空", }, { panel: "へっ!きたねぇ花火だ", tab: "ベジータ", }, { panel: "私の戦闘力は530000です。ですがもちろんフルパワーであなたと戦う気はありませんからご心配なく……", tab: "フリーザ", }, ], [], ) return ( ) ``` ## Props ### Tabs.Root | Prop | Default | Type | Description | | -------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"line"` | `"line" \| "outline" \| "plain" \| "subtle"` | The variant of the component. | | `align` | `"start"` | `"center" \| "end" \| "start"` | The alignment of the tabs. | | `defaultIndex` | - | `number` | The index of the selected tab. | | `fitted` | `false` | `boolean` | If `true`, tabs will stretch to width of the tablist. | | `index` | - | `number` | The index of the selected tab. | | `items` | - | `TabsItem[]` | If provided, generate tab and panel components based on tabs. | | `lazy` | `false` | `boolean` | If `true`, rendering of the content will be deferred until it is selected. | | `lazyBehavior` | `"unmount"` | `LazyBehavior` | The lazy behavior of content when not active. Only works when `lazy={true}`. - `unmount`: The content is always unmounted. - `keepMounted`: The content is initially unmounted, but stays mounted when selected. | | `manual` | `false` | `boolean` | If `true`, the tabs will be manually activated and display its panel by pressing Space or Enter. If `false`, the tabs will be automatically activated and their panel is displayed when they receive focus. | | `onChange` | - | `(index: number) => void` | The callback invoked when the index changes. | | `orientation` | `"horizontal"` | `"horizontal" \| "vertical"` | The orientation of the tab list. | ### Tabs.List | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Tabs.Panel | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `index` | - | `number` | The index of the tab panel. | ### Tabs.Tab | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `index` | - | `number` | The index of the tab. | ## アクセシビリティ 現在、v2の移行に伴い、このセクションは更新中です。