Skip to content

Commit

Permalink
add flexible size (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-clemente authored Sep 24, 2024
1 parent 6fd8164 commit 133408a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
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",
}
: 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

0 comments on commit 133408a

Please sign in to comment.