- Frames
- 0
- Active effects
- 0
- Renderer
- none
button.ts
import "@shader-ui/core/styles.css";
import { watchForm } from "@shader-ui/core";
const applyCode = (code: string) =>
new Promise<void>((resolve, reject) => setTimeout(code === "WELCOME10" ? resolve : reject, 1200));
document.body.insertAdjacentHTML(
"beforeend",
`<form id="promo">
<label for="code">Promo code (try WELCOME10)</label>
<input id="code" name="code" autocomplete="off" required>
<button type="submit">Apply</button>
<p id="message" role="status"></p>
</form>`,
);
const form = document.querySelector<HTMLFormElement>("#promo")!;
const message = document.querySelector("#message")!;
watchForm(form, {
onSubmit: (event) => {
event.preventDefault();
message.textContent = "Checking the code…";
return applyCode(String(new FormData(form).get("code")).trim().toUpperCase()).then(
() => (message.textContent = "Code applied: 10% off."),
(error) => {
message.textContent = "This code has expired.";
throw error;
},
);
},
});PromoCode.tsx
import { Button, Form, Input } from "@shader-ui/react";
import { useState } from "react";
const applyCode = (code: string) =>
new Promise<void>((resolve, reject) => setTimeout(code === "WELCOME10" ? resolve : reject, 1200));
export function PromoCode() {
const [message, setMessage] = useState("");
return (
<Form
onSubmit={(e) => {
e.preventDefault();
setMessage("Checking the code…");
return applyCode(String(new FormData(e.currentTarget).get("code")).trim().toUpperCase()).then(
() => setMessage("Code applied: 10% off."),
(error) => {
setMessage("This code has expired.");
throw error;
},
);
}}
>
<label htmlFor="code">Promo code (try WELCOME10)</label>
<Input id="code" name="code" autoComplete="off" required />
<Button type="submit">Apply</Button>
<p role="status">{message}</p>
</Form>
);
}When to use it
- To submit a form, or for an action that waits for an answer (save, sync), with aria-busy.
- Not to go to another page: use a link.
Recommendations
- Put the submit button first in the form: it is the one that gets the "ready" pulse.
- No disabled to enforce validity: let native validation stop the submission.
- No effect on every click (onClick: "ripple" everywhere): one effect, one reason.
Accessibility
The library
- Native <button> element, never disabled by the library: it keeps focus, and an incomplete form is stopped by native validation.
- While sending, aria-busy is set, so it is announced; the orbit lasts 5 s at most.
- Hover, press and focus in CSS, with 3:1 contrast.
- Reduced motion: a simple fade. High contrast: system colors, no shader.
Integration
- A failed submission explained by a text, announced with aria-live.
- No disabled on a submit button: it loses focus, and the user does not know what is missing.
WCAG 2.2 criteria: Non-text Contrast · Animation from Interactions · Focus Visible · Error Identification · Status Messages
Props
| Prop | Type | Purpose |
|---|---|---|
aria-busy | boolean | Set by the Form while sending, or by you: orbit and diode. Never disabled. |
effects | { onClick: "ripple" } | Effects triggered by DOM events. |
onEffectEnd | (e) => void | Always called, even when the effect is skipped. |
Spec
The normative spec for this component: Formulaire
Availability and resources
- Status
- available · open source
- Packages
- @shader-ui/react (React, Next.js)
@shader-ui/core (TypeScript, plain HTML) - Source code
- button.ts (core) · Button.tsx (React)