"use client"; import { Color } from "react-aria-components"; import { Input } from "components/Input"; import { useState } from "react"; import { useEntity, useReplicache } from "src/replicache"; import { Menu } from "components/Menu"; import { pickers } from "../ThemeSetter"; import { ColorPicker } from "./ColorPicker"; 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 TextColorPicker = (props: { openPicker: pickers; setOpenPicker: (thisPicker: pickers) => void; value: Color; setValue: (c: Color) => void; }) => { return ( props.setOpenPicker("null")} /> ); }; type FontAttribute = "theme/heading-font" | "theme/body-font"; export const FontPicker = (props: { label: string; entityID: string; attribute: FontAttribute; }) => { let isMobile = useIsMobile(); let { rep } = useReplicache(); let [showCustomInput, setShowCustomInput] = useState(false); let [customFontValue, setCustomFontValue] = useState(""); let currentFont = useEntity(props.entityID, props.attribute); let fontId = currentFont?.data.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, ); rep?.mutate.assertFact({ entity: props.entityID, attribute: props.attribute, data: { type: "string", value: 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 ( { rep?.mutate.assertFact({ entity: props.entityID, attribute: props.attribute, data: { type: "string", value: 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}
); };