Examples › Custom content

You can also display custom content in the context menu by providing a render callback to the showContextMenu function instead of an array of items.
The render callback will receive a close function that you can use to close the menu.
Have a look at the example below to see how it works:

'use client';

import { useContextMenu } from 'mantine-contextmenu';
import { Flex } from '@mantine/core';
import { IconCopy, IconDownload, IconFlipHorizontal, IconFlipVertical } from '@tabler/icons-react';
import { useState } from 'react';
import { Picture } from '~/components/Picture';
import { copyImageToClipboard, downloadImage, unsplashImages } from '~/lib/images';
import { FancyButton } from './FancyButton';

export function CustomContentExample() {
  const { showContextMenu } = useContextMenu();

  const [flipVertical, setFlipVertical] = useState(false);
  const [flipHorizontal, setFlipHorizontal] = useState(false);
  const image = unsplashImages[7];
  const { src } = image.file;
  return (
    <Picture
      image={image}
      flipVertical={flipVertical}
      flipHorizontal={flipHorizontal}
      onContextMenu={showContextMenu((close) => (
        <Flex wrap="wrap" w={200}>
          <FancyButton
            icon={<IconCopy size={16} />}
            text="Copy"
            onClick={() => {
              close();
              copyImageToClipboard(src);
            }}
          />
          <FancyButton
            icon={<IconDownload size={16} />}
            text="Download"
            onClick={() => {
              close();
              downloadImage(src);
            }}
          />
          <FancyButton
            icon={<IconFlipVertical size={16} />}
            text="Flip vertical"
            onClick={() => {
              close();
              setFlipVertical((v) => !v);
            }}
          />
          <FancyButton
            icon={<IconFlipHorizontal size={16} />}
            text="Flip horizontal"
            onClick={() => {
              close();
              setFlipHorizontal((v) => !v);
            }}
          />
        </Flex>
      ))}
    />
  );
}

Right-click on the image to trigger the context menu:

Interactive content

Custom content can contain interactive elements such as inputs, sliders or checkboxes; the menu is only closed when clicking outside of it, pressing Escape, or when you call the close function.
Keep in mind that the render callback is called with the values available when the menu is shown, so interactive content should keep its own state in a separate component, like in the example below:

'use client';

import { useContextMenu } from 'mantine-contextmenu';
import { Button, Group, Slider, Stack, Text } from '@mantine/core';
import { useState } from 'react';
import { Picture } from '~/components/Picture';
import { unsplashImages } from '~/lib/images';

type Adjustments = { brightness: number; contrast: number; saturation: number };

const DEFAULT_ADJUSTMENTS: Adjustments = { brightness: 100, contrast: 100, saturation: 100 };

type AdjustmentsMenuProps = {
  initialValue: Adjustments;
  onChange: (value: Adjustments) => void;
  close: () => void;
};

// The menu content is rendered from the callback passed to `showContextMenu` when the menu is shown,
// so interactive controls should keep their own state in a component like this one
function AdjustmentsMenu({ initialValue, onChange, close }: AdjustmentsMenuProps) {
  const [value, setValue] = useState(initialValue);

  const update = (changes: Partial<Adjustments>) => {
    const newValue = { ...value, ...changes };
    setValue(newValue);
    onChange(newValue);
  };

  return (
    <Stack w={240} p="sm" gap="xs">
      {(Object.keys(DEFAULT_ADJUSTMENTS) as (keyof Adjustments)[]).map((key) => (
        <div key={key}>
          <Text size="xs" tt="capitalize">
            {key}
          </Text>
          <Slider
            size="sm"
            min={0}
            max={200}
            label={(v) => `${v}%`}
            value={value[key]}
            onChange={(v) => update({ [key]: v })}
            thumbLabel={key}
          />
        </div>
      ))}
      <Group justify="space-between" mt="xs">
        <Button size="compact-xs" variant="default" onClick={() => update(DEFAULT_ADJUSTMENTS)}>
          Reset
        </Button>
        <Button size="compact-xs" onClick={close}>
          Done
        </Button>
      </Group>
    </Stack>
  );
}

export function InteractiveContentExample() {
  const { showContextMenu } = useContextMenu();
  const [adjustments, setAdjustments] = useState(DEFAULT_ADJUSTMENTS);
  const { brightness, contrast, saturation } = adjustments;

  return (
    <Picture
      image={unsplashImages[4]}
      filter={`brightness(${brightness}%) contrast(${contrast}%) saturate(${saturation}%)`}
      onContextMenu={showContextMenu((close) => (
        <AdjustmentsMenu initialValue={adjustments} onChange={setAdjustments} close={close} />
      ))}
    />
  );
}

Right-click on the image to adjust it:

Picture by Roberto Huczek | Mantine ContextMenu

Picture by Roberto Huczek

Head over to the next example to discover other features.