@esigncomply/react
v1.0.1
Published
React hooks for the Esign Comply browser SDK. Hooks only: you build every element the signer sees.
Downloads
568
Maintainers
Readme
@esigncomply/react
React hooks for the Esign Comply browser SDK. Hooks only, no visual components: you build the notice, the links, the checkbox and the button, and the hooks connect them to the ceremony.
npm install @esigncomply/react @esigncomply/browserPeer dependencies: react 18 or later and @esigncomply/browser. The built file starts with "use client".
Hooks
useCeremony({ clientToken, apiUrl? })
Attaches to the session in an effect, on the client, and follows it.
const { ceremony, status, remainingSteps, findings, error, canComplete } = useCeremony({ clientToken, apiUrl });| Field | Meaning |
|---|---|
| ceremony | The attached ceremony from @esigncomply/browser, or null until status is ready. |
| status | idle (no token yet), attaching, ready, error, completed. |
| remainingSteps | Steps still outstanding. It follows the page: unticking the box puts intent.captured back. |
| findings | Conformance findings that did not pass, the latest for each step. Each has rule, status, message, fix, doc_url. |
| error | The last EsignComplyError, with code, rule and fix. |
| canComplete | true when only the signing act is left. Use it to enable the button. |
The session is attached once per token. React strict mode runs effects twice in development; the hook still attaches once. The ceremony is destroyed when the last component that uses the token unmounts.
useDocument(ceremony, { versionId, mode })
Presents one document version of the session.
const tos = useDocument<HTMLAnchorElement>(ceremony, { versionId, mode: "hyperlink" });
// tos.ref goes on the link (hyperlink) or on the element that holds the document text (inline, scrollbox)
// tos.presented is true once the API recorded the presentation; tos.error holds a failureSignup form
The server action creates the session with the secret key and returns clientToken, apiUrl and the pinned version IDs. The form does the rest.
"use client";
import { useEffect, useRef, useState } from "react";
import { useCeremony, useDocument } from "@esigncomply/react";
export function SignupForm({ clientToken, apiUrl, tosVersionId, privacyVersionId }: Props) {
const { ceremony, status, findings, canComplete } = useCeremony({ clientToken, apiUrl });
const tos = useDocument<HTMLAnchorElement>(ceremony, { versionId: tosVersionId, mode: "hyperlink" });
const privacy = useDocument<HTMLAnchorElement>(ceremony, { versionId: privacyVersionId, mode: "hyperlink" });
const assentRef = useRef<HTMLDivElement>(null);
const checkboxRef = useRef<HTMLInputElement>(null);
const submitRef = useRef<HTMLButtonElement>(null);
const [message, setMessage] = useState("");
// Observe the assent controls once both documents are presented.
useEffect(() => {
if (!ceremony || !tos.presented || !privacy.presented) return;
ceremony.observeAssent({
region: assentRef.current, // notice, links, checkbox, button
checkbox: checkboxRef.current,
submit: submitRef.current,
});
}, [ceremony, tos.presented, privacy.presented]);
// The signing act, from the signer's real click.
async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!ceremony) return;
try {
const result = await ceremony.complete(); // { recordId, receipt, verifyUrl, status }
await finishSignup(new FormData(event.currentTarget), result.recordId); // server action: confirms the record, then creates the account
} catch (err) {
setMessage(err instanceof Error ? err.message : "We could not record your acceptance."); // not signed: do not create the account
}
}
return (
<form onSubmit={onSubmit}>
<input name="email" type="email" required />
<div ref={assentRef}>
<p>
By checking the box and clicking Create account, you agree to the{" "}
<a ref={tos.ref} href="/legal/terms">Terms of Service</a> and acknowledge the{" "}
<a ref={privacy.ref} href="/legal/privacy">Privacy Notice</a>.
</p>
<label>
<input ref={checkboxRef} type="checkbox" /> I agree to the Terms of Service and Privacy Notice
</label>
<button ref={submitRef} type="submit" disabled={!canComplete}>
Create account
</button>
</div>
{status === "error" && <p role="alert">We could not start the signing step. Reload the page to try again.</p>}
{message && <p role="alert">{message}</p>}
{process.env.NODE_ENV !== "production" && findings.map((f) => <p key={f.rule + f.event_type}>{f.rule}: {f.message} {f.fix}</p>)}
</form>
);
}Points that matter:
- Render the checkbox unchecked and leave it uncontrolled, or control it with state that starts
false. A pre-checked box is reported as pre-checked (rule CR-01). - Keep the notice, the links, the checkbox and one primary button inside the element you pass as
region. - Give document links a colour that differs from the surrounding text by at least 3 to 1, and keep them underlined (rule CR-05). The browser's default blue on black text measures 2.23 to 1.
- Start the ceremony when the form mounts, not on submit.
- A resolved
complete()is not proof. Your server confirms the record withGET /v1/records/{id}before it creates the account.
Everything else is on the ceremony itself: ceremony.reviewPresented(...), ceremony.cancel() and the rest are described in the @esigncomply/browser README. So is the optional code step, ceremony.verify.start(...), for L2 profiles on Unlimited and Enterprise.
Development
node packages/react/build.mjs # dist/index.js and dist/index.d.ts (builds the browser SDK first if needed)
node packages/react/test/e2e/run.mjs # the form above inside <StrictMode>, in Chrome, against a local API