element-call/src/button/Button.jsx

145 lines
3.7 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";
import { Tooltip, 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],
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],
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,
...rest
},
2021-12-08 01:59:55 +00:00
ref
) => {
2021-12-07 19:59:57 +00:00
const buttonRef = useObjectRef(ref);
const { buttonProps } = useButton(rest, buttonRef);
// 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,
}
)}
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 ButtonTooltip({ className, children }) {
return (
<div className={classNames(styles.buttonTooltip, className)}>
{children}
</div>
);
}
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>
{(props) => (
<Tooltip position="top" {...props}>
{muted ? "Unmute microphone" : "Mute microphone"}
</Tooltip>
)}
</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>
{(props) => (
<Tooltip position="top" {...props}>
{muted ? "Turn on camera" : "Turn off camera"}
</Tooltip>
)}
</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>
<Button variant="toolbar" {...rest} on={enabled}>
<ScreenshareIcon />
</Button>
{(props) => (
<Tooltip position="top" {...props}>
{enabled ? "Stop sharing screen" : "Share screen"}
</Tooltip>
)}
</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>
{(props) => (
<Tooltip position="top" {...props}>
Leave
</Tooltip>
)}
</TooltipTrigger>
2021-12-07 19:59:57 +00:00
);
}