Click to Copy Feature Guide
Material React Table has an easy-to-implement feature that allows a user to copy a cell's value to the clipboard.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| MRT Click to Copy Docs | ||
2 |
| Material UI Button Props | |||
Relevant Column Options
# | Column Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| MRT Click to Copy Docs | |||
2 |
| Material UI Button API | |||
Enable Click to Copy Per Column
Most likely, there will just be a couple columns that you want to enable click to copy for. You can do this by setting the enableClickToCopy
option to true
per column on the column definition.
const columns = [//...{accessorKey: 'email',header: 'Email',enableClickToCopy: true,},//...];
Enable Click to Copy For All Cells
Alternatively, you can enable click to copy globally by setting the enableClickToCopy
table option to true
. You could then opt out per column by setting the enableClickToCopy
option to false
on the column definition.
const table = useMaterialReactTable({columns,data,enableClickToCopy: true,});
Customize Copy Buttons
The click to copy feature is built on top of the Material UnstyledButton and CopyButton components. You can customize the copy button by passing in the muiCopyButtonProps
table or column option.
const table = useMaterialReactTable({columns,data,enableClickToCopy: true,muiCopyButtonProps: {sx: { width: '100%' },startIcon: <ContentCopy />,},});
Click to Copy Example
First Name | Last Name | Email | City |
---|---|---|---|
Dylan | Murray | ||
Raquel | Kohler | ||
Ervin | Reinger | ||
Brittany | McCullough | ||
Branson | Frami |
10
1import { useMemo } from 'react';2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { ContentCopy } from '@mui/icons-material';4import { data, type Person } from './makeData';56const Example = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 () => [9 {10 accessorKey: 'firstName',11 header: 'First Name',12 },13 {14 accessorKey: 'lastName',15 header: 'Last Name',16 },17 {18 accessorKey: 'email',19 header: 'Email',20 enableClickToCopy: true,21 muiCopyButtonProps: {22 fullWidth: true,23 startIcon: <ContentCopy />,24 sx: { justifyContent: 'flex-start' },25 },26 },27 {28 accessorKey: 'city',29 header: 'City',30 enableClickToCopy: true,31 },32 ],33 [],34 );3536 return <MaterialReactTable columns={columns} data={data} />;37};3839export default Example;40
View Extra Storybook Examples