Signature pad
A signature drawn with a mouse, finger or pen, or typed as a name instead.
Example
<form class="stack" id="sign-demo-form"> <focsd-signature-pad label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></focsd-signature-pad> <div class="row"> <focsd-button type="submit" variant="action">Accept the agreement</focsd-button> <focsd-button type="reset">Start again</focsd-button> </div> <output id="sign-demo-out" for="sign-demo-form" aria-live="polite"></output></form>
<script type="module">const form = document.getElementById('sign-demo-form');
form.addEventListener('submit', (event) => { event.preventDefault(); const file = new FormData(form).get('signature'); document.getElementById('sign-demo-out').textContent = file ? 'Signed: ' + file.name + ' (' + file.size + ' bytes)' : 'Please sign first.';});</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-signature-pad.js';</script>
<focsd-signature-pad label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></focsd-signature-pad>import { SignaturePad } from '@focsd-dev/react';
export function Example() { return <SignaturePad onChange={(e) => console.log(e.target)} label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></SignaturePad>;}<script setup>import '@focsd-dev/components/focsd-signature-pad.js';</script>
<template> <focsd-signature-pad @change="onChange" label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></focsd-signature-pad></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-signature-pad.js';
// template<focsd-signature-pad (change)="onChange($event)" label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></focsd-signature-pad><script> import '@focsd-dev/components/focsd-signature-pad.js';</script>
<focsd-signature-pad onchange={(e) => console.log(e.target)} label="Signature" name="signature" hint="Draw with a mouse, finger or pen, or choose Type instead." required></focsd-signature-pad>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
pen-color | penColor | string | — |
pen-width | penWidth | number | 2 |
typed | typed | boolean | false |
typed-name | typedName | string | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
clear() | Remove the signature (the drawing and the typed name), from code: no events fire, as for native value writes. |
undo() | Remove the last stroke, from code (no events). |
toDataURL() | The signature as a PNG data URL, or an empty string when there is none. |
toSVG() | The signature as SVG (stroke paths, or the typed name as text), or an empty string when there is none. |
Events
| Event | Description |
|---|---|
input | Fired at the end of each stroke and on each typed character. |
change | Fired when the pointer or focus leaves after drawing, when the typed name is committed, and after Clear, Undo or a mode switch. |
CSS parts
Style the inside of the component with ::part(), for example focsd-signature-pad::part(group).
| Part | Description |
|---|---|
group | The group around the pad and its buttons, named by the label. |
label | The label element. |
layout | The wrapper that places the label beside or above the body. |
body | The pad, its buttons, the hint and message. |
typed-field | The text field for the typed name (typed mode only). |
surface | The box around the canvas and its baseline. |
canvas | The drawing (`role=img`). |
baseline | The line to sign on. |
actions | The row of buttons. |
mode | The "Type instead" / "Draw instead" button. |
undo | The Undo button (drawing mode only). |
clear | The Clear button. |
status | The visually hidden region that announces "Signature added" or "Signature cleared". |
hint | The author's hint. |
message | The shown error, warning or success message (also `error`, `warning` or `success`). |
message-icon | The message's icon. |
announcer | The visually hidden region for messages. |
Accessibility
- Drawing is not the only way in. "Type instead" turns the pad into a text field labeled "Type your name for Signature", and the name is drawn as a typed signature. People who cannot draw, or use a keyboard or a screen reader, sign this way.
- The group is named by the label and described by the hint and any message (read once when focus enters). The canvas is an image named "Signature, signed" or "Signature, empty". A polite status says "Signature added" or "Signature cleared" after each change. When
requiredand empty, the message is "Add your signature." - It uses pointer events, so mouse, touch and pen all work, and it keeps
touch-action: noneon the canvas only, so the page still scrolls around it. requiredwith an empty pad gives the "Required" message, anchored on the mode button (or on the typed field in typed mode).- The form receives a PNG file named
signature.png, plussignature-typedset totruewhen the name was typed. It is empty, not a blank image, until someone signs. - The ink is black unless you set
pen-color, so a signature stays readable on paper in a dark theme. In forced colors the canvas draws in the system text color. - Agents never fill this field: WebMCP leaves the signature pad out of its tools, so nothing signs on a person's behalf.
Keyboard
| Key | Action |
|---|---|
| Tab | Move through Type instead (or Draw instead), Undo and Clear |
| Enter / Space | Press the focused button |
| Type your name | In typed mode, an ordinary text field |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.