'use client' import { CircleCheckFilled, CrossfadeIcon } from '~/components/CrossfadeIcon' import { Copy, Download } from 'lucide-react' import { useTheme } from 'next-themes' import { useRouter } from 'next/compat/router' import Link from 'next/link' import { Fragment, MouseEvent, ReactNode, useEffect, useRef, useState } from 'react' import { useClickAway } from 'react-use' import { cn, copyToClipboard, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from 'ui' import SupabaseWordmark from './SupabaseWordmark' /** * Right click on the Supabase logo in the website navbar * for quick access to brand assets. */ const RightClickBrandLogo = () => { const ref = useRef(null) const router = useRouter() const [open, setOpen] = useState(false) const [copiedLabel, setCopiedLabel] = useState(null) const copiedTimeoutRef = useRef>(undefined) const [logoHighlight, setLogoHighlight] = useState() const { resolvedTheme } = useTheme() /** * Close dropdown by clicking outside */ useClickAway(ref, () => { setOpen(false) }) /** * Close dropdown by using the Escape key */ useEffect(() => { const onKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { setOpen(false) } } window.addEventListener('keydown', onKeyDown) return () => window.removeEventListener('keydown', onKeyDown) }, [setOpen]) /** * Open dropdown by right clicking on the Supabase logo */ const handleRightClick = (e: MouseEvent) => { e.preventDefault() if (e.type === 'contextmenu' || e.nativeEvent.button === 2) { setLogoHighlight(undefined) setOpen(true) } } /** * A11y open dropdown by typing Shift+Enter when the logo is focused */ const handleKeyboardOpen = () => { const onKeyDown = (e: KeyboardEvent) => { if (e.shiftKey && e.key === 'Enter') { setOpen(true) } } document?.addEventListener('keydown', onKeyDown) return () => { document?.removeEventListener('keydown', onKeyDown) } } /** * Copy to clipboard logo SVG */ const handleCopyToClipboard = (menuItem: MenuItemProps) => { // the ClipboardItem promise keeps the fetched copy inside the user gesture in safari // the copied file matches the nav logo, which follows the site theme const clipboardUrl = menuItem.clipboardUrls?.[resolvedTheme?.includes('dark') ? 'dark' : 'light'] if (!clipboardUrl) return copyToClipboard( fetch(clipboardUrl).then((response) => { if (!response.ok) throw new Error(`${response.status} ${clipboardUrl}`) return response.text() }), () => { setCopiedLabel(menuItem.label) // an older timer would clear the newer feedback early clearTimeout(copiedTimeoutRef.current) copiedTimeoutRef.current = setTimeout(() => { setCopiedLabel(null) }, 2000) } ) } const handleItemPointerEnter = (menuItem: MenuItemProps) => setLogoHighlight(menuItem.logoHighlight) const handleMenuPointerLeave = () => setLogoHighlight(undefined) const isFrameVisible = open && logoHighlight !== undefined const isFrameOnSymbol = logoHighlight !== 'wordmark' return ( e.key === 'Enter' && router?.push('/')} className={cn( 'relative isolate block w-auto h-6 box-content -mx-2 -my-3 px-2 py-3 focus-ring focus-visible:ring-offset-background-alternative rounded-md', 'transition-colors duration-200 ease-out motion-reduce:transition-none', isFrameVisible && isFrameOnSymbol && 'text-foreground-muted' )} > {menuItems.map((section, sectionIdx) => ( {sectionIdx !== 0 && }
{section.map((menuItem, i) => ( handleItemPointerEnter(menuItem)} className="w-full flex justify-between gap-2 items-center p-2! cursor-pointer" > {menuItem.type === 'download' || menuItem.type === 'link' ? ( ) : ( menuItem.type === 'clipboard' && ( // DropdownMenuItem asChild manages roving tabIndex; don't override it. // eslint-disable-next-line supabase/require-explicit-tabindex -- in-menu item ) )} ))}
))}
{copiedLabel ? 'Copied to clipboard' : ''}
) } const DropdownSeparator = () =>
interface MenuItemProps { label: string type: 'clipboard' | 'download' | 'link' icon?: ReactNode href?: string clipboardUrls?: Record<'light' | 'dark', string> logoHighlight?: 'icon' | 'wordmark' } /** * Brand assets dropdown menu items. * First array is to divide the menu by sections; * Second array is the list of items inside each section. */ const menuItems: MenuItemProps[][] = [ [ { label: 'Copy logo', logoHighlight: 'icon', type: 'clipboard', icon: ( ), clipboardUrls: { light: '/brand-assets/supabase-icon-green-transparent.svg', dark: '/brand-assets/supabase-icon-green-transparent.svg', }, }, { label: 'Copy wordmark', logoHighlight: 'wordmark', type: 'clipboard', icon: , clipboardUrls: { light: '/brand-assets/supabase-logo-green-black-transparent.svg', dark: '/brand-assets/supabase-logo-green-white-transparent.svg', }, }, { label: 'Download wordmark', type: 'download', icon: , href: '/supabase-wordmark.zip', }, { label: 'Download brand assets', type: 'download', href: '/brand-assets.zip', icon: , }, ], [ { label: 'Go to Brand Guidelines', type: 'link', href: '/brand', }, ], ] interface MenuItemContentProps { icon?: ReactNode label: string copied?: boolean } /** * MenuItem content */ const MenuItemContent = ({ icon, label, copied }: MenuItemContentProps) => ( <> {icon && ( {copied === undefined ? ( icon ) : ( } inactiveIcon={icon} className="size-4" /> )} )} {label} ) export default RightClickBrandLogo