Skip to content
Star

Form

A project brief with inline checks and an agent that drafts the rest. Free and MIT licensed, as HTML, a typed React component or an Astro component, in three styles with light and dark mode.

$npx designme add form
$npx shadcn@latest add https://designme.dev/r/shadcn/form.json

Need this designed into your product? Book a call

New project brief
Type
01

Install

Add it to your project with the CLI.

HTML, CSS and JS

$npx designme add form

React

$npx designme add form --react

Astro

$npx designme add form --astro

shadcn, React

$npx shadcn@latest add https://designme.dev/r/shadcn/form.json

shadcn, Astro

$npx shadcn@latest add https://designme.dev/r/shadcn/astro/form.json

The CLI copies the source into your project, so you own it and can change anything. Where each version lands, what it needs and the shadcn theme bridge: Installation.

02

About

What it is for and what it does.

Form is a short form, set up as a project brief by default, with a Draft with agent button that fills the fields in for the person. Fields the agent filled carry a small sparkle after their label until the person edits them, and the line by the submit button says to review what the agent drafted, so it is clear what still needs a human read. On submit every field is checked, and a confirmation replaces the form once it goes through.

  • When an agent can fill in a brief, ticket or request from context and a person should review it
  • When people need to see which answers they typed and which the agent wrote
  • When a short form needs inline messages per field instead of a page of errors
  • When creating something should hand off to the agent, as in "the agent is gathering references now"
03

Usage

Import it in React and drive it from your own state, or place it in an Astro page.

React

import { Form } from '@/components/designme/form/Form';

React example

The demo above as a React component, 44 lines you can copy.

'use client';
// The animated preview from designme.dev, rebuilt from the React component: an example of driving it from your own state.
import { useRef, useState } from 'react';
import { useScript, useTakeover, useWait } from '../lib/useScript';
import { Form, type FormField, type FormHandle, type FormValues } from './Form';

const FIELDS: FormField[] = [
  { key: 'name', label: 'Project name', placeholder: 'Pricing refresh', maxLength: 40, validate: v => !v.trim() && 'Give it a name' },
  { key: 'goal', label: 'Goal', type: 'textarea', rows: 2, placeholder: 'What should change, and why?', validate: v => v.trim().length < 12 && 'Add a sentence about the goal' },
  { key: 'type', label: 'Type', type: 'choice', options: [{ value: 'web', label: 'Website', tone: 'b' }, { value: 'app', label: 'App', tone: 'l' }, { value: 'brand', label: 'Brand', tone: 'p' }], validate: v => !v && 'Pick a type' },
  { key: 'due', label: 'Due', placeholder: 'Nov 14' }
];

/* ---------- Form: a project brief; the agent can draft it, and submit checks every field ---------- */
export default function FormDemo() {
  const [values, setValues] = useState<FormValues>({});
  const latest = useRef<FormValues>({});
  const set = (v: FormValues) => { latest.current = v; setValues(v); };
  const wait = useWait();

  // the agent types each field in, a letter at a time
  const typeIn = async (key: string, text: string) => {
    set({ ...latest.current, [key]: '' });
    for (let i = 1; i <= text.length; i++) { set({ ...latest.current, [key]: text.slice(0, i) }); await wait(18); }
  };
  const draft = async () => {
    if (!latest.current.name) await typeIn('name', 'Pricing refresh');
    await typeIn('goal', 'Lift trial to paid by showing annual plans first and keeping one CTA per card.');
    if (!latest.current.type) set({ ...latest.current, type: 'web' });
    if (!latest.current.due) await typeIn('due', 'Nov 14');
  };

  // a moment after it appears, the agent drafts the brief, unless someone has started on it
  // the preview has the agent fill it in once on its own, unless you take over first
  const form = useRef<FormHandle>(null), ref = useRef<HTMLDivElement>(null), mine = useTakeover(ref);
  useScript(async s => { await s.wait(1400); if (!mine() && !latest.current.name) await form.current?.draft(); });

  return (
    <div ref={ref} style={{ width: '100%' }}>
      <Form ref={form} fields={FIELDS} values={values} onValuesChange={set} onDraft={draft} onSubmit={() => wait(800)} />
    </div>
  );
}

Astro

In a page in src/pages. It plays the demo above; change its form.js to show your own data. Attributes like class go on its root element.

---
import Form from '../components/designme/form/Form.astro';
---
<Form />
<Form data-style="soft" class="my-class" />
04

Props

Everything Form takes.

PropTypeWhat it does
titleReactNodeDefaults to "New project brief"
fieldsrequiredFormField[]
valuesFormValuesThe values by field key. Leave it out to let the fields hold their own
defaultValuesFormValues
onValuesChange(values: FormValues) => void
onDraft() => void | Promise<void>Adds "Draft with agent". Fill the values in while the promise runs; the fields it fills get a small sparkle after their label until someone edits them
draftLabelstring
draftingLabelstring
draftedLabelstringThe sparkle's tooltip and its name for screen readers. Defaults to "Drafted by the agent"
draftedMessagestringShown beside the submit button after a draft. Defaults to "Review what the agent drafted"
onSubmit(values: FormValues) => void | Promise<void>Called with the values once every field passes. Return a promise and the button shows a spinner and submittingLabel until it settles
submitLabelstring
submittingLabelstringOn the button while onSubmit's promise runs. Defaults to "Creating"
invalidMessagestringShown beside the submit button when a field needs a look
errorMessagestringShown when onSubmit rejects without a message of its own
doneTitle(values: FormValues) => ReactNodeThe line after a submit. Defaults to "<first field> created"
doneDetailReactNode
classNamestring
refRef<FormHandle>Gives access to draft(), to start the agent's draft without a click
05

