Skip to content

Commit

Permalink
docs: add next.js section
Browse files Browse the repository at this point in the history
  • Loading branch information
DSergiu committed Apr 22, 2024
1 parent 0b555d7 commit 68906f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mainstream web frameworks such as: React, Preact, Vue.js, Angular, Stencil.js, e
* [Preact](#reactjs-and-preact)
* [Angular 2+](#angular)
* [Angular.JS](#angularjs)
* [Next.JS](#nextjs)
* [Vanilla](#vanillajs)
* You can find more examples in the `<root>/examples/cdn` directory.

Expand Down Expand Up @@ -177,6 +178,54 @@ mainstream web frameworks such as: React, Preact, Vue.js, Angular, Stencil.js, e
</html>
```

### Next.JS

> Example: display normal size hCaptcha with dark theme.

1. Add `h-captcha` web component type by extending `JSX.IntrinsicElements` in `*.d.ts`.
```ts
import * as React from 'react';
declare global {
declare namespace JSX {
interface IntrinsicElements {
'h-captcha': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement> & {
[k: string]: unknown;
}, HTMLElement>;
}
}
}
```

2. Integrate in your next.js page.
```js
export default function HomeComponent() {
const sitekey = '781559eb-513a-4bae-8d29-d4af340e3624';
const captchaRef = createRef<HTMLElement>();
useEffect(() => {
import('@hcaptcha/vanilla-hcaptcha');
if (captchaRef.current) {
captchaRef.current.addEventListener('verified', (e: Event) => {
console.log('hCaptcha event: verified', { token: e.token });
});
}
}, []);
return (
<main>
<h-captcha
ref={captchaRef}
sitekey={sitekey}
size="normal"
theme="dark"
></h-captcha>
</main>
);
}
```

### Vanilla.JS

> Example: display normal size hCaptcha accessible by keyboard (see [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex)).
Expand Down
4 changes: 3 additions & 1 deletion packages/vanilla-hcaptcha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"vue.js",
"preact",
"react",
"react.js"
"react.js",
"nextjs",
"next.js"
]
}

0 comments on commit 68906f9

Please sign in to comment.