Merge pull request #311 from vector-im/dbkr/fix_pt_button_behaviour
Fix mouseup/down behaviour of PTT button
This commit is contained in:
commit
a6eb52ae76
1 changed files with 21 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import styles from "./PTTButton.module.css";
|
import styles from "./PTTButton.module.css";
|
||||||
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
|
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
|
||||||
|
@ -14,14 +14,32 @@ export function PTTButton({
|
||||||
startTalking,
|
startTalking,
|
||||||
stopTalking,
|
stopTalking,
|
||||||
}) {
|
}) {
|
||||||
|
const [isHeld, setHeld] = useState(false);
|
||||||
|
const onDocumentMouseUp = useCallback(() => {
|
||||||
|
if (isHeld) stopTalking();
|
||||||
|
setHeld(false);
|
||||||
|
}, [isHeld, setHeld]);
|
||||||
|
|
||||||
|
const onButtonMouseDown = useCallback(() => {
|
||||||
|
setHeld(true);
|
||||||
|
startTalking();
|
||||||
|
}, [setHeld]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener("mouseup", onDocumentMouseUp);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("mouseup", onDocumentMouseUp);
|
||||||
|
};
|
||||||
|
}, [onDocumentMouseUp]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
className={classNames(styles.pttButton, {
|
className={classNames(styles.pttButton, {
|
||||||
[styles.talking]: activeSpeakerUserId,
|
[styles.talking]: activeSpeakerUserId,
|
||||||
[styles.error]: showTalkOverError,
|
[styles.error]: showTalkOverError,
|
||||||
})}
|
})}
|
||||||
onMouseDown={startTalking}
|
onMouseDown={onButtonMouseDown}
|
||||||
onMouseUp={stopTalking}
|
|
||||||
>
|
>
|
||||||
{activeSpeakerIsLocalUser || !activeSpeakerUserId ? (
|
{activeSpeakerIsLocalUser || !activeSpeakerUserId ? (
|
||||||
<MicIcon
|
<MicIcon
|
||||||
|
|
Loading…
Add table
Reference in a new issue