Code

The full source, as the CLI installs it.

Form.tsxReact · 192 lines
'use client';
import { useEffect, useImperativeHandle, useRef, useState, type FormEvent, type ReactNode, type Ref, type RefObject } from 'react';
import { cx } from '../lib/cx';
import { useControllable, useIsoLayoutEffect, useReducedMotion } from '../lib/hooks';
import { Icon } from '../lib/icons';
import { enter, Spin, Swap, Think, Tip, useGrow } from '../lib/motion';
import '../designme.css';
import './form.css';

export type FormValues = Record<string, string>;

/** What a ref to the form can do */
export interface FormHandle {
  /** Runs onDraft as if "Draft with agent" were pressed, with the same Drafting state and drafted marks */
  draft: () => Promise<void>;
}

export interface FormField {
  key: string;
  /** Like "Project name" */
  label: string;
  /** text (the default), textarea, or choice: a row of chips that picks one */
  type?: 'text' | 'textarea' | 'choice';
  placeholder?: string;
  /** Shows a counter like 0/40 and stops typing there */
  maxLength?: number;
  /** Rows for a textarea. Defaults to 2 */
  rows?: number;
  /** The chips of a choice field. A `tone` gives the option its core tag colour: a dot in it at rest, and its tint and text colour once picked */
  options?: { value: string; label: string; tone?: 'b' | 'l' | 'm' | 'p' | 'r' | 'y' | 'k' | 't' }[];
  autoComplete?: string;
  /** Returns the message to show when the value won't do, like "Give it a name". Checked on submit */
  validate?: (value: string, values: FormValues) => string | false | null | undefined;
}

export interface FormProps {
  /** Defaults to "New project brief" */
  title?: ReactNode;
  fields: FormField[];
  /** The values by field key. Leave it out to let the fields hold their own */
  values?: FormValues;
  defaultValues?: FormValues;
  onValuesChange?: (values: FormValues) => void;
  /** Adds "Draft with agent". Fill the values in while the promise runs; the fields it fills get a small sparkle after their label until someone edits them */
  onDraft?: () => void | Promise<void>;
  draftLabel?: string;
  draftingLabel?: string;
  /** The sparkle's tooltip and its name for screen readers. Defaults to "Drafted by the agent" */
  draftedLabel?: string;
  /** Shown beside the submit button after a draft. Defaults to "Review what the agent drafted" */
  draftedMessage?: string;
  /** Called with the values once every field passes. Return a promise and the button shows a spinner and submittingLabel until it settles */
  onSubmit?: (values: FormValues) => void | Promise<void>;
  submitLabel?: string;
  /** On the button while onSubmit's promise runs. Defaults to "Creating" */
  submittingLabel?: string;
  /** Shown beside the submit button when a field needs a look */
  invalidMessage?: string;
  /** Shown when onSubmit rejects without a message of its own */
  errorMessage?: string;
  /** The line after a submit. Defaults to "<first field> created" */
  doneTitle?: (values: FormValues) => ReactNode;
  doneDetail?: ReactNode;
  className?: string;
  /** Gives access to draft(), to start the agent's draft without a click */
  ref?: Ref<FormHandle>;
}

/** A button whose words change eases to their width, with no room kept for the longer ones */
function useEaseWidth(ref: RefObject<HTMLElement | null>, key: unknown) {
  const reduce = useReducedMotion(), w = useRef(0);
  useIsoLayoutEffect(() => {
    const el = ref.current; if (!el) return;
    const w1 = el.offsetWidth, w0 = w.current; w.current = w1;
    if (!w0 || reduce || Math.abs(w1 - w0) < .5) return;
    const cs = getComputedStyle(el);
    el.animate([{ width: w0 + 'px' }, { width: w1 + 'px' }], { duration: (parseFloat(cs.getPropertyValue('--dm-t')) || .34) * 1000, easing: cs.getPropertyValue('--dm-ease').trim() || 'ease' });
  }, [key]);
}

/** A short form an agent can draft for you: fields that check themselves on submit, and a confirmation. The card grows with
    its messages and eases to the confirmation's height, keeping no room for either. */
