2020-09-15 15:01:07 +02:00
|
|
|
import * as React from 'react';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { ToolbarButtonInfo } from '@joplin/lib/services/commands/ToolbarButtonUtils';
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
interface Props {
|
2020-11-12 21:29:22 +02:00
|
|
|
readonly themeId: number;
|
|
|
|
readonly toolbarButtonInfo?: ToolbarButtonInfo;
|
|
|
|
readonly title?: string;
|
|
|
|
readonly tooltip?: string;
|
|
|
|
readonly iconName?: string;
|
|
|
|
readonly disabled?: boolean;
|
|
|
|
readonly backgroundHover?: boolean;
|
2024-08-03 17:42:46 +02:00
|
|
|
readonly tabIndex?: number;
|
|
|
|
|
|
|
|
buttonRef?: React.Ref<HTMLButtonElement>;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function isFontAwesomeIcon(iconName: string) {
|
2020-09-15 15:01:07 +02:00
|
|
|
const s = iconName.split(' ');
|
|
|
|
return s.length === 2 && ['fa', 'fas'].includes(s[0]);
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function getProp(props: Props, name: string, defaultValue: any = null) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-09-15 15:01:07 +02:00
|
|
|
if (props.toolbarButtonInfo && (name in props.toolbarButtonInfo)) return (props.toolbarButtonInfo as any)[name];
|
|
|
|
if (!(name in props)) return defaultValue;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-09-15 15:01:07 +02:00
|
|
|
return (props as any)[name];
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function ToolbarButton(props: Props) {
|
2020-09-15 15:01:07 +02:00
|
|
|
const title = getProp(props, 'title', '');
|
|
|
|
const tooltip = getProp(props, 'tooltip', title);
|
|
|
|
|
|
|
|
let icon = null;
|
|
|
|
const iconName = getProp(props, 'iconName');
|
|
|
|
if (iconName) {
|
2024-10-11 23:03:41 +02:00
|
|
|
const iconProps: React.HTMLProps<HTMLDivElement> = {
|
2024-11-09 14:50:06 +02:00
|
|
|
'aria-hidden': true,
|
2024-10-11 23:03:41 +02:00
|
|
|
role: 'img',
|
|
|
|
className: `toolbar-icon ${title ? '-has-title' : ''} ${iconName}`,
|
|
|
|
};
|
|
|
|
icon = isFontAwesomeIcon(iconName) ? <i {...iconProps} /> : <span {...iconProps} />;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep this for legacy compatibility but for consistency we should use "disabled" prop
|
|
|
|
let isEnabled = getProp(props, 'enabled', null);
|
|
|
|
if (isEnabled === null) isEnabled = true;
|
|
|
|
if (props.disabled) isEnabled = false;
|
|
|
|
|
2024-08-03 17:42:46 +02:00
|
|
|
const classes = ['button', 'toolbar-button'];
|
2020-09-15 15:01:07 +02:00
|
|
|
if (!isEnabled) classes.push('disabled');
|
2024-08-03 17:42:46 +02:00
|
|
|
if (title) classes.push('-has-title');
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
const onClick = getProp(props, 'onClick');
|
2024-03-04 12:33:39 +02:00
|
|
|
const style: React.CSSProperties = {
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
overflow: 'hidden',
|
|
|
|
textOverflow: 'ellipsis' };
|
2024-07-28 15:53:32 +02:00
|
|
|
const disabled = !isEnabled;
|
2024-08-03 17:42:46 +02:00
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
return (
|
2024-08-03 17:42:46 +02:00
|
|
|
<button
|
2020-09-15 15:01:07 +02:00
|
|
|
className={classes.join(' ')}
|
|
|
|
title={tooltip}
|
|
|
|
onClick={() => {
|
|
|
|
if (isEnabled && onClick) onClick();
|
|
|
|
}}
|
2024-08-03 17:42:46 +02:00
|
|
|
ref={props.buttonRef}
|
2024-07-28 15:53:32 +02:00
|
|
|
|
|
|
|
// At least on MacOS, the disabled HTML prop isn't sufficient for the screen reader
|
|
|
|
// to read the element as disable. For this, aria-disabled is necessary.
|
|
|
|
disabled={disabled}
|
2024-08-03 17:42:46 +02:00
|
|
|
aria-label={!title ? tooltip : undefined}
|
|
|
|
aria-description={title ? tooltip : undefined}
|
2024-07-28 15:53:32 +02:00
|
|
|
aria-disabled={!isEnabled}
|
2024-08-03 17:42:46 +02:00
|
|
|
tabIndex={props.tabIndex}
|
2020-09-15 15:01:07 +02:00
|
|
|
>
|
|
|
|
{icon}
|
2024-03-04 12:33:39 +02:00
|
|
|
<span style={style}>{title}</span>
|
2024-08-03 17:42:46 +02:00
|
|
|
</button>
|
2020-09-15 15:01:07 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|