Table with a fixed header
A long table whose header stays in place as you scroll.
A long table whose header stays in place as you scroll.
npm install @mantine/core clsx
First time with Mantine UI? Set it up once.
/**
* Component from Mantine UI, https://ui.mantine.dev
* MIT License, Copyright (c) 2022 Vitaly Rtischev. Full text: https://github.com/mantinedev/ui.mantine.dev/blob/master/LICENCE
* Changed for Tastebud: sample addresses use example.com.
*/
import { useState } from 'react';
import cx from 'clsx';
import { ScrollArea, Table } from '@mantine/core';
import classes from './TableScrollArea.module.css';
const data = [
{
name: 'Athena Weissnat',
company: 'Little - Rippin',
email: 'Elouise.Prohaska@example.com',
},
{
name: 'Deangelo Runolfsson',
company: 'Greenfelder - Krajcik',
email: 'Kadin_Trantow87@example.com',
},
{
name: 'Danny Carter',
company: 'Kohler and Sons',
email: 'Marina3@example.com',
},
{
name: 'Trace Tremblay PhD',
company: 'Crona, Aufderhar and Senger',
email: 'Antonina.Pouros@example.com',
},
{
name: 'Derek Dibbert',
company: 'Gottlieb LLC',
email: 'Abagail29@example.com',
},
{
name: 'Viola Bernhard',
company: 'Funk, Rohan and Kreiger',
email: 'Jamie23@example.com',
},
{
name: 'Austin Jacobi',
company: 'Botsford - Corwin',
email: 'Genesis42@example.com',
},
{
name: 'Hershel Mosciski',
company: 'Okuneva, Farrell and Kilback',
email: 'Idella.Stehr28@example.com',
},
{
name: 'Mylene Ebert',
company: 'Kirlin and Sons',
email: 'Hildegard17@example.com',
},
{
name: 'Lou Trantow',
company: 'Parisian - Lemke',
email: 'Hillard.Barrows1@example.com',
},
{
name: 'Dariana Weimann',
company: 'Schowalter - Donnelly',
email: 'Colleen80@example.com',
},
{
name: 'Dr. Christy Herman',
company: 'VonRueden - Labadie',
email: 'Lilyan98@example.com',
},
{
name: 'Katelin Schuster',
company: 'Jacobson - Smitham',
email: 'Erich_Brekke76@example.com',
},
{
name: 'Melyna Macejkovic',
company: 'Schuster LLC',
email: 'Kylee4@example.com',
},
{
name: 'Pinkie Rice',
company: 'Wolf, Trantow and Zulauf',
email: 'Fiona.Kutch@example.com',
},
{
name: 'Brain Kreiger',
company: 'Lueilwitz Group',
email: 'Rico98@example.com',
},
{
name: 'Myrtice McGlynn',
company: 'Feest, Beahan and Johnston',
email: 'Julius_Tremblay29@example.com',
},
{
name: 'Chester Carter PhD',
company: 'Gaylord - Labadie',
email: 'Jensen_McKenzie@example.com',
},
{
name: 'Mrs. Ericka Bahringer',
company: 'Conn and Sons',
email: 'Lisandro56@example.com',
},
{
name: 'Korbin Buckridge Sr.',
company: 'Mraz, Rolfson and Predovic',
email: 'Leatha9@example.com',
},
{
name: 'Dr. Daisy Becker',
company: 'Carter - Mueller',
email: 'Keaton_Sanford27@example.com',
},
{
name: 'Derrick Buckridge Sr.',
company: "O'Reilly LLC",
email: 'Kay83@example.com',
},
{
name: 'Ernie Hickle',
company: "Terry, O'Reilly and Farrell",
email: 'Americo.Leffler89@example.com',
},
{
name: 'Jewell Littel',
company: "O'Connell Group",
email: 'Hester.Hettinger9@example.com',
},
{
name: 'Cyrus Howell',
company: 'Windler, Yost and Fadel',
email: 'Rick0@example.com',
},
{
name: 'Dr. Orie Jast',
company: 'Hilll - Pacocha',
email: 'Anna56@example.com',
},
{
name: 'Luisa Murphy',
company: 'Turner and Sons',
email: 'Christine32@example.com',
},
{
name: 'Lea Witting',
company: 'Hodkiewicz Inc',
email: 'Ford_Kovacek4@example.com',
},
{
name: 'Kelli Runolfsson',
company: "Feest - O'Hara",
email: 'Dimitri87@example.com',
},
{
name: 'Brook Gaylord',
company: 'Conn, Huel and Nader',
email: 'Immanuel77@example.com',
},
];
export function TableScrollArea() {
const [scrolled, setScrolled] = useState(false);
const rows = data.map((row) => (
<Table.Tr key={row.name}>
<Table.Td>{row.name}</Table.Td>
<Table.Td>{row.email}</Table.Td>
<Table.Td>{row.company}</Table.Td>
</Table.Tr>
));
return (
<ScrollArea h={300} onScrollPositionChange={({ y }) => setScrolled(y !== 0)}>
<Table miw={700}>
<Table.Thead className={cx(classes.header, { [classes.scrolled]: scrolled })}>
<Table.Tr>
<Table.Th>Name</Table.Th>
<Table.Th>Email</Table.Th>
<Table.Th>Company</Table.Th>
</Table.Tr>
</Table.Thead>
<Table.Tbody>{rows}</Table.Tbody>
</Table>
</ScrollArea>
);
}
/**
* Component from Mantine UI, https://ui.mantine.dev
* MIT License, Copyright (c) 2022 Vitaly Rtischev. Full text: https://github.com/mantinedev/ui.mantine.dev/blob/master/LICENCE
*/
.header {
position: sticky;
top: 0;
background-color: var(--mantine-color-body);
transition: box-shadow 150ms ease;
&::after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: 0;
border-bottom: 1px solid light-dark(var(--mantine-color-gray-2), var(--mantine-color-dark-3));
}
}
.scrolled {
box-shadow: var(--mantine-shadow-sm);
}
Changed for this site: Sample addresses use example.com.