Table
A component for rendering data in rows and columns with customizable headers, cells, and responsive behavior.
Usage
Basic usage example to quickly see how the Table
works.
Name | Symbol | Position | Mass |
---|---|---|---|
Carbon | C | 6 | 12.011 |
Nitrogen | N | 7 | 14.007 |
Yttrium | Y | 39 | 88.906 |
Properties
Interactive configurator to explore customization options for the Table
component.
Element name | Symbol | Position | Atomic mass | Description | |
---|---|---|---|---|---|
1 | Na | 11 | 22.99 | A soft metal that reacts vigorously with water. | |
2 | Mg | 12 | 24.305 | A metal used in lightweight alloys. | |
3 | Al | 13 | 26.982 | A lightweight, corrosion-resistant metal. | |
4 | Si | 14 | 28.085 | A key component in semiconductors. | |
5 | P | 15 | 30.974 | Essential for DNA and energy transfer in cells. | |
6 | S | 16 | 32.06 | A yellow, nonmetallic element used in fertilizers. | |
7 | Cl | 17 | 35.45 | A reactive halogen used for water purification. | |
8 | Ar | 18 | 39.948 | A noble gas used in light bulbs and welding. | |
9 | K | 19 | 39.098 | An essential element for human health. | |
10 | Ca | 20 | 40.078 | A metal crucial for bones and teeth. | |
11 | Fe | 26 | 55.845 | A key element in hemoglobin, the oxygen-carrying molecule in blood. | |
12 | Cu | 29 | 63.546 | A conductor of electricity used in wiring. | |
13 | Zn | 30 | 65.38 | An essential trace element for humans. | |
14 | Y | 39 | 88.906 | A rare-earth metal used in lasers and superconductors. | |
15 | Ba | 56 | 137.33 | A metal used in medical imaging. | |
16 | Ce | 58 | 140.12 | A rare-earth element used in catalytic converters. | |
17 | H | 1 | 1.008 | The lightest element and most abundant chemical substance in the universe. | |
18 | He | 2 | 4.0026 | A noble gas used in balloons and cooling systems. | |
19 | Li | 3 | 6.94 | A soft, silvery metal used in rechargeable batteries. | |
20 | Be | 4 | 9.0122 | A hard metal used in aerospace materials. | |
21 | B | 5 | 10.81 | An element used in glass and detergents. | |
22 | C | 6 | 12.011 | A versatile element, the basis for organic life. | |
23 | N | 7 | 14.007 | Makes up 78% of Earth's atmosphere. | |
24 | O | 8 | 15.999 | Essential for respiration in most living organisms. | |
25 | F | 9 | 18.998 | A highly reactive gas used in toothpaste. | |
26 | Ne | 10 | 20.18 | A noble gas used in neon signs and lighting. |
Structure
Decide for yourself how you develop the Table
component according to your needs. Currently the Table
component can be used with several declarations, you can take advantage of it.
Independent Components
import { Table, TableBody, TableCaption, TableData, TableHead, TableHeader, TableRow } from "@/ui/table";
const data = [
{ name: "Sodium", symbol: "Na", position: 11, mass: 22.99 },
{ name: "Magnesium", symbol: "Mg", position: 12, mass: 24.305 }
// others
];
export function TableDemo() {
return (
<Table>
<TableCaption>
Flat Component Structure
<br />
(Independent Components)
</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead>Symbol</TableHead>
<TableHead>Position</TableHead>
<TableHead>Mass</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{data.slice(0, 3).map(i => (
<TableRow key={i.name}>
<TableData>{i.name}</TableData>
<TableData>{i.symbol}</TableData>
<TableData>{i.position}</TableData>
<TableData>{i.mass}</TableData>
</TableRow>
))}
</TableBody>
</Table>
);
}
Compound Components
"use client";
import { Table } from "@/ui/table";
const data = [
{ name: "Sodium", symbol: "Na", position: 11, mass: 22.99 },
{ name: "Magnesium", symbol: "Mg", position: 12, mass: 24.305 }
// others
];
export function TableDemo() {
return (
<Table>
<Table.Caption>
Namespaced Component Structure
<br />
(Compound Components)
</Table.Caption>
<Table.Header>
<Table.Row>
<Table.Head>Name</Table.Head>
<Table.Head>Symbol</Table.Head>
<Table.Head>Position</Table.Head>
<Table.Head>Mass</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{data.slice(3, 6).map(i => (
<Table.Row key={i.name}>
<Table.Data>{i.name}</Table.Data>
<Table.Data>{i.symbol}</Table.Data>
<Table.Data>{i.position}</Table.Data>
<Table.Data>{i.mass}</Table.Data>
</Table.Row>
))}
</Table.Body>
</Table>
);
}
Declarative Props API
import { Table, dataRenderer, type DataTableProps } from "@/ui/table";
const data = [
{
name: "Sodium",
symbol: "Na",
position: 11,
mass: 22.99,
description: "A soft metal that reacts vigorously with water."
},
{
name: "Magnesium",
symbol: "Mg",
position: 12,
mass: 24.305,
description: "A metal used in lightweight alloys."
}
// others
];
const tableData: DataTableProps = {
caption: "Some elements from periodic table",
head: ["Element name", "Symbol", "Element position", "Atomic mass", "Description"],
body: dataRenderer(data, ["name", "symbol", "position", "mass", "description"])
};
export function TableDemo() {
return <Table data={tableData} overflowWrap spacing={{ x: "16px", y: "20px" }} />;
}
API References
Styles API
type SeLv_0 = "root" | "table" | "caption" | "resizer";
type SeLv_1 = "thead" | "tbody" | "tfoot";
type SeLv_2 = `${SeLv_1}.tr`;
type SeLv_3 = "thead.tr.th" | "tbody.tr.td" | "tfoot.tr.td";
type SeLv_S = "tr" | "th" | "td";
type Selector = NonNullable<SeLv_0 | SeLv_1 | SeLv_2 | SeLv_3 | SeLv_S>;
export type StyleComposed<R> = Partial<Record<SeLv_0 | SeLv_1 | SeLv_S, R>> & Partial<Record<SeLv_2 | SeLv_3, R | ((index: number) => R)>>;
type CSSProperties = React.CSSProperties & { [key: string]: any };
Styles API | Type | Default | Annotation |
---|---|---|---|
variant? | "default" | "tile" | default | defined style and classes |
className? | string | undefined | pass to root component <div> |
style? | CSSProperties | undefined | pass to root component <div> |
unstyled? | boolean | StyleComposed<boolean> | false | if true , default styles will be removed |
classNames? | StyleComposed<string | false> | undefined | |
styles? | StyleComposed<CSSProperties> | undefined |
Props API
type Spacing = { x?: number | string; y?: number | string };
type Striped = boolean | "odd" | "even" | undefined;
Props API | Type | Default | Annotation |
---|---|---|---|
data? | DataTableProps | undefined | Data that should be used to generate table, ignored if children prop is set |
orientation? | "horizontal" | "vertical" | "horizontal" | Determines whether the first column in each row is sticky or not |
layout? | ["tableLayout"] | auto | Value of table-layout style |
striped? | Striped | undefined | Determines whether every odd/even row background should be changed to strippedColor |
captionSide? | "top" | "bottom" | bottom | Determines on which side Table.Caption is displayed |
overflowWrap? | boolean | true | Set table scroll area |
stickyHeader? | boolean | true | Determines whether Table.Thead should be sticky |
stickyHeaderOffset? | number | string | 0 | Offset from top at which Table.Thead should become sticky |
spacing? | Spacing | DEFAULT_SPACE (by variant) | x = Horizontal, y = Vertical cells spacing or any valid CSS value for padding |
highlightOnHover? | boolean | true | Determines whether table rows background should change to highlightOnHoverColor when hovered |
withTableBorder? | boolean | true | Determines whether the table should have outer border |
withColumnBorders? | boolean | false | Determines whether the table should have borders between columns |
withRowBorders? | boolean | true | Determines whether the table should have borders between rows |
stickyHeader? | boolean | false | Set table scroll area |
highlightOnHoverColor? | Property.Color | hsl(var(--primitive)) | Background color of table rows when hovered, key of CSS color |
borderColor? | Property.Color | hsl(var(--border)) | Color of table borders, key of CSS color |
stripedColor? | Property.Color | hsl(var(--background-box)) | Background color of striped rows, key of CSS color |
Source Codes
Full working code example, including necessary markup and styles. You can copy and paste this code directly to start using the component immediately.