2021-02-23 19:28:48 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CSSProperties, MouseEventHandler, ReactNode } from 'react';
|
|
|
|
import React from 'react';
|
2021-02-23 19:28:48 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import { assert } from '../util/assert';
|
|
|
|
|
2021-04-30 22:58:57 +00:00
|
|
|
export enum ButtonSize {
|
|
|
|
Medium,
|
|
|
|
Small,
|
|
|
|
}
|
|
|
|
|
2021-02-23 19:28:48 +00:00
|
|
|
export enum ButtonVariant {
|
2021-10-20 23:46:41 +00:00
|
|
|
Calling = 'Calling',
|
|
|
|
Destructive = 'Destructive',
|
|
|
|
Details = 'Details',
|
2021-08-26 20:51:55 +00:00
|
|
|
Primary = 'Primary',
|
|
|
|
Secondary = 'Secondary',
|
|
|
|
SecondaryAffirmative = 'SecondaryAffirmative',
|
|
|
|
SecondaryDestructive = 'SecondaryDestructive',
|
|
|
|
SystemMessage = 'SystemMessage',
|
2021-02-23 19:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 23:46:41 +00:00
|
|
|
export enum ButtonIconType {
|
|
|
|
audio = 'audio',
|
|
|
|
muted = 'muted',
|
|
|
|
photo = 'photo',
|
|
|
|
search = 'search',
|
|
|
|
text = 'text',
|
|
|
|
unmuted = 'unmuted',
|
|
|
|
video = 'video',
|
|
|
|
}
|
|
|
|
|
2021-02-23 19:28:48 +00:00
|
|
|
type PropsType = {
|
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
2021-10-20 23:46:41 +00:00
|
|
|
icon?: ButtonIconType;
|
2021-04-30 22:58:57 +00:00
|
|
|
size?: ButtonSize;
|
2021-08-17 21:45:18 +00:00
|
|
|
style?: CSSProperties;
|
|
|
|
tabIndex?: number;
|
2021-02-23 19:28:48 +00:00
|
|
|
variant?: ButtonVariant;
|
2021-03-09 19:16:56 +00:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
onClick: MouseEventHandler<HTMLButtonElement>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'submit';
|
|
|
|
}
|
2021-04-27 22:35:35 +00:00
|
|
|
) &
|
|
|
|
(
|
|
|
|
| {
|
|
|
|
'aria-label': string;
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
'aria-label'?: string;
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
'aria-label': string;
|
|
|
|
children?: ReactNode;
|
|
|
|
}
|
|
|
|
);
|
2021-02-23 19:28:48 +00:00
|
|
|
|
2021-04-30 22:58:57 +00:00
|
|
|
const SIZE_CLASS_NAMES = new Map<ButtonSize, string>([
|
|
|
|
[ButtonSize.Medium, 'module-Button--medium'],
|
|
|
|
[ButtonSize.Small, 'module-Button--small'],
|
|
|
|
]);
|
|
|
|
|
2021-02-23 19:28:48 +00:00
|
|
|
const VARIANT_CLASS_NAMES = new Map<ButtonVariant, string>([
|
|
|
|
[ButtonVariant.Primary, 'module-Button--primary'],
|
|
|
|
[ButtonVariant.Secondary, 'module-Button--secondary'],
|
2021-04-06 22:14:18 +00:00
|
|
|
[
|
|
|
|
ButtonVariant.SecondaryAffirmative,
|
|
|
|
'module-Button--secondary module-Button--secondary--affirmative',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
ButtonVariant.SecondaryDestructive,
|
|
|
|
'module-Button--secondary module-Button--secondary--destructive',
|
|
|
|
],
|
2021-02-23 19:28:48 +00:00
|
|
|
[ButtonVariant.Destructive, 'module-Button--destructive'],
|
2021-08-17 21:45:18 +00:00
|
|
|
[ButtonVariant.Calling, 'module-Button--calling'],
|
2021-08-26 20:51:55 +00:00
|
|
|
[ButtonVariant.SystemMessage, 'module-Button--system-message'],
|
2021-10-20 23:46:41 +00:00
|
|
|
[ButtonVariant.Details, 'module-Button--details'],
|
2021-02-23 19:28:48 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
export const Button = React.forwardRef<HTMLButtonElement, PropsType>(
|
2021-03-09 19:16:56 +00:00
|
|
|
(props, ref) => {
|
|
|
|
const {
|
2021-02-23 19:28:48 +00:00
|
|
|
children,
|
|
|
|
className,
|
|
|
|
disabled = false,
|
2021-10-20 23:46:41 +00:00
|
|
|
icon,
|
2021-08-17 21:45:18 +00:00
|
|
|
style,
|
|
|
|
tabIndex,
|
2021-02-23 19:28:48 +00:00
|
|
|
variant = ButtonVariant.Primary,
|
2021-10-20 23:46:41 +00:00
|
|
|
size = variant === ButtonVariant.Details
|
|
|
|
? ButtonSize.Small
|
|
|
|
: ButtonSize.Medium,
|
2021-03-09 19:16:56 +00:00
|
|
|
} = props;
|
2021-04-27 22:35:35 +00:00
|
|
|
const ariaLabel = props['aria-label'];
|
2021-03-09 19:16:56 +00:00
|
|
|
|
|
|
|
let onClick: undefined | MouseEventHandler<HTMLButtonElement>;
|
|
|
|
let type: 'button' | 'submit';
|
|
|
|
if ('onClick' in props) {
|
|
|
|
({ onClick } = props);
|
|
|
|
type = 'button';
|
|
|
|
} else {
|
|
|
|
onClick = undefined;
|
|
|
|
({ type } = props);
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:58:57 +00:00
|
|
|
const sizeClassName = SIZE_CLASS_NAMES.get(size);
|
|
|
|
assert(sizeClassName, '<Button> size not found');
|
|
|
|
|
2021-02-23 19:28:48 +00:00
|
|
|
const variantClassName = VARIANT_CLASS_NAMES.get(variant);
|
|
|
|
assert(variantClassName, '<Button> variant not found');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
2021-04-27 22:35:35 +00:00
|
|
|
aria-label={ariaLabel}
|
2021-04-30 22:58:57 +00:00
|
|
|
className={classNames(
|
|
|
|
'module-Button',
|
|
|
|
sizeClassName,
|
|
|
|
variantClassName,
|
2021-10-20 23:46:41 +00:00
|
|
|
`module-Button__icon--${icon}`,
|
2021-04-30 22:58:57 +00:00
|
|
|
className
|
|
|
|
)}
|
2021-02-23 19:28:48 +00:00
|
|
|
disabled={disabled}
|
|
|
|
onClick={onClick}
|
|
|
|
ref={ref}
|
2021-08-17 21:45:18 +00:00
|
|
|
style={style}
|
|
|
|
tabIndex={tabIndex}
|
2021-03-09 19:16:56 +00:00
|
|
|
// The `type` should either be "button" or "submit", which is effectively static.
|
|
|
|
// eslint-disable-next-line react/button-has-type
|
|
|
|
type={type}
|
2021-02-23 19:28:48 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|