Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 180 additions & 101 deletions src/components/BrandContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement> {
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<HTMLSpanElement>(null)

const onContextMenu = (e: React.MouseEvent) => {
e.preventDefault()
setCoords({ x: e.clientX, y: e.clientY })
setOpen(true)
}
const firstItemRef = React.useRef<HTMLDivElement>(null)
const focusFirstItemRef = React.useRef(false)
const triggerRef = React.useRef<HTMLButtonElement>(null)
const [finalFocus, setFinalFocus] = React.useState<
boolean | React.RefObject<HTMLElement | null>
>(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 {
Expand Down Expand Up @@ -58,90 +61,166 @@ export function BrandContextMenu({ children, ...rest }: BrandContextMenuProps) {
}

return (
<div onContextMenu={onContextMenu} {...rest}>
{children}
<Dropdown open={open} onOpenChange={setOpen} modal>
<DropdownTrigger
nativeButton={false}
render={
<span
ref={virtualTriggerRef}
style={{
position: 'fixed',
left: coords.x,
top: coords.y,
width: 1,
height: 1,
}}
/>
}
/>
<DropdownContent
sideOffset={4}
<Menu.Root open={open} onOpenChange={setOpen}>
<div
role="group"
aria-label="Logo assets"
{...rest}
onContextMenu={(event) => {
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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}}
>
{children}
{showTrigger && (
<Menu.Trigger
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
ref={triggerRef}
type="button"
className="sr-only focus:not-sr-only focus:absolute focus:z-50 focus:rounded-md focus:bg-white focus:px-3 focus:py-2 focus:text-sm focus:text-black focus:ring-2 focus:ring-blue-500 dark:focus:bg-gray-900 dark:focus:text-white"
onKeyDown={(event) => {
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
</Menu.Trigger>
)}
</div>
<Menu.Portal>
<Menu.Positioner
anchor={anchorRef.current}
side="right"
sideOffset={2}
align="start"
className="dark:bg-gray-900/50 backdrop-blur-md"
className="z-[1200]"
>
{[
{
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 }) => (
<DropdownItem
key={label}
className="py-1"
onSelect={async () => {
try {
const res = await fetch(url)
const text = await res.text()
await copyText(text)
} catch (err) {
console.error('Failed to copy logo', err)
}
}}
>
{label}
<div
className={twMerge(
'p-1 rounded-full',
darkBg
? 'bg-black text-white shadow-md'
: 'bg-white text-black shadow-md',
)}
<Menu.Popup
// Keep the trigger as a ref so focus follows its replacement when
// Redact remounts it while the menu is open (TanStack/redact#92).
finalFocus={finalFocus}
onFocus={(event) => {
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) => (
<Menu.Item
key={label}
ref={index === 0 ? firstItemRef : undefined}
className="flex cursor-pointer select-none items-center gap-2 rounded-md px-2 py-1 text-sm text-gray-700 outline-none focus:bg-gray-100 dark:text-gray-300 dark:focus:bg-gray-700/50"
onClick={async () => {
try {
const res = await fetch(url)
const text = await res.text()
await copyText(text)
} catch (err) {
console.error('Failed to copy logo', err)
}
}}
>
<img src={url} alt={label} className="h-6" />
</div>
</DropdownItem>
))}
<DropdownSeparator />
<DropdownItem onSelect={() => navigate({ to: '/ds/logos' })}>
All Logo Assets
</DropdownItem>
</DropdownContent>
</Dropdown>
</div>
{label}
<div
className={twMerge(
'p-1 rounded-full',
darkBg
? 'bg-black text-white shadow-md'
: 'bg-white text-black shadow-md',
)}
>
<img src={url} alt={label} className="h-6" />
</div>
</Menu.Item>
))}
<Menu.Separator className="my-1 h-px bg-gray-200 dark:bg-gray-700" />
<Menu.Item
className="cursor-pointer rounded-md px-2 py-1.5 text-sm outline-none focus:bg-gray-100 dark:focus:bg-gray-700/50"
onClick={() => navigate({ to: '/ds/logos' })}
>
All Logo Assets
</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
)
}
1 change: 1 addition & 0 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ export function Navbar({ children }: { children: React.ReactNode }) {
/>
<div className="flex min-w-0 flex-1 items-center gap-2 min-[1120px]:gap-3">
<BrandContextMenu
showTrigger
className={twMerge(`flex items-center group shrink-0`)}
>
<LogoSection />
Expand Down
2 changes: 1 addition & 1 deletion src/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function SplashImage() {
return (
<div className="flex items-center justify-center mb-4">
<ClientOnly fallback={fallback}>
<BrandContextMenu className="cursor-pointer">
<BrandContextMenu showTrigger className="cursor-pointer">
<SplashImages />
</BrandContextMenu>
</ClientOnly>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/oauth/authorize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function SplashImage() {
return (
<div className="flex items-center justify-center mb-4">
<ClientOnly fallback={fallback}>
<BrandContextMenu className="cursor-pointer">
<BrandContextMenu showTrigger className="cursor-pointer">
<SplashImages />
</BrandContextMenu>
</ClientOnly>
Expand Down
Loading