0.1.2 • Published 3 years ago

react-custom-captcha v0.1.2

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

react-custom-captcha

A simple but powerfully and highly customizable captcha code component without a single package dependency, except React itself.

Installation

Via npm:

$ npm install react-custom-captcha

via yarn:

$ yarn add react-custom-captcha

Usage

App.js: Add CaptchaProvider

...
import { CaptchaProvider } from "react-custom-captcha";
...
    return(
        ...
            <CaptchaProvider length={...} format={...} {...moreOptions} >
                ...
            </CaptchaProvider>
        ...
        );
...

MyComponent.js: Add Captcha and useCaptcha

...
import { Captcha, useCaptcha } from "react-custom-captcha";
import { useState, useCallback } from "react";
...

const { validate, refresh } = useCaptcha();
const [captcha, setCaptcha] = useState("");
...
const onSubmitHandler = useCallback(
    (event) => {
      const isValidCaptcha = validate(captcha);
      setCaptcha("");
      ...
      event.preventDefault();
    },
    [captcha, validate]
  );
...
  const onRefreshHandler = useCallback(() => {
    refresh();
  }, [refresh]);
...
    return(
        ...
            <Captcha bgColor={...} width={...} height={...} {...moreOptions}/>
            ...
             <button onClick={onRefreshHandler}>Refresh captcha</button>
            ...
            <input
                type="text"
                required={true}
                placeholder="enter captcha code"
                value={captcha}
                onChange={(event) => setCaptcha(event.target.value)}
            />
            ...
            <button onClick={onSubmitHandler}>Submit</button>
        ...
        );
...

Configuration Options

PropertyREADMECaptchaProviderCaptchaDefault Value
lengthcharacter count of code-5
formatcharset to be used (see details below)-a1
widthcanvas width (in px)150
heightcanvas height (in px)50
bgcolorbgcolor of canvas (see details below)#eee
titletext displayed on mouse over (click to refresh)Refresh code
colorscolor(s) of text, lines and circles (see details below)8
fontsused font(s) (see details below)8
resizevary font size of chars (boolean)true
rotaterotate characters (boolean)true
verticalmove characters vertically (boolean)true
horizontalmove characters horizontally (boolean)true
shadowdraw shadow for characters (boolean)true
linesnumber of lines in the background (integer)5
circlesnumber of circles in the background (integer)5