From 8c298a638bd4779a8373e2957e22b8e1451cfd50 Mon Sep 17 00:00:00 2001 From: GHOSt Date: Thu, 29 Sep 2022 22:59:49 +0100 Subject: [PATCH] feat: lib --- README.md | 46 +++++++++++++++++ src/lib/Turnstile.svelte | 104 +++++++++++++++++++++++++++++++++++++++ src/lib/events.d.ts | 9 ++++ src/lib/index.d.ts | 2 + src/lib/index.ts | 1 + src/routes/+page.svelte | 9 ++++ 6 files changed, 171 insertions(+) create mode 100644 README.md create mode 100644 src/lib/Turnstile.svelte create mode 100644 src/lib/events.d.ts create mode 100644 src/lib/index.d.ts create mode 100644 src/lib/index.ts create mode 100644 src/routes/+page.svelte diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1aaade --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# Svelte Turnstile + +[Cloudflare's Turnstile](https://developers.cloudflare.com/turnstile/) is a new CAPTCHA alternative, this library allows you to easily integrate it into your svelte projects. + +# Installing + +```sh +npm install svelte-turnstile -D +``` + +# Using + +The only required prop is the `siteKey` which you can get from [adding a site here](https://dash.cloudflare.com/?to=/:account/turnstile). + +```html + + + +``` + +## Props + +| Prop | Type | Description | Required | +|------------|-------------------------------|-------------------------------------------------------------------------------|----------| +| `siteKey` | `string` | sitekey for your website | ✅ | +| `theme` | `'light' \| 'dark' \| 'auto'` | colour theme of the widget (defaults to auto) | ❌ | +| `action` | `string` | A string that can be used to differentiate widgets, returned on validation | ❌ | +| `cData` | `string` | A string that can attach customer data to a challange, returned on validation | ❌ | +| `tabIndex` | `number` | Used for accessibility (defaults to 0) | ❌ | + +For more information about some of the props [checkout the Cloudflare Documentation](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configurations). + +## Events + +| Event | Data | Description | +|----------------------|---------------------|------------------------------------------------------------------------------| +| `turnstile-error` | `{}` | Emitted when a user fails verification | +| `turnstile-expired` | `{}` | Emitted when a challenge expires, this library will auto-renew the challenge | +| `turnstile-callback` | `{ token: string }` | Emitted when a user passes a challenge | + +# Support + +- Join the [discord](https://discord.gg/2Vd4wAjJnm)
+- Create a issue on the [github](https://github.com/ghostdevv/svelte-turnstile) diff --git a/src/lib/Turnstile.svelte b/src/lib/Turnstile.svelte new file mode 100644 index 0000000..1d98ca5 --- /dev/null +++ b/src/lib/Turnstile.svelte @@ -0,0 +1,104 @@ + + + + + + {#if mounted} + + {/if} + + +
diff --git a/src/lib/events.d.ts b/src/lib/events.d.ts new file mode 100644 index 0000000..6d8475d --- /dev/null +++ b/src/lib/events.d.ts @@ -0,0 +1,9 @@ +declare namespace svelte.JSX { + interface HTMLProps { + 'onturnstile-error'?: (event: CustomEvent<{}>) => void; + 'onturnstile-expired'?: (event: CustomEvent<{}>) => void; + 'onturnstile-callback'?: ( + event: CustomEvent<{ token: string }>, + ) => void; + } +} diff --git a/src/lib/index.d.ts b/src/lib/index.d.ts new file mode 100644 index 0000000..c1567f3 --- /dev/null +++ b/src/lib/index.d.ts @@ -0,0 +1,2 @@ +import './events'; +export { default as Turnstile } from './Turnstile.svelte'; diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..8a80412 --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1 @@ +export { default as Turnstile } from './Turnstile.svelte'; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte new file mode 100644 index 0000000..3971154 --- /dev/null +++ b/src/routes/+page.svelte @@ -0,0 +1,9 @@ + + + alert(e.detail.token)} + on:turnstile-error={() => alert('error')} + on:turnstile-expired={() => alert('expire')} />