All components outlinesubtlesmmdlg
Signature Pad
Freehand drawing surface for signatures with smooth pressure-aware strokes, a guide line, a clear button, and a hidden input for forms.
12345678910111213141516171819202122import { SignaturePad } from "#components/ui/ark-ui/signature-pad.tsx";
export function Example() { return ( // variant/size resolve once on Root. Ink is filled with currentColor // (semantic fg), so it follows dark mode. ClearTrigger ships a default // icon and hides while the pad is empty; HiddenInput carries an SVG data // URL of the strokes into form submissions. <SignaturePad.Root name="signature" variant="outline" size="md"> <SignaturePad.Label>Sign here</SignaturePad.Label> <SignaturePad.Control> <SignaturePad.Segment /> <SignaturePad.Guide /> <SignaturePad.ClearTrigger /> </SignaturePad.Control> <SignaturePad.HiddenInput /> </SignaturePad.Root> );}
// export: onDrawEnd={(d) => d.getDataUrl("image/png").then(upload)}// pen: drawing={{ size: 4, thinning: 0.6, simulatePressure: false }}Variants
Sizes
In a form
HiddenInput submits the signature as an SVG data URL under the Root's `name`; the line below shows its current value.
signature: (empty)
12345678910<SignaturePad.Root name="signature"> <SignaturePad.Label>Sign to accept</SignaturePad.Label> <SignaturePad.Control> <SignaturePad.Segment /> <SignaturePad.Guide /> <SignaturePad.ClearTrigger /> </SignaturePad.Control> {/* submits an SVG data URL under "signature" */} <SignaturePad.HiddenInput /></SignaturePad.Root>Install
1npx stylexblocks add signature-padSource
components/ui/ark-ui/signature-pad.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317import * as stylex from "@stylexjs/stylex";import { useEffect, useState } from "react";import { SignaturePad as ArkSignaturePad, useSignaturePadContext,} from "@ark-ui/react/signature-pad";import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const signaturePadRoot = stylex.create({ base: { display: 'flex', flexDirection: 'column', gap: spacing._1_5, width: '100%', },})
const signaturePadLabel = stylex.create({ base: { fontWeight: fontWeights.medium, fontSize: fontSizes.sm, lineHeight: lineHeights.sm, color: semanticColors.fg, opacity: { default: null, ':is([data-disabled])': 0.5, }, },})
// The drawing surface. Strokes are filled with `currentColor`, so the// semantic fg color keeps the ink legible in light and dark mode.const signaturePadControl = stylex.create({ base: { position: 'relative', width: '100%', touchAction: 'none', userSelect: 'none', color: semanticColors.fg, borderRadius: semanticRadii.panel, borderWidth: '1px', cursor: { default: 'crosshair', ':is([data-disabled])': 'not-allowed', }, opacity: { default: null, ':is([data-disabled])': 0.5, }, outlineWidth: { default: null, ':focus-visible': 'var(--focus-ring-width, 0)', }, outlineOffset: { default: null, ':focus-visible': '0px', }, outlineStyle: { default: null, ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, },})
const signaturePadControlSizes = stylex.create({ sm: { height: sizes._32, }, md: { height: sizes._40, }, lg: { height: sizes._48, },})
// `outline` matches the file-upload dropzone (dashed border on the page// background); `subtle` matches the subtle input (muted fill, no border).const signaturePadControlVariants = stylex.create({ outline: { backgroundColor: semanticColors.bg, borderStyle: 'dashed', borderColor: { default: semanticColors.border, ':hover': semanticColors.borderEmphasized, ':is([data-disabled])': semanticColors.border, ':focus-visible': colorPalette.focusRing, }, }, subtle: { backgroundColor: semanticColors.bgMuted, borderStyle: 'solid', borderColor: { default: 'transparent', ':focus-visible': colorPalette.focusRing, }, },})
const signaturePadSegment = stylex.create({ base: { position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible', pointerEvents: 'none', fill: 'currentColor', },})
// The baseline to sign on.const signaturePadGuide = stylex.create({ base: { position: 'absolute', insetInline: spacing._4, bottom: '28%', borderBottomWidth: '1px', borderBottomStyle: 'dashed', borderBottomColor: semanticColors.borderEmphasized, pointerEvents: 'none', },})
const signaturePadClearTrigger = stylex.create({ base: { position: 'absolute', top: spacing._2, insetInlineEnd: spacing._2, zIndex: 1, display: { default: 'inline-flex', ':is([hidden])': 'none', }, alignItems: 'center', justifyContent: 'center', width: sizes.controlXs, height: sizes.controlXs, borderRadius: semanticRadii.control, color: { default: semanticColors.fgMuted, ':hover': semanticColors.fg, }, backgroundColor: { default: 'transparent', ':hover': semanticColors.bgSubtle, }, cursor: { default: 'pointer', ':disabled': 'not-allowed', }, outlineWidth: { default: null, ':focus-visible': '2px', }, outlineOffset: { default: null, ':focus-visible': '0px', }, outlineStyle: { default: null, ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, },})
export type SignaturePadVariant = keyof typeof signaturePadControlVariants
export type SignaturePadSize = keyof typeof signaturePadControlSizes
const signaturePadRecipe = defineSlotRecipe({ slots: { root: { styles: signaturePadRoot }, label: { styles: signaturePadLabel }, control: { styles: signaturePadControl, sizes: signaturePadControlSizes, variants: signaturePadControlVariants, }, segment: { styles: signaturePadSegment }, guide: { styles: signaturePadGuide }, clearTrigger: { styles: signaturePadClearTrigger }, }, defaultVariants: { variant: "outline", size: "md" },});
const chrome = stylex.create({ btnReset: { appearance: "none", borderWidth: 0, borderStyle: "none", padding: 0, margin: 0, fontFamily: "inherit", fontSize: "inherit", },});
function ClearIcon(props: React.ComponentProps<"svg">) { return ( <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}> <path d="M2.5 8a5.5 5.5 0 1 0 1.6-3.9M2.5 2.5v2.6h2.6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" /> </svg> );}
const { withProvider, withContext } = createSlotRecipeContext(signaturePadRecipe);
const StyledRoot = withProvider(ArkSignaturePad.Root, "root", "SignaturePadRoot");
/** * Root — Ark's signature-pad machine. Strokes are filled with * `currentColor` (the control's semantic fg) unless `drawing.fill` is set, * so ink follows light/dark mode. */function Root(props: React.ComponentProps<typeof StyledRoot>) { const { drawing, ...rest } = props; return <StyledRoot drawing={{ size: 3, simulatePressure: true, ...drawing }} {...rest} />;}Root.displayName = "SignaturePadRoot";
const StyledClearTrigger = withContext( ArkSignaturePad.ClearTrigger, (v) => [chrome.btnReset, ...resolveSlotStyles(signaturePadRecipe.slots.clearTrigger, v)], "SignaturePadClearTrigger",);
/** Clears the pad; hidden while empty. Ships a default reset icon. */function ClearTrigger(props: React.ComponentProps<typeof StyledClearTrigger>) { const { children, ...rest } = props; return ( <StyledClearTrigger aria-label="Clear signature" {...rest}> {children ?? <ClearIcon />} </StyledClearTrigger> );}ClearTrigger.displayName = "SignaturePadClearTrigger";
/** * HiddenInput — carries the signature into form submissions. Without a * `value` it fills in an SVG data URL of the current strokes (empty when * the pad is blank). */function HiddenInput( props: Omit<React.ComponentProps<typeof ArkSignaturePad.HiddenInput>, "value"> & { value?: string; },) { const { value, ...rest } = props; const api = useSignaturePadContext(); const [dataUrl, setDataUrl] = useState(""); const pathsKey = api.paths.join("\n"); useEffect(() => { if (value !== undefined) return; if (!pathsKey) { setDataUrl(""); return; } let live = true; api.getDataUrl("image/svg+xml").then((url) => { if (live) setDataUrl(url); }); return () => { live = false; }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [pathsKey, value]); return <ArkSignaturePad.HiddenInput value={value ?? dataUrl} {...rest} />;}HiddenInput.displayName = "SignaturePadHiddenInput";
/** * SignaturePad — Ark UI's signature-pad machine with the StyleX slot * recipe: a drawing surface in the file-upload dropzone look (`outline`) or * a muted fill (`subtle`), sized `sm`/`md`/`lg`, with a dashed guide line * and a clear button. Draw with mouse, pen or touch. */export const SignaturePad = { Root, Label: withContext(ArkSignaturePad.Label, "label", "SignaturePadLabel"), Control: withContext(ArkSignaturePad.Control, "control", "SignaturePadControl"), Segment: withContext(ArkSignaturePad.Segment, "segment", "SignaturePadSegment"), Guide: withContext(ArkSignaturePad.Guide, "guide", "SignaturePadGuide"), ClearTrigger, HiddenInput, Context: ArkSignaturePad.Context,};
export type SignaturePadRootProps = React.ComponentProps<typeof SignaturePad.Root>;export type { SignaturePadDrawDetails, SignaturePadDrawEndDetails,} from "@ark-ui/react/signature-pad";