Skip to content

FSHSP-266 feat(field-sign): add ui-field-sign, the handwritten signature field - #196

Merged
LBU4SH merged 6 commits into
mainfrom
feat/FSHSP-266-field-sign
Nov 25, 2026
Merged

LBU4SH merged 6 commits into
mainfrom
feat/FSHSP-266-field-sign

Conversation

@LBU4SH

@LBU4SH LBU4SH commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Jira: FSHSP-266. Follow-ups: FSHSP-269 (Figma component, node-id for the story), FSHSP-270 (React port).

New ui-field-sign (@4sh/ui-kit/forms/ui-field-sign): a handwritten signature field whose box is the drawing area — a <canvas> projected into the ui-field shell, driven by Pointer Events (finger, stylus, mouse). Label, helper, levels, disabled/readonly come from the shell; CVA via BaseFormField.

  • Value — a data URL (format: png | svg), null while empty so required works as is. Emitted when a stroke ends, never mid-stroke. PNG exported at 2× the drawing area, the same on every device.
  • Two inks — on screen the ink follows the tokens (dark theme included); the export is painted in exportColor (black) on a transparent background, otherwise a signature drawn in dark mode came out white. exportColor accepts a token (var(--…)), resolved to rgb(); an unresolved one or a non-colour falls back to black.
  • Screen = export — the box at the first stroke is the signature's region, the exported frame is that region grown to every stroke; a resized box (rotated phone) refits it, a stroke drawn in the room left around it grows the frame, points are clamped to the box.
  • Value written by the form — must be an image data URL (a remote URL would taint the export); shown as a silhouette in the current ink, kept under new strokes until clear(). An SVG the field exported itself is re-exported flat (its strokes copied, read back attribute by attribute with its geometry checked — anything else, or ink reaching outside its own frame, stays an <image>), so the value grows by the new strokes only. An image that fails to load is shown and announced as unreadable (unreadableLabel, error level, clearable), emits imageError and stays the value until cleared or signed over (the control stays valid: the doc shows how to block it); in PNG the export waits for it rather than drop it.
  • Mobile — touch-action: none + pointer capture; a second finger is ignored; a resize mid-stroke is applied when it ends; disabled under the pen, the stroke is dropped.
  • Editing — clear button (disabled while empty, hidden in readonly), public clear() / undo() (undo() also restores a clear), Backspace / Ctrl+Z undo, Delete clears.
  • A11y — the area is a tab stop, role="img" named by the label and described by its state (placeholder / signedLabel, then requiredLabel) then the message.
  • Geometry in a pure module (ui-field-sign-strokes.ts): smoothing shared by the canvas and the SVG export, so screen and export cannot drift; unit-tested without a canvas. Internal, like the SVG read-back (ui-field-sign-svg.ts): the entry point exports the component and its two types only.
  • floatLabel is listed in UNDOCUMENTED_INPUTS on purpose: the box is the drawing, there is no value for a resting label to stand in for.
  • Docs in English (FSHSP-267, merged meanwhile). Template-driven / Reactive / Signal Forms stories. Story + MDX, --ui-* hooks (generated component-vars + figma/README.md counts), registries (READMEs EN/FR, Overview.mdx, components-index.md), CHANGELOG [Unreleased].

Tests: ui-field-sign.spec.ts (48 cases, reactive forms included) + ui-field-sign-strokes.spec.ts (26) + ui-field-sign-svg.spec.ts (42). pnpm test (465), eslint, ng build ui-kit, docs:config:check green.

Four reviews (senior-code-reviewer), addressed in e89cd01, b9b6b8b, 1c224f8, 934bf94 and 32b0e3b — see their commit messages. The tests added since the second one each fail against a mutation of the code they cover.

