1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-desktop/gui/ToolbarButton/ToolbarButton.tsx

82 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-09-15 15:01:07 +02:00
import * as React from 'react';
import { ToolbarButtonInfo } from '@joplin/lib/services/commands/ToolbarButtonUtils';
import { StyledIconSpan, StyledIconI } from './styles';
2020-09-15 15:01:07 +02:00
interface Props {
readonly themeId: number;
readonly toolbarButtonInfo?: ToolbarButtonInfo;
readonly title?: string;
readonly tooltip?: string;
readonly iconName?: string;
readonly disabled?: boolean;
readonly backgroundHover?: boolean;
readonly tabIndex?: number;
buttonRef?: React.Ref<HTMLButtonElement>;
2020-09-15 15:01:07 +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]);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
function getProp(props: Props, name: string, defaultValue: any = null) {
// 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;
// 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];
}
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) {
const IconClass = isFontAwesomeIcon(iconName) ? StyledIconI : StyledIconSpan;
icon = <IconClass className={iconName} aria-label='' hasTitle={!!title} role='img'/>;
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;
const classes = ['button', 'toolbar-button'];
2020-09-15 15:01:07 +02:00
if (!isEnabled) classes.push('disabled');
if (title) classes.push('-has-title');
2020-09-15 15:01:07 +02:00
const onClick = getProp(props, 'onClick');
const style: React.CSSProperties = {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis' };
const disabled = !isEnabled;
2020-09-15 15:01:07 +02:00
return (
<button
2020-09-15 15:01:07 +02:00
className={classes.join(' ')}
title={tooltip}
onClick={() => {
if (isEnabled && onClick) onClick();
}}
ref={props.buttonRef}
// 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}
aria-label={!title ? tooltip : undefined}
aria-description={title ? tooltip : undefined}
aria-disabled={!isEnabled}
tabIndex={props.tabIndex}
2020-09-15 15:01:07 +02:00
>
{icon}
<span style={style}>{title}</span>
</button>
2020-09-15 15:01:07 +02:00
);
}