Skip to content

Commit 3d10c64

Browse files
authored
fix: respect widget size when appearance is interaction-only (#229)
1 parent 3cc798a commit 3d10c64

4 files changed

Lines changed: 26 additions & 20 deletions

File tree

.changeset/sour-states-give.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@marsidev/react-turnstile": patch
3+
---
4+
5+
fix: respect widget size when appearance is interaction-only
6+
7+
Previously, setting `appearance: "interaction-only"` would ignore the `size` prop and always use a hardcoded `fit-content` width. Now the container respects the chosen size (e.g. `flexible`, `compact`, `normal`) while still collapsing height for the smaller widget.

packages/lib/src/lib.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
6969

7070
const widgetSize = options.size;
7171

72-
const calculateContainerStyle = useCallback(() => {
73-
return typeof widgetSize === "undefined"
74-
? {}
75-
: options.execution === "execute"
76-
? CONTAINER_STYLE_SET.invisible
77-
: options.appearance === "interaction-only"
78-
? CONTAINER_STYLE_SET.interactionOnly
79-
: CONTAINER_STYLE_SET[widgetSize];
72+
const getContainerStyle = useCallback(() => {
73+
if (typeof widgetSize === "undefined") return {};
74+
if (options.execution === "execute") return CONTAINER_STYLE_SET.invisible;
75+
76+
const baseStyle = CONTAINER_STYLE_SET[widgetSize];
77+
78+
if (options.appearance === "interaction-only") {
79+
return { ...baseStyle, height: "auto" };
80+
}
81+
82+
return baseStyle;
8083
}, [options.execution, widgetSize, options.appearance]);
8184

82-
const [containerStyle, setContainerStyle] = useState(calculateContainerStyle());
85+
const [containerStyle, setContainerStyle] = useState(getContainerStyle());
8386
const containerRef = useRef<HTMLElement | null>(null);
8487
const [turnstileLoaded, setTurnstileLoaded] = useState(false);
8588
const widgetId = useRef<string | undefined | null>(undefined);
@@ -344,7 +347,7 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
344347
if (widgetId.current) onWidgetLoad?.(widgetId.current);
345348

346349
if (options.execution !== "execute") {
347-
setContainerStyle(widgetSize ? CONTAINER_STYLE_SET[widgetSize] : {});
350+
setContainerStyle(getContainerStyle());
348351
}
349352

350353
return id;
@@ -367,7 +370,7 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
367370
}
368371

369372
turnstile.execute(containerRef.current);
370-
setContainerStyle(widgetSize ? CONTAINER_STYLE_SET[widgetSize] : {});
373+
setContainerStyle(getContainerStyle());
371374
},
372375

373376
isExpired() {
@@ -387,7 +390,8 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
387390
containerRef,
388391
checkIfTurnstileLoaded,
389392
turnstileLoaded,
390-
onWidgetLoad
393+
onWidgetLoad,
394+
getContainerStyle
391395
]);
392396

393397
/* Set the turnstile as loaded, in case the onload callback never runs. (e.g., when manually injecting the script without specifying the `onload` param) */
@@ -416,7 +420,7 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
416420

417421
// Update style
418422
useEffect(() => {
419-
setContainerStyle(calculateContainerStyle());
423+
setContainerStyle(getContainerStyle());
420424
}, [options.execution, widgetSize, appearance]);
421425

422426
// onLoadScript callback

packages/lib/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export interface InjectTurnstileScriptParams {
243243
}
244244

245245
export type ContainerSizeSet = {
246-
[size in NonNullable<ComponentRenderOptions["size"]> | "interactionOnly"]: React.CSSProperties;
246+
[size in NonNullable<ComponentRenderOptions["size"]>]: React.CSSProperties;
247247
};
248248

249249
/**

packages/lib/src/utils.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ export const CONTAINER_STYLE_SET: ContainerSizeSet = {
105105
minWidth: 300,
106106
width: "100%",
107107
height: 65
108-
},
109-
interactionOnly: {
110-
width: "fit-content",
111-
height: "auto",
112-
display: "flex"
113108
}
114109
};
115110

@@ -121,7 +116,7 @@ export const CONTAINER_STYLE_SET: ContainerSizeSet = {
121116
* @returns
122117
*/
123118
export function getTurnstileSizeOpts(size: keyof ContainerSizeSet | undefined) {
124-
if (size !== "invisible" && size !== "interactionOnly") {
119+
if (size !== "invisible") {
125120
return size;
126121
}
127122

0 commit comments

Comments
 (0)