forked from chakra-ui/zag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add signature pad machine (chakra-ui#1399)
* chore: initial design * chore: wip * chore: add more examples * chore: add svelte example * chore: update * chore: rename parts * docs: add changeset
- Loading branch information
1 parent
009eda5
commit 9816eec
Showing
36 changed files
with
1,495 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zag-js/signature-pad": minor | ||
--- | ||
|
||
[NEW] Signature Pad machine to allow capturing user signatures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"use strict"; | ||
|
||
var _xstate = require("xstate"); | ||
const { | ||
actions, | ||
createMachine, | ||
assign | ||
} = _xstate; | ||
const { | ||
choose | ||
} = actions; | ||
const fetchMachine = createMachine({ | ||
id: "signature-pad", | ||
initial: "idle", | ||
context: {}, | ||
on: { | ||
CLEAR: { | ||
actions: ["clearPoints", "invokeOnDrawEnd", "focusCanvasEl"] | ||
} | ||
}, | ||
on: { | ||
UPDATE_CONTEXT: { | ||
actions: "updateContext" | ||
} | ||
}, | ||
states: { | ||
idle: { | ||
on: { | ||
POINTER_DOWN: { | ||
target: "drawing", | ||
actions: ["addPoint"] | ||
} | ||
} | ||
}, | ||
drawing: { | ||
activities: ["trackPointerMove"], | ||
on: { | ||
POINTER_MOVE: { | ||
actions: ["addPoint", "invokeOnDraw"] | ||
}, | ||
POINTER_UP: { | ||
target: "idle", | ||
actions: ["endStroke", "invokeOnDrawEnd"] | ||
} | ||
} | ||
} | ||
} | ||
}, { | ||
actions: { | ||
updateContext: assign((context, event) => { | ||
return { | ||
[event.contextKey]: true | ||
}; | ||
}) | ||
}, | ||
guards: {} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { normalizeProps, useMachine } from "@zag-js/react" | ||
import { signaturePadControls } from "@zag-js/shared" | ||
import * as signaturePad from "@zag-js/signature-pad" | ||
import { RotateCcw } from "lucide-react" | ||
import { useId, useState } from "react" | ||
import { StateVisualizer } from "../components/state-visualizer" | ||
import { Toolbar } from "../components/toolbar" | ||
import { useControls } from "../hooks/use-controls" | ||
|
||
export default function Page() { | ||
const [url, setUrl] = useState("") | ||
|
||
const controls = useControls(signaturePadControls) | ||
|
||
const [state, send] = useMachine( | ||
signaturePad.machine({ | ||
id: useId(), | ||
onDrawEnd(details) { | ||
details.getDataUrl("image/png").then(setUrl) | ||
}, | ||
drawing: { | ||
fill: "red", | ||
size: 4, | ||
simulatePressure: true, | ||
}, | ||
}), | ||
{ context: controls.context }, | ||
) | ||
|
||
const api = signaturePad.connect(state, send, normalizeProps) | ||
|
||
return ( | ||
<> | ||
<main className="signature-pad"> | ||
<div {...api.rootProps}> | ||
<label {...api.labelProps}>Signature Pad</label> | ||
|
||
<div {...api.controlProps}> | ||
<svg {...api.segmentProps}> | ||
{api.paths.map((path, i) => ( | ||
<path key={i} {...api.getSegmentPathProps({ path })} /> | ||
))} | ||
{api.currentPath && <path {...api.getSegmentPathProps({ path: api.currentPath })} />} | ||
</svg> | ||
|
||
<div {...api.separatorProps} /> | ||
</div> | ||
|
||
<button {...api.clearTriggerProps}> | ||
<RotateCcw /> | ||
</button> | ||
</div> | ||
|
||
<button | ||
onClick={() => { | ||
api.getDataUrl("image/png").then(setUrl) | ||
}} | ||
> | ||
Show Image | ||
</button> | ||
{url && <img data-part="preview" alt="signature" src={url} />} | ||
</main> | ||
|
||
<Toolbar controls={controls.ui}> | ||
<StateVisualizer state={state} omit={["points"]} /> | ||
</Toolbar> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<script setup lang="ts"> | ||
import * as signaturePad from "@zag-js/signature-pad" | ||
import { signaturePadControls } from "@zag-js/shared" | ||
import { normalizeProps, useMachine } from "@zag-js/vue" | ||
const url = ref("") | ||
const setUrl = (v: string) => (url.value = v) | ||
const controls = useControls(signaturePadControls) | ||
const [state, send] = useMachine(signaturePad.machine({ id: "1" }), { | ||
context: controls.context, | ||
}) | ||
const api = computed(() => signaturePad.connect(state.value, send, normalizeProps)) | ||
</script> | ||
|
||
<template> | ||
<main className="signature-pad"> | ||
<div v-bind="api.rootProps"> | ||
<label v-bind="api.labelProps">Signature Pad</label> | ||
|
||
<div v-bind="api.controlProps"> | ||
<svg v-bind="api.segmentProps"> | ||
<path v-for="path of api.paths" key="{i}" v-bind="api.getSegmentPathProps({ path })" /> | ||
<path v-if="api.currentPath" v-bind="api.getSegmentPathProps({ path: api.currentPath })" /> | ||
</svg> | ||
<div v-bind="api.separatorProps" /> | ||
</div> | ||
|
||
<button v-bind="api.clearTriggerProps"> | ||
<RotateCcw /> | ||
</button> | ||
</div> | ||
|
||
<button | ||
@click=" | ||
() => { | ||
api.getDataUrl('image/png').then(setUrl) | ||
} | ||
" | ||
> | ||
Show Image | ||
</button> | ||
<img v-if="url" data-part="preview" alt="signature" :src="url" /> | ||
</main> | ||
|
||
<Toolbar> | ||
<StateVisualizer :state="state" /> | ||
<template #controls> | ||
<Controls :control="controls" /> | ||
</template> | ||
</Toolbar> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { signaturePadControls } from "@zag-js/shared" | ||
import * as signaturePad from "@zag-js/signature-pad" | ||
import { normalizeProps, useMachine } from "@zag-js/solid" | ||
import { For, Show, createMemo, createSignal, createUniqueId } from "solid-js" | ||
import { StateVisualizer } from "../components/state-visualizer" | ||
import { Toolbar } from "../components/toolbar" | ||
import { useControls } from "../hooks/use-controls" | ||
import { RotateCcw } from "lucide-solid" | ||
|
||
export default function Page() { | ||
const [url, setUrl] = createSignal("") | ||
|
||
const controls = useControls(signaturePadControls) | ||
|
||
const [state, send] = useMachine( | ||
signaturePad.machine({ | ||
id: createUniqueId(), | ||
onDrawEnd(details) { | ||
details.getDataUrl("image/png").then(setUrl) | ||
}, | ||
}), | ||
{ | ||
context: controls.context, | ||
}, | ||
) | ||
|
||
const api = createMemo(() => signaturePad.connect(state, send, normalizeProps)) | ||
|
||
return ( | ||
<> | ||
<main class="signature-pad"> | ||
<div {...api().rootProps}> | ||
<label {...api().labelProps}>Signature Pad</label> | ||
|
||
<div {...api().controlProps}> | ||
<svg {...api().segmentProps}> | ||
<For each={api().paths}>{(path) => <path {...api().getSegmentPathProps({ path })} />}</For> | ||
<Show when={api().currentPath}> | ||
{(path) => <path {...api().getSegmentPathProps({ path: path() })} />} | ||
</Show> | ||
</svg> | ||
<div {...api().separatorProps} /> | ||
</div> | ||
|
||
<button {...api().clearTriggerProps}> | ||
<RotateCcw /> | ||
</button> | ||
</div> | ||
|
||
<button | ||
onClick={() => { | ||
api().getDataUrl("image/png").then(setUrl) | ||
}} | ||
> | ||
Show Image | ||
</button> | ||
|
||
<Show when={url()}> | ||
<img data-part="preview" alt="signature" src={url()} /> | ||
</Show> | ||
</main> | ||
|
||
<Toolbar controls={controls.ui}> | ||
<StateVisualizer state={state} /> | ||
</Toolbar> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.