Number 90: Was this helpful
Specification
- Catalogue
- No. 90 of 110
- Section
- Forms and inputs
- Interaction
- Feedback
- Element
- Form, Input
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
A quick rating with three faces, then a short follow-up asking why, with ready reasons.
One tap gives the answer; the follow-up only appears after it, with reasons to tick instead of a blank box.
Follow-up opens with a gliding height (the same mechanism as Height that glides) in 300ms. Faces drawn as one line icon set; the answer is a radio group. Sending is simulated here; a real build posts it somewhere the owner reads.
Code
React and plain CSS"use client";
import { useEffect, useId, useRef, useState } from "react";
import { AutoHeight } from "@/components/pieces/motion-helpers";
import { radioArrows } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/styles/form-fields.css";
import "./helpful-rating.css";
const FACES = [
{ id: "no", label: "No", mouth: "M8 16.5c1.2-1.4 2.5-2 4-2s2.8.6 4 2" },
{ id: "partly", label: "Partly", mouth: "M8.5 15.5h7" },
{ id: "yes", label: "Yes", mouth: "M8 14.5c1.2 1.4 2.5 2 4 2s2.8-.6 4-2" },
] as const;
const REASONS: Record<string, string[]> = {
no: ["Did not answer my question", "Hard to follow", "Out of date"],
partly: ["Needed an example", "Too long", "Missing a step"],
yes: ["Clear steps", "Answered my question", "Good examples"],
};
function HelpfulRatingDemo() {
const [face, setFace] = useState<string | null>(null);
const [why, setWhy] = useState<string[]>([]);
const [note, setNote] = useState("");
const [sent, setSent] = useState(false);
const done = useRef<HTMLParagraphElement>(null);
const uid = useId();
// The form is gone once sent: focus moves to the thanks, not to the page.
useEffect(() => {
if (sent) done.current?.focus();
}, [sent]);
return (
<div className="dm">
<div className="hr">
<AutoHeight>
{sent ? (
<p ref={done} tabIndex={-1} className="hr-done" role="status">
Thanks. Your answer was sent to the team. (Sample: nothing was sent.)
</p>
) : (
<div className="hr-body">
<div className="hr-q">
<span id={`${uid}-q`}>Was this guide helpful?</span>
<div className="hr-faces" role="radiogroup" aria-labelledby={`${uid}-q`} onKeyDown={radioArrows}>
{FACES.map((f) => (
<button
key={f.id}
type="button"
role="radio"
aria-checked={face === f.id}
className="hr-face"
onClick={() => {
setFace(f.id);
setWhy([]);
}}
>
<svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" aria-hidden="true">
<circle cx="12" cy="12" r="9.5" />
<path d="M9 10h.01M15 10h.01" strokeWidth="2.4" />
<path d={f.mouth} />
</svg>
<span>{f.label}</span>
</button>
))}
</div>
</div>
{face && (
<form
className="hr-more"
onSubmit={(e) => {
e.preventDefault();
setSent(true);
}}
>
<fieldset>
<legend className="dm-small">What made it so? Pick any</legend>
<div className="hr-chips">
{REASONS[face]?.map((r) => (
<label key={r} className="hr-chip">
<input type="checkbox" checked={why.includes(r)} onChange={() => setWhy((w) => (w.includes(r) ? w.filter((x) => x !== r) : [...w, r]))} />
{r}
</label>
))}
</div>
</fieldset>
<label className="st-field">
<span>Anything else? (optional)</span>
<input value={note} onChange={(e) => setNote(e.target.value)} />
</label>
<div className="qz-nav">
<button type="submit" className="dm-btn" data-variant="solid">
Send
</button>
</div>
</form>
)}
</div>
)}
</AutoHeight>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [{ click: ".hr-face", nth: 1, wait: 2400 }];
export default function HelpfulRatingEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Was this helpful" tour={TOUR}>
<HelpfulRatingDemo />
</PieceStage>
);
}