import React, {useState} from "react"; export type EmulatedButtonProps = React.ButtonHTMLAttributes; /** * Because WordPress is sigh capturing all mouse down clicks, and sometimes you have to click multiple times for the fuckass button to react. so let's capture the mouse down and up events and trigger click manually. sigh again. */ export const EmulatedButton = React.forwardRef(({ onClick, onMouseDownCapture, onMouseUp, onMouseLeave, children, ...rest }, ref) => { const [pressed, setPressed] = useState(false); const handleMouseDownCapture: React.MouseEventHandler = (e) => { if (onMouseDownCapture?.(e) === false) { return; } e.preventDefault(); // Prevent focus change e.stopPropagation(); // Prevent other handlers setPressed(true); }; const handleMouseUp: React.MouseEventHandler = (e) => { e.preventDefault(); // Prevent focus change e.stopPropagation(); // Prevent other handlers onMouseUp?.(e); if (pressed) { // Manually invoke the provided click handler. onClick?.(e as any); } setPressed(false); }; const handleMouseLeave: React.MouseEventHandler = (e) => { onMouseLeave?.(e); setPressed(false); }; return ( ); });