@onewelcome/react-lib-components 1.1.0 → 1.3.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.
- package/dist/hooks/useDebouncedCallback.d.ts +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/react-lib-components.cjs.development.js +107 -52
- package/dist/react-lib-components.cjs.development.js.map +1 -1
- package/dist/react-lib-components.cjs.production.min.js +1 -1
- package/dist/react-lib-components.cjs.production.min.js.map +1 -1
- package/dist/react-lib-components.esm.js +105 -53
- package/dist/react-lib-components.esm.js.map +1 -1
- package/dist/util/helper.d.ts +2 -0
- package/package.json +1 -1
- package/src/Button/Button.module.scss +9 -0
- package/src/Button/Button.tsx +1 -1
- package/src/Form/Input/Input.module.scss +5 -5
- package/src/Form/Select/Select.module.scss +3 -2
- package/src/Form/Textarea/Textarea.module.scss +1 -1
- package/src/Form/Toggle/Toggle.module.scss +4 -2
- package/src/Form/Wrapper/InputWrapper/InputWrapper.module.scss +1 -1
- package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.module.scss +1 -1
- package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +3 -1
- package/src/Icon/Icon.module.scss +1 -0
- package/src/Notifications/BaseModal/BaseModal.module.scss +3 -1
- package/src/Notifications/SlideInModal/SlideInModal.module.scss +3 -1
- package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +2 -1
- package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +3 -3
- package/src/Popover/Popover.module.scss +7 -4
- package/src/Tabs/Tabs.module.scss +6 -3
- package/src/TextEllipsis/TextEllipsis.module.scss +1 -1
- package/src/Tiles/Tile.module.scss +1 -1
- package/src/Tooltip/Tooltip.module.scss +7 -4
- package/src/_BaseStyling_/BaseStyling.tsx +0 -3
- package/src/hooks/useDebouncedCallback.test.ts +140 -0
- package/src/hooks/useDebouncedCallback.tsx +32 -0
- package/src/index.ts +2 -1
- package/src/mixins.module.scss +21 -5
- package/src/readyclasses.module.scss +8 -0
- package/src/util/helper.test.tsx +128 -1
- package/src/util/helper.tsx +28 -0
package/dist/util/helper.d.ts
CHANGED
|
@@ -3,4 +3,6 @@ declare type KeyValuePair = {
|
|
|
3
3
|
};
|
|
4
4
|
export declare const generateID: (length?: number, stringToWeaveIn?: string) => string;
|
|
5
5
|
export declare const filterProps: (props: any, regexPattern: RegExp, returnFiltered?: boolean) => KeyValuePair;
|
|
6
|
+
export declare const debounce: (fn: (...args: unknown[]) => unknown, delay: number) => (...args: unknown[]) => void;
|
|
7
|
+
export declare const throttle: (fn: (...args: unknown[]) => unknown, delay: number) => () => void;
|
|
6
8
|
export {};
|
package/package.json
CHANGED
package/src/Button/Button.tsx
CHANGED
|
@@ -62,7 +62,7 @@ export const Button = React.forwardRef<HTMLButtonElement, Props>(
|
|
|
62
62
|
ref={ref}
|
|
63
63
|
className={`${classes[color]} ${classes[variant]} ${additionalClasses.join(" ")}`}
|
|
64
64
|
>
|
|
65
|
-
{startIcon && <i>{startIcon}
|
|
65
|
+
{startIcon && <i>{startIcon}</i>}
|
|
66
66
|
<span>{children}</span>
|
|
67
67
|
{endIcon && <i> {endIcon}</i>}
|
|
68
68
|
</BaseButton>
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
border-radius: var(--input-border-radius);
|
|
26
26
|
background-color: var(--input-background-color);
|
|
27
27
|
padding: 0 1.25rem;
|
|
28
|
-
transition
|
|
28
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
29
29
|
|
|
30
30
|
// General autofill styles
|
|
31
31
|
input:-webkit-autofill,
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
box-sizing: border-box;
|
|
77
77
|
border: 0;
|
|
78
78
|
border-radius: var(--input-border-radius);
|
|
79
|
-
transition
|
|
79
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
80
80
|
background-color: transparent;
|
|
81
81
|
z-index: 1;
|
|
82
82
|
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
padding-right: 3.5rem;
|
|
104
104
|
|
|
105
105
|
&.remove-extra-indent {
|
|
106
|
-
padding-right:
|
|
106
|
+
padding-right: 0;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
@@ -113,7 +113,7 @@
|
|
|
113
113
|
height: 100%;
|
|
114
114
|
width: 1.25rem;
|
|
115
115
|
right: 1.25rem;
|
|
116
|
-
top:
|
|
116
|
+
top: 0;
|
|
117
117
|
font-size: 1.125rem;
|
|
118
118
|
display: flex;
|
|
119
119
|
align-items: center;
|
|
@@ -127,7 +127,7 @@
|
|
|
127
127
|
|
|
128
128
|
.input-wrapper [data-prefix],
|
|
129
129
|
.input-wrapper [data-suffix] {
|
|
130
|
-
transition
|
|
130
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
131
131
|
display: block;
|
|
132
132
|
z-index: 1;
|
|
133
133
|
}
|
|
@@ -15,13 +15,14 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
@import "../../readyclasses.module.scss";
|
|
18
|
+
@import "../../mixins.module.scss";
|
|
18
19
|
|
|
19
20
|
$listItemHeight: 2.125rem;
|
|
20
21
|
|
|
21
22
|
.select {
|
|
22
23
|
position: relative;
|
|
23
24
|
box-sizing: border-box;
|
|
24
|
-
transition
|
|
25
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
25
26
|
border: 0;
|
|
26
27
|
border-radius: var(--input-border-radius);
|
|
27
28
|
background-color: var(--input-background-color);
|
|
@@ -65,7 +66,7 @@ $listItemHeight: 2.125rem;
|
|
|
65
66
|
border-radius: var(--input-border-radius);
|
|
66
67
|
font-size: var(--font-size);
|
|
67
68
|
cursor: pointer;
|
|
68
|
-
transition
|
|
69
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
69
70
|
|
|
70
71
|
&:focus {
|
|
71
72
|
outline: 0;
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
box-sizing: border-box;
|
|
30
30
|
border: 0;
|
|
31
31
|
border-radius: var(--input-border-radius);
|
|
32
|
-
transition
|
|
32
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
33
33
|
font-family: var(--font-family);
|
|
34
34
|
font-size: var(--font-size);
|
|
35
35
|
color: var(--greyed-out);
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
$borderRadius: 2.5rem;
|
|
18
20
|
|
|
19
21
|
.toggle-wrapper {
|
|
@@ -29,7 +31,7 @@ $borderRadius: 2.5rem;
|
|
|
29
31
|
background-color: var(--default);
|
|
30
32
|
border-radius: $borderRadius;
|
|
31
33
|
position: relative;
|
|
32
|
-
transition
|
|
34
|
+
@include transition(background-color, 0.2s, ease-in-out);
|
|
33
35
|
display: block;
|
|
34
36
|
pointer-events: none;
|
|
35
37
|
|
|
@@ -44,7 +46,7 @@ $borderRadius: 2.5rem;
|
|
|
44
46
|
top: 50%;
|
|
45
47
|
transform: translateY(-50%) translateX(0);
|
|
46
48
|
left: 0.125rem;
|
|
47
|
-
transition
|
|
49
|
+
@include transition(transform, 0.2s, ease-in-out);
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
&.checked {
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../../../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
.floating-wrapper {
|
|
18
20
|
position: relative;
|
|
19
21
|
|
|
@@ -25,7 +27,7 @@
|
|
|
25
27
|
top: 1.3125rem;
|
|
26
28
|
left: 1.25rem;
|
|
27
29
|
transform-origin: left top;
|
|
28
|
-
transition
|
|
30
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
29
31
|
pointer-events: none;
|
|
30
32
|
|
|
31
33
|
&.floating-label-active {
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
$zIndex: 10;
|
|
18
20
|
$marginTop: 3.125rem;
|
|
19
21
|
|
|
@@ -39,7 +41,7 @@ $marginTop: 3.125rem;
|
|
|
39
41
|
left: 0;
|
|
40
42
|
top: 0;
|
|
41
43
|
background-color: transparent;
|
|
42
|
-
transition
|
|
44
|
+
@include transition(background-color, 0.5s, ease);
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
&.visible {
|
|
@@ -14,12 +14,14 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
.slide-in-modal {
|
|
18
20
|
justify-content: flex-end;
|
|
19
21
|
|
|
20
22
|
visibility: visible;
|
|
21
23
|
|
|
22
|
-
transition
|
|
24
|
+
@include transition(transform, 0.5s, ease-in-out);
|
|
23
25
|
transform: translate(120%);
|
|
24
26
|
|
|
25
27
|
&.hidden {
|
|
@@ -13,14 +13,15 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
@import "../../../mixins.module.scss";
|
|
16
17
|
|
|
17
18
|
.snackbars {
|
|
18
19
|
position: fixed;
|
|
19
20
|
z-index: 20;
|
|
20
21
|
width: 100%;
|
|
21
|
-
transition: height 0.2s ease-in-out;
|
|
22
22
|
display: flex;
|
|
23
23
|
flex-direction: column;
|
|
24
|
+
@include transition(height, 0.2s, ease-in-out);
|
|
24
25
|
|
|
25
26
|
&.bottom {
|
|
26
27
|
bottom: 3.5rem;
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
@import "../../../readyclasses.module.scss";
|
|
18
|
+
@import "../../../mixins.module.scss";
|
|
18
19
|
|
|
19
20
|
.snackbar {
|
|
20
21
|
padding: 1rem 1.25rem;
|
|
@@ -22,7 +23,7 @@
|
|
|
22
23
|
display: flex;
|
|
23
24
|
width: 100%;
|
|
24
25
|
box-sizing: border-box;
|
|
25
|
-
transition
|
|
26
|
+
@include transition(height, 0.2s, ease-in-out);
|
|
26
27
|
flex-grow: 0;
|
|
27
28
|
|
|
28
29
|
&.info {
|
|
@@ -122,8 +123,7 @@
|
|
|
122
123
|
|
|
123
124
|
&:active {
|
|
124
125
|
position: relative;
|
|
125
|
-
border
|
|
126
|
-
border: 0;
|
|
126
|
+
border: 0 transparent;
|
|
127
127
|
padding: calc(0.625rem + var(--button-border-width))
|
|
128
128
|
calc(1.25rem + var(--button-border-width));
|
|
129
129
|
|
|
@@ -14,17 +14,20 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
.popover {
|
|
20
|
+
$transition-property: transform, opacity;
|
|
21
|
+
|
|
18
22
|
position: fixed;
|
|
19
23
|
pointer-events: none;
|
|
20
24
|
background-color: var(--light);
|
|
21
25
|
border-radius: 8px;
|
|
22
|
-
box-shadow:
|
|
26
|
+
box-shadow: 0 30px 50px rgba(0, 0, 0, 0.29);
|
|
23
27
|
opacity: 0;
|
|
24
28
|
transform: scale(0.5);
|
|
25
|
-
|
|
26
|
-
transition-
|
|
27
|
-
transition-timing-function: ease-in-out;
|
|
29
|
+
|
|
30
|
+
@include transition($transition-property, 0.2s, ease-in-out);
|
|
28
31
|
|
|
29
32
|
&.show {
|
|
30
33
|
transform: scale(1);
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
$focus-width: 5px;
|
|
18
20
|
|
|
19
21
|
.tabs {
|
|
@@ -37,12 +39,13 @@ $focus-width: 5px;
|
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
.indicator {
|
|
42
|
+
$transition-property: left, width;
|
|
43
|
+
|
|
40
44
|
position: absolute;
|
|
41
45
|
height: var(--tab-active-border-height);
|
|
42
46
|
background-color: var(--tab-active-border-color);
|
|
43
|
-
|
|
44
|
-
transition-
|
|
45
|
-
transition-timing-function: ease-in-out;
|
|
47
|
+
|
|
48
|
+
@include transition($transition-property, 0.2s, ease-in-out);
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
.tabdivider {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
padding: 0.6875rem 0.6875rem 0.6875rem 0.875rem;
|
|
26
26
|
border-radius: 0.25rem;
|
|
27
27
|
|
|
28
|
-
box-shadow:
|
|
28
|
+
box-shadow: 0 0.1875rem 0.375rem rgba(0, 0, 0, 0.29);
|
|
29
29
|
white-space: pre-wrap;
|
|
30
30
|
|
|
31
31
|
transition-property: opacity;
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
@import "../mixins.module.scss";
|
|
18
|
+
|
|
17
19
|
.wrapper {
|
|
18
20
|
display: flex;
|
|
19
21
|
align-items: center;
|
|
@@ -22,7 +24,7 @@
|
|
|
22
24
|
.tooltip-wrapper {
|
|
23
25
|
.icon {
|
|
24
26
|
margin-left: 0.5rem;
|
|
25
|
-
transition
|
|
27
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
26
28
|
color: var(--greyed-out);
|
|
27
29
|
|
|
28
30
|
&:hover {
|
|
@@ -32,6 +34,8 @@
|
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
.tooltip {
|
|
37
|
+
$transition-property: opacity, transform;
|
|
38
|
+
|
|
35
39
|
position: fixed;
|
|
36
40
|
background-color: var(--default);
|
|
37
41
|
padding: 0.1875rem 0.5rem;
|
|
@@ -39,12 +43,11 @@
|
|
|
39
43
|
max-width: 330px;
|
|
40
44
|
opacity: 0;
|
|
41
45
|
transform: scale(0);
|
|
42
|
-
transition-duration: 0.2s;
|
|
43
|
-
transition-timing-function: ease-in-out;
|
|
44
|
-
transition-property: opacity, transform;
|
|
45
46
|
transform-origin: center left;
|
|
46
47
|
pointer-events: none;
|
|
47
48
|
|
|
49
|
+
@include transition($transition-property, 0.2s, ease-in-out);
|
|
50
|
+
|
|
48
51
|
&.visible {
|
|
49
52
|
opacity: 1;
|
|
50
53
|
transform: scale(1);
|
|
@@ -140,7 +140,6 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
|
|
|
140
140
|
/** We need a loading state, because otherwise you see the colors flash from the default to the possible overridden ones. */
|
|
141
141
|
const [isLoading, setIsLoading] = useState(true);
|
|
142
142
|
|
|
143
|
-
/** Set the actual CSS properties on the HTML :root object */
|
|
144
143
|
const setCSSProperties = (CSSPropertiesObject: CSSProperties) => {
|
|
145
144
|
for (const [key, value] of Object.entries(CSSPropertiesObject)) {
|
|
146
145
|
const formattedPropertyName = key.replace(/([A-Z])/g, val => `-${val.toLowerCase()}`);
|
|
@@ -148,7 +147,6 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
|
|
|
148
147
|
}
|
|
149
148
|
};
|
|
150
149
|
|
|
151
|
-
/** Check if the properties prop object is filled with anything. If it is, we want to shallow merge it with the default BaseStyling. */
|
|
152
150
|
useEffect(() => {
|
|
153
151
|
if (Object.keys(properties).length !== 0) {
|
|
154
152
|
const mergedState = { ...defaultProperties, ...properties };
|
|
@@ -159,6 +157,5 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
|
|
|
159
157
|
setIsLoading(false);
|
|
160
158
|
}, [properties]);
|
|
161
159
|
|
|
162
|
-
/** Only render if we're not loading */
|
|
163
160
|
return !isLoading ? <Fragment>{children}</Fragment> : null;
|
|
164
161
|
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { renderHook, act } from "@testing-library/react-hooks";
|
|
2
|
+
import { useDebouncedCallback } from "./useDebouncedCallback";
|
|
3
|
+
|
|
4
|
+
const BASE_DELAY = 300;
|
|
5
|
+
const EXTRA_DELAY = 200;
|
|
6
|
+
|
|
7
|
+
const sleep = (delay: number) => new Promise(resolve => setTimeout(resolve, delay));
|
|
8
|
+
|
|
9
|
+
describe("Testing the useDebouncedCallback hook", () => {
|
|
10
|
+
it("should run callback after specified delay", async () => {
|
|
11
|
+
const delay = BASE_DELAY;
|
|
12
|
+
const callback = jest.fn();
|
|
13
|
+
|
|
14
|
+
const { result } = renderHook(() => useDebouncedCallback(callback, delay));
|
|
15
|
+
|
|
16
|
+
expect(callback).toHaveBeenCalledTimes(0);
|
|
17
|
+
act(result.current);
|
|
18
|
+
await sleep(delay + EXTRA_DELAY);
|
|
19
|
+
|
|
20
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
21
|
+
|
|
22
|
+
act(result.current);
|
|
23
|
+
await sleep(delay + EXTRA_DELAY);
|
|
24
|
+
|
|
25
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should pass args through to callback function", async () => {
|
|
29
|
+
let mutableValue = "";
|
|
30
|
+
const delay = BASE_DELAY;
|
|
31
|
+
const callback = jest.fn(arg => (mutableValue = `got arg ${arg}`));
|
|
32
|
+
|
|
33
|
+
const { result } = renderHook(() => useDebouncedCallback(callback, delay));
|
|
34
|
+
|
|
35
|
+
act(() => result.current("passthrough"));
|
|
36
|
+
await sleep(delay + EXTRA_DELAY);
|
|
37
|
+
|
|
38
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
39
|
+
expect(mutableValue).toBe(`got arg passthrough`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should cancel pending call if fn is called again", async () => {
|
|
43
|
+
const delay = BASE_DELAY;
|
|
44
|
+
const callback = jest.fn();
|
|
45
|
+
|
|
46
|
+
const { result } = renderHook(() => useDebouncedCallback(callback, delay));
|
|
47
|
+
|
|
48
|
+
expect(callback).toHaveBeenCalledTimes(0);
|
|
49
|
+
act(result.current);
|
|
50
|
+
await sleep(delay / 2);
|
|
51
|
+
|
|
52
|
+
act(result.current);
|
|
53
|
+
await sleep(delay + EXTRA_DELAY);
|
|
54
|
+
|
|
55
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should cancel pending call if callback changes", async () => {
|
|
59
|
+
const delay = BASE_DELAY;
|
|
60
|
+
const callback1 = jest.fn();
|
|
61
|
+
const callback2 = jest.fn();
|
|
62
|
+
|
|
63
|
+
const { result, rerender } = renderHook(
|
|
64
|
+
({ callback, delay }) => useDebouncedCallback(callback, delay),
|
|
65
|
+
{ initialProps: { callback: callback1, delay } }
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
act(result.current);
|
|
69
|
+
await sleep(delay / 2);
|
|
70
|
+
|
|
71
|
+
rerender({ callback: callback2, delay });
|
|
72
|
+
act(result.current);
|
|
73
|
+
await sleep(delay + EXTRA_DELAY);
|
|
74
|
+
|
|
75
|
+
expect(callback1).toHaveBeenCalledTimes(0);
|
|
76
|
+
expect(callback2).toHaveBeenCalledTimes(1);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should cancel pending call if delay changes", async () => {
|
|
80
|
+
const delay1 = BASE_DELAY;
|
|
81
|
+
const delay2 = BASE_DELAY * 2;
|
|
82
|
+
const callback = jest.fn();
|
|
83
|
+
|
|
84
|
+
const { result, rerender } = renderHook(
|
|
85
|
+
({ callback, delay }) => useDebouncedCallback(callback, delay),
|
|
86
|
+
{ initialProps: { callback, delay: delay1 } }
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
act(result.current);
|
|
90
|
+
await sleep(delay1 / 2);
|
|
91
|
+
|
|
92
|
+
rerender({ callback, delay: delay2 });
|
|
93
|
+
act(result.current);
|
|
94
|
+
await sleep(delay2 + EXTRA_DELAY);
|
|
95
|
+
|
|
96
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should cancel pending call if dependencies change", async () => {
|
|
100
|
+
const delay = BASE_DELAY;
|
|
101
|
+
const callback = jest.fn();
|
|
102
|
+
|
|
103
|
+
const { result, rerender } = renderHook(
|
|
104
|
+
({ callback, delay, dependencies }) => useDebouncedCallback(callback, delay, dependencies),
|
|
105
|
+
{ initialProps: { callback, delay, dependencies: ["a"] } }
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
act(result.current);
|
|
109
|
+
await sleep(delay / 2);
|
|
110
|
+
|
|
111
|
+
rerender({ callback, delay, dependencies: ["b"] });
|
|
112
|
+
act(result.current);
|
|
113
|
+
await sleep(delay + EXTRA_DELAY);
|
|
114
|
+
|
|
115
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should support async callbacks", async () => {
|
|
119
|
+
const makeAsyncCall = jest.fn(
|
|
120
|
+
() =>
|
|
121
|
+
new Promise(async resolve => {
|
|
122
|
+
await sleep(delay / 2);
|
|
123
|
+
resolve(null);
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const delay = BASE_DELAY;
|
|
128
|
+
const callback = jest.fn(async () => {
|
|
129
|
+
await makeAsyncCall();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const { result } = renderHook(() => useDebouncedCallback(callback, delay));
|
|
133
|
+
|
|
134
|
+
await act(async () => result.current());
|
|
135
|
+
await sleep(delay + EXTRA_DELAY);
|
|
136
|
+
|
|
137
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
138
|
+
expect(makeAsyncCall).toHaveBeenCalledTimes(1);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 OneWelcome B.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import React from "react";
|
|
18
|
+
|
|
19
|
+
export const useDebouncedCallback = (callback: Function, delay: number, dependencies?: any[]) => {
|
|
20
|
+
const timeout = React.useRef<ReturnType<typeof setTimeout>>();
|
|
21
|
+
const comboDeps = dependencies ? [callback, delay, ...dependencies] : [callback, delay];
|
|
22
|
+
|
|
23
|
+
return React.useCallback((...args) => {
|
|
24
|
+
if (timeout.current != null) {
|
|
25
|
+
clearTimeout(timeout.current);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
timeout.current = setTimeout(() => {
|
|
29
|
+
callback(...args);
|
|
30
|
+
}, delay);
|
|
31
|
+
}, comboDeps);
|
|
32
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -43,7 +43,8 @@ export {
|
|
|
43
43
|
|
|
44
44
|
/* Utils */
|
|
45
45
|
export { useRepeater } from "./hooks/useRepeater";
|
|
46
|
-
export {
|
|
46
|
+
export { useDebouncedCallback } from "./hooks/useDebouncedCallback";
|
|
47
|
+
export { generateID, debounce, throttle } from "./util/helper";
|
|
47
48
|
|
|
48
49
|
/* Notifications */
|
|
49
50
|
export { Modal, Props as ModalProps } from "./Notifications/Modal/Modal";
|
package/src/mixins.module.scss
CHANGED
|
@@ -133,6 +133,7 @@
|
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
@mixin buttonBase($type: "default") {
|
|
136
|
+
$transition-property: color, background-color, border-color;
|
|
136
137
|
$disabledSelector: if($type == "link", ".disabled", ":disabled");
|
|
137
138
|
|
|
138
139
|
border-width: var(--button-border-width);
|
|
@@ -144,9 +145,9 @@
|
|
|
144
145
|
padding: 0.5rem 1.25rem;
|
|
145
146
|
min-height: 2.5rem;
|
|
146
147
|
cursor: pointer;
|
|
147
|
-
|
|
148
|
-
transition-
|
|
149
|
-
|
|
148
|
+
|
|
149
|
+
@include transition($transition-property, 0.2s, ease-in-out);
|
|
150
|
+
|
|
150
151
|
font-family: var(--font-family);
|
|
151
152
|
|
|
152
153
|
&#{$disabledSelector} {
|
|
@@ -166,7 +167,7 @@
|
|
|
166
167
|
border-style: var(--input-border-style);
|
|
167
168
|
border-width: var(--input-border-width);
|
|
168
169
|
border-radius: var(--input-border-radius);
|
|
169
|
-
transition
|
|
170
|
+
@include transition(all, 0.2s, ease-in-out);
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
173
|
|
|
@@ -223,8 +224,13 @@
|
|
|
223
224
|
rgba(#fff, 0.5) 60%,
|
|
224
225
|
rgba(#fff, 0)
|
|
225
226
|
);
|
|
226
|
-
animation: shimmer 2s infinite;
|
|
227
227
|
content: "";
|
|
228
|
+
|
|
229
|
+
animation: shimmer 2s infinite;
|
|
230
|
+
|
|
231
|
+
@media (prefers-reduced-motion: reduce) {
|
|
232
|
+
animation: none;
|
|
233
|
+
}
|
|
228
234
|
}
|
|
229
235
|
|
|
230
236
|
@keyframes shimmer {
|
|
@@ -233,3 +239,13 @@
|
|
|
233
239
|
}
|
|
234
240
|
}
|
|
235
241
|
}
|
|
242
|
+
|
|
243
|
+
@mixin transition($property, $duration, $function) {
|
|
244
|
+
transition-property: $property;
|
|
245
|
+
transition-duration: $duration;
|
|
246
|
+
transition-timing-function: $function;
|
|
247
|
+
|
|
248
|
+
@media (prefers-reduced-motion: reduce) {
|
|
249
|
+
transition-duration: 0.1ms;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -31,10 +31,18 @@
|
|
|
31
31
|
|
|
32
32
|
.slide-in {
|
|
33
33
|
animation: slide-in 0.5s forwards;
|
|
34
|
+
|
|
35
|
+
@media (prefers-reduced-motion: reduce) {
|
|
36
|
+
animation-duration: 0.1ms;
|
|
37
|
+
}
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
.slide-out {
|
|
37
41
|
animation: slide-out 0.5s forwards;
|
|
42
|
+
|
|
43
|
+
@media (prefers-reduced-motion: reduce) {
|
|
44
|
+
animation-duration: 0.1ms;
|
|
45
|
+
}
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
@keyframes slide-in {
|