@onewelcome/react-lib-components 1.1.0 → 1.2.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/index.d.ts +1 -1
- package/dist/react-lib-components.cjs.development.js +72 -49
- 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 +71 -50
- 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/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/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/index.ts +1 -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
|
@@ -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
|
};
|
package/src/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ export {
|
|
|
43
43
|
|
|
44
44
|
/* Utils */
|
|
45
45
|
export { useRepeater } from "./hooks/useRepeater";
|
|
46
|
-
export { generateID } from "./util/helper";
|
|
46
|
+
export { generateID, debounce, throttle } from "./util/helper";
|
|
47
47
|
|
|
48
48
|
/* Notifications */
|
|
49
49
|
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 {
|
package/src/util/helper.test.tsx
CHANGED
|
@@ -14,7 +14,10 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import React, { useCallback, useEffect, useState } from "react";
|
|
18
|
+
import { fireEvent, waitFor } from "@testing-library/dom";
|
|
19
|
+
import { generateID, filterProps, debounce, throttle } from "./helper";
|
|
20
|
+
import { render } from "@testing-library/react";
|
|
18
21
|
|
|
19
22
|
/* Generate an ID of 20 characters with a string woven in */
|
|
20
23
|
|
|
@@ -66,3 +69,127 @@ describe("filterprops should return the correct object", () => {
|
|
|
66
69
|
});
|
|
67
70
|
});
|
|
68
71
|
});
|
|
72
|
+
|
|
73
|
+
describe("debounce function", () => {
|
|
74
|
+
it("debounced the resizing of the window, it will only execute 1 time", async () => {
|
|
75
|
+
const debouncedFunction = jest.fn();
|
|
76
|
+
|
|
77
|
+
window.addEventListener("resize", debounce(debouncedFunction, 200));
|
|
78
|
+
|
|
79
|
+
await fireEvent.resize(window);
|
|
80
|
+
await fireEvent.resize(window);
|
|
81
|
+
await fireEvent.resize(window);
|
|
82
|
+
await fireEvent.resize(window);
|
|
83
|
+
await fireEvent.resize(window);
|
|
84
|
+
|
|
85
|
+
await waitFor(() => expect(debouncedFunction).toHaveBeenCalledTimes(1));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("Works in a react component as well, it should only fire the exampleFunction once.", async () => {
|
|
89
|
+
const ExampleComponent = ({
|
|
90
|
+
debouncedFunction
|
|
91
|
+
}: {
|
|
92
|
+
debouncedFunction: (...args: unknown[]) => unknown;
|
|
93
|
+
}) => {
|
|
94
|
+
const [variable, setVariable] = useState(0);
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
debouncedFunction(variable);
|
|
98
|
+
}, [variable]);
|
|
99
|
+
|
|
100
|
+
const incrementVariable = () => {
|
|
101
|
+
setVariable(Math.random());
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
window.addEventListener("resize", debounce(incrementVariable, 200));
|
|
106
|
+
}, []);
|
|
107
|
+
|
|
108
|
+
return null;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const exampleFunction = jest.fn();
|
|
112
|
+
|
|
113
|
+
render(<ExampleComponent debouncedFunction={exampleFunction} />);
|
|
114
|
+
|
|
115
|
+
await fireEvent.resize(window);
|
|
116
|
+
await fireEvent.resize(window);
|
|
117
|
+
await fireEvent.resize(window);
|
|
118
|
+
await fireEvent.resize(window);
|
|
119
|
+
await fireEvent.resize(window);
|
|
120
|
+
|
|
121
|
+
expect(exampleFunction).toHaveBeenCalledTimes(1);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("throttling works", () => {
|
|
126
|
+
it("throttles the function", async () => {
|
|
127
|
+
const throttledFunction = jest.fn();
|
|
128
|
+
|
|
129
|
+
window.addEventListener("resize", throttle(throttledFunction, 1));
|
|
130
|
+
|
|
131
|
+
await fireEvent.resize(window);
|
|
132
|
+
await fireEvent.resize(window);
|
|
133
|
+
await fireEvent.resize(window);
|
|
134
|
+
await fireEvent.resize(window);
|
|
135
|
+
await fireEvent.resize(window);
|
|
136
|
+
await fireEvent.resize(window);
|
|
137
|
+
await fireEvent.resize(window);
|
|
138
|
+
await fireEvent.resize(window);
|
|
139
|
+
await fireEvent.resize(window);
|
|
140
|
+
await fireEvent.resize(window);
|
|
141
|
+
await fireEvent.resize(window);
|
|
142
|
+
await fireEvent.resize(window);
|
|
143
|
+
await fireEvent.resize(window);
|
|
144
|
+
await fireEvent.resize(window);
|
|
145
|
+
await fireEvent.resize(window);
|
|
146
|
+
await fireEvent.resize(window);
|
|
147
|
+
await fireEvent.resize(window);
|
|
148
|
+
await fireEvent.resize(window);
|
|
149
|
+
|
|
150
|
+
expect(throttledFunction).not.toHaveBeenCalledTimes(1);
|
|
151
|
+
expect(throttledFunction).not.toHaveBeenCalledTimes(10);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("Works in a react component as well, it should only fire the exampleFunction once.", async () => {
|
|
155
|
+
const ExampleComponent = ({
|
|
156
|
+
throttledFunction
|
|
157
|
+
}: {
|
|
158
|
+
throttledFunction: (...args: unknown[]) => unknown;
|
|
159
|
+
}) => {
|
|
160
|
+
const [variable, setVariable] = useState(0);
|
|
161
|
+
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
throttledFunction(variable);
|
|
164
|
+
}, [variable]);
|
|
165
|
+
|
|
166
|
+
const incrementVariable = useCallback(() => {
|
|
167
|
+
setVariable(Math.random());
|
|
168
|
+
}, []);
|
|
169
|
+
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
window.addEventListener("resize", throttle(incrementVariable, 1));
|
|
172
|
+
}, []);
|
|
173
|
+
|
|
174
|
+
return null;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const exampleFunction = jest.fn();
|
|
178
|
+
|
|
179
|
+
render(<ExampleComponent throttledFunction={exampleFunction} />);
|
|
180
|
+
|
|
181
|
+
await fireEvent.resize(window);
|
|
182
|
+
await fireEvent.resize(window);
|
|
183
|
+
await fireEvent.resize(window);
|
|
184
|
+
await fireEvent.resize(window);
|
|
185
|
+
await fireEvent.resize(window);
|
|
186
|
+
await fireEvent.resize(window);
|
|
187
|
+
await fireEvent.resize(window);
|
|
188
|
+
await fireEvent.resize(window);
|
|
189
|
+
await fireEvent.resize(window);
|
|
190
|
+
await fireEvent.resize(window);
|
|
191
|
+
|
|
192
|
+
expect(exampleFunction).not.toHaveBeenCalledTimes(1);
|
|
193
|
+
expect(exampleFunction).not.toHaveBeenCalledTimes(10);
|
|
194
|
+
});
|
|
195
|
+
});
|
package/src/util/helper.tsx
CHANGED
|
@@ -115,3 +115,31 @@ export const filterProps = (props: any, regexPattern: RegExp, returnFiltered: bo
|
|
|
115
115
|
);
|
|
116
116
|
}
|
|
117
117
|
};
|
|
118
|
+
|
|
119
|
+
export const debounce = (fn: (...args: unknown[]) => unknown, delay: number) => {
|
|
120
|
+
let timeout: undefined | ReturnType<typeof setTimeout>;
|
|
121
|
+
|
|
122
|
+
return function executedFunction(...args: unknown[]) {
|
|
123
|
+
const later = () => {
|
|
124
|
+
clearTimeout(timeout);
|
|
125
|
+
fn(...args);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
clearTimeout(timeout);
|
|
129
|
+
|
|
130
|
+
timeout = setTimeout(later, delay);
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const throttle = (fn: (...args: unknown[]) => unknown, delay: number) => {
|
|
135
|
+
let lastTime = 0;
|
|
136
|
+
|
|
137
|
+
return function () {
|
|
138
|
+
let now = Date.now();
|
|
139
|
+
|
|
140
|
+
if (now - lastTime >= delay) {
|
|
141
|
+
fn();
|
|
142
|
+
lastTime = now;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
};
|