mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 14:43:06 +08:00
7879e1f600
* fix: add execCommand fallback for clipboard
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
// Source: https://github.com/mantinedev/mantine/blob/master/packages/@mantine/core/src/components/CopyButton/CopyButton.tsx - MIT
|
|
// modified to use the polyfilled clipboard api
|
|
import React from "react";
|
|
import { useClipboard } from "@/hooks/use-clipboard";
|
|
import { useProps } from "@mantine/core";
|
|
|
|
interface CopyButtonProps {
|
|
/** Children callback, provides current status and copy function as an argument */
|
|
children: (payload: { copied: boolean; copy: () => void }) => React.ReactNode;
|
|
|
|
/** Value that is copied to the clipboard when the button is clicked */
|
|
value: string;
|
|
|
|
/** Copied status timeout in ms @default `1000` */
|
|
timeout?: number;
|
|
}
|
|
|
|
const defaultProps = {
|
|
timeout: 1000,
|
|
} satisfies Partial<CopyButtonProps>;
|
|
|
|
export function CopyButton(props: CopyButtonProps) {
|
|
const { children, timeout, value, ...others } = useProps(
|
|
"CopyButton",
|
|
defaultProps,
|
|
props,
|
|
);
|
|
const clipboard = useClipboard({ timeout });
|
|
const copy = () => clipboard.copy(value);
|
|
return <>{children({ copy, copied: clipboard.copied, ...others })}</>;
|
|
}
|
|
|
|
CopyButton.displayName = "@mantine/core/CopyButton";
|