element-call/src/button/Button.tsx

238 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022 Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-12-07 11:59:57 -08:00
import React, { forwardRef } from "react";
2022-06-11 23:21:20 +02:00
import { PressEvent } from "@react-types/shared";
2021-12-07 11:59:57 -08:00
import classNames from "classnames";
2022-06-11 23:21:20 +02:00
import { useButton } from "@react-aria/button";
import { mergeProps, useObjectRef } from "@react-aria/utils";
2021-12-07 11:59:57 -08:00
import styles from "./Button.module.css";
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
import { ReactComponent as MuteMicIcon } from "../icons/MuteMic.svg";
import { ReactComponent as VideoIcon } from "../icons/Video.svg";
import { ReactComponent as DisableVideoIcon } from "../icons/DisableVideo.svg";
import { ReactComponent as HangupIcon } from "../icons/Hangup.svg";
import { ReactComponent as ScreenshareIcon } from "../icons/Screenshare.svg";
2022-04-27 16:47:23 -07:00
import { ReactComponent as SettingsIcon } from "../icons/Settings.svg";
import { ReactComponent as AddUserIcon } from "../icons/AddUser.svg";
import { ReactComponent as ArrowDownIcon } from "../icons/ArrowDown.svg";
2022-02-04 12:31:59 -08:00
import { TooltipTrigger } from "../Tooltip";
2021-12-07 11:59:57 -08:00
2022-07-02 21:20:53 +02:00
export type ButtonVariant =
| "default"
| "toolbar"
| "toolbarSecondary"
| "icon"
| "secondary"
| "copy"
| "secondaryCopy"
| "iconCopy"
| "secondaryHangup"
| "dropdown"
| "link";
2021-12-14 16:12:58 -08:00
export const variantToClassName = {
2021-12-07 11:59:57 -08:00
default: [styles.button],
toolbar: [styles.toolbarButton],
2022-02-15 12:58:55 -08:00
toolbarSecondary: [styles.toolbarButtonSecondary],
2021-12-07 11:59:57 -08:00
icon: [styles.iconButton],
2021-12-13 14:54:44 -08:00
secondary: [styles.secondary],
2021-12-07 11:59:57 -08:00
copy: [styles.copyButton],
2022-06-02 16:30:35 -04:00
secondaryCopy: [styles.secondaryCopy, styles.copyButton],
2021-12-13 12:39:29 -08:00
iconCopy: [styles.iconCopyButton],
2022-04-22 18:05:48 -07:00
secondaryHangup: [styles.secondaryHangup],
dropdown: [styles.dropdownButton],
link: [styles.linkButton],
2021-12-07 11:59:57 -08:00
};
2022-07-02 21:20:53 +02:00
export type ButtonSize = "lg";
export const sizeToClassName: { lg: string[] } = {
2021-12-14 16:12:58 -08:00
lg: [styles.lg],
};
2022-06-11 23:21:20 +02:00
interface Props {
2022-07-02 21:20:53 +02:00
variant: ButtonVariant;
size: ButtonSize;
2022-06-11 23:21:20 +02:00
on: () => void;
off: () => void;
iconStyle: string;
className: string;
children: Element[];
onPress: (e: PressEvent) => void;
onPressStart: (e: PressEvent) => void;
[index: string]: unknown;
}
export const Button = forwardRef<HTMLAnchorElement, Props>(
2021-12-07 17:59:55 -08:00
(
2021-12-14 16:12:58 -08:00
{
variant = "default",
size,
on,
off,
iconStyle,
className,
children,
2022-01-18 14:25:02 -08:00
onPress,
onPressStart,
2021-12-14 16:12:58 -08:00
...rest
},
2021-12-07 17:59:55 -08:00
ref
) => {
2022-06-11 23:21:20 +02:00
const buttonRef = useObjectRef<HTMLAnchorElement>(ref);
2022-01-18 14:25:02 -08:00
const { buttonProps } = useButton(
{ onPress, onPressStart, ...rest },
buttonRef
);
2021-12-07 11:59:57 -08:00
// TODO: react-aria's useButton hook prevents form submission via keyboard
// Remove the hack below after this is merged https://github.com/adobe/react-spectrum/pull/904
let filteredButtonProps = buttonProps;
if (rest.type === "submit" && !rest.onPress) {
2022-06-11 23:21:20 +02:00
const { ...filtered } = buttonProps;
2021-12-07 11:59:57 -08:00
filteredButtonProps = filtered;
}
return (
<button
2021-12-07 17:59:55 -08:00
className={classNames(
variantToClassName[variant],
2021-12-14 16:12:58 -08:00
sizeToClassName[size],
2021-12-07 17:59:55 -08:00
styles[iconStyle],
className,
{
[styles.on]: on,
[styles.off]: off,
}
)}
2021-12-23 14:40:23 -08:00
{...mergeProps(rest, filteredButtonProps)}
2021-12-07 11:59:57 -08:00
ref={buttonRef}
>
{children}
{variant === "dropdown" && <ArrowDownIcon />}
2021-12-07 11:59:57 -08:00
</button>
);
}
);
2022-07-02 21:20:53 +02:00
export function MicButton({
muted,
...rest
}: {
muted: boolean;
[index: string]: unknown;
}) {
2021-12-07 11:59:57 -08:00
return (
2021-12-14 22:00:00 -08:00
<TooltipTrigger>
<Button variant="toolbar" {...rest} off={muted}>
{muted ? <MuteMicIcon /> : <MicIcon />}
</Button>
2022-01-20 13:03:54 -08:00
{() => (muted ? "Unmute microphone" : "Mute microphone")}
2021-12-14 22:00:00 -08:00
</TooltipTrigger>
2021-12-07 11:59:57 -08:00
);
}
2022-07-02 21:20:53 +02:00
export function VideoButton({
muted,
...rest
}: {
muted: boolean;
[index: string]: unknown;
}) {
2021-12-07 11:59:57 -08:00
return (
2021-12-14 22:00:00 -08:00
<TooltipTrigger>
<Button variant="toolbar" {...rest} off={muted}>
{muted ? <DisableVideoIcon /> : <VideoIcon />}
</Button>
2022-01-20 13:03:54 -08:00
{() => (muted ? "Turn on camera" : "Turn off camera")}
2021-12-14 22:00:00 -08:00
</TooltipTrigger>
2021-12-07 11:59:57 -08:00
);
}
2022-07-02 21:20:53 +02:00
export function ScreenshareButton({
enabled,
className,
...rest
}: {
enabled: boolean;
className: string;
[index: string]: unknown;
}) {
2021-12-07 11:59:57 -08:00
return (
2021-12-14 22:00:00 -08:00
<TooltipTrigger>
2022-02-15 12:58:55 -08:00
<Button variant="toolbarSecondary" {...rest} on={enabled}>
2021-12-14 22:00:00 -08:00
<ScreenshareIcon />
</Button>
2022-01-20 13:03:54 -08:00
{() => (enabled ? "Stop sharing screen" : "Share screen")}
2021-12-14 22:00:00 -08:00
</TooltipTrigger>
2021-12-07 11:59:57 -08:00
);
}
2022-07-02 21:20:53 +02:00
export function HangupButton({
className,
...rest
}: {
className: string;
[index: string]: unknown;
}) {
2021-12-07 11:59:57 -08:00
return (
2021-12-14 22:00:00 -08:00
<TooltipTrigger>
<Button
variant="toolbar"
className={classNames(styles.hangupButton, className)}
{...rest}
>
<HangupIcon />
</Button>
2022-01-20 13:03:54 -08:00
{() => "Leave"}
2021-12-14 22:00:00 -08:00
</TooltipTrigger>
2021-12-07 11:59:57 -08:00
);
}
2022-04-27 16:47:23 -07:00
2022-07-02 21:20:53 +02:00
export function SettingsButton({
className,
...rest
}: {
className: string;
[index: string]: unknown;
}) {
2022-04-27 16:47:23 -07:00
return (
<TooltipTrigger>
<Button variant="toolbar" {...rest}>
<SettingsIcon />
</Button>
{() => "Settings"}
</TooltipTrigger>
);
}
2022-07-02 21:20:53 +02:00
export function InviteButton({
className,
...rest
}: {
className: string;
[index: string]: unknown;
}) {
2022-04-27 16:47:23 -07:00
return (
<TooltipTrigger>
<Button variant="toolbar" {...rest}>
<AddUserIcon />
</Button>
{() => "Invite"}
</TooltipTrigger>
);
}