diff --git a/src/components/BrandContextMenu.tsx b/src/components/BrandContextMenu.tsx index 3599cadb9..da46e6ba7 100644 --- a/src/components/BrandContextMenu.tsx +++ b/src/components/BrandContextMenu.tsx @@ -2,34 +2,37 @@ import * as React from 'react' import { useToast } from '~/components/ToastProvider' import { useNavigate } from '@tanstack/react-router' import { twMerge } from 'tailwind-merge' -import { - Dropdown, - DropdownTrigger, - DropdownContent, - DropdownItem, - DropdownSeparator, -} from './Dropdown' +import { Menu } from '@base-ui/react/menu' import { copyTextToClipboard } from '~/utils/browser-effects' interface BrandContextMenuProps extends React.HTMLAttributes { children: React.ReactNode + showTrigger?: boolean } -export function BrandContextMenu({ children, ...rest }: BrandContextMenuProps) { +export function BrandContextMenu({ + children, + showTrigger = false, + ...rest +}: BrandContextMenuProps) { const navigate = useNavigate() const { notify } = useToast() const [open, setOpen] = React.useState(false) - const [coords, setCoords] = React.useState<{ x: number; y: number }>({ - x: 0, - y: 0, - }) - const virtualTriggerRef = React.useRef(null) - - const onContextMenu = (e: React.MouseEvent) => { - e.preventDefault() - setCoords({ x: e.clientX, y: e.clientY }) - setOpen(true) - } + const firstItemRef = React.useRef(null) + const focusFirstItemRef = React.useRef(false) + const triggerRef = React.useRef(null) + const [finalFocus, setFinalFocus] = React.useState< + boolean | React.RefObject + >(true) + const [point, setPoint] = React.useState({ x: 0, y: 0 }) + const anchorRef = React.useMemo( + () => ({ + current: { + getBoundingClientRect: () => new DOMRect(point.x, point.y, 0, 0), + }, + }), + [point], + ) const copyText = async (text: string) => { try { @@ -58,90 +61,166 @@ export function BrandContextMenu({ children, ...rest }: BrandContextMenuProps) { } return ( -
- {children} - - - } - /> - +
{ + rest.onContextMenu?.(event) + if (event.defaultPrevented) return + event.preventDefault() + event.stopPropagation() + focusFirstItemRef.current = false + const active = document.activeElement + setFinalFocus( + active === triggerRef.current + ? triggerRef + : active instanceof HTMLElement && + event.currentTarget.contains(active) + ? { current: active } + : false, + ) + setPoint({ x: event.clientX, y: event.clientY }) + setOpen(true) + }} + onKeyDown={(event) => { + rest.onKeyDown?.(event) + if ( + event.defaultPrevented || + !( + event.key === 'ContextMenu' || + (event.shiftKey && event.key === 'F10') + ) + ) + return + event.preventDefault() + event.stopPropagation() + const target = + event.target instanceof HTMLElement + ? event.target + : event.currentTarget + focusFirstItemRef.current = true + setFinalFocus( + target === triggerRef.current ? triggerRef : { current: target }, + ) + const rect = target.getBoundingClientRect() + setPoint({ x: rect.left, y: rect.bottom }) + setOpen(true) + }} + > + {children} + {showTrigger && ( + { + if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { + setFinalFocus(triggerRef) + const rect = event.currentTarget.getBoundingClientRect() + setPoint({ x: rect.left, y: rect.bottom }) + } + }} + onClick={(event) => { + setFinalFocus(triggerRef) + const rect = event.currentTarget.getBoundingClientRect() + setPoint({ x: rect.left, y: rect.bottom }) + }} + > + Logo assets + + )} +
+ + - {[ - { - label: 'Logo — Landscape (Black)', - url: '/images/brand/tanstack-landscape-black.svg', - }, - { - label: 'Logo — Landscape (White)', - url: '/images/brand/tanstack-landscape-white.svg', - darkBg: true, - }, - { - label: 'Logo — Stacked (Black)', - url: '/images/brand/tanstack-stacked-black.svg', - }, - { - label: 'Logo — Stacked (White)', - url: '/images/brand/tanstack-stacked-white.svg', - darkBg: true, - }, - { - label: 'Emblem (Black)', - url: '/images/brand/tanstack-emblem-black.svg', - }, - { - label: 'Emblem (White)', - url: '/images/brand/tanstack-emblem-white.svg', - darkBg: true, - }, - ].map(({ label, url, darkBg }) => ( - { - try { - const res = await fetch(url) - const text = await res.text() - await copyText(text) - } catch (err) { - console.error('Failed to copy logo', err) - } - }} - > - {label} -
{ + if ( + event.target === event.currentTarget && + focusFirstItemRef.current + ) { + focusFirstItemRef.current = false + firstItemRef.current?.focus() + } + }} + className="dropdown-content z-[1200] min-w-48 rounded-lg border border-gray-200 bg-white p-1.5 shadow-lg dark:border-gray-700 dark:bg-gray-900/50 backdrop-blur-md" + > + {[ + { + label: 'Logo — Landscape (Black)', + url: '/images/brand/tanstack-landscape-black.svg', + }, + { + label: 'Logo — Landscape (White)', + url: '/images/brand/tanstack-landscape-white.svg', + darkBg: true, + }, + { + label: 'Logo — Stacked (Black)', + url: '/images/brand/tanstack-stacked-black.svg', + }, + { + label: 'Logo — Stacked (White)', + url: '/images/brand/tanstack-stacked-white.svg', + darkBg: true, + }, + { + label: 'Emblem (Black)', + url: '/images/brand/tanstack-emblem-black.svg', + }, + { + label: 'Emblem (White)', + url: '/images/brand/tanstack-emblem-white.svg', + darkBg: true, + }, + ].map(({ label, url, darkBg }, index) => ( + { + try { + const res = await fetch(url) + const text = await res.text() + await copyText(text) + } catch (err) { + console.error('Failed to copy logo', err) + } + }} > - {label} -
-
- ))} - - navigate({ to: '/ds/logos' })}> - All Logo Assets - -
-
-
+ {label} +
+ {label} +
+ + ))} + + navigate({ to: '/ds/logos' })} + > + All Logo Assets + + + + + ) } diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 53cb3e71c..8fbf06960 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -620,6 +620,7 @@ export function Navbar({ children }: { children: React.ReactNode }) { />
diff --git a/src/routes/login.tsx b/src/routes/login.tsx index 392df8213..5b8f54bff 100644 --- a/src/routes/login.tsx +++ b/src/routes/login.tsx @@ -57,7 +57,7 @@ function SplashImage() { return (
- + diff --git a/src/routes/oauth/authorize.tsx b/src/routes/oauth/authorize.tsx index 1aa3b58df..cd1bb7018 100644 --- a/src/routes/oauth/authorize.tsx +++ b/src/routes/oauth/authorize.tsx @@ -202,7 +202,7 @@ function SplashImage() { return (
- +