2025-08-03 19:50:39 +03:00
|
|
|
import { useRef } from "preact/hooks";
|
2025-08-03 19:06:21 +03:00
|
|
|
|
|
|
|
|
interface ButtonProps {
|
|
|
|
|
text: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
keyboardShortcut?: string;
|
2025-08-03 20:01:54 +03:00
|
|
|
/** Called when the button is clicked. If not set, the button will submit the form (if any). */
|
2025-08-03 19:50:39 +03:00
|
|
|
onClick?: () => void;
|
2025-08-03 19:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Button({ className, text, onClick, keyboardShortcut }: ButtonProps) {
|
|
|
|
|
const classes: string[] = ["btn"];
|
|
|
|
|
classes.push("btn-primary");
|
|
|
|
|
if (className) {
|
|
|
|
|
classes.push(className);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
|
|
|
const splitShortcut = (keyboardShortcut ?? "").split("+");
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
className={classes.join(" ")}
|
2025-08-03 20:01:54 +03:00
|
|
|
type={onClick ? "button" : "submit"}
|
2025-08-03 19:06:21 +03:00
|
|
|
onClick={onClick}
|
|
|
|
|
ref={buttonRef}
|
|
|
|
|
>
|
|
|
|
|
{text} {keyboardShortcut && (
|
|
|
|
|
splitShortcut.map((key, index) => (
|
|
|
|
|
<>
|
|
|
|
|
<kbd key={index}>{key.toUpperCase()}</kbd>{ index < splitShortcut.length - 1 ? "+" : "" }
|
|
|
|
|
</>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|