2020-01-25 14:52:03 -03:00
|
|
|
import "./ToolIcon.scss";
|
|
|
|
|
2020-01-29 16:31:49 +00:00
|
|
|
import React, { useEffect } from "react";
|
2020-01-25 14:52:03 -03:00
|
|
|
|
|
|
|
type ToolIconSize = "s" | "m";
|
|
|
|
|
|
|
|
type ToolButtonBaseProps = {
|
2020-01-29 02:30:03 +02:00
|
|
|
icon?: React.ReactNode;
|
2020-01-25 14:52:03 -03:00
|
|
|
"aria-label": string;
|
|
|
|
"aria-keyshortcuts"?: string;
|
2020-01-29 02:30:03 +02:00
|
|
|
label?: string;
|
2020-01-25 14:52:03 -03:00
|
|
|
title?: string;
|
|
|
|
name?: string;
|
|
|
|
id?: string;
|
|
|
|
size?: ToolIconSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ToolButtonProps =
|
|
|
|
| (ToolButtonBaseProps & { type: "button"; onClick?(): void })
|
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "radio";
|
|
|
|
|
|
|
|
checked: boolean;
|
|
|
|
onChange?(): void;
|
|
|
|
});
|
|
|
|
|
|
|
|
const DEFAULT_SIZE: ToolIconSize = "m";
|
|
|
|
|
2020-01-25 19:37:58 -03:00
|
|
|
export const ToolButton = React.forwardRef(function(
|
|
|
|
props: ToolButtonProps,
|
|
|
|
ref,
|
|
|
|
) {
|
2020-01-29 16:31:49 +00:00
|
|
|
const innerRef = React.useRef<HTMLInputElement | HTMLButtonElement | null>(
|
|
|
|
null,
|
|
|
|
);
|
2020-01-25 19:37:58 -03:00
|
|
|
React.useImperativeHandle(ref, () => innerRef.current);
|
2020-01-25 14:52:03 -03:00
|
|
|
const sizeCn = `ToolIcon_size_${props.size || DEFAULT_SIZE}`;
|
|
|
|
|
2020-01-29 16:31:49 +00:00
|
|
|
const prevChecked = React.useRef<boolean>(
|
|
|
|
"checked" in props && props.checked,
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (props.type !== "button") {
|
|
|
|
if (props.checked && !prevChecked.current && innerRef.current) {
|
|
|
|
innerRef.current.focus();
|
|
|
|
}
|
|
|
|
prevChecked.current = props.checked;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-25 14:52:03 -03:00
|
|
|
if (props.type === "button")
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={`ToolIcon_type_button ToolIcon ${sizeCn}`}
|
|
|
|
title={props.title}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
type="button"
|
|
|
|
onClick={props.onClick}
|
2020-01-29 16:31:49 +00:00
|
|
|
ref={node => (innerRef.current = node)}
|
2020-01-25 14:52:03 -03:00
|
|
|
>
|
|
|
|
<div className="ToolIcon__icon" aria-hidden="true">
|
2020-01-29 02:30:03 +02:00
|
|
|
{props.icon || props.label}
|
2020-01-25 14:52:03 -03:00
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2020-01-26 20:49:18 +01:00
|
|
|
<label className="ToolIcon" title={props.title}>
|
2020-01-25 14:52:03 -03:00
|
|
|
<input
|
|
|
|
className={`ToolIcon_type_radio ${sizeCn}`}
|
|
|
|
type="radio"
|
|
|
|
name={props.name}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
aria-keyshortcuts={props["aria-keyshortcuts"]}
|
|
|
|
id={props.id}
|
|
|
|
onChange={props.onChange}
|
|
|
|
checked={props.checked}
|
2020-01-29 16:31:49 +00:00
|
|
|
ref={node => (innerRef.current = node)}
|
2020-01-25 14:52:03 -03:00
|
|
|
/>
|
|
|
|
<div className="ToolIcon__icon">{props.icon}</div>
|
|
|
|
</label>
|
|
|
|
);
|
2020-01-25 19:37:58 -03:00
|
|
|
});
|