fix: Improve share popover focus behavior

This commit is contained in:
Tom Moor
2021-11-07 15:48:32 -08:00
parent 7bc7d7cd6b
commit 3ac7a839ad
2 changed files with 19 additions and 7 deletions

View File

@@ -60,7 +60,7 @@ function ShareButton({ document }: Props) {
document={document} document={document}
share={share} share={share}
sharedParent={sharedParent} sharedParent={sharedParent}
onSubmit={popover.hide} onRequestClose={popover.hide}
visible={popover.visible} visible={popover.visible}
/> />
</Popover> </Popover>

View File

@@ -15,6 +15,7 @@ import HelpText from "components/HelpText";
import Input from "components/Input"; import Input from "components/Input";
import Notice from "components/Notice"; import Notice from "components/Notice";
import Switch from "components/Switch"; import Switch from "components/Switch";
import useKeyDown from "hooks/useKeyDown";
import useStores from "hooks/useStores"; import useStores from "hooks/useStores";
import useToasts from "hooks/useToasts"; import useToasts from "hooks/useToasts";
@@ -22,7 +23,7 @@ type Props = {|
document: Document, document: Document,
share: Share, share: Share,
sharedParent: ?Share, sharedParent: ?Share,
onSubmit: () => void, onRequestClose: () => void,
visible: boolean, visible: boolean,
|}; |};
@@ -30,7 +31,7 @@ function SharePopover({
document, document,
share, share,
sharedParent, sharedParent,
onSubmit, onRequestClose,
visible, visible,
}: Props) { }: Props) {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -38,6 +39,7 @@ function SharePopover({
const { showToast } = useToasts(); const { showToast } = useToasts();
const [isCopied, setIsCopied] = React.useState(false); const [isCopied, setIsCopied] = React.useState(false);
const timeout = React.useRef<?TimeoutID>(); const timeout = React.useRef<?TimeoutID>();
const buttonRef = React.useRef<?HTMLButtonElement>();
const can = policies.abilities(share ? share.id : ""); const can = policies.abilities(share ? share.id : "");
const documentAbilities = policies.abilities(document.id); const documentAbilities = policies.abilities(document.id);
const canPublish = const canPublish =
@@ -47,8 +49,13 @@ function SharePopover({
documentAbilities.share; documentAbilities.share;
const isPubliclyShared = (share && share.published) || sharedParent; const isPubliclyShared = (share && share.published) || sharedParent;
useKeyDown("Escape", onRequestClose);
React.useEffect(() => { React.useEffect(() => {
if (visible) document.share(); if (visible) {
document.share();
buttonRef.current?.focus();
}
return () => clearTimeout(timeout.current); return () => clearTimeout(timeout.current);
}, [document, visible]); }, [document, visible]);
@@ -87,11 +94,11 @@ function SharePopover({
timeout.current = setTimeout(() => { timeout.current = setTimeout(() => {
setIsCopied(false); setIsCopied(false);
onSubmit(); onRequestClose();
showToast(t("Share link copied"), { type: "info" }); showToast(t("Share link copied"), { type: "info" });
}, 250); }, 250);
}, [t, onSubmit, showToast]); }, [t, onRequestClose, showToast]);
return ( return (
<> <>
@@ -176,7 +183,12 @@ function SharePopover({
readOnly readOnly
/> />
<CopyToClipboard text={share ? share.url : ""} onCopy={handleCopied}> <CopyToClipboard text={share ? share.url : ""} onCopy={handleCopied}>
<Button type="submit" disabled={isCopied || !share} primary> <Button
type="submit"
disabled={isCopied || !share}
ref={buttonRef}
primary
>
{t("Copy link")} {t("Copy link")}
</Button> </Button>
</CopyToClipboard> </CopyToClipboard>