Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve keyboard shortcut behavior

Tim Wilson1c67f2c8ce94
diff --git a/apps/web/ui/links/link-controls.tsx b/apps/web/ui/links/link-controls.tsx
index 4b42c47fa3..dfe522b392 100644
--- a/apps/web/ui/links/link-controls.tsx
+++ b/apps/web/ui/links/link-controls.tsx
@@ -4,6 +4,7 @@ import { useDeleteLinkModal } from "@/ui/modals/delete-link-modal";
import { useLinkQRModal } from "@/ui/modals/link-qr-modal";
import {
Button,
+ CardList,
IconMenu,
PenWriting,
Popover,
@@ -13,17 +14,24 @@ import { BoxArchive, CircleCheck, Copy, QRCode } from "@dub/ui/src/icons";
import { cn, isDubDomain, nanoid, punycode } from "@dub/utils";
import { CopyPlus, Delete, FolderInput } from "lucide-react";
import { useParams } from "next/navigation";
-import { useEffect, useState } from "react";
+import { useContext, useEffect, useState } from "react";
import { toast } from "sonner";
import { mutate } from "swr";
import { useTransferLinkModal } from "../modals/transfer-link-modal";
import { ThreeDots } from "../shared/icons";
-import { ResponseLink } from "./links-container";
+import { LinksListContext, ResponseLink } from "./links-container";
export function LinkControls({ link }: { link: ResponseLink }) {
const { slug } = useParams() as { slug?: string };
- const [openPopover, setOpenPopover] = useState(false);
+ const { hovered } = useContext(CardList.Card.Context);
+
+ const { openMenuLinkId, setOpenMenuLinkId } = useContext(LinksListContext);
+ const openPopover = openMenuLinkId === link.id;
+ const setOpenPopover = (open: boolean) => {
+ setOpenMenuLinkId(open ? link.id : null);
+ };
+
const [copiedLinkId, setCopiedLinkId] = useState(false);
const copyLinkId = () => {
@@ -72,9 +80,12 @@ export function LinkControls({ link }: { link: ResponseLink }) {
const onKeyDown = (e: any) => {
const key = e.key.toLowerCase();
// only run shortcut logic if:
- // - the 3 dots menu is open
+ // - the 3 dots menu is open or the link card is hovered
// - the key pressed is one of the shortcuts
- if (openPopover && ["e", "d", "q", "a", "t", "i", "x"].includes(key)) {
+ if (
+ (openPopover || (openMenuLinkId === null && hovered)) &&
+ ["e", "d", "q", "a", "t", "i", "x"].includes(key)
+ ) {
e.preventDefault();
setOpenPopover(false);
switch (key) {
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 4510997a63..0d46b9fbb3 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -5,6 +5,7 @@ import useLinksCount from "@/lib/swr/use-links-count";
import { LinkWithTagsProps, UserProps } from "@/lib/types";
import { CardList, MaxWidthWrapper, useRouterStuff } from "@dub/ui";
import { useSearchParams } from "next/navigation";
+import { Dispatch, SetStateAction, createContext, useState } from "react";
import LinkCardPlaceholder from "./link-card-placeholder";
import { LinkDetailsColumn } from "./link-details-column";
import LinkNotFound from "./link-not-found";
@@ -41,6 +42,14 @@ export default function LinksContainer({
);
}
+export const LinksListContext = createContext<{
+ openMenuLinkId: string | null;
+ setOpenMenuLinkId: Dispatch<SetStateAction<string | null>>;
+}>({
+ openMenuLinkId: null,
+ setOpenMenuLinkId: () => {},
+});
+
function LinksList({
AddEditLinkButton,
links,
@@ -58,6 +67,8 @@ function LinksList({
const searchParams = useSearchParams();
const page = (parseInt(searchParams?.get("page") || "1") || 1) - 1;
+ const [openMenuLinkId, setOpenMenuLinkId] = useState<string | null>(null);
+
const isFiltered = [
"domain",
"tagId",
@@ -67,7 +78,7 @@ function LinksList({
].some((param) => searchParams.has(param));
return loading || links?.length ? (
- <div>
+ <LinksListContext.Provider value={{ openMenuLinkId, setOpenMenuLinkId }}>
{/* Cards */}
<CardList variant={compact ? "compact" : "loose"} loading={loading}>
{links?.length
@@ -110,7 +121,7 @@ function LinksList({
totalCount={count ?? links.length}
/>
)}
- </div>
+ </LinksListContext.Provider>
) : isFiltered ? (
<LinkNotFound />
) : (