Field states

One text field in four states: with helper text, with an error, confirmed and switched off. The message keeps its place under the field, so nothing jumps when a field is checked.

Account details

Options

Screen
Colour

Code

import { useId } from "react";
import "./field-states.css";

const marks = {
  error:
    "M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm0 3a.75.75 0 0 1 .75.75v3a.75.75 0 0 1-1.5 0v-3A.75.75 0 0 1 8 4.5Zm0 5.5a.9.9 0 1 1 0 1.800.9.9 0 0 1 0-1.800Z",
  success:
    "M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm3.030 4.970a.75.75 0 0 1 0 1.060l-3.500 3.500a.75.75 0 0 1-1.060 0l-1.500-1.500a.75.75 0 1 1 1.060-1.060l.970.970 2.970-2.970a.75.75 0 0 1 1.060 0Z",
};

/* A field with a message under it. state is "error", "success" or left out;
   message is the helper text, the error or the confirmation.

   <Field label="Email" state="error" message="Enter a full email address." /> */
export function Field({ label, state, message, ...props }) {
  const id = useId();
  return (
    <label className="dh-state" data-state={state}>
      <span>{label}</span>
      <input type="text" aria-invalid={state === "error" || undefined} aria-describedby={id} {...props} />
      <small id={id} role={state === "error" ? "alert" : undefined}>
        {marks[state] && (
          <svg viewBox="0 0 16 16" aria-hidden="true">
            <path d={marks[state]} />
          </svg>
        )}
        {message}
      </small>
    </label>
  );
}

export function Example() {
  return (
    <section className="dh-state-card">
      <h3>Account details</h3>
      <div className="dh-state-stack">
        <Field label="Company" defaultValue="Logoipsum" message="As it should appear on invoices." />
        <Field
          label="Email"
          type="email"
          defaultValue="maya@example"
          state="error"
          message="Enter a full email address, like [email protected]."
        />
        <Field label="Username" defaultValue="maya-okafor" state="success" message="This name is free." />
        <Field label="Plan" defaultValue="Team, yearly" disabled message="Ask an owner to change the plan." />
      </div>
      <footer>
        <button type="button">Cancel</button>
        <button type="button">Save changes</button>
      </footer>
    </section>
  );
}

Account details