export function Form({
  title = 'New project brief', fields, values, defaultValues = {}, onValuesChange, onDraft, draftLabel = 'Draft with agent', draftingLabel = 'Drafting',
  draftedLabel = 'Drafted by the agent', draftedMessage = 'Review what the agent drafted', onSubmit, submitLabel = 'Create brief', submittingLabel = 'Creating', invalidMessage = 'A few fields need a look',
  errorMessage = 'Something went wrong, try again', doneTitle, doneDetail = 'The agent is gathering references now', className, ref
}: FormProps) {
  const [vals, setVals] = useControllable(values, defaultValues, onValuesChange);
  const [errors, setErrors] = useState<Record<string, string>>({});
  const [ai, setAi] = useState<string[]>([]);
  const [draft, setDraft] = useState<'idle' | 'drafting' | 'drafted'>('idle');
  const [message, setMessage] = useState('');
  const [busy, setBusy] = useState(false);
  const [done, setDone] = useState(false);
  const own = useRef<string | null>(null);
  const [outer, inner] = useGrow<HTMLFormElement>();
  const ai_ = useRef<HTMLButtonElement>(null), go = useRef<HTMLButtonElement>(null);
  useEaseWidth(ai_, draft); useEaseWidth(go, busy);

  // a changed value clears its field's message, whoever changed it; while a draft runs, the text fields it fills are marked Drafted
  // until the person edits them
  const before = useRef(vals);
  useEffect(() => {
    const was = before.current; before.current = vals;
    const edited = own.current; own.current = null;
    const changed = fields.filter(f => f.key !== edited && (was[f.key] ?? '') !== (vals[f.key] ?? '')).map(f => f.key);
    if (!changed.length) return;
    setErrors(e => Object.fromEntries(Object.entries(e).filter(([k]) => !changed.includes(k))));
    const filled = draft === 'drafting' ? changed.filter(k => fields.find(f => f.key === k)?.type !== 'choice') : [];
    if (filled.length) setAi(a => [...new Set([...a, ...filled])]);
  }, [vals]); // eslint-disable-line react-hooks/exhaustive-deps

  const set = (key: string, value: string) => {
    own.current = key;
    setVals({ ...vals, [key]: value });
    setErrors(({ [key]: _, ...rest }) => rest);
    setAi(a => a.filter(k => k !== key));
  };

  const runDraft = async () => {
    if (!onDraft || draft === 'drafting') return;
    setDraft('drafting');
    try { await onDraft(); } finally { setDraft('drafted'); }
    setMessage(draftedMessage);
  };
  useImperativeHandle(ref, () => ({ draft: runDraft }));

  const submit = async (e: FormEvent) => {
    e.preventDefault();
    if (busy) return;
    const found = { ...errors };
    for (const f of fields) { const msg = f.validate?.(vals[f.key] ?? '', vals); if (msg && !found[f.key]) found[f.key] = msg; }
    setErrors(found);
    if (fields.some(f => f.validate?.(vals[f.key] ?? '', vals))) { setMessage(invalidMessage); return; }
    setBusy(true);
    try { await onSubmit?.(vals); } catch (err) { setBusy(false); setMessage(err instanceof Error && err.message ? err.message : errorMessage); return; }
    setBusy(false); setDone(true);
  };

  const first = fields[0] ? vals[fields[0].key] : '';
  return (
    <form ref={outer} data-dm="" className={cx('dm-fmx dm-panel dm-growing', className)} noValidate onSubmit={done ? e => e.preventDefault() : submit}>
      <div ref={inner} className="dm-grow">
        {done ? (
          <div className="dm-fmx-done" role="status">
            <Icon name="ok" className="dm-enter" />
            <b {...enter(70)}>{doneTitle ? doneTitle(vals) : `${first} created`}</b>
            <span className="dm-muted dm-enter" style={enter(140).style}>{doneDetail}</span>
          </div>
        ) : <>
          <div className="dm-fmx-h">
            <b>{title}</b>
            {onDraft && (
              <button ref={ai_} type="button" className="dm-pill dm-line dm-sm dm-fmx-ai" onClick={runDraft}>
                {draft === 'drafting' ? <><Think /><span className="dm-shim">{draftingLabel}</span></> : <><Icon name="spark" size="xs" />{draftLabel}</>}
              </button>
            )}
          </div>
          {fields.map(f => {
            const value = vals[f.key] ?? '', err = errors[f.key];
            const state = cx('dm-fmx-f', err && 'dm-bad', ai.includes(f.key) && 'dm-ai');
            const note = err && <span className="dm-er" role="alert">{err}</span>;
            if (f.type === 'choice') return (
              <div key={f.key} className={state}>
                <span>{f.label}</span>
                <div className="dm-fmx-ty" role="group" aria-label={f.label}>
                  {f.options?.map(o => <button type="button" key={o.value} data-tone={o.tone} className={cx('dm-chip dm-sm', value === o.value && 'dm-on')} aria-pressed={value === o.value} onClick={() => set(f.key, o.value)}>{o.label}</button>)}
                </div>
                {note}
              </div>
            );
            return (
              <label key={f.key} className={state}>
                <span>{f.label}<Tip label={draftedLabel} off={!ai.includes(f.key)}><i className="dm-fmx-dr" role="img" aria-label={draftedLabel}><Icon name="spark" size="xs" /></i></Tip></span>
                {f.type === 'textarea'
                  ? <textarea name={f.key} rows={f.rows ?? 2} placeholder={f.placeholder} maxLength={f.maxLength} autoComplete={f.autoComplete} aria-invalid={!!err || undefined} value={value} onChange={e => set(f.key, e.target.value)} />
                  : <input name={f.key} placeholder={f.placeholder} maxLength={f.maxLength} autoComplete={f.autoComplete} aria-invalid={!!err || undefined} value={value} onChange={e => set(f.key, e.target.value)} />}
                {f.maxLength && f.type !== 'textarea' && <em className="dm-fmx-c dm-num">{value.length}/{f.maxLength}</em>}
                {note}
              </label>
            );
          })}
          <div className="dm-fmx-ft">
            <Swap className="dm-fmx-ms dm-muted">{message}</Swap>
            <button ref={go} type="submit" className="dm-pill dm-primary dm-sm dm-fmx-go">{busy ? <><Spin /><span className="dm-shim">{submittingLabel}</span></> : submitLabel}</button>
          </div>
        </>}
      </div>
    </form>
  );
}
form.cssReact · 61 lines
/* Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync. */
/* ---------- Form ---------- */
.dm-fmx{padding:var(--dm-s-4);display:flex;flex-direction:column;gap:var(--dm-s-3);max-width:380px;margin:0 auto;width:100%}
.dm-fmx-h{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-2)}
.dm-fmx-h b{font-weight:500;font-size:14px}
.dm-fmx-f{display:flex;flex-direction:column;gap:5px;font-size:12.5px;font-weight:500;position:relative}
.dm-fmx-f input,.dm-fmx-f textarea{border:0;outline:0;border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);padding:10px var(--dm-s-3);font:inherit;font-size:13.5px;line-height:20px;font-weight:400;color:var(--dm-fg);resize:none;transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.dm-fmx-f :is(input,textarea){corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.dm-fmx-f input::placeholder,.dm-fmx-f textarea::placeholder{color:var(--dm-muted)}
.dm-fmx-f input:focus,.dm-fmx-f textarea:focus{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
/* a field marked wrong keeps its red edge, focused or not, until it is fixed; the message says what is wrong */
.dm-fmx-f.dm-bad input,.dm-fmx-f.dm-bad textarea{box-shadow:inset 0 0 0 1px var(--dm-bad)}
.dm-fmx-f .dm-er{font-size:11.5px;color:var(--dm-bad);font-weight:400;animation:dm-enter .35s var(--dm-ease) backwards}
/* what the agent drafted is marked by a small sparkle right after its label, with no word (owner, 27 Nov: the foot line says
   it once in words): pointing at it says "Drafted by the agent", which is also its name for screen readers. It shows until
   someone edits the field; hidden, it is out of the accessibility tree too */
.dm-fmx-f>span:first-child{display:flex;align-items:center;gap:var(--dm-s-1)}
.dm-fmx-dr{display:inline-grid;place-items:center;font-style:normal;color:var(--dm-accent-mark,var(--dm-accent));cursor:default;visibility:hidden;opacity:0;transform:translateX(-3px);transition:opacity var(--dm-t) var(--dm-ease),transform var(--dm-t) var(--dm-ease),visibility var(--dm-t)}
.dm-fmx-f.dm-ai .dm-fmx-dr{visibility:visible;opacity:1;transform:none}
.dm-fmx-c{position:absolute;right:12px;top:24px;height:40px;display:flex;align-items:center;font-style:normal;font-size:11px;color:var(--dm-muted);font-weight:400;pointer-events:none}
.dm-fmx-ty{display:flex;gap:6px}
/* the types are white chips with a dimmed grey edge; the picked one's edge is a step stronger. A hover is a quiet fill */
.dm-fmx-ty .dm-chip{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent)}
@media (hover:hover) and (pointer:fine){.dm-fmx-ty .dm-chip:hover{background:color-mix(in srgb,var(--dm-fg) 4%,var(--dm-surface))}}
/* in dark mode the 4% hover barely shows on the dark surface, so it takes a step more (check-rules R6, 27 Nov) */
@media (prefers-color-scheme:dark){@media (hover:hover) and (pointer:fine){:root:not([data-theme="light"]) .dm-fmx-ty .dm-chip:hover{background:color-mix(in srgb,var(--dm-fg) 9%,var(--dm-surface))}}}
@media (hover:hover) and (pointer:fine){:root[data-theme="dark"] .dm-fmx-ty .dm-chip:hover{background:color-mix(in srgb,var(--dm-fg) 9%,var(--dm-surface))}}
.dm-fmx-ty .dm-chip.dm-on{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-on) 50%,transparent)}
/* each type is a label with its own core tag colour, wherever it shows (owner, 27 Nov: "add some color"): at rest a small dot
   in its colour on the neutral chip; picked, the colour's tint with its text colour (as .tag draws it) and the selected edge */
