Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade package to use svelte kit packaging with typescript #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
.DS_Store
node_modules
/dist/

# Output
.output
.vercel
/.svelte-kit
/build
/dist

# OS
.DS_Store
Thumbs.db

# Env
.env
.env.*
!.env.example
!.env.test

# Vite
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
7 changes: 4 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
/dist/
# Package Managers
package-lock.json
pnpm-lock.yaml
yarn.lock
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"useTabs": false,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
68 changes: 52 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ This library is heavily inspired by [react-hcaptcha](https://github.com/hCaptcha

You can install this library via npm:

`npm install svelte-hcaptcha --save-dev`
`pnpm install svelte-hcaptcha --save-dev`

## Usage

The two requirements for usage are the `sitekey` prop and a parent component such as a `<form />`. The component `HCaptcha` component will automatically include and load the hCaptcha API library and append it to the parent component, ready for use.

The `HCaptcha` component dispatches various events which you can listen to in the parent;
* `mount` - the component has been mounted
* `load` - the hCaptcha API script has successfully loaded
* `success` - a user has successfully completed an hCaptcha challenge. The payload of this event contains a `token` which can be used to verify the captcha
* `error` - something went wrong when the user attempted the captcha
* `close` - the captcha was closed
* `expired` - the captcha has expired and needs to be re-verified

- `mount` - the component has been mounted
- `load` - the hCaptcha API script has successfully loaded
- `success` - a user has successfully completed an hCaptcha challenge. The payload of this event contains a `token` which can be used to verify the captcha
- `error` - something went wrong when the user attempted the captcha
- `close` - the captcha was closed
- `expired` - the captcha has expired and needs to be re-verified

You can read more about these events [below](#events). In particular, errors are described [here](https://docs.hcaptcha.com/configuration/#error-codes).

Expand All @@ -39,33 +40,30 @@ Note also that it's not possible to develop against `localhost` or `127.0.0.1` w

```svelte
<form>
<HCaptcha
sitekey={mySitekey}
<HCaptcha
sitekey={mySitekey}
theme={CaptchaTheme.DARK}
on:success={handleSuccess}
on:error={handleError}
/>
</form>
```

If you want to be able to **reset** the component (hint: you probably want to do this, for instance, if captcha verification fails), then you'll need to *bind* to it in the parent. The component exposes a `.reset()` method;
If you want to be able to **reset** the component (hint: you probably want to do this, for instance, if captcha verification fails), then you'll need to _bind_ to it in the parent. The component exposes a `.reset()` method;

```svelte
<script>
let captcha;

const handleError = () => {
captcha.reset();
}
};
</script>

...

<form>
<HCaptcha
bind:this={captcha}
on:error={handleError}
/>
<HCaptcha bind:this={captcha} on:error={handleError} />
</form>
```

Expand Down Expand Up @@ -101,4 +99,42 @@ If you want to be able to **reset** the component (hint: you probably want to do

Pull requests, suggestions, comments, critiques - all welcome :)

Please get in touch with the maintainers if you need help or advice to get the project to run.
Please get in touch with the maintainers if you need help or advice to get the project to run.


## Developing

Install dependencies with `pnpm install` (or `npm install` or `yarn`), to start a development server:

```bash
pnpm run dev

# or start the server and open the app in a new browser tab
pnpm run dev -- --open
```

Everything inside `src/lib` is part of the library.

## Building

To build this library:

```bash
pnpm run package
```

To create a production version:

```bash
pnpm run build
```

You can preview the production build with `pnpm run preview`.

## Publishing

To publish your library to [npm](https://www.npmjs.com):

```bash
pnpm publish
```
33 changes: 33 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import js from '@eslint/js';
import ts from 'typescript-eslint';
import svelte from 'eslint-plugin-svelte';
import prettier from 'eslint-config-prettier';
import globals from 'globals';

/** @type {import('eslint').Linter.Config[]} */
export default [
js.configs.recommended,
...ts.configs.recommended,
...svelte.configs['flat/recommended'],
prettier,
...svelte.configs['flat/prettier'],
{
languageOptions: {
globals: {
...globals.browser,
...globals.node
}
}
},
{
files: ['**/*.svelte'],
languageOptions: {
parserOptions: {
parser: ts.parser
}
}
},
{
ignores: ['build/', '.svelte-kit/', 'dist/']
}
];
Loading