├── .github └── workflows │ └── ci.yml ├── tsconfig.json ├── package.json ├── LICENSE ├── .gitignore ├── HISTORY.md ├── README.md └── src └── index.tsx /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches-ignore: 6 | - "dependabot/**" 7 | pull_request_target: 8 | 9 | jobs: 10 | lint: 11 | timeout-minutes: 5 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checkout repo 15 | uses: actions/checkout@v3 16 | 17 | - name: Setup node 18 | uses: actions/setup-node@v3 19 | with: 20 | node-version: "22" 21 | 22 | - name: Install dependencies 23 | run: npm i 24 | 25 | - name: Linting 26 | run: npx -y prettier -c . 27 | 28 | - name: Type check 29 | run: npx -y tsc --strict 30 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "CommonJS", 4 | "declaration": true, 5 | "sourceMap": true, 6 | "rootDir": "./src", 7 | "strict": true, 8 | "outDir": "./dist", 9 | "noUnusedLocals": true, 10 | "noUnusedParameters": true, 11 | "noImplicitReturns": true, 12 | "noFallthroughCasesInSwitch": true, 13 | "downlevelIteration": true, 14 | "moduleResolution": "node", 15 | "baseUrl": "./", 16 | "paths": { 17 | "*": ["src/*", "node_modules/*"] 18 | }, 19 | "jsx": "react", 20 | "esModuleInterop": true, 21 | "target": "ES2019", 22 | "allowJs": true, 23 | "skipLibCheck": true, 24 | "forceConsistentCasingInFileNames": true, 25 | "resolveJsonModule": true, 26 | "isolatedModules": true, 27 | "types": ["turnstile-types"] 28 | }, 29 | "exclude": ["node_modules", "dist"] 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-turnstile", 3 | "version": "1.1.4", 4 | "description": "React library for Cloudflare's Turnstile CAPTCHA alternative", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "prepublishOnly": "npm run build", 8 | "build": "tsc", 9 | "style-fix": "npx prettier -w ." 10 | }, 11 | "exports": { 12 | "import": "./dist/index.js", 13 | "require": "./dist/index.js", 14 | "types": "./dist/index.d.ts" 15 | }, 16 | "types": "./dist/index.d.ts", 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/Le0developer/react-turnstile" 20 | }, 21 | "peerDependencies": { 22 | "react": ">= 16.13.1", 23 | "react-dom": ">= 16.13.1" 24 | }, 25 | "author": "Leo Developer", 26 | "license": "MIT", 27 | "devDependencies": { 28 | "@types/react": "^18.0.21", 29 | "prettier": "^2.7.1", 30 | "turnstile-types": "^1.2.3", 31 | "typescript": "^4.8.4" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022-2023 LeoDeveloper 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* -------------------------------------------------------------------------------- /HISTORY.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [1.1.4] - 2024-09-24 11 | 12 | ### Added 13 | 14 | - `onSuccess` callback with the additional `preClearanceObtained` argument 15 | - `flexible` option for `size` prop 16 | 17 | ### Changed 18 | 19 | - Update `turnstile-types` to v1.2.3 20 | 21 | ## [1.1.3] - 2024-02-19 22 | 23 | ### Changed 24 | 25 | - React peer dependency version to v16.13.1 26 | 27 | ## [1.1.2] - 2023-09-18 28 | 29 | ### Added 30 | 31 | - `onAfterInteractive`, `onBeforeInteractive`, `onUnsupported` callbacks 32 | 33 | ### Changed 34 | 35 | - `onVerify` is no longer required 36 | 37 | ### Removed 38 | 39 | - Custom `retry` logic (issue fixed upstream in Turnstile itself) 40 | 41 | ### Fixed 42 | 43 | - Remount issue with certain callbacks (#15) 44 | 45 | ## [1.1.1] - 2023-06-25 46 | 47 | ### Added 48 | 49 | - `useTurnstile` hook 50 | - `fixedSize` option to reduce layout shift 51 | - Missing argument for `onExpire` 52 | - `BoundTurnstileObject` argument to callbacks 53 | 54 | ### Fixed 55 | 56 | - `global` -> `globalNamespace` (name conflict) (#10) 57 | - Implement `retry` logic ourselves (#14) 58 | - Missing `onError` error argument passthrough 59 | 60 | ## [1.1.0] - 2023-03-09 61 | 62 | ### Added 63 | 64 | - `refreshExpired` option 65 | - `language` option 66 | - `appearance` option 67 | - `execution` option 68 | 69 | ### Changed 70 | 71 | - Temporary load callback function is now removed after load 72 | 73 | ### Fixed 74 | 75 | - `onTimeout` callback not properly registering 76 | - `ref` not passing properly - pass `userRef` instead 77 | - `globalThis` errors on older browsers 78 | 79 | ### Removed 80 | 81 | - `autoResetOnExpire` - use `refreshExpired` instead 82 | 83 | ## [1.0.6] - 2022-11-25 84 | 85 | ### Added 86 | 87 | - Support for the new `onTimeout` callback 88 | - `autoResetOnExpire` for automatically resetting the Turnstile widget once the token expires 89 | - `retry` & `retryInterval` 90 | - custom `ref` argument for using your own ref (#7) 91 | 92 | ## [1.0.5] - 2022-10-22 93 | 94 | ### Added 95 | 96 | - require module support (#6) 97 | - `size` support 98 | 99 | ## [1.0.4] - 2022-10-11 100 | 101 | ### Changed 102 | 103 | - `onLoad` callback now includes the turnstile widget id (#5) 104 | 105 | ## [1.0.3] - 2022-10-05 106 | 107 | ### Fixed 108 | 109 | - Race condition by using `useRef` instead of `createRef` (#4) 110 | 111 | ## [1.0.2] - 2022-10-05 112 | 113 | ### Added 114 | 115 | - Experimental (undocumented) fields `responseField` and `responseFieldName` for controlling the `` element generated by Turnstile. 116 | - `style` prop which is directly passed to the internal `
` 117 | 118 | ### Changes 119 | 120 | - Using explicit rendering for Turnstile now which prevents [implicit renders](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#implicitly-render-the-turnstile-widget) (also undocumented) 121 | - Using `turnstile.remove()` to remove widgets after being unloaded 122 | 123 | ### Fixed 124 | 125 | - A race condition when loading Turnstile on page load in dev mode has been fixed 126 | - Callback props will now update as expected 127 | - Entrypoint pointing to the wrong file (#3) 128 | 129 | ## [1.0.1] - 2022-10-01 130 | 131 | ### Fixed 132 | 133 | - Fix double render in vite (#1) 134 | - Fix tslib being a dependency (#2) 135 | 136 | ## [1.0.0] - 2022-09-29 137 | 138 | Initial release. 139 | 140 | [unreleased]: https://github.com/Le0Developer/react-turnstile/compare/v1.1.4...HEAD 141 | [1.1.4]: https://github.com/le0developer/react-turnstile/compare/v1.1.3...v1.1.4 142 | [1.1.3]: https://github.com/le0developer/react-turnstile/compare/v1.1.2...v1.1.3 143 | [1.1.2]: https://github.com/le0developer/react-turnstile/compare/v1.1.1...v1.1.2 144 | [1.1.1]: https://github.com/le0developer/react-turnstile/compare/v1.1.0...v1.1.1 145 | [1.1.0]: https://github.com/le0developer/react-turnstile/compare/v1.0.6...v1.1.0 146 | [1.0.6]: https://github.com/le0developer/react-turnstile/compare/v1.0.5...v1.0.6 147 | [1.0.5]: https://github.com/le0developer/react-turnstile/compare/v1.0.4...v1.0.5 148 | [1.0.4]: https://github.com/le0developer/react-turnstile/compare/v1.0.3...v1.0.4 149 | [1.0.3]: https://github.com/le0developer/react-turnstile/compare/v1.0.2...v1.0.3 150 | [1.0.2]: https://github.com/le0developer/react-turnstile/compare/v1.0.1...v1.0.2 151 | [1.0.1]: https://github.com/le0developer/react-turnstile/compare/v1.0.0...v1.0.1 152 | [1.0.0]: https://github.com/Le0Developer/react-turnstile/releases/tag/v1.0.0 153 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-turnstile 2 | 3 | A very simple React library for [Cloudflare Turnstile](https://challenges.cloudflare.com). 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm i react-turnstile 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```jsx 14 | import Turnstile, { useTurnstile } from "react-turnstile"; 15 | 16 | // ... 17 | 18 | function TurnstileWidget() { 19 | const turnstile = useTurnstile(); 20 | return ( 21 |