.dm-fmx-ty .dm-chip[data-tone]::before{content:"";flex:none;width:6px;height:6px;margin-left:-1px;border-radius:50%;corner-shape:round;background:var(--dm-tn)}
.dm-fmx-ty .dm-chip[data-tone="b"]{--dm-tn:var(--dm-pb);--dm-tn-bg:var(--dm-pb-bg)}.dm-fmx-ty .dm-chip[data-tone="l"]{--dm-tn:var(--dm-pl);--dm-tn-bg:var(--dm-pl-bg)}
.dm-fmx-ty .dm-chip[data-tone="m"]{--dm-tn:var(--dm-pm);--dm-tn-bg:var(--dm-pm-bg)}.dm-fmx-ty .dm-chip[data-tone="p"]{--dm-tn:var(--dm-pp);--dm-tn-bg:var(--dm-pp-bg)}
.dm-fmx-ty .dm-chip[data-tone="r"]{--dm-tn:var(--dm-pr);--dm-tn-bg:var(--dm-pr-bg)}.dm-fmx-ty .dm-chip[data-tone="y"]{--dm-tn:var(--dm-py);--dm-tn-bg:var(--dm-py-bg)}
.dm-fmx-ty .dm-chip[data-tone="k"]{--dm-tn:var(--dm-pk);--dm-tn-bg:var(--dm-pk-bg)}.dm-fmx-ty .dm-chip[data-tone="t"]{--dm-tn:var(--dm-pt);--dm-tn-bg:var(--dm-pt-bg)}
.dm-fmx-ty .dm-chip[data-tone].dm-on{background:var(--dm-tn-bg);color:var(--dm-tn)}
@media (hover:hover) and (pointer:fine){.dm-fmx-ty .dm-chip[data-tone].dm-on:hover{background:var(--dm-tn-bg);color:var(--dm-tn)}}
/* the footer sits under a hairline in Soft and Blueprint (owner, 21 Nov), 16px under the fields and 12px over its button */
.dm-fmx-ft{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-2);margin-top:var(--dm-s-1);padding-top:var(--dm-s-4);border-top:1px solid color-mix(in srgb,var(--dm-line) 50%,transparent)}
/* Studio: the footer is the card's off-white foot strip (owner, 21 Nov: "studio doesnt follow its style with buttons being
   separated by a divider line and on a gray bg"): its words and Create brief 16px in, 16px from the strip's top and bottom (owner, 23 Nov) */
