@pushwoosh/dumb-components 4.3.3 → 4.4.0

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.
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { autoUpdate, flip, offset, shift, useFloating } from '@floating-ui/react-dom';
3
- import { useEffect, useState } from 'react';
3
+ import { useState } from 'react';
4
4
  import { createPortal } from 'react-dom';
5
5
  import { Color } from '@pushwoosh/kit-constants';
6
6
  import { Horizontal, Vertical } from '@pushwoosh/kit-helpers';
@@ -45,12 +45,18 @@ export const CalendarDropdown = props => {
45
45
  const [value, setValue] = useState(props.value ?? (isRangePicker ? [] : undefined));
46
46
  const initialTimezone = calendarProps.timezone || 'UTC';
47
47
  const [timezone, setTimezone] = useState(initialTimezone);
48
- useEffect(() => {
48
+ const valueDeps = [props.value, isRangePicker];
49
+ const [prevValueDeps, setPrevValueDeps] = useState(null);
50
+ if (prevValueDeps === null || valueDeps.some((dep, index) => dep !== prevValueDeps[index])) {
51
+ setPrevValueDeps(valueDeps);
49
52
  setValue(props.value ?? (isRangePicker ? [] : undefined));
50
- }, [props.value, isRangePicker]);
51
- useEffect(() => {
53
+ }
54
+ const timezoneDeps = [calendarProps.timezone];
55
+ const [prevTimezoneDeps, setPrevTimezoneDeps] = useState(null);
56
+ if (prevTimezoneDeps === null || timezoneDeps.some((dep, index) => dep !== prevTimezoneDeps[index])) {
57
+ setPrevTimezoneDeps(timezoneDeps);
52
58
  setTimezone(calendarProps.timezone || 'UTC');
53
- }, [calendarProps.timezone]);
59
+ }
54
60
  const closePopover = () => {
55
61
  setIsOpen(false);
56
62
  setValue(props.value ?? (isRangePicker ? [] : undefined));
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useState, useEffect } from 'react';
2
+ import { useState } from 'react';
3
3
  import { Color } from '@pushwoosh/kit-constants';
4
4
  import { Spinner } from '../Spinner';
5
5
  import { RootBox, ImgPreview, PlaceholderWrapper } from './styles';
@@ -21,12 +21,15 @@ export function ExternalImagePreview(props) {
21
21
  } = props;
22
22
  const [isLoading, setIsLoading] = useState(false);
23
23
  const [isError, setIsError] = useState(false);
24
- useEffect(() => {
24
+ const srcDeps = [src];
25
+ const [prevSrcDeps, setPrevSrcDeps] = useState(null);
26
+ if (prevSrcDeps === null || srcDeps.some((dep, index) => dep !== prevSrcDeps[index])) {
27
+ setPrevSrcDeps(srcDeps);
25
28
  if (src) {
26
29
  setIsLoading(true);
27
30
  setIsError(false);
28
31
  }
29
- }, [src]);
32
+ }
30
33
  const handleOnError = error => {
31
34
  setIsLoading(false);
32
35
  setIsError(true);
@@ -36,9 +36,12 @@ export function NumberPicker({
36
36
  onChange(newValue);
37
37
  }
38
38
  });
39
- useEffect(() => {
39
+ const valueDeps = [value];
40
+ const [prevValueDeps, setPrevValueDeps] = useState(null);
41
+ if (prevValueDeps === null || valueDeps.some((dep, index) => dep !== prevValueDeps[index])) {
42
+ setPrevValueDeps(valueDeps);
40
43
  setLocalValue(value !== undefined ? value.toString() : '');
41
- }, [value]);
44
+ }
42
45
  useEffect(() => {
43
46
  if (!autoDiff) {
44
47
  return;
@@ -27,9 +27,12 @@ export function SandboxedHtmlFrame(props) {
27
27
  const onReady = mode === 'inspectable' ? props.onReady : undefined;
28
28
  const $ref = useRef(null);
29
29
  const [isReady, setIsReady] = useState(false);
30
- useEffect(() => {
30
+ const htmlDeps = [html];
31
+ const [prevHtmlDeps, setPrevHtmlDeps] = useState(null);
32
+ if (prevHtmlDeps === null || htmlDeps.some((dep, index) => dep !== prevHtmlDeps[index])) {
33
+ setPrevHtmlDeps(htmlDeps);
31
34
  setIsReady(false);
32
- }, [html]);
35
+ }
33
36
  const handleLoad = useCallback(() => {
34
37
  setIsReady(true);
35
38
  }, []);
@@ -52,11 +52,14 @@ export function Tooltip({
52
52
  assignRef(originalRef, node);
53
53
  }, [originalRef]);
54
54
  const externalReferenceEl = reference?.current ?? null;
55
- useEffect(() => {
55
+ const referenceDeps = [reference, externalReferenceEl];
56
+ const [prevReferenceDeps, setPrevReferenceDeps] = useState(null);
57
+ if (prevReferenceDeps === null || referenceDeps.some((dep, index) => dep !== prevReferenceDeps[index])) {
58
+ setPrevReferenceDeps(referenceDeps);
56
59
  if (reference) {
57
60
  setReferenceEl(externalReferenceEl);
58
61
  }
59
- }, [reference, externalReferenceEl]);
62
+ }
60
63
  useEffect(() => {
61
64
  setReference(referenceEl);
62
65
  }, [referenceEl, setReference]);
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useCallback, useEffect, useState } from 'react';
2
+ import { useCallback, useState } from 'react';
3
3
  import { Color } from '@pushwoosh/kit-constants';
4
4
  import { Columns, Vertical } from '@pushwoosh/kit-helpers';
5
5
  import { ImageIcon } from '@pushwoosh/kit-icons';
@@ -26,7 +26,12 @@ export function WhatsAppAttachment(props) {
26
26
  const [hasFailed, setHasFailed] = useState(false);
27
27
  const kind = resolveAttachmentKind(attachment);
28
28
  const extension = getAttachmentExtension(attachment);
29
- useEffect(() => setHasFailed(false), [source, thumbnailSource]);
29
+ const sourceDeps = [source, thumbnailSource];
30
+ const [prevSourceDeps, setPrevSourceDeps] = useState(null);
31
+ if (prevSourceDeps === null || sourceDeps.some((dep, index) => dep !== prevSourceDeps[index])) {
32
+ setPrevSourceDeps(sourceDeps);
33
+ setHasFailed(false);
34
+ }
30
35
  const handleError = useCallback(() => setHasFailed(true), []);
31
36
  const handleLoadedMetadata = useCallback(event => {
32
37
  const {
package/hooks/useLayer.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
1
+ import { useId, useLayoutEffect, useRef, useSyncExternalStore } from 'react';
2
2
  const BASE_Z_INDEX = 10000;
3
3
  const Z_INDEX_STEP = 100;
4
4
  const GLOBAL_KEY = '__pushwoosh_dumb_components_layer_state__';
@@ -80,6 +80,12 @@ function resolveZIndex(id, fallbackZIndex) {
80
80
  }
81
81
  return fallbackZIndex ?? BASE_Z_INDEX;
82
82
  }
83
+ function subscribeToLayers(onChange) {
84
+ getState().subscribers.add(onChange);
85
+ return () => {
86
+ getState().subscribers.delete(onChange);
87
+ };
88
+ }
83
89
  /**
84
90
  * Manages a stacked overlay layer (modals, drawers, popovers): registers while
85
91
  * `isOpen`, computes a stacking `zIndex` above lower layers, and calls `onClose` on
@@ -97,14 +103,6 @@ export function useLayer({
97
103
  onCloseRef.current = onClose;
98
104
  const zIndexRef = useRef(zIndex);
99
105
  zIndexRef.current = zIndex;
100
- const [, forceRender] = useState(0);
101
- useEffect(() => {
102
- const subscriber = () => forceRender(value => value + 1);
103
- getState().subscribers.add(subscriber);
104
- return () => {
105
- getState().subscribers.delete(subscriber);
106
- };
107
- }, []);
108
106
  useLayoutEffect(() => {
109
107
  if (!isOpen) {
110
108
  return undefined;
@@ -114,7 +112,6 @@ export function useLayer({
114
112
  close: () => onCloseRef.current?.(),
115
113
  baseZIndex: zIndexRef.current
116
114
  });
117
- forceRender(value => value + 1);
118
115
  return () => unregisterLayer(id);
119
116
  }, [isOpen, id]);
120
117
  useLayoutEffect(() => {
@@ -124,7 +121,7 @@ export function useLayer({
124
121
  notify();
125
122
  }
126
123
  }, [zIndex, id]);
127
- const resolvedZIndex = resolveZIndex(id, zIndex);
124
+ const resolvedZIndex = useSyncExternalStore(subscribeToLayers, () => resolveZIndex(id, zIndex));
128
125
  return {
129
126
  zIndex: resolvedZIndex
130
127
  };
package/index.d.ts CHANGED
@@ -19,7 +19,6 @@ export { ProgressBar } from './ProgressBar';
19
19
  export { Popover } from './Popover';
20
20
  export { Radio } from './Radio';
21
21
  export { RadioCard } from './RadioCard';
22
- export { ReCaptcha, useRecaptcha, asyncScriptLoad } from './ReCaptcha';
23
22
  export { Spinner } from './Spinner';
24
23
  export { SortButton, type SortButtonParams } from './SortButton';
25
24
  export { SplitButton, type SplitButtonProps } from './SplitButton';
package/index.js CHANGED
@@ -19,7 +19,6 @@ export { ProgressBar } from './ProgressBar';
19
19
  export { Popover } from './Popover';
20
20
  export { Radio } from './Radio';
21
21
  export { RadioCard } from './RadioCard';
22
- export { ReCaptcha, useRecaptcha, asyncScriptLoad } from './ReCaptcha';
23
22
  export { Spinner } from './Spinner';
24
23
  export { SortButton } from './SortButton';
25
24
  export { SplitButton } from './SplitButton';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/dumb-components",
3
- "version": "4.3.3",
3
+ "version": "4.4.0",
4
4
  "description": "React components to build Pushwoosh products",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -1,16 +1,19 @@
1
- import { useEffect, useState } from 'react';
1
+ import { useState } from 'react';
2
2
  /**
3
3
  * Holds the active (highlighted) row index, clamping it to `total` and resetting to
4
4
  * `resetIndex` when the list changes.
5
5
  */
6
6
  export function useActiveIndex(total, resetIndex) {
7
7
  const [activeIndex, setActiveIndex] = useState(resetIndex);
8
- useEffect(() => {
9
- setActiveIndex(prev => {
10
- if (total === 0) return -1;
11
- if (prev < 0 || prev >= total) return resetIndex;
12
- return prev;
13
- });
14
- }, [total, resetIndex]);
8
+ const clampDeps = [total, resetIndex];
9
+ const [prevClampDeps, setPrevClampDeps] = useState(null);
10
+ if (prevClampDeps === null || clampDeps.some((dep, index) => dep !== prevClampDeps[index])) {
11
+ setPrevClampDeps(clampDeps);
12
+ if (total === 0) {
13
+ setActiveIndex(-1);
14
+ } else if (activeIndex < 0 || activeIndex >= total) {
15
+ setActiveIndex(resetIndex);
16
+ }
17
+ }
15
18
  return [activeIndex, setActiveIndex];
16
19
  }
@@ -2,9 +2,11 @@ import { useEffect, useState } from 'react';
2
2
  /** Returns `value` debounced by `delay` ms (updates immediately when `delay <= 0`). */
3
3
  export function useDebouncedValue(value, delay) {
4
4
  const [debounced, setDebounced] = useState(value);
5
+ if (delay <= 0 && debounced !== value) {
6
+ setDebounced(value);
7
+ }
5
8
  useEffect(() => {
6
9
  if (delay <= 0) {
7
- setDebounced(value);
8
10
  return undefined;
9
11
  }
10
12
  const id = setTimeout(() => setDebounced(value), delay);
@@ -1,10 +0,0 @@
1
- import { type ReactElement } from 'react';
2
- /**
3
- * Renders the (invisible) reCAPTCHA host element. Pair with {@link useRecaptcha} to
4
- * load the script and obtain tokens.
5
- *
6
- * @example
7
- * const { getRecaptchaToken } = useRecaptcha();
8
- * return <ReCaptcha />;
9
- */
10
- export declare function ReCaptcha(): ReactElement;
@@ -1,16 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { RECAPTCHA_CONTAINER_ID } from './constants';
3
- import { RecaptchaElement } from './styles';
4
- /**
5
- * Renders the (invisible) reCAPTCHA host element. Pair with {@link useRecaptcha} to
6
- * load the script and obtain tokens.
7
- *
8
- * @example
9
- * const { getRecaptchaToken } = useRecaptcha();
10
- * return <ReCaptcha />;
11
- */
12
- export function ReCaptcha() {
13
- return _jsx(RecaptchaElement, {
14
- id: RECAPTCHA_CONTAINER_ID
15
- });
16
- }
@@ -1,3 +0,0 @@
1
- export declare const RECAPTCHA_KEY = "6LdS7DYUAAAAABVgt0djIBoyyMQ0oDAUl7jt1Mxh";
2
- export declare const RECAPTCHA_SCRIPT_URL = "https://www.google.com/recaptcha/api.js?render=explicit";
3
- export declare const RECAPTCHA_CONTAINER_ID = "g-recaptcha";
@@ -1,4 +0,0 @@
1
- export const RECAPTCHA_KEY = '6LdS7DYUAAAAABVgt0djIBoyyMQ0oDAUl7jt1Mxh';
2
- // onload script: https://www.google.com/recaptcha/api.js?onload=recaptchaOnLoad&render=explicit'
3
- export const RECAPTCHA_SCRIPT_URL = 'https://www.google.com/recaptcha/api.js?render=explicit';
4
- export const RECAPTCHA_CONTAINER_ID = 'g-recaptcha';
@@ -1,5 +0,0 @@
1
- /**
2
- * Injects a `<script>` with the given `src` once and resolves when it loads
3
- * (resolves immediately if already present); rejects on load error.
4
- */
5
- export declare function asyncScriptLoad(scriptUrl: string): Promise<Event | void>;
@@ -1,19 +0,0 @@
1
- /**
2
- * Injects a `<script>` with the given `src` once and resolves when it loads
3
- * (resolves immediately if already present); rejects on load error.
4
- */
5
- export async function asyncScriptLoad(scriptUrl) {
6
- return new Promise((resolve, reject) => {
7
- const isAlreadyLoaded = document.querySelector(`script[src="${scriptUrl}"]`);
8
- if (isAlreadyLoaded) {
9
- resolve();
10
- return;
11
- }
12
- const script = document.createElement('script');
13
- script.src = scriptUrl;
14
- script.async = true;
15
- document.body.appendChild(script);
16
- script.onload = resolve;
17
- script.onerror = reject;
18
- });
19
- }
@@ -1,6 +0,0 @@
1
- import type { RecaptchaExposedApi } from './types';
2
- /**
3
- * Loads the reCAPTCHA script, renders an invisible widget into the {@link ReCaptcha}
4
- * element, and returns `{ isRecaptchaReady, recaptchaErrorCode, getRecaptchaToken }`.
5
- */
6
- export declare function useRecaptcha(): RecaptchaExposedApi;
@@ -1,65 +0,0 @@
1
- import { useState, useEffect, useCallback, useRef } from 'react';
2
- import { RECAPTCHA_KEY, RECAPTCHA_SCRIPT_URL, RECAPTCHA_CONTAINER_ID } from './constants';
3
- import { asyncScriptLoad } from './helpers';
4
- /**
5
- * Loads the reCAPTCHA script, renders an invisible widget into the {@link ReCaptcha}
6
- * element, and returns `{ isRecaptchaReady, recaptchaErrorCode, getRecaptchaToken }`.
7
- */
8
- export function useRecaptcha() {
9
- const promiseRef = useRef({});
10
- const [isRecaptchaLoaded, setRecaptchaLoaded] = useState(!!window?.grecaptcha?.render);
11
- const [widgetID, setWidgetID] = useState(null);
12
- const [isRecaptchaReady, setRecaptchaReady] = useState(!!(isRecaptchaLoaded && widgetID));
13
- const [recaptchaErrorCode, setRecaptchaErrorCode] = useState(''); // RIEXXX - ReCaptcha Internal Error
14
- const [intervalID, setIntervalID] = useState(0);
15
- // Load external script effect, fires once
16
- useEffect(() => {
17
- asyncScriptLoad(RECAPTCHA_SCRIPT_URL).catch(() => setRecaptchaErrorCode('RIE001'));
18
- }, []);
19
- // Set interval while library download and initialize
20
- useEffect(() => {
21
- const newInterval = window.setInterval(() => {
22
- if (window?.grecaptcha?.render) {
23
- setRecaptchaLoaded(true);
24
- }
25
- }, 500);
26
- setIntervalID(newInterval);
27
- return () => {
28
- clearInterval(newInterval);
29
- };
30
- }, []);
31
- // Render recaptcha widget when script loaded, stop interval
32
- useEffect(() => {
33
- if (isRecaptchaLoaded && widgetID === null) {
34
- clearInterval(intervalID);
35
- try {
36
- const widget = window.grecaptcha.render(RECAPTCHA_CONTAINER_ID, {
37
- sitekey: RECAPTCHA_KEY,
38
- size: 'invisible',
39
- callback: token => promiseRef.current?.resolve(token)
40
- // 'expired-callback': () => {},
41
- // 'error-callback': () => {},
42
- });
43
- setWidgetID(widget);
44
- setRecaptchaReady(true);
45
- } catch {
46
- setRecaptchaErrorCode('RIE002');
47
- }
48
- }
49
- }, [isRecaptchaLoaded, widgetID, intervalID, promiseRef]);
50
- // async function to get recaptcha token, resolves by resolveAction callback
51
- const getRecaptchaToken = useCallback(async () => new Promise((resolve, reject) => {
52
- try {
53
- window.grecaptcha.reset(widgetID);
54
- window.grecaptcha.execute(widgetID);
55
- promiseRef.current.resolve = resolve;
56
- } catch (error) {
57
- reject(error);
58
- }
59
- }), [widgetID, promiseRef]);
60
- return {
61
- isRecaptchaReady,
62
- recaptchaErrorCode,
63
- getRecaptchaToken
64
- };
65
- }
@@ -1,3 +0,0 @@
1
- export { ReCaptcha } from './ReCaptcha';
2
- export { useRecaptcha } from './hooks';
3
- export { asyncScriptLoad } from './helpers';
@@ -1,3 +0,0 @@
1
- export { ReCaptcha } from './ReCaptcha';
2
- export { useRecaptcha } from './hooks';
3
- export { asyncScriptLoad } from './helpers';
@@ -1,3 +0,0 @@
1
- export declare const RecaptchaElement: import("styled-components/dist/types").IStyledComponentBase<"web", Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "style"> & {
2
- style?: import("react").CSSProperties | import("styled-components/dist/types").CSSPropertiesWithVars | undefined;
3
- }> & string;
@@ -1,5 +0,0 @@
1
- import styled from 'styled-components';
2
- export const RecaptchaElement = styled.div.withConfig({
3
- displayName: "RecaptchaElement",
4
- componentId: "sc-100g1t3-0"
5
- })(["display:none;"]);
@@ -1,13 +0,0 @@
1
- /** Value returned by {@link useRecaptcha}. */
2
- export type RecaptchaExposedApi = {
3
- /** Whether the widget is rendered and ready to execute. */
4
- readonly isRecaptchaReady: boolean;
5
- /** Internal error code (`RIExxx`), or empty string when there's no error. */
6
- readonly recaptchaErrorCode: string;
7
- /** Runs an invisible challenge and resolves with the verification token. */
8
- readonly getRecaptchaToken: () => Promise<string>;
9
- };
10
- /** Holds the pending token resolver between `execute` and the reCAPTCHA callback. */
11
- export type PromiseRef = {
12
- resolve?: (token: string) => void;
13
- };
@@ -1 +0,0 @@
1
- export {};