import React, { Fragment, MouseEvent, ReactNode, useRef, useState } from 'react' import Image from 'next/image' import Link from 'next/link' import { useRouter } from 'next/router' import { useClickAway, useKey } from 'react-use' import { CheckIcon, ClipboardIcon } from '@heroicons/react/outline' import { cn, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from 'ui' import * as supabaseLogoWordmarkDark from 'common/assets/images/supabase-logo-wordmark--dark.png' import * as supabaseLogoWordmarkLight from 'common/assets/images/supabase-logo-wordmark--light.png' /** * 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 [copied, setCopied] = useState(false) /** * Close dropdown by clicking outside */ useClickAway(ref, () => { setOpen(false) }) /** * Close dropdown by using the Escape key */ useKey('Escape', () => setOpen(false)) /** * Open dropdown by right clicking on the Supabase logo */ const handleRightClick = (e: MouseEvent) => { e.preventDefault() if (e.type === 'contextmenu' || e.nativeEvent.button === 2) { 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) => { navigator.clipboard.writeText(menuItem.clipboard ?? '').then(() => { setCopied(true) setTimeout(() => { setCopied(false) }, 2000) }) } return ( e.key === 'Enter' && router.push('/')} className="block w-auto h-6 focus-visible:ring-2 focus-visible:outline-none focus-visible:ring-foreground-lighter focus-visible:ring-offset-4 focus-visible:ring-offset-background-alternative focus-visible:rounded-sm" > Supabase Logo Supabase Logo {menuItems.map((section, sectionIdx) => ( {sectionIdx !== 0 && }
{section.map((menuItem, i) => ( {menuItem.type === 'download' || menuItem.type === 'link' ? ( ) : ( menuItem.type === 'clipboard' && ( ) )} ))}
))}
) } const DropdownSeparator = () =>
interface MenuItemProps { label: string type: 'clipboard' | 'download' | 'link' icon?: ReactNode href?: string clipboard?: string } /** * 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 as SVG', type: 'clipboard', icon: ( ), clipboard: ` `, }, { label: 'Download wordmark', type: 'download', icon: ( ), href: '/supabase-wordmark.zip', }, { label: 'Download brand assets', type: 'download', href: '/brand-assets.zip', icon: ( ), }, ], [ { label: 'Brand Assets', type: 'link', href: '/brand-assets', }, ], ] interface MenuItemContentProps { icon?: ReactNode type: string label: string copied?: boolean } /** * MenuItem content */ const MenuItemContent = ({ icon, type, label, copied }: MenuItemContentProps) => ( <> {icon && {icon}} {label} {type === 'clipboard' && ( {!copied ? ( ) : ( )} )} ) export default RightClickBrandLogo