Checked in Storybook (static build + Playwright, system Chrome):

  • axe: 0 violations on the 12 stories;
  • box widened after signing, stroke in the left margin: in the value (export 488 → 860 px), canvas keeps the box height; exportColor token exported as its rgb(); touch-action back to auto in read-only / disabled; Forms stories invalid → valid once signed;
  • mouse: no export mid-stroke, PNG on release, Ctrl+Z ×2 back to empty;
  • touch (CDP touch events, scrollable page): pointerup with no pointercancel, value emitted, scrollY unchanged;
  • dark theme: light ink on screen, export with 0 light pixels; disabled ink = form-high-content-disabled;
  • readonly / disabled refuse the pen;
  • the story's sample signature signed over in SVG: 4 paths, no <image>, old strokes still rendered; a tap with moves on the spot exports M80 50L80.2 50;
  • a stroke dragged past the edge: PNG still 2× the box (608 × 352), the frame no longer grows;
  • the sample made unreadable: "Signature illisible" shown and announced, _error shell, clear button enabled, axe 0 violations.

Not covered: a real phone (only emulated touch), WebKit (Chromium only — the tainted-canvas case falls back to SVG), and no Figma design link yet (FSHSP-269).

🤖 Generated with Claude Code

LBU4SH added a commit that referenced this pull request Nov 24, 2026
… frame, undoable clear

From the review of #196:

- A previous signature can no longer vanish from the export. The field only
  accepts an image data URL (a remote URL tainted the canvas: toDataURL threw
  on pointerup and the form kept the old value while the screen showed the
  new stroke). An image that fails to load leaves the field empty instead of
  announced as signed over a blank box; in PNG a stroke drawn before it loads
  holds the export until it has, and a canvas that still refuses to be read
  back falls back to SVG.
- Fixed frame: the box size at the first stroke becomes the signature frame,
  points live in it and the screen fits it into the box. A box shrinking
  afterwards (a rotated phone) scaled nothing and cropped half the signature
  out of the value; measured in Storybook, the export stays 608x352.
- The recoloured previous signature is cached instead of allocating a
  full-size canvas per frame while drawing over it.
- The echo guard is kept, with its real reason: `control.setValue(value)` does
  call writeValue. The test now goes through a FormControl and fails without
  the guard (the ngModel one passed either way).
- Backspace undoes the last stroke and Delete clears; `undo()` brings back
  what `clear()` removed — one key no longer destroys the whole signature.
- A lost pointer capture ends the stroke; buttons other than the main one
  (stylus eraser) do not draw; an invalid `exportColor` falls back to black
  (it made the SVG export invisible); the ink width is a registered <length>,
  read back in px whatever unit the hook uses; the state announces
  `requiredLabel`, `role="img"` taking no aria-required.
- Docs in English, per FSHSP-267 merged meanwhile: MDX, argTypes, demo values
  and `///` comments (figma/component-vars.json regenerated). Template-driven,
  Reactive and Signal Forms stories, plus reactive-forms tests.
LBU4SH added a commit that referenced this pull request Nov 24, 2026
…rtColor tokens

From the second review of #196:

- The fixed frame left dead bands: once the box changed proportions (a phone
  rotated), the frame was centred with room around it, and ink drawn there was
  shown but outside the value — a truncated signature that `required` accepts.
  The box at the first stroke is now the signature's region, and the exported
  frame is that region grown to every stroke; the view only refits on resize,
  never under the pen, and points are clamped to the box. Measured in
  Storybook: widening the box then signing in the left margin grows the export
  from 488 to 860 px, ink to the edge.
- Measuring that surfaced a layout bug present since the first commit: in an
  `auto` grid track the canvas' `height: 100%` did not resolve, so widening the
  box made the canvas taller than the box (644x465 in a 176px field), its lower
  part clipped. Definite tracks now.
- `exportColor` is resolved to rgb() on a probe under the host: a `var(--token)`
  passed CSS.supports, then painted nothing in the SVG export (black in PNG).
  An unresolved var() or a non-colour falls back to black, with a warning. The
  test no longer relies on a CSS.supports fake no browser behaves like.
