2025-08-10 15:21:49 +03:00
|
|
|
import type { RefObject } from "preact";
|
|
|
|
|
import type { CSSProperties } from "preact/compat";
|
2025-08-10 17:19:39 +03:00
|
|
|
import { useRef, useMemo } from "preact/hooks";
|
|
|
|
|
import { memo } from "preact/compat";
|
2025-08-03 19:06:21 +03:00
|
|
|
|
|
|
|
|
interface ButtonProps {
|
2025-08-03 23:20:32 +03:00
|
|
|
/** Reference to the button element. Mostly useful for requesting focus. */
|
2025-08-04 12:58:42 +03:00
|
|
|
buttonRef?: RefObject<HTMLButtonElement>;
|
2025-08-03 19:06:21 +03:00
|
|
|
text: string;
|
|
|
|
|
className?: string;
|
2025-08-05 15:39:49 +03:00
|
|
|
icon?: string;
|
2025-08-03 19:06:21 +03:00
|
|
|
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-05 18:05:41 +03:00
|
|
|
primary?: boolean;
|
2025-08-05 23:03:38 +03:00
|
|
|
disabled?: boolean;
|
2025-08-14 21:31:09 +03:00
|
|
|
size: "normal" | "small" | "micro";
|
2025-08-06 16:16:30 +03:00
|
|
|
style?: CSSProperties;
|
2025-08-03 19:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 21:31:09 +03:00
|
|
|
const Button = memo(({ buttonRef: _buttonRef, className, text, onClick, keyboardShortcut, icon, primary, disabled, size, style }: ButtonProps) => {
|
2025-08-10 17:19:39 +03:00
|
|
|
// Memoize classes array to prevent recreation
|
|
|
|
|
const classes = useMemo(() => {
|
|
|
|
|
const classList: string[] = ["btn"];
|
|
|
|
|
if (primary) {
|
|
|
|
|
classList.push("btn-primary");
|
|
|
|
|
} else {
|
|
|
|
|
classList.push("btn-secondary");
|
|
|
|
|
}
|
|
|
|
|
if (className) {
|
|
|
|
|
classList.push(className);
|
|
|
|
|
}
|
2025-08-14 21:31:09 +03:00
|
|
|
if (size === "small") {
|
2025-08-10 17:19:39 +03:00
|
|
|
classList.push("btn-sm");
|
2025-08-14 21:31:09 +03:00
|
|
|
} else if (size === "micro") {
|
|
|
|
|
classList.push("btn-micro");
|
2025-08-10 17:19:39 +03:00
|
|
|
}
|
|
|
|
|
return classList.join(" ");
|
2025-08-14 21:31:09 +03:00
|
|
|
}, [primary, className, size]);
|
2025-08-03 19:06:21 +03:00
|
|
|
|
2025-08-03 23:20:32 +03:00
|
|
|
const buttonRef = _buttonRef ?? useRef<HTMLButtonElement>(null);
|
2025-08-10 17:19:39 +03:00
|
|
|
|
|
|
|
|
// Memoize keyboard shortcut rendering
|
|
|
|
|
const shortcutElements = useMemo(() => {
|
|
|
|
|
if (!keyboardShortcut) return null;
|
|
|
|
|
const splitShortcut = keyboardShortcut.split("+");
|
|
|
|
|
return splitShortcut.map((key, index) => (
|
|
|
|
|
<>
|
|
|
|
|
<kbd key={index}>{key.toUpperCase()}</kbd>
|
|
|
|
|
{index < splitShortcut.length - 1 ? "+" : ""}
|
|
|
|
|
</>
|
|
|
|
|
));
|
|
|
|
|
}, [keyboardShortcut]);
|
2025-08-03 19:06:21 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
2025-08-10 17:19:39 +03:00
|
|
|
className={classes}
|
2025-08-03 20:01:54 +03:00
|
|
|
type={onClick ? "button" : "submit"}
|
2025-08-03 19:06:21 +03:00
|
|
|
onClick={onClick}
|
|
|
|
|
ref={buttonRef}
|
2025-08-05 23:03:38 +03:00
|
|
|
disabled={disabled}
|
2025-08-06 16:16:30 +03:00
|
|
|
style={style}
|
2025-08-03 19:06:21 +03:00
|
|
|
>
|
2025-08-05 15:39:49 +03:00
|
|
|
{icon && <span className={`bx ${icon}`}></span>}
|
2025-08-10 17:19:39 +03:00
|
|
|
{text} {shortcutElements}
|
2025-08-03 19:06:21 +03:00
|
|
|
</button>
|
|
|
|
|
);
|
2025-08-10 17:19:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default Button;
|