Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm">
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Usage
import { Table } from "@chakra-ui/react"
<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader />
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell />
</Table.Row>
</Table.Body>
<Table.Footer>
<Table.Row>
<Table.Cell />
</Table.Row>
</Table.Footer>
</Table.Root>
Examples
Sizes
Use the size
prop to change the size of the table.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { For, Stack, Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="10">
<For each={["sm", "md", "lg"]}>
{(size) => (
<Table.Root key={size} size={size}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)}
</For>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Variants
Use the variant
prop to change the appearance of the table.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { For, Stack, Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="10">
<For each={["line", "outline"]}>
{(variant) => (
<Table.Root key={variant} size="sm" variant={variant}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)}
</For>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Striped
Use the striped
prop to add zebra-stripes to the table.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" striped>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Column Border
Use the showColumnBorder
prop to add borders between columns.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" showColumnBorder>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Overflow
Render the Table.ScrollArea
component to enable horizontal scrolling.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.ScrollArea borderWidth="1px" maxW="xl">
<Table.Root size="sm" variant="outline">
<Table.Header>
<Table.Row>
<Table.ColumnHeader minW="400px">Product</Table.ColumnHeader>
<Table.ColumnHeader minW="400px">Category</Table.ColumnHeader>
<Table.ColumnHeader minW="200px" textAlign="end">
Price
</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
</Table.ScrollArea>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Sticky Header
Use the stickyHeader
prop to make the table header sticky.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.ScrollArea borderWidth="1px" rounded="md" height="160px">
<Table.Root size="sm" stickyHeader>
<Table.Header>
<Table.Row bg="bg.subtle">
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
</Table.ScrollArea>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Highlight on Hover
Use the interactive
prop to highlight rows on hover.
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" interactive>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Pagination
Here's an example of how to compose a table with pagination.
Products
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { HStack, Heading, Stack, Table } from "@chakra-ui/react"
import {
PaginationItems,
PaginationNextTrigger,
PaginationPrevTrigger,
PaginationRoot,
} from "@/components/ui/pagination"
const Demo = () => {
return (
<Stack width="full" gap="5">
<Heading size="xl">Products</Heading>
<Table.Root size="sm" variant="outline" striped>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
<PaginationRoot count={items.length * 5} pageSize={5} page={1}>
<HStack wrap="wrap">
<PaginationPrevTrigger />
<PaginationItems />
<PaginationNextTrigger />
</HStack>
</PaginationRoot>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Action Bar
Here's an example of how to compose a table with an action bar and checkboxes. This is useful for showing actions for selected table rows.
Product | Category | Price | |
---|---|---|---|
Laptop | Electronics | $999.99 | |
Coffee Maker | Home Appliances | $49.99 | |
Desk Chair | Furniture | $150 | |
Smartphone | Electronics | $799.99 | |
Headphones | Accessories | $199.99 |
"use client"
import { Kbd, Table } from "@chakra-ui/react"
import {
ActionBarContent,
ActionBarRoot,
ActionBarSelectionTrigger,
ActionBarSeparator,
} from "@/components/ui/action-bar"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { useState } from "react"
const Demo = () => {
const [selection, setSelection] = useState<string[]>([])
const hasSelection = selection.length > 0
const indeterminate = hasSelection && selection.length < items.length
const rows = items.map((item) => (
<Table.Row
key={item.name}
data-selected={selection.includes(item.name) ? "" : undefined}
>
<Table.Cell>
<Checkbox
top="1"
aria-label="Select row"
checked={selection.includes(item.name)}
onCheckedChange={(changes) => {
setSelection((prev) =>
changes.checked
? [...prev, item.name]
: selection.filter((name) => name !== item.name),
)
}}
/>
</Table.Cell>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell>${item.price}</Table.Cell>
</Table.Row>
))
return (
<>
<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader w="6">
<Checkbox
top="1"
aria-label="Select all rows"
checked={indeterminate ? "indeterminate" : selection.length > 0}
onCheckedChange={(changes) => {
setSelection(
changes.checked ? items.map((item) => item.name) : [],
)
}}
/>
</Table.ColumnHeader>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader>Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>{rows}</Table.Body>
</Table.Root>
<ActionBarRoot open={hasSelection}>
<ActionBarContent>
<ActionBarSelectionTrigger>
{selection.length} selected
</ActionBarSelectionTrigger>
<ActionBarSeparator />
<Button variant="outline" size="sm">
Delete <Kbd>⌫</Kbd>
</Button>
<Button variant="outline" size="sm">
Share <Kbd>T</Kbd>
</Button>
</ActionBarContent>
</ActionBarRoot>
</>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Column Group
Use the Table.ColumnGroup
component to distribute the column widths using the
html colgroup
element.
htmlWidth
Product | Category | Price |
---|---|---|
Laptop | Electronics | 999.99 |
Coffee Maker | Home Appliances | 49.99 |
Desk Chair | Furniture | 150 |
Smartphone | Electronics | 799.99 |
Headphones | Accessories | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" variant="outline">
<Table.ColumnGroup>
<Table.Column htmlWidth="50%" />
<Table.Column htmlWidth="40%" />
<Table.Column />
</Table.ColumnGroup>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
Props
Root
Prop | Default | Type |
---|---|---|
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent' The color palette of the component |
variant | 'line' | 'line' | 'outline' The variant of the component |
size | 'md' | 'sm' | 'md' | 'lg' The size of the component |
interactive | 'true' | 'false' The interactive of the component | |
stickyHeader | 'true' | 'false' The stickyHeader of the component | |
striped | 'true' | 'false' The striped of the component | |
showColumnBorder | 'true' | 'false' The showColumnBorder of the component | |
native | boolean If `true`, the table will style its descendants with nested selectors | |
as | React.ElementType The underlying element to render. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | boolean Whether to remove the component's style. |