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 3 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
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.2",
"typescript": "^4.8.4"
}
}
24 changes: 14 additions & 10 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,12 @@ export default function Turnstile({

const ref = userRef ?? ownRef;

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

useEffect(() => {
if (!ref.current) return;
let cancelled = false;
Expand All @@ -110,7 +116,7 @@ export default function Turnstile({
}
if (cancelled || !ref.current) return;
let boundTurnstileObject: BoundTurnstileObject;
const turnstileOptions: TurnstileOptions = {
const turnstileOptions: RenderParameters = {
sitekey,
action,
cData,
Expand All @@ -135,8 +141,8 @@ export default function Turnstile({
},
"error-callback": (error?: any) =>
inplaceState.onError?.(error, boundTurnstileObject),
"expired-callback": (token: string) =>
inplaceState.onExpire?.(token, boundTurnstileObject),
"expired-callback": () =>
inplaceState.onExpire?.(boundTurnstileObject),
"timeout-callback": () =>
inplaceState.onTimeout?.(boundTurnstileObject),
"after-interactive-callback": () =>
Expand Down Expand Up @@ -202,8 +208,6 @@ export default function Turnstile({
fixedSize
? {
...(style ?? {}),
width: size === "compact" ? "130px" : "300px",
height: size === "compact" ? "120px" : "65px",
Le0Developer marked this conversation as resolved.
Show resolved Hide resolved
}
: style
}
Expand All @@ -220,7 +224,7 @@ export interface TurnstileProps extends TurnstileCallbacks {
tabIndex?: number;
responseField?: boolean;
responseFieldName?: string;
size?: "normal" | "invisible" | "compact";
size?: "normal" | "compact" | "flexible";
fixedSize?: boolean;
retry?: "auto" | "never";
retryInterval?: number;
Expand All @@ -245,15 +249,15 @@ export interface TurnstileCallbacks {
error?: Error | any,
boundTurnstile?: BoundTurnstileObject,
) => void;
onExpire?: (token: string, boundTurnstile: BoundTurnstileObject) => void;
onExpire?: (boundTurnstile: BoundTurnstileObject) => void;
onTimeout?: (boundTurnstile: BoundTurnstileObject) => void;
onAfterInteractive?: (boundTurnstile: BoundTurnstileObject) => void;
onBeforeInteractive?: (boundTurnstile: BoundTurnstileObject) => void;
onUnsupported?: (boundTurnstile: BoundTurnstileObject) => void;
}

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