Files
Trilium/apps/client/src/widgets/react/Button.tsx

42 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-08-03 23:20:32 +03:00
import { RefObject } from "preact";
2025-08-03 19:50:39 +03:00
import { useRef } from "preact/hooks";
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>;
text: string;
className?: string;
icon?: string;
keyboardShortcut?: string;
/** 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;
}
export default function Button({ buttonRef: _buttonRef, className, text, onClick, keyboardShortcut, icon }: ButtonProps) {
const classes: string[] = ["btn"];
classes.push("btn-primary");
if (className) {
classes.push(className);
}
2025-08-03 23:20:32 +03:00
const buttonRef = _buttonRef ?? useRef<HTMLButtonElement>(null);
const splitShortcut = (keyboardShortcut ?? "").split("+");
return (
<button
className={classes.join(" ")}
type={onClick ? "button" : "submit"}
onClick={onClick}
ref={buttonRef}
>
{icon && <span className={`bx ${icon}`}></span>}
{text} {keyboardShortcut && (
splitShortcut.map((key, index) => (
<>
<kbd key={index}>{key.toUpperCase()}</kbd>{ index < splitShortcut.length - 1 ? "+" : "" }
</>
))
)}
</button>
);
}