Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flexible size #32

Merged
merged 7 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `onSuccess` callback with the additional `preClearanceObtained` argument
- `flexible` option for `size` prop

### Changed

- Update `turnstile-types` to v1.2.3

## [1.1.3] - 2024-02-19

Expand Down
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@types/react": "^18.0.21",
"prettier": "^2.7.1",
"turnstile-types": "^1.1.3",
"turnstile-types": "^1.2.3",
"typescript": "^4.8.4"
}
}
42 changes: 18 additions & 24 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useState, useRef } from "react";
import {
TurnstileObject,
TurnstileOptions,
SupportedLanguages,
RenderParameters,
} from "turnstile-types";

const globalNamespace = (
Expand Down Expand Up @@ -52,7 +52,7 @@ const turnstileLoadPromise = new Promise((resolve, reject) => {
export default function Turnstile({
id,
className,
style,
style: customStyle,
sitekey,
action,
cData,
Expand Down Expand Up @@ -94,6 +94,15 @@ export default function Turnstile({

const ref = userRef ?? ownRef;

const style = fixedSize
? {
width:
size === "compact" ? "130px" : size === "flexible" ? "100%" : "300px",
height: size === "compact" ? "120px" : "65px",
...customStyle,
}
: customStyle;

useEffect(() => {
if (!ref.current) return;
let cancelled = false;
Expand All @@ -110,7 +119,7 @@ export default function Turnstile({
}
if (cancelled || !ref.current) return;
let boundTurnstileObject: BoundTurnstileObject;
const turnstileOptions: TurnstileOptions = {
const turnstileOptions: RenderParameters = {
sitekey,
action,
cData,
Expand All @@ -130,7 +139,7 @@ export default function Turnstile({
inplaceState.onSuccess?.(
token,
preClearanceObtained,
boundTurnstileObject,
boundTurnstileObject
);
},
"error-callback": (error?: any) =>
Expand Down Expand Up @@ -193,22 +202,7 @@ export default function Turnstile({
onUnsupported,
]);

return (
<div
ref={ref}
id={id}
className={className}
style={
fixedSize
? {
...(style ?? {}),
width: size === "compact" ? "130px" : "300px",
height: size === "compact" ? "120px" : "65px",
Le0Developer marked this conversation as resolved.
Show resolved Hide resolved
}
: style
}
/>
);
return <div ref={ref} id={id} className={className} style={style} />;
}

export interface TurnstileProps extends TurnstileCallbacks {
Expand All @@ -220,7 +214,7 @@ export interface TurnstileProps extends TurnstileCallbacks {
tabIndex?: number;
responseField?: boolean;
responseFieldName?: string;
size?: "normal" | "invisible" | "compact";
size?: "normal" | "compact" | "flexible" | "invisible";
fixedSize?: boolean;
retry?: "auto" | "never";
retryInterval?: number;
Expand All @@ -238,12 +232,12 @@ export interface TurnstileCallbacks {
onSuccess?: (
token: string,
preClearanceObtained: boolean,
boundTurnstile: BoundTurnstileObject,
boundTurnstile: BoundTurnstileObject
) => void;
onLoad?: (widgetId: string, boundTurnstile: BoundTurnstileObject) => void;
onError?: (
error?: Error | any,
boundTurnstile?: BoundTurnstileObject,
boundTurnstile?: BoundTurnstileObject
) => void;
onExpire?: (token: string, boundTurnstile: BoundTurnstileObject) => void;
onTimeout?: (boundTurnstile: BoundTurnstileObject) => void;
Expand All @@ -253,7 +247,7 @@ export interface TurnstileCallbacks {
}

export interface BoundTurnstileObject {
execute: (options?: TurnstileOptions) => void;
execute: (options?: RenderParameters) => void;
reset: () => void;
getResponse: () => void;
isExpired: () => boolean;
Expand Down
Loading