React components for X-Captcha service

Modern, customizable React components for the X-Captcha service
bash
Using npm
npm install @elsikora/x-captcha-reactUsing yarn
yarn add @elsikora/x-captcha-reactUsing pnpm
pnpm add @elsikora/x-captcha-reactUsing bun
bun add @elsikora/x-captcha-react
`💡 Usage
Basic Usage
The simplest way to use X-Captcha-React is with the
CaptchaWidget component:`tsx
import { CaptchaWidget } from '@elsikora/x-captcha-react';
import React from 'react';function MyForm() {
const handleVerify = (token: string) => {
console.log('Verification successful!', token);
// Send the token to your server for validation
};
const handleError = (error: string) => {
console.error('Verification failed:', error);
};
return (
Contact Form
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
onVerify={handleVerify}
onError={handleError}
/>
);
}
`Complete Form Integration
For a more complete solution, use the
CaptchaForm component which includes the widget and form handling:`tsx
import { CaptchaForm } from '@elsikora/x-captcha-react';
import React, { FormEvent } from 'react';function ContactForm() {
const handleSubmit = (token: string, event: FormEvent) => {
// Access form data
const formData = new FormData(event.target as HTMLFormElement);
// Submit to your server with the captcha token
fetch('/api/contact', {
method: 'POST',
body: JSON.stringify({
name: formData.get('name'),
email: formData.get('email'),
message: formData.get('message'),
captchaToken: token
}),
headers: {
'Content-Type': 'application/json'
}
});
};
return (
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
onSubmit={handleSubmit}
submitButtonText="Send Message"
>
);
}
`Using the CaptchaProvider
For applications that use captcha in multiple places, use the
CaptchaProvider:`tsx
import { CaptchaProvider, CaptchaWidget } from '@elsikora/x-captcha-react';
import React from 'react';function App() {
return (
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
>
);
}// Then in your components:
function LoginForm() {
const handleVerify = (token: string) => {
// Handle verification
};
return (
);
}
`Customizing the Theme
X-Captcha-React offers extensive theming options through a comprehensive
theme object:`tsx
import { CaptchaWidget } from '@elsikora/x-captcha-react';
import React from 'react';function CustomThemedCaptcha() {
return (
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
// Complete theme customization
theme={{
colors: {
primary: "#6200EA", // Main theme color
background: "#F5F5F5", // Widget background
text: "#212121", // Main text color
textLight: "#757575", // Secondary text/brand name
error: "#D50000", // Error messages
border: "#E0E0E0", // Borders
disabled: "#9E9E9E", // Disabled state text
disabledBackground: "#F5F5F5" // Disabled state background
},
checkbox: {
checkmarkColor: "#00C853", // Verified checkmark
borderColor: "#6200EA", // Checkbox border
size: "24px", // Checkbox size
borderRadius: "4px" // Checkbox corners
},
retryButton: {
backgroundColor: "#E0E0E0", // Retry button background
textColor: "#424242", // Retry button text
hoverBackgroundColor: "#D0D0D0", // Hover state
iconColor: "#616161", // Icon color
borderColor: "#BDBDBD", // Button border
borderRadius: "6px" // Button corners
},
submitButton: {
backgroundColor: "#6200EA", // Submit button background
textColor: "#FFFFFF", // Submit button text
hoverBackgroundColor: "#5300D5", // Hover state
disabledBackgroundColor: "#E0E0E0", // Disabled state
disabledTextColor: "#9E9E9E" // Disabled text
},
spinner: {
color: "#6200EA", // Loading spinner color
size: "20px", // Spinner size
borderWidth: "2px" // Spinner thickness
},
typography: {
fontFamily: "Inter, sans-serif", // Font family
fontSizeXs: "10px", // Extra small text
fontSizeSm: "14px", // Small text
fontSizeMd: "15px" // Medium text
},
effects: {
shadowSm: "0 1px 3px rgba(0,0,0,0.08)",
shadow: "0 4px 6px rgba(0,0,0,0.1)",
shadowLg: "0 6px 12px rgba(0,0,0,0.15)",
transitionFast: "0.2s",
transitionNormal: "0.3s"
},
spacing: {
xs: "4px",
sm: "8px",
normal: "10px",
md: "16px",
lg: "24px"
},
borderRadius: {
sm: "4px",
normal: "6px",
lg: "8px"
}
}}
// Size customization
width={320}
height={80}
/>
);
}
`$3
For basic customization, you can provide just the properties you want to change:
`tsx
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
theme={{
colors: {
primary: "#FF5722", // Orange theme
background: "#FFF3E0"
},
retryButton: {
backgroundColor: "#FFE0B2",
textColor: "#E65100"
}
}}
/>
`Language Customization
X-Captcha-React supports 30+ languages with automatic detection:
`tsx
import { CaptchaWidget } from '@elsikora/x-captcha-react';
import React from 'react';function MultiLanguageCaptcha() {
return (
{/ Will auto-detect the user's browser language /}
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
/>
{/ Or specify a language /}
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType="click"
language="fr"
/>
);
}
`Advanced: Proof of Work Configuration
For Proof of Work challenges, you can customize the solver:
`tsx
import { CaptchaWidget } from '@elsikora/x-captcha-react';
import { EChallengeType } from '@elsikora/x-captcha-react';
import React from 'react';function AdvancedPowCaptcha() {
return (
apiUrl="https://api.x-captcha.com"
publicKey="your-public-key"
challengeType={EChallengeType.POW}
powSolver={{
batchSize: 2000, // Number of attempts per batch
maxAttempts: 2000000, // Maximum number of attempts
workerTimeout: 60000 // Timeout in milliseconds
}}
/>
);
}
`TypeScript Support
X-Captcha-React is written in TypeScript and exports all necessary types:
`tsx
import type {
ICaptchaWidgetProperties,
ICaptchaFormProperties,
ICaptchaTheme,
EChallengeType
} from '@elsikora/x-captcha-react';// Use the types for better type safety
const customTheme: ICaptchaTheme = {
colors: {
primary: "#2196F3",
background: "#FFFFFF",
error: "#F44336"
},
retryButton: {
backgroundColor: "#E3F2FD",
textColor: "#1976D2",
hoverBackgroundColor: "#BBDEFB"
}
};
const widgetProps: ICaptchaWidgetProperties = {
apiUrl: "https://api.x-captcha.com",
publicKey: "your-public-key",
challengeType: EChallengeType.CLICK,
theme: customTheme,
onVerify: (token: string) => console.log(token)
};
``Copyright (c) 2025 ElsiKora
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**.