/opt/canhelp/node_modules/@marsidev/react-turnstile/skills/basic-setup
NameSizeModeActions
SKILL.md52940644editdlrm
Edit: /opt/canhelp/node_modules/@marsidev/react-turnstile/skills/basic-setup/SKILL.md (5294B)
--- name: basic-setup description: > Install and render your first Turnstile widget with required configuration. Activate this skill when starting a new project with react-turnstile or when implementing basic CAPTCHA protection for the first time. triggers: - 'install turnstile react' - 'add captcha to form' - 'setup cloudflare turnstile' - 'basic turnstile example' - 'how to use @marsidev/react-turnstile' - 'turnstile widget not showing' category: core metadata: library: '@marsidev/react-turnstile' library_version: '1.4.2' framework: React --- # Basic Setup Install and render a Cloudflare Turnstile widget in your React application. ## Installation ```bash # npm npm install @marsidev/react-turnstile # pnpm pnpm add @marsidev/react-turnstile # yarn yarn add @marsidev/react-turnstile # bun bun add @marsidev/react-turnstile ``` ## Get Your Site Key Before using Turnstile, you need a site key from Cloudflare: 1. Go to the [Cloudflare Turnstile dashboard](https://developers.cloudflare.com/turnstile/get-started/) 2. Create a new widget 3. Copy your site key (starts with `0x` or `1x` for testing) **Note:** Site keys are **NOT** secret. They are safe to include in client-side code. ## Basic Usage ```tsx import { Turnstile } from '@marsidev/react-turnstile' export default function LoginForm() { return (
{/* Basic Turnstile widget */} ) } ``` ## Widget Sizes Choose the size that fits your layout: | Size | Dimensions | Best For | |------|------------|----------| | `normal` (default) | 300×65px | Most forms, standard layouts | | `compact` | 150×140px | Mobile, tight spaces | | `flexible` | 100% width × 65px | Responsive layouts | | `invisible` | 0×0px | Hidden widgets (requires Invisible widget type from Cloudflare) | ```tsx // Compact size for mobile // Flexible width ``` ## Handling the Success Token Use the `onSuccess` callback to receive the verification token: ```tsx import { Turnstile } from '@marsidev/react-turnstile' import { useState } from 'react' export default function LoginForm() { const [token, setToken] = useState(null) return (
setToken(token)} /> ) } ``` ## Complete Form Example ```tsx 'use client' // For Next.js App Router import { Turnstile } from '@marsidev/react-turnstile' import { useState } from 'react' export default function ContactForm() { const [token, setToken] = useState(null) const [isSubmitting, setIsSubmitting] = useState(false) async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!token) return setIsSubmitting(true) // Send token to your server for validation const response = await fetch('/api/contact', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, /* other form data */ }) }) if (response.ok) { // Handle success } setIsSubmitting(false) } return (