element-call/src/button/Button.jsx

130 lines
3.5 KiB
React
Raw Normal View History

2021-12-07 19:59:57 +00:00
import React, { forwardRef } from "react";
import classNames from "classnames";
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";
import { useButton } from "@react-aria/button";
2021-12-15 06:00:00 +00:00
import { mergeProps, useObjectRef } from "@react-aria/utils";
2022-02-04 20:31:59 +00:00
import { TooltipTrigger } from "../Tooltip";
2021-12-07 19:59:57 +00:00
2021-12-15 00:12:58 +00:00
export const variantToClassName = {
2021-12-07 19:59:57 +00:00
default: [styles.button],
toolbar: [styles.toolbarButton],
2022-02-15 20:58:55 +00:00
toolbarSecondary: [styles.toolbarButtonSecondary],
2021-12-07 19:59:57 +00:00
icon: [styles.iconButton],
2021-12-13 22:54:44 +00:00
secondary: [styles.secondary],
2021-12-07 19:59:57 +00:00
copy: [styles.copyButton],
2021-12-13 20:39:29 +00:00
iconCopy: [styles.iconCopyButton],
2022-02-04 20:31:59 +00:00
secondaryCopy: [styles.copyButton],
2022-04-23 01:05:48 +00:00
secondaryHangup: [styles.secondaryHangup],
2021-12-07 19:59:57 +00:00
};
2021-12-15 00:12:58 +00:00
export const sizeToClassName = {
lg: [styles.lg],
};
2021-12-07 19:59:57 +00:00
export const Button = forwardRef(
2021-12-08 01:59:55 +00:00
(
2021-12-15 00:12:58 +00:00
{
variant = "default",
size,
on,
off,
iconStyle,
className,
children,
2022-01-18 22:25:02 +00:00
onPress,
onPressStart,
2021-12-15 00:12:58 +00:00
...rest
},
2021-12-08 01:59:55 +00:00
ref
) => {
2021-12-07 19:59:57 +00:00
const buttonRef = useObjectRef(ref);
2022-01-18 22:25:02 +00:00
const { buttonProps } = useButton(
{ onPress, onPressStart, ...rest },
buttonRef
);
2021-12-07 19:59:57 +00: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) {
const { onKeyDown, onKeyUp, ...filtered } = buttonProps;
filteredButtonProps = filtered;
}
return (
<button
2021-12-08 01:59:55 +00:00
className={classNames(
variantToClassName[variant],
2021-12-15 00:12:58 +00:00
sizeToClassName[size],
2021-12-08 01:59:55 +00:00
styles[iconStyle],
className,
{
[styles.on]: on,
[styles.off]: off,
2022-02-04 20:31:59 +00:00
[styles.secondaryCopy]: variant === "secondaryCopy",
2021-12-08 01:59:55 +00:00
}
)}
2021-12-23 22:40:23 +00:00
{...mergeProps(rest, filteredButtonProps)}
2021-12-07 19:59:57 +00:00
ref={buttonRef}
>
{children}
</button>
);
}
);
export function MicButton({ muted, ...rest }) {
return (
2021-12-15 06:00:00 +00:00
<TooltipTrigger>
<Button variant="toolbar" {...rest} off={muted}>
{muted ? <MuteMicIcon /> : <MicIcon />}
</Button>
2022-01-20 21:03:54 +00:00
{() => (muted ? "Unmute microphone" : "Mute microphone")}
2021-12-15 06:00:00 +00:00
</TooltipTrigger>
2021-12-07 19:59:57 +00:00
);
}
export function VideoButton({ muted, ...rest }) {
return (
2021-12-15 06:00:00 +00:00
<TooltipTrigger>
<Button variant="toolbar" {...rest} off={muted}>
{muted ? <DisableVideoIcon /> : <VideoIcon />}
</Button>
2022-01-20 21:03:54 +00:00
{() => (muted ? "Turn on camera" : "Turn off camera")}
2021-12-15 06:00:00 +00:00
</TooltipTrigger>
2021-12-07 19:59:57 +00:00
);
}
export function ScreenshareButton({ enabled, className, ...rest }) {
return (
2021-12-15 06:00:00 +00:00
<TooltipTrigger>
2022-02-15 20:58:55 +00:00
<Button variant="toolbarSecondary" {...rest} on={enabled}>
2021-12-15 06:00:00 +00:00
<ScreenshareIcon />
</Button>
2022-01-20 21:03:54 +00:00
{() => (enabled ? "Stop sharing screen" : "Share screen")}
2021-12-15 06:00:00 +00:00
</TooltipTrigger>
2021-12-07 19:59:57 +00:00
);
}
export function HangupButton({ className, ...rest }) {
return (
2021-12-15 06:00:00 +00:00
<TooltipTrigger>
<Button
variant="toolbar"
className={classNames(styles.hangupButton, className)}
{...rest}
>
<HangupIcon />
</Button>
2022-01-20 21:03:54 +00:00
{() => "Leave"}
2021-12-15 06:00:00 +00:00
</TooltipTrigger>
2021-12-07 19:59:57 +00:00
);
}