2020-01-25 14:52:03 -03:00
|
|
|
import "./ToolIcon.scss";
|
|
|
|
|
2020-01-30 10:40:49 +01:00
|
|
|
import React 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-04-07 14:39:06 +03:00
|
|
|
"data-testid"?: 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;
|
2020-02-01 17:31:28 +00:00
|
|
|
keyBindingLabel?: string;
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel?: boolean;
|
2020-02-21 14:34:18 -05:00
|
|
|
visible?: boolean;
|
2020-03-01 14:39:03 -05:00
|
|
|
selected?: boolean;
|
2020-03-11 19:42:18 +01:00
|
|
|
className?: string;
|
2020-01-25 14:52:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
type ToolButtonProps =
|
2020-03-11 22:48:27 +01:00
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "button";
|
|
|
|
children?: React.ReactNode;
|
|
|
|
onClick?(): void;
|
|
|
|
})
|
2020-01-25 14:52:03 -03:00
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "radio";
|
|
|
|
|
|
|
|
checked: boolean;
|
|
|
|
onChange?(): void;
|
|
|
|
});
|
|
|
|
|
|
|
|
const DEFAULT_SIZE: ToolIconSize = "m";
|
|
|
|
|
2020-03-23 13:05:07 +02:00
|
|
|
export const ToolButton = React.forwardRef(function (
|
2020-01-25 19:37:58 -03:00
|
|
|
props: ToolButtonProps,
|
|
|
|
ref,
|
|
|
|
) {
|
2020-01-30 10:40:49 +01:00
|
|
|
const innerRef = React.useRef(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-02-02 20:04:35 +02:00
|
|
|
if (props.type === "button") {
|
2020-01-25 14:52:03 -03:00
|
|
|
return (
|
|
|
|
<button
|
2020-03-01 14:39:03 -05:00
|
|
|
className={`ToolIcon_type_button ToolIcon ${sizeCn}${
|
|
|
|
props.selected ? " ToolIcon--selected" : ""
|
2020-04-25 18:43:02 +05:30
|
|
|
} ${props.className || ""} ${
|
|
|
|
props.visible
|
|
|
|
? "ToolIcon_type_button--hide"
|
|
|
|
: "ToolIcon_type_button--show"
|
|
|
|
}`}
|
2020-01-25 14:52:03 -03:00
|
|
|
title={props.title}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
type="button"
|
|
|
|
onClick={props.onClick}
|
2020-01-30 10:40:49 +01:00
|
|
|
ref={innerRef}
|
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>
|
2020-02-20 18:44:38 -05:00
|
|
|
{props.showAriaLabel && (
|
|
|
|
<div className="ToolIcon__label">{props["aria-label"]}</div>
|
|
|
|
)}
|
2020-03-11 22:48:27 +01:00
|
|
|
{props.children}
|
2020-01-25 14:52:03 -03:00
|
|
|
</button>
|
|
|
|
);
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2020-01-25 14:52:03 -03:00
|
|
|
|
|
|
|
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"]}
|
2020-04-07 14:39:06 +03:00
|
|
|
data-testid={props["data-testid"]}
|
2020-01-25 14:52:03 -03:00
|
|
|
id={props.id}
|
|
|
|
onChange={props.onChange}
|
|
|
|
checked={props.checked}
|
2020-01-30 10:40:49 +01:00
|
|
|
ref={innerRef}
|
2020-01-25 14:52:03 -03:00
|
|
|
/>
|
2020-02-01 17:31:28 +00:00
|
|
|
<div className="ToolIcon__icon">
|
|
|
|
{props.icon}
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
<span className="ToolIcon__keybinding">{props.keyBindingLabel}</span>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-01-25 14:52:03 -03:00
|
|
|
</label>
|
|
|
|
);
|
2020-01-25 19:37:58 -03:00
|
|
|
});
|