Try 'magnetic button'
View code

Number 70: Search clear that dissolves word by word

Specification

Catalogue
No. 70 of 110
Interaction
Type, Feedback, Text Animation
Element
Input
Preview
Live component
Imports
react 19.3.1
Added

Notes

Pressing clear lifts the query away word by word into a small blur, then the placeholder comes back.

A small, clear answer to "did it clear?" in a search field, where the text otherwise just vanishes. It adds a detail to expanding search rather than repeating it.

About one second in all: each word rises 12px, blurs 2px and fades over 400ms cubic-bezier(.22, 1, .36, 1), the words starting up to 200ms apart, then the placeholder returns from 12px below over 400ms with the same ease. A mirror layer draws the leaving words while the field itself is already empty, so nothing is drawn twice and the field is ready to type into straight away; focus stays in it. Escape clears too. The clear button is a 44px target and only appears when there is something to clear. The browser's own clear cross is hidden. Instant under reduced motion. No coloured streak runs over the words.

Code

React and plain CSS
"use client";

import { useEffect, useId, useRef, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { reducedNow } from "@/components/pieces/motion-helpers";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./search-clear.css";

const PLACEHOLDER = "Search the help centre";
/** Each word leaves over 400ms; the words start up to 200ms apart in all, then the placeholder returns over 400ms. */
const WORD_MS = 400;
const SPREAD_MS = 200;

function SearchClearDemo() {
  const uid = useId();
  const [value, setValue] = useState("");
  const [leaving, setLeaving] = useState<string[] | null>(null);
  const [back, setBack] = useState(false);
  const input = useRef<HTMLInputElement>(null);
  const [scroll, setScroll] = useState(0);
  const timer = useRef<number | undefined>(undefined);

  useEffect(() => () => window.clearTimeout(timer.current), []);

  const clear = () => {
    if (!value || leaving) return;
    input.current?.focus({ preventScroll: true });
    if (reducedNow()) {
      setValue("");
      return;
    }
    // A mirror draws the words while the field's own text is hidden, so nothing is drawn twice.
    setScroll(input.current?.scrollLeft ?? 0);
    const words = value.split(/(\s+)/);
    setLeaving(words);
    setValue("");
    const count = words.filter((w) => w.trim()).length;
    const stagger = count > 1 ? SPREAD_MS / (count - 1) : 0;
    timer.current = window.setTimeout(() => {
      setLeaving(null);
      setBack(true);
    }, WORD_MS + stagger * (count - 1));
  };

  let n = 0;
  const count = leaving?.filter((w) => w.trim()).length ?? 0;
  const stagger = count > 1 ? SPREAD_MS / (count - 1) : 0;

  return (
    <div className="dm sx">
      <div className="sx-field" data-empty={!value || undefined}>
        <svg className="sx-glass" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
          <circle cx="11" cy="11" r="7" />
          <path d="m20 20-3.5-3.5" />
        </svg>
        <label htmlFor={`${uid}-q`} className="sr-only">
          Search the help centre
        </label>
        <input
          ref={input}
          id={`${uid}-q`}
          className="sx-input"
          type="search"
          enterKeyHint="search"
          autoComplete="off"
          value={value}
          onChange={(e) => {
            setBack(false);
            setValue(e.target.value);
          }}
          onKeyDown={(e) => {
            if (e.key === "Escape" && value) {
              e.preventDefault();
              clear();
            }
          }}
        />
        {!value && !leaving && (
          <span key={back ? "back" : "rest"} className="sx-ph" data-back={back || undefined} aria-hidden="true">
            {PLACEHOLDER}
          </span>
        )}
        {leaving && (
          <span className="sx-mirror" aria-hidden="true" style={{ translate: `${-scroll}px 0` }}>
            {leaving.map((w, i) =>
              w.trim() ? (
                <span key={i} className="sx-word" style={{ "--d": `${(n++ * stagger).toFixed(0)}ms` } as CSSProperties}>
                  {w}
                </span>
              ) : (
                <span key={i}>{w}</span>
              ),
            )}
          </span>
        )}
        <button type="button" className="sx-clear" aria-label="Clear search" onClick={clear} data-shown={!!value || undefined} tabIndex={value ? 0 : -1}>
          <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
            <path d="M7 7l10 10M17 7 7 17" />
          </svg>
        </button>
      </div>
      <p className="sx-hint">Type something, then clear it with the cross or Escape</p>
      <p className="sr-only" role="status">
        {back && !value ? "Search cleared" : ""}
      </p>
    </div>
  );
}

// What a hovered card shows: a question typed, then cleared word by word.
const TOUR: Tour = [
  { input: ".sx-input", value: "Opening hours over Christmas week", wait: 900 },
  { click: ".sx-clear", wait: 1500 },
];

export default function SearchClearEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Search clear that dissolves word by word" tour={TOUR}>
      <SearchClearDemo />
    </PieceStage>
  );
}

More like this

Sign-up form that waits before it complains

Type, FeedbackForms and inputs

Code field that behaves as one input

Type, FeedbackForms and inputs

Form in named steps

Type, FeedbackForms and inputs

Estimate slider

Drag, TypeForms and inputs

Sign-up that hands over the next issue

Type, FeedbackForms and inputs

Cookie choice that folds away

FeedbackOverlays and toasts