- Undo after a clear made while the previous signature was loading reloads it
  (the PNG export stalled); clear() and undo() do nothing under the pen (a blank
  image passed `required`); `touch-action: none` only while the area can be
  signed, so a read-only signature lets the page scroll; the silhouette cache
  keys on the image element, not on a string fingerprint.
- Docs: PNG may fall back to SVG (check the MIME type), the value is untrusted
  on the server, pick a theme-independent exportColor.

New tests (ratio change, clamping, token / unresolved / invalid colour, reload
on undo, clear under the pen, undo after Delete, PNG→SVG fallback) each fail
against a mutation of the code they cover.
…ure field

The field box IS the drawing area: a <canvas> projected into the `ui-field`
shell, driven by Pointer Events — finger, stylus or mouse. Label, helper,
levels and disabled/readonly come from the shell like every other field.

- The value is a data URL (`format`: png or svg), `null` while empty, so
  `required` works as is. It leaves once a stroke ends, never mid-stroke: an
  autosave would otherwise get an image per pointer move.
- Two inks. On screen the ink follows the tokens, dark theme included; the
  export is painted in `exportColor` on a transparent background. Measured in
  Storybook: a stroke drawn in dark mode exports with zero light pixels —
  without this split it came out white, invisible on the PDF it is for.
- A value written by the form is shown, redrawn as a silhouette in the
  current ink (a black export stays legible on a dark field), and kept under
  new strokes until cleared.
- `touch-action: none` + pointer capture: measured with CDP touch events, the
  page does not scroll under the finger; a second finger is ignored.
- Clear button, public `clear()`/`undo()`, Delete and Ctrl+Z on the area.

The smoothing lives in a pure module shared by the canvas and the SVG export,
so screen and export cannot drift, and it is unit-tested without a canvas
(jsdom has none). `floatLabel` is listed as undocumented on purpose: there is
no value for a resting label to stand in for.
… frame, undoable clear

From the review of #196:

- A previous signature can no longer vanish from the export. The field only
  accepts an image data URL (a remote URL tainted the canvas: toDataURL threw
  on pointerup and the form kept the old value while the screen showed the
  new stroke). An image that fails to load leaves the field empty instead of
  announced as signed over a blank box; in PNG a stroke drawn before it loads
  holds the export until it has, and a canvas that still refuses to be read
  back falls back to SVG.
- Fixed frame: the box size at the first stroke becomes the signature frame,
  points live in it and the screen fits it into the box. A box shrinking
  afterwards (a rotated phone) scaled nothing and cropped half the signature
  out of the value; measured in Storybook, the export stays 608x352.
- The recoloured previous signature is cached instead of allocating a
  full-size canvas per frame while drawing over it.
- The echo guard is kept, with its real reason: `control.setValue(value)` does
  call writeValue. The test now goes through a FormControl and fails without
  the guard (the ngModel one passed either way).
- Backspace undoes the last stroke and Delete clears; `undo()` brings back
  what `clear()` removed — one key no longer destroys the whole signature.
- A lost pointer capture ends the stroke; buttons other than the main one
  (stylus eraser) do not draw; an invalid `exportColor` falls back to black
  (it made the SVG export invisible); the ink width is a registered <length>,
  read back in px whatever unit the hook uses; the state announces
  `requiredLabel`, `role="img"` taking no aria-required.
- Docs in English, per FSHSP-267 merged meanwhile: MDX, argTypes, demo values
  and `///` comments (figma/component-vars.json regenerated). Template-driven,
  Reactive and Signal Forms stories, plus reactive-forms tests.
…rtColor tokens

From the second review of #196:

- The fixed frame left dead bands: once the box changed proportions (a phone
  rotated), the frame was centred with room around it, and ink drawn there was
  shown but outside the value — a truncated signature that `required` accepts.
  The box at the first stroke is now the signature's region, and the exported
  frame is that region grown to every stroke; the view only refits on resize,
  never under the pen, and points are clamped to the box. Measured in
  Storybook: widening the box then signing in the left margin grows the export
  from 488 to 860 px, ink to the edge.
