Created
May 17, 2025 03:14
-
-
Save SandroMaglione/3ef1f83b212af1a15c4221d22b2ac3e3 to your computer and use it in GitHub Desktop.
Revisions
-
SandroMaglione created this gist
May 17, 2025 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import { useActor } from "@xstate/react"; import { setup } from "xstate"; const copyMachine = setup({ types: { events: {} as { type: "copy"; text: string }, }, }).createMachine({ initial: "Idle", states: { Idle: { on: { copy: { target: "Copied", actions: ({ event }) => { window.navigator.clipboard.writeText(event.text); }, }, }, }, Copied: { after: { 2000: "Idle", }, }, }, }); export default function CopyLink() { const [snapshot, send] = useActor(copyMachine); return ( <button onClick={() => send({ type: "copy", text: "some text" })}> {snapshot.matches("Copied") ? "Copied!" : "Copy link"} </button> ); }