import { Fragment, h, render } from "preact"; import { useState } from "preact/hooks"; const formatter = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }); type HighlightFormProps = { onSubmit(code: string, language: string); } const HighlightForm = ({ onSubmit }: HighlightFormProps) => { const [code, setCode] = useState(""); const [language, setLanguage] = useState(""); const handleReset = () => { setCode(""); setLanguage(""); }; const handleSubmit = () => onSubmit && onSubmit(code, language); return (
setLanguage((ev.target as HTMLInputElement).value) }/>
) }; const Loading = () =>
; type HighlightedCodeProps = { highlighted: HighlightedResponse; onClose?(): void; } const HighlightedCode = ({ highlighted, onClose }: HighlightedCodeProps) =>

        
Total time: { formatter.format(highlighted.times.total) }ms
; type DemoState = "form" | "submitting" | "show"; type HighlightedResponse = { html: string; times: { total: number; tokenization: number; processing: number; formatting: number; } } const KeylighterDemo = () => { const [state, setState] = useState("form"); const [highlighted, setHighlighted] = useState(null); const handleCodeHighlight = async (source, language) => { setState("submitting"); setHighlighted(await fetch('/highlight', { credentials: "include", method: "POST", body: JSON.stringify({ source, language }), headers: { 'Content-Type': 'application/json' } }).then(res => res.json())); setState("show"); }; return (
{ state === "form" && } { state === "submitting" && } { state === "show" && setState("form") } /> }
) }; const root = document.getElementById('try-form'); render(, root.parentElement, root);