:root[data-style="studio"] .dm-fmx-ft,:root [data-style="studio"] .dm-fmx-ft,:root [data-style="studio"].dm-fmx-ft{padding-top:0;border-top:0}
:root[data-style="studio"] .dm-fmx.dm-panel .dm-fmx-ft,:root [data-style="studio"] .dm-fmx.dm-panel .dm-fmx-ft,:root [data-style="studio"].dm-fmx.dm-panel .dm-fmx-ft{margin:var(--dm-s-1) -16px -16px;padding:var(--dm-strip-y) 16px;background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
.dm-fmx-ms{font-size:12px}
/* nothing in it grows or shrinks when pressed but Studio's ink primary; a button whose words change eases to their width */
.dm-fmx :is(.dm-pill,.dm-chip):active{transform:none}
.dm-fmx :is(.dm-fmx-ai,.dm-fmx-go){overflow:hidden}
.dm-fmx-ai .dm-shim{font-size:inherit}
/* the words on a filled button sweep in its own text colour */
.dm-fmx-go .dm-shim{background-image:linear-gradient(90deg,var(--dm-on-accent) 0%,var(--dm-on-accent) 38%,color-mix(in srgb,var(--dm-on-accent) 45%,transparent) 50%,var(--dm-on-accent) 62%,var(--dm-on-accent) 100%);font-size:inherit}
:root[data-style="studio"] .dm-fmx-go .dm-shim,:root [data-style="studio"] .dm-fmx-go .dm-shim,:root [data-style="studio"].dm-fmx-go .dm-shim{background-image:linear-gradient(90deg,var(--dm-on-btn) 0%,var(--dm-on-btn) 38%,color-mix(in srgb,var(--dm-on-btn) 45%,transparent) 50%,var(--dm-on-btn) 62%,var(--dm-on-btn) 100%)}
/* the type chips keep their quiet edge: the one hover fills them, and a picked one has the stronger fill (owner, 21 Nov) */
:root[data-style="studio"] .dm-fmx-ty .dm-chip,:root [data-style="studio"] .dm-fmx-ty .dm-chip,:root [data-style="studio"].dm-fmx-ty .dm-chip{transition:background-color var(--dm-t-fast) var(--dm-ease),box-shadow var(--dm-t-fast) var(--dm-ease),color var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] .dm-fmx-ty .dm-chip:not(.dm-on):hover,:root [data-style="studio"] .dm-fmx-ty .dm-chip:not(.dm-on):hover,:root [data-style="studio"].dm-fmx-ty .dm-chip:not(.dm-on):hover{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent),inset 0 0 0 99px var(--dm-row)}}
:root[data-style="studio"] .dm-fmx-ty .dm-chip.dm-on,:root [data-style="studio"] .dm-fmx-ty .dm-chip.dm-on,:root [data-style="studio"].dm-fmx-ty .dm-chip.dm-on{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent),inset 0 0 0 99px var(--dm-row-on)}
:root[data-style="studio"] .dm-fmx-ty .dm-chip[data-tone].dm-on,:root [data-style="studio"] .dm-fmx-ty .dm-chip[data-tone].dm-on,:root [data-style="studio"].dm-fmx-ty .dm-chip[data-tone].dm-on{background:var(--dm-tn-bg);color:var(--dm-tn);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent)}
/* done: the card eases to the confirmation's height (grow()), with the vivid green tick */
.dm-fmx-done{display:flex;flex-direction:column;align-items:center;gap:6px;padding:var(--dm-s-6) 0;text-align:center;font-size:13px}
.dm-fmx-done>svg.dm-i{width:28px;height:28px;color:var(--dm-ok-mark)}
.dm-fmx-done>b{font-weight:500;font-size:14px}
Form.astroAstro · 31 lines
---
// Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync.
// Form: A project brief with inline checks and an agent that drafts the rest.
// The markup, CSS and script are designme.dev's own, run by the shared core in ../core, which mounts the component when it scrolls
// into view and plays its demo. To show your own data, change form.js. The markup is kept exactly as it is (is:raw).
import type { HTMLAttributes } from 'astro/types';
import '../core/designme.css';
import './form.css';

export interface Props extends HTMLAttributes<'div'> {
  /** "soft" or "studio" draws it in that style. Leave it out for Blueprint, or for the style set on <html> (data-style="soft" or "studio") */
  'data-style'?: 'soft' | 'studio';
}
const { class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} data-c="form" {...rest}>
  <div class="stage-in"><Fragment is:raw>
