Skip to content

Commit 133408a

Browse files
add flexible size (#32)
1 parent 6fd8164 commit 133408a

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- `onSuccess` callback with the additional `preClearanceObtained` argument
13+
- `flexible` option for `size` prop
14+
15+
### Changed
16+
17+
- Update `turnstile-types` to v1.2.3
1318

1419
## [1.1.3] - 2024-02-19
1520

package-lock.json

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"@types/react": "^18.0.21",
2929
"prettier": "^2.7.1",
30-
"turnstile-types": "^1.1.3",
30+
"turnstile-types": "^1.2.3",
3131
"typescript": "^4.8.4"
3232
}
3333
}

src/index.tsx

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useEffect, useState, useRef } from "react";
22
import {
33
TurnstileObject,
4-
TurnstileOptions,
54
SupportedLanguages,
5+
RenderParameters,
66
} from "turnstile-types";
77

88
const globalNamespace = (
@@ -52,7 +52,7 @@ const turnstileLoadPromise = new Promise((resolve, reject) => {
5252
export default function Turnstile({
5353
id,
5454
className,
55-
style,
55+
style: customStyle,
5656
sitekey,
5757
action,
5858
cData,
@@ -94,6 +94,15 @@ export default function Turnstile({
9494

9595
const ref = userRef ?? ownRef;
9696

97+
const style = fixedSize
98+
? {
99+
width:
100+
size === "compact" ? "130px" : size === "flexible" ? "100%" : "300px",
101+
height: size === "compact" ? "120px" : "65px",
102+
...customStyle,
103+
}
104+
: customStyle;
105+
97106
useEffect(() => {
98107
if (!ref.current) return;
99108
let cancelled = false;
@@ -110,7 +119,7 @@ export default function Turnstile({
110119
}
111120
if (cancelled || !ref.current) return;
112121
let boundTurnstileObject: BoundTurnstileObject;
113-
const turnstileOptions: TurnstileOptions = {
122+
const turnstileOptions: RenderParameters = {
114123
sitekey,
115124
action,
116125
cData,
@@ -130,7 +139,7 @@ export default function Turnstile({
130139
inplaceState.onSuccess?.(
131140
token,
132141
preClearanceObtained,
133-
boundTurnstileObject,
142+
boundTurnstileObject
134143
);
135144
},
136145
"error-callback": (error?: any) =>
@@ -193,22 +202,7 @@ export default function Turnstile({
193202
onUnsupported,
194203
]);
195204

196-
return (
197-
<div
198-
ref={ref}
199-
id={id}
200-
className={className}
201-
style={
202-
fixedSize
203-
? {
204-
...(style ?? {}),
205-
width: size === "compact" ? "130px" : "300px",
206-
height: size === "compact" ? "120px" : "65px",
207-
}
208-
: style
209-
}
210-
/>
211-
);
205+
return <div ref={ref} id={id} className={className} style={style} />;
212206
}
213207

214208
export interface TurnstileProps extends TurnstileCallbacks {
@@ -220,7 +214,7 @@ export interface TurnstileProps extends TurnstileCallbacks {
220214
tabIndex?: number;
221215
responseField?: boolean;
222216
responseFieldName?: string;
223-
size?: "normal" | "invisible" | "compact";
217+
size?: "normal" | "compact" | "flexible" | "invisible";
224218
fixedSize?: boolean;
225219
retry?: "auto" | "never";
226220
retryInterval?: number;
@@ -238,12 +232,12 @@ export interface TurnstileCallbacks {
238232
onSuccess?: (
239233
token: string,
240234
preClearanceObtained: boolean,
241-
boundTurnstile: BoundTurnstileObject,
235+
boundTurnstile: BoundTurnstileObject
242236
) => void;
243237
onLoad?: (widgetId: string, boundTurnstile: BoundTurnstileObject) => void;
244238
onError?: (
245239
error?: Error | any,
246-
boundTurnstile?: BoundTurnstileObject,
240+
boundTurnstile?: BoundTurnstileObject
247241
) => void;
248242
onExpire?: (token: string, boundTurnstile: BoundTurnstileObject) => void;
249243
onTimeout?: (boundTurnstile: BoundTurnstileObject) => void;
@@ -253,7 +247,7 @@ export interface TurnstileCallbacks {
253247
}
254248

255249
export interface BoundTurnstileObject {
256-
execute: (options?: TurnstileOptions) => void;
250+
execute: (options?: RenderParameters) => void;
257251
reset: () => void;
258252
getResponse: () => void;
259253
isExpired: () => boolean;

0 commit comments

Comments
 (0)