MRT logoMaterial React Table

Legacy V2 Docs
On This Page

    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

    1
    boolean | 'context-menu' | ((cell: MRT_Cell<TData>) => 'context-menu' | boolean)
    false
    MRT Click to Copy Docs
    2
    ButtonProps | ({ cell, column, row, table }) => ButtonProps
    Material UI Button Props

    Relevant Column Options

    1
    boolean | 'context-menu' | ((cell: MRT_Cell<TData>) => 'context-menu' | boolean)
    MRT Click to Copy Docs
    2
    ButtonProps | ({ cell, column, row, table }) => ButtonProps
    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

    Demo

    Open StackblitzOpen Code SandboxOpen on GitHub
    DylanMurray
    RaquelKohler
    ErvinReinger
    BrittanyMcCullough
    BransonFrami
    1-5 of 5

    Source Code

    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';
    5
    6const 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 );
    35
    36 return <MaterialReactTable columns={columns} data={data} />;
    37};
    38
    39export default Example;
    40

    View Extra Storybook Examples