<form class="fmx panel" novalidate>
  <div class="fmx-h"><b>New project brief</b><button type="button" class="pill line sm fmx-ai"><svg class="i xs"><use href="#i-spark"/></svg>Draft with agent</button></div>
  <label class="fmx-f"><span>Project name<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><input name="n" placeholder="Pricing refresh" maxlength="40"><em class="fmx-c num">0/40</em></label>
  <label class="fmx-f"><span>Goal<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><textarea name="g" rows="2" placeholder="What should change, and why?"></textarea></label>
  <div class="fmx-f"><span>Type</span><div class="fmx-ty"><button type="button" class="chip sm" data-v="web" data-tone="b">Website</button><button type="button" class="chip sm" data-v="app" data-tone="l">App</button><button type="button" class="chip sm" data-v="brand" data-tone="p">Brand</button></div></div>
  <label class="fmx-f"><span>Due<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><input name="d" type="text" placeholder="Nov 14"></label>
  <div class="fmx-ft"><span class="fmx-ms muted"></span><button class="pill primary sm fmx-go" type="submit">Create brief</button></div>
</form>
  </Fragment></div>
</div>
<script>
  import './form.js';
</script>
form.htmlHTML · 13 lines
<div class="card" data-c="form">
  <div class="stage-in">
    <form class="fmx panel" novalidate>
      <div class="fmx-h"><b>New project brief</b><button type="button" class="pill line sm fmx-ai"><svg class="i xs"><use href="#i-spark"/></svg>Draft with agent</button></div>
      <label class="fmx-f"><span>Project name<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><input name="n" placeholder="Pricing refresh" maxlength="40"><em class="fmx-c num">0/40</em></label>
      <label class="fmx-f"><span>Goal<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><textarea name="g" rows="2" placeholder="What should change, and why?"></textarea></label>
      <div class="fmx-f"><span>Type</span><div class="fmx-ty"><button type="button" class="chip sm" data-v="web" data-tone="b">Website</button><button type="button" class="chip sm" data-v="app" data-tone="l">App</button><button type="button" class="chip sm" data-v="brand" data-tone="p">Brand</button></div></div>
      <label class="fmx-f"><span>Due<i class="fmx-dr" role="img" aria-label="Drafted by the agent" data-tip="Drafted by the agent"><svg class="i xs"><use href="#i-spark"/></svg></i></span><input name="d" type="text" placeholder="Nov 14"></label>
      <div class="fmx-ft"><span class="fmx-ms muted"></span><button class="pill primary sm fmx-go" type="submit">Create brief</button></div>
    </form>
  </div>
</div>
form.cssHTML · 60 lines
/* ---------- Form ---------- */
.fmx{padding:var(--dm-s-4);display:flex;flex-direction:column;gap:var(--dm-s-3);max-width:380px;margin:0 auto;width:100%}
.fmx-h{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-2)}
.fmx-h b{font-weight:500;font-size:14px}
.fmx-f{display:flex;flex-direction:column;gap:5px;font-size:12.5px;font-weight:500;position:relative}
.fmx-f input,.fmx-f textarea{border:0;outline:0;border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);padding:10px var(--dm-s-3);font:inherit;font-size:13.5px;line-height:20px;font-weight:400;color:var(--dm-fg);resize:none;transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.fmx-f :is(input,textarea){corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.fmx-f input::placeholder,.fmx-f textarea::placeholder{color:var(--dm-muted)}
.fmx-f input:focus,.fmx-f textarea:focus{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
/* a field marked wrong keeps its red edge, focused or not, until it is fixed; the message says what is wrong */
.fmx-f.bad input,.fmx-f.bad textarea{box-shadow:inset 0 0 0 1px var(--dm-bad)}
.fmx-f .er{font-size:11.5px;color:var(--dm-bad);font-weight:400;animation:enter .35s var(--dm-ease) backwards}
/* what the agent drafted is marked by a small sparkle right after its label, with no word (owner, 27 Nov: the foot line says
   it once in words): pointing at it says "Drafted by the agent", which is also its name for screen readers. It shows until
   someone edits the field; hidden, it is out of the accessibility tree too */
.fmx-f>span:first-child{display:flex;align-items:center;gap:var(--dm-s-1)}
.fmx-dr{display:inline-grid;place-items:center;font-style:normal;color:var(--dm-accent-mark,var(--dm-accent));cursor:default;visibility:hidden;opacity:0;transform:translateX(-3px);transition:opacity var(--dm-t) var(--dm-ease),transform var(--dm-t) var(--dm-ease),visibility var(--dm-t)}
.fmx-f.ai .fmx-dr{visibility:visible;opacity:1;transform:none}
.fmx-c{position:absolute;right:12px;top:24px;height:40px;display:flex;align-items:center;font-style:normal;font-size:11px;color:var(--dm-muted);font-weight:400;pointer-events:none}
.fmx-ty{display:flex;gap:6px}
/* the types are white chips with a dimmed grey edge; the picked one's edge is a step stronger. A hover is a quiet fill */
.fmx-ty .chip{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent)}
@media (hover:hover) and (pointer:fine){.fmx-ty .chip:hover{background:color-mix(in srgb,var(--dm-fg) 4%,var(--dm-surface))}}
/* in dark mode the 4% hover barely shows on the dark surface, so it takes a step more (check-rules R6, 27 Nov) */
@media (prefers-color-scheme:dark){@media (hover:hover) and (pointer:fine){:root:not([data-theme="light"]) .fmx-ty .chip:hover{background:color-mix(in srgb,var(--dm-fg) 9%,var(--dm-surface))}}}
@media (hover:hover) and (pointer:fine){:root[data-theme="dark"] .fmx-ty .chip:hover{background:color-mix(in srgb,var(--dm-fg) 9%,var(--dm-surface))}}
.fmx-ty .chip.on{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-on) 50%,transparent)}
/* each type is a label with its own core tag colour, wherever it shows (owner, 27 Nov: "add some color"): at rest a small dot
   in its colour on the neutral chip; picked, the colour's tint with its text colour (as .tag draws it) and the selected edge */