- Measuring that surfaced a layout bug present since the first commit: in an
  `auto` grid track the canvas' `height: 100%` did not resolve, so widening the
  box made the canvas taller than the box (644x465 in a 176px field), its lower
  part clipped. Definite tracks now.
- `exportColor` is resolved to rgb() on a probe under the host: a `var(--token)`
  passed CSS.supports, then painted nothing in the SVG export (black in PNG).
  An unresolved var() or a non-colour falls back to black, with a warning. The
  test no longer relies on a CSS.supports fake no browser behaves like.
- Undo after a clear made while the previous signature was loading reloads it
  (the PNG export stalled); clear() and undo() do nothing under the pen (a blank
  image passed `required`); `touch-action: none` only while the area can be
  signed, so a read-only signature lets the page scroll; the silhouette cache
  keys on the image element, not on a string fingerprint.
- Docs: PNG may fall back to SVG (check the MIME type), the value is untrusted
  on the server, pick a theme-independent exportColor.

New tests (ratio change, clamping, token / unresolved / invalid colour, reload
on undo, clear under the pen, undo after Delete, PNG→SVG fallback) each fail
against a mutation of the code they cover.
…images, keep taps visible

- An SVG value the field exported is read back and its strokes copied next to the new
  ones, instead of being nested in an <image> re-encoded on every edit. Only the field's
  own shape is recognised, rebuilt from the values read; anything else stays an image.
- A previous signature that cannot be loaded emits `imageError`; the form's value is
  left alone (it may be readable elsewhere).
- A single tap exports a 0.2px segment, which the SVG rounding cannot collapse to zero.
…values

Third review:
- SVG read-back: plain finite numbers only, explicit path grammar, every layer inside its
  viewBox and no stroke wider than it, no DOCTYPE, no colourless ink. A value whose ink
  the screen would clip stays an <image> instead of reaching the export flat.
- An unreadable value is shown and announced as such (`unreadableLabel`), in error, and
  clearable; it stays the value until cleared or signed over (undo brings it back).
- A tap with moves on the spot exports a dot; a resize mid-stroke is applied when it ends;
  a stroke is dropped when the field is disabled under the pen; points stay half the ink
  width inside the box, so the frame no longer grows edit after edit.
- Public API narrowed to the component; the read-back is memoised per value and cannot
  throw on commit; an empty PNG (`data:,`) falls back to SVG; no empty ink group, and
  line breaks escaped in attributes.
… disable races

Fourth review:
- The read-back bound was inverted: the viewBox was grown by half the ink width instead of
  holding it. Every point, with the ink around it, must now fit the viewBox; a tap's
  segment counts in the stroke bounds so the field's own exports always pass.
- A tap is detected on rounded coordinates, so jitter below the export's precision can no
  longer round to a zero-length segment.
- SVG: a value emitted with a previous signature still loading is emitted again without it
  if it turns out unreadable; with no stroke over it, a previous signature is re-emitted
  word for word (undo of a clear) instead of re-exported. The first stroke's region is no
  longer dropped by a load error under the pen.
- Disabled under the pen: the pointer handlers drop the stroke themselves, without waiting
  for the effect; an abandoned first stroke releases its region.
- Scales keep six significant digits; the pointer rectangle follows a resize mid-stroke;
  the docs say an unreadable value leaves the control valid, and how to block it.
@LBU4SH
LBU4SH force-pushed the feat/FSHSP-266-field-sign branch from 32b0e3b to ac9bb74 Compare November 25, 2026 11:15
@LBU4SH
LBU4SH merged commit 36cbf3a into main Nov 25, 2026
4 checks passed
@LBU4SH
LBU4SH deleted the feat/FSHSP-266-field-sign branch November 25, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant