"use client"; import { Input } from "components/Input"; import { useState } from "react"; import { Menu } from "components/Menu"; import * as DropdownMenu from "@radix-ui/react-dropdown-menu"; import { useIsMobile } from "src/hooks/isMobile"; import { fonts, defaultFontId, FontConfig, isCustomFontId, parseGoogleFontInput, createCustomFontId, getFontConfig, } from "src/fonts"; export const FontPicker = (props: { label: string; value: string | undefined; onChange: (fontId: string) => void; }) => { let isMobile = useIsMobile(); let [showCustomInput, setShowCustomInput] = useState(false); let [customFontValue, setCustomFontValue] = useState(""); let fontId = props.value || defaultFontId; let font = getFontConfig(fontId); let isCustom = isCustomFontId(fontId); let fontList = Object.values(fonts).sort((a, b) => a.displayName.localeCompare(b.displayName), ); const handleCustomSubmit = () => { const parsed = parseGoogleFontInput(customFontValue); if (parsed) { const customId = createCustomFontId( parsed.fontName, parsed.googleFontsFamily, ); props.onChange(customId); setShowCustomInput(false); setCustomFontValue(""); } }; return (
Aa
{props.label}
{font.displayName}
} side={isMobile ? "bottom" : "right"} align="start" className="w-[250px] !gap-0 !outline-none max-h-72 " > {showCustomInput ? (
Paste a Google Font name
setCustomFontValue(e.currentTarget.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); handleCustomSubmit(); } else if (e.key === "Escape") { setShowCustomInput(false); setCustomFontValue(""); } }} />
) : (
{fontList.map((fontOption) => { return ( { props.onChange(fontOption.id); }} font={fontOption} selected={fontOption.id === fontId} /> ); })} {isCustom && ( {}} font={font} selected={true} /> )}
{ e.preventDefault(); setShowCustomInput(true); }} className={` fontOption z-10 px-1 py-0.5 text-left text-secondary data-[highlighted]:bg-border-light data-[highlighted]:text-secondary hover:bg-border-light hover:text-secondary outline-none cursor-pointer `} >
Custom Google Font...
)}
); }; const FontOption = (props: { onSelect: () => void; font: FontConfig; selected: boolean; }) => { return (
{props.font.displayName}
); };