.fmx-ty .chip[data-tone]::before{content:"";flex:none;width:6px;height:6px;margin-left:-1px;border-radius:50%;corner-shape:round;background:var(--dm-tn)}
.fmx-ty .chip[data-tone="b"]{--dm-tn:var(--dm-pb);--dm-tn-bg:var(--dm-pb-bg)}.fmx-ty .chip[data-tone="l"]{--dm-tn:var(--dm-pl);--dm-tn-bg:var(--dm-pl-bg)}
.fmx-ty .chip[data-tone="m"]{--dm-tn:var(--dm-pm);--dm-tn-bg:var(--dm-pm-bg)}.fmx-ty .chip[data-tone="p"]{--dm-tn:var(--dm-pp);--dm-tn-bg:var(--dm-pp-bg)}
.fmx-ty .chip[data-tone="r"]{--dm-tn:var(--dm-pr);--dm-tn-bg:var(--dm-pr-bg)}.fmx-ty .chip[data-tone="y"]{--dm-tn:var(--dm-py);--dm-tn-bg:var(--dm-py-bg)}
.fmx-ty .chip[data-tone="k"]{--dm-tn:var(--dm-pk);--dm-tn-bg:var(--dm-pk-bg)}.fmx-ty .chip[data-tone="t"]{--dm-tn:var(--dm-pt);--dm-tn-bg:var(--dm-pt-bg)}
.fmx-ty .chip[data-tone].on{background:var(--dm-tn-bg);color:var(--dm-tn)}
@media (hover:hover) and (pointer:fine){.fmx-ty .chip[data-tone].on:hover{background:var(--dm-tn-bg);color:var(--dm-tn)}}
/* the footer sits under a hairline in Soft and Blueprint (owner, 21 Nov), 16px under the fields and 12px over its button */
.fmx-ft{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-2);margin-top:var(--dm-s-1);padding-top:var(--dm-s-4);border-top:1px solid color-mix(in srgb,var(--dm-line) 50%,transparent)}
/* Studio: the footer is the card's off-white foot strip (owner, 21 Nov: "studio doesnt follow its style with buttons being
   separated by a divider line and on a gray bg"): its words and Create brief 16px in, 16px from the strip's top and bottom (owner, 23 Nov) */
:root[data-style="studio"] .fmx-ft,:root [data-style="studio"] .fmx-ft,:root [data-style="studio"].fmx-ft{padding-top:0;border-top:0}
:root[data-style="studio"] .fmx.panel .fmx-ft,:root [data-style="studio"] .fmx.panel .fmx-ft,:root [data-style="studio"].fmx.panel .fmx-ft{margin:var(--dm-s-1) -16px -16px;padding:var(--dm-strip-y) 16px;background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
.fmx-ms{font-size:12px}
/* nothing in it grows or shrinks when pressed but Studio's ink primary; a button whose words change eases to their width */
.fmx :is(.pill,.chip):active{transform:none}
.fmx :is(.fmx-ai,.fmx-go){overflow:hidden}
.fmx-ai .shim{font-size:inherit}
/* the words on a filled button sweep in its own text colour */
.fmx-go .shim{background-image:linear-gradient(90deg,var(--dm-on-accent) 0%,var(--dm-on-accent) 38%,color-mix(in srgb,var(--dm-on-accent) 45%,transparent) 50%,var(--dm-on-accent) 62%,var(--dm-on-accent) 100%);font-size:inherit}
:root[data-style="studio"] .fmx-go .shim,:root [data-style="studio"] .fmx-go .shim,:root [data-style="studio"].fmx-go .shim{background-image:linear-gradient(90deg,var(--dm-on-btn) 0%,var(--dm-on-btn) 38%,color-mix(in srgb,var(--dm-on-btn) 45%,transparent) 50%,var(--dm-on-btn) 62%,var(--dm-on-btn) 100%)}
/* the type chips keep their quiet edge: the one hover fills them, and a picked one has the stronger fill (owner, 21 Nov) */
:root[data-style="studio"] .fmx-ty .chip,:root [data-style="studio"] .fmx-ty .chip,:root [data-style="studio"].fmx-ty .chip{transition:background-color var(--dm-t-fast) var(--dm-ease),box-shadow var(--dm-t-fast) var(--dm-ease),color var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] .fmx-ty .chip:not(.on):hover,:root [data-style="studio"] .fmx-ty .chip:not(.on):hover,:root [data-style="studio"].fmx-ty .chip:not(.on):hover{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent),inset 0 0 0 99px var(--dm-row)}}
:root[data-style="studio"] .fmx-ty .chip.on,:root [data-style="studio"] .fmx-ty .chip.on,:root [data-style="studio"].fmx-ty .chip.on{background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent),inset 0 0 0 99px var(--dm-row-on)}
:root[data-style="studio"] .fmx-ty .chip[data-tone].on,:root [data-style="studio"] .fmx-ty .chip[data-tone].on,:root [data-style="studio"].fmx-ty .chip[data-tone].on{background:var(--dm-tn-bg);color:var(--dm-tn);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-pick-line) 50%,transparent)}
/* done: the card eases to the confirmation's height (grow()), with the vivid green tick */
.fmx-done{display:flex;flex-direction:column;align-items:center;gap:6px;padding:var(--dm-s-6) 0;text-align:center;font-size:13px}
.fmx-done>svg.i{width:28px;height:28px;color:var(--dm-ok-mark)}
.fmx-done>b{font-weight:500;font-size:14px}
form.jsHTML · 43 lines
DM.def('form', (r, c) => {
  r.innerHTML = r.innerHTML.split('{spark}').join(I('spark','xs'));
  const form = $('.fmx', r), n = $('[name=n]', r), g = $('[name=g]', r), d = $('[name=d]', r), cnt = $('.fmx-c', r), ms = $('.fmx-ms', r);
  // the card grows with its messages and eases to the confirmation's height, keeping no room for either
  c.offs.push(grow(form));
  // a button whose words change eases to their width, with no room kept for the longer ones
  const label = (b, h) => { const w0 = b.offsetWidth; b.innerHTML = h; const w1 = b.offsetWidth, cs = getComputedStyle(form);
    if (!REDUCE && w0 && Math.abs(w1 - w0) > .5) b.animate([{ width:w0 + 'px' }, { width:w1 + 'px' }], { duration:(parseFloat(cs.getPropertyValue('--dm-t')) || .34) * 1000, easing:cs.getPropertyValue('--dm-ease').trim() || 'ease' }); };
  let type = null;
  n.oninput = () => { cnt.textContent = n.value.length + '/40'; clear(n); };
  g.oninput = () => clear(g); d.oninput = () => clear(d);
  const chips = $$('.fmx-ty .chip', r);
  chips.forEach(ch => ch.onclick = () => { type = ch.dataset.v; chips.forEach(x => x.classList.toggle('on', x === ch)); clear(ch.parentElement); });
  function clear(el){ const f = el.closest('.fmx-f'); f.classList.remove('bad', 'ai'); $('.er', f)?.remove(); }
  function bad(el, t){ const f = el.closest('.fmx-f'); if (f.classList.contains('bad')) return; f.classList.add('bad'); f.insertAdjacentHTML('beforeend', `<span class="er">${t}</span>`); }
  async function type_(el, s){ el.closest('.fmx-f').classList.add('ai'); el.value = ''; for (const ch of s){ el.value += ch; el.dispatchEvent(new Event('input')); el.closest('.fmx-f').classList.add('ai'); await c.wait(18); } }
  // the agent's draft: the button says Drafting with the highlight sweep while it types, then goes back to Draft with agent;
  // each field it filled shows its sparkle, and the foot line says it once in words
  let drafting = false;
  $('.fmx-ai', r).onclick = () => { if (drafting) return; drafting = true; c.run(async () => {
    const b = $('.fmx-ai', r); label(b, '<span class="think on"><i></i><i></i><i></i><i></i></span><span class="shim">Drafting</span>');
    if (!n.value) await type_(n, 'Pricing refresh');
    await type_(g, 'Lift trial to paid by showing annual plans first and keeping one CTA per card.');
    if (!type) chips[0].click();
    if (!d.value) await type_(d, 'Nov 14');
    label(b, I('spark','xs') + 'Draft with agent'); swap(ms, 'Review what the agent drafted'); drafting = false;
  }); };
  // the preview has the agent fill it in once on its own, unless you take over first. The agent's typing keeps going under the
  // pointer (DM.PLAYS); the press that starts it waits while a mouse rests on the card
  c.auto(1400, function go(){ if (c.mine) return; if (r.matches(':hover')) return c.later(250, go); if (!n.value) $('.fmx-ai', r).click(); });
  let sent = false;
  form.onsubmit = e => { e.preventDefault(); let ok = true;
    if (!n.value.trim()){ bad(n, 'Give it a name'); ok = false; }
    if (g.value.trim().length < 12){ bad(g, 'Add a sentence about the goal'); ok = false; }
    if (!type){ bad(chips[0], 'Pick a type'); ok = false; }
    if (!ok) return swap(ms, 'A few fields need a look');
    if (sent) return; sent = true;
    c.run(async () => { const go = $('.fmx-go', r); label(go, '<span class="spin"></span><span class="shim">Creating</span>'); await c.wait(800);
      // the fields give way to the confirmation, and the card eases to its height
      const into = $(':scope>.dm-grow', form) || form;
      [...into.children].forEach(k => k.hidden = true); const dn = html(`<div class="fmx-done" role="status">${I('ok')}<b>${n.value} created</b><span class="muted">The agent is gathering references now</span></div>`); into.appendChild(dn); stagger([...dn.children], 70); }); };
});
06

Questions

What people ask about it.

How does the agent fill in the fields?
Control the form with values and onValuesChange, and update the values inside your onDraft promise. Any text field that changes while the draft runs gets the drafted sparkle.
Can I start the agent's draft without a click?
Yes. Put a ref on the form and call ref.current.draft(). It runs onDraft with the same Drafting state and drafted marks as the button.
How do I validate a field against another field?
validate receives the field's value and all the form's values. Return a message string to block the submit, or a falsy value to let it pass.
07

More in Forms and inputs

Buttons, fields, pickers and sign-in flows for any product.

TooltipQuiet labels on hover, with shortcuts and a richer preview card.
Search inputSearch with scoped filters, recent queries and an agent answer.