A beautiful, modern drag-and-drop puzzle CAPTCHA component for React applications
npm install drag-puzzle-captchabash
npm install drag-puzzle-captcha
`
š Quick Start
$3
`jsx
import React, { useState } from 'react';
import DragPuzzleCaptcha from 'drag-puzzle-captcha';
import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
function App() {
const [isVerified, setIsVerified] = useState(false);
const handleVerification = (success) => {
setIsVerified(success);
if (success) {
console.log('CAPTCHA verified successfully!');
}
};
return (
My Form
{isVerified && ā
Verification successful!
}
);
}
export default App;
`
$3
For Vite projects, you can import styles in multiple ways:
`jsx
// Method 1: Import CSS directly
import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
// Method 2: Import as style (alias)
import 'drag-puzzle-captcha/style';
// Method 3: Import in your CSS file
/ In your main CSS file /
@import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
`
$3
`jsx
import React, { useState } from 'react';
import dynamic from 'next/dynamic';
import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
// Dynamic import to avoid SSR issues
const DragPuzzleCaptcha = dynamic(
() => import('drag-puzzle-captcha'),
{ ssr: false }
);
export default function MyPage() {
const [isVerified, setIsVerified] = useState(false);
return (
Secure Form
{isVerified && ā
Verified!
}
);
}
`
$3
`jsx
import React, { useState } from 'react';
import DragPuzzleCaptcha from 'drag-puzzle-captcha';
import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
function App() {
const [showCaptcha, setShowCaptcha] = useState(false);
const [isVerified, setIsVerified] = useState(false);
const handleVerification = (success) => {
setIsVerified(success);
if (success) {
setShowCaptcha(false);
}
};
return (
{showCaptcha && (
showModal={true}
onVerify={handleVerification}
onCloseModal={() => setShowCaptcha(false)}
/>
)}
);
}
`
$3
`tsx
import React, { useState, useRef } from 'react';
import DragPuzzleCaptcha, {
DragPuzzleCaptchaProps,
DragPuzzleCaptchaRef
} from 'drag-puzzle-captcha';
import 'drag-puzzle-captcha/DragPuzzleCaptcha.css';
const MyComponent: React.FC = () => {
const [isVerified, setIsVerified] = useState(false);
const captchaRef = useRef(null);
const handleVerification = (success: boolean) => {
setIsVerified(success);
};
const resetCaptcha = () => {
captchaRef.current?.reset();
};
return (
ref={captchaRef}
onVerify={handleVerification}
language="eng"
/>
);
};
`
š API
$3
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| onVerify | (success: boolean) => void | - | Callback function called when verification completes |
| language | 'eng' \| 'fr' | 'eng' | Language for component text |
| showModal | boolean | false | Whether to display the component in a modal |
| onCloseModal | () => void | - | Callback when modal is closed |
$3
| Method | Type | Description |
|--------|------|-------------|
| reset | () => void | Reset the puzzle to initial state |
| isVerified | () => boolean | Check if puzzle is currently verified |
šØ Styling
The component comes with default styles, but you can customize the appearance:
`css
/ Override default styles /
.drag-puzzle-container {
--primary-color: #your-color;
--background-color: #your-bg;
}
.drag-puzzle-piece {
border-radius: 12px; / Custom border radius /
}
.drag-puzzle-track {
background: your-custom-gradient;
}
`
š Internationalization
Currently supports:
- English ('eng')
- French ('fr')
`jsx
`
ā” Performance Tips
1. Lazy Loading: Use dynamic imports for better bundle splitting
2. CSS Optimization: Import styles only where needed
3. SSR: Use ssr: false` for Next.js to avoid hydration issues