// packages/components/src/clipboard-button/index.tsx
import clsx from "clsx";
import { useRef, useEffect } from "@wordpress/element";
import { useCopyToClipboard } from "@wordpress/compose";
import deprecated from "@wordpress/deprecated";
import Button from "../button/index.js";
import { jsx as _jsx } from "react/jsx-runtime";
var TIMEOUT = 4e3;
function ClipboardButton({
  className,
  children,
  onCopy,
  onFinishCopy,
  text,
  ...buttonProps
}) {
  deprecated("wp.components.ClipboardButton", {
    since: "5.8",
    alternative: "wp.compose.useCopyToClipboard"
  });
  const timeoutIdRef = useRef();
  const ref = useCopyToClipboard(text, () => {
    onCopy();
    if (timeoutIdRef.current) {
      clearTimeout(timeoutIdRef.current);
    }
    if (onFinishCopy) {
      timeoutIdRef.current = setTimeout(() => onFinishCopy(), TIMEOUT);
    }
  });
  useEffect(() => {
    return () => {
      if (timeoutIdRef.current) {
        clearTimeout(timeoutIdRef.current);
      }
    };
  }, []);
  const classes = clsx("components-clipboard-button", className);
  const focusOnCopyEventTarget = (event) => {
    event.target.focus();
  };
  return /* @__PURE__ */ _jsx(Button, {
    ...buttonProps,
    className: classes,
    ref,
    onCopy: focusOnCopyEventTarget,
    children
  });
}
export {
  ClipboardButton as default
};
//# sourceMappingURL=index.js.map
