@thecb/components 11.3.2-beta.0 → 11.3.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "11.3.2-beta.0",
3
+ "version": "11.3.2-beta.1",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -3,7 +3,8 @@ import React, {
3
3
  useEffect,
4
4
  useRef,
5
5
  forwardRef,
6
- useImperativeHandle
6
+ useImperativeHandle,
7
+ useCallback
7
8
  } from "react";
8
9
  import { Box } from "../../atoms/layouts";
9
10
  import Text from "../../atoms/text";
@@ -15,10 +16,9 @@ import { ERROR_COLOR } from "../../../constants/colors";
15
16
  const TurnstileWidgetContainer = styled(Box)`
16
17
  display: flex;
17
18
  flex-direction: column;
18
- align-items: flex-end;
19
19
  padding: ${({ padding }) => padding};
20
20
  justify-content: ${({ justify }) => justify};
21
- align-items: ${({ justify }) => justify};
21
+ align-items: ${({ align }) => align};
22
22
  width: 100%;
23
23
  `;
24
24
 
@@ -53,6 +53,7 @@ const TurnstileWidget = forwardRef(
53
53
  onExpired = noop,
54
54
  padding = "1rem",
55
55
  justify = "flex-end",
56
+ align = "flex-end",
56
57
  size = "normal",
57
58
  tabindex = 0,
58
59
  retry = "never",
@@ -107,44 +108,62 @@ const TurnstileWidget = forwardRef(
107
108
  return "Something went wrong. Please refresh and try again.";
108
109
  };
109
110
 
111
+ // Reset the Turnstile widget when requested
112
+ const resetWidget = useCallback(() => {
113
+ if (widgetIdRef.current && window.turnstile) {
114
+ window.turnstile.reset(widgetIdRef.current);
115
+ clearError();
116
+ }
117
+ }, [clearError]);
118
+
110
119
  // Allow the parent component to reset the Turnstile widget
111
120
  useImperativeHandle(
112
121
  ref,
113
122
  () => ({
114
- reset: () => {
115
- if (widgetIdRef.current && window.turnstile) {
116
- window.turnstile.reset(widgetIdRef.current);
117
- clearError();
118
- }
119
- }
123
+ reset: resetWidget
120
124
  }),
121
- []
125
+ [resetWidget]
122
126
  );
123
127
 
124
128
  useEffect(() => {
129
+ if (scriptError) {
130
+ setError(
131
+ "Unable to load security verification. Please refresh the page."
132
+ );
133
+ onError?.({ type: "script_load_failed", error: scriptError });
134
+ return;
135
+ }
136
+
125
137
  if (widgetIdRef.current || !window.turnstile || !scriptLoaded) return;
126
138
 
127
- widgetIdRef.current = window.turnstile.render(widgetRef.current, {
128
- sitekey: siteKey,
129
- size: size,
130
- retry: retry,
131
- tabindex: tabindex,
132
- theme: theme,
133
- callback: token => {
134
- clearError();
135
- onSuccess(token);
136
- },
137
- "error-callback": errorCode => {
138
- if (retry === "never") {
139
- handleError(errorCode);
139
+ try {
140
+ widgetIdRef.current = window.turnstile.render(widgetRef.current, {
141
+ sitekey: siteKey,
142
+ size: size,
143
+ retry: retry,
144
+ tabindex: tabindex,
145
+ theme: theme,
146
+ callback: token => {
147
+ clearError();
148
+ onSuccess(token);
149
+ },
150
+ "error-callback": errorCode => {
151
+ if (retry === "never") {
152
+ handleError(errorCode);
153
+ }
154
+ onError?.(errorCode);
155
+ },
156
+ "expired-callback": () => {
157
+ clearError();
158
+ onExpired?.();
140
159
  }
141
- onError?.(errorCode);
142
- },
143
- "expired-callback": () => {
144
- clearError();
145
- onExpired?.();
146
- }
147
- });
160
+ });
161
+ } catch (error) {
162
+ setError(
163
+ "Unable to load security verification. Please refresh the page."
164
+ );
165
+ onError?.({ type: "render_failed", error });
166
+ }
148
167
 
149
168
  return () => {
150
169
  if (widgetIdRef.current && window.turnstile) {
@@ -160,6 +179,7 @@ const TurnstileWidget = forwardRef(
160
179
  <TurnstileWidgetContainer
161
180
  padding={padding}
162
181
  justify={justify}
182
+ align={align}
163
183
  extraStyles={extraStyles}
164
184
  >
165
185
  <div ref={widgetRef} />
@@ -175,18 +195,6 @@ const TurnstileWidget = forwardRef(
175
195
  {error}
176
196
  </Text>
177
197
  )}
178
- {scriptError && (
179
- <Text
180
- color={ERROR_COLOR}
181
- variant="pXS"
182
- extraStyles={`
183
- word-break: break-word;
184
- text-align: end;
185
- `}
186
- >
187
- Unable to load security verification. Please refresh the page.
188
- </Text>
189
- )}
190
198
  </TurnstileWidgetContainer>
191
199
  </>
192
200
  );
@@ -7,7 +7,12 @@ import { useEffect, useState } from "react";
7
7
  */
8
8
  const useTurnstileScript = verifyURL => {
9
9
  const [scriptLoaded, setScriptLoaded] = useState(false);
10
- const [scriptError, setScriptError] = useState(null);
10
+ const [scriptError, setScriptError] = useState(false);
11
+
12
+ const handleScriptError = () => {
13
+ setScriptError(true);
14
+ setScriptLoaded(false);
15
+ };
11
16
 
12
17
  useEffect(() => {
13
18
  if (typeof window === "undefined") {
@@ -25,18 +30,17 @@ const useTurnstileScript = verifyURL => {
25
30
  setScriptLoaded(true);
26
31
  };
27
32
  script.onerror = () => {
28
- setScriptError(true);
29
- setScriptLoaded(false);
33
+ handleScriptError();
30
34
  };
31
35
  script.onabort = () => {
32
- setScriptError(true);
33
- setScriptLoaded(false);
36
+ handleScriptError();
34
37
  };
35
38
  script.defer = true;
36
39
  document.getElementsByTagName("head")[0].appendChild(script);
37
40
 
38
41
  return () => {
39
42
  setScriptLoaded(false);
43
+ setScriptError(false);
40
44
  };
41
45
  }, [verifyURL]);
42
46