@darajs/ui-utils 0.4.8
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/LICENSE +201 -0
- package/dist/clipboard-utils.d.ts +26 -0
- package/dist/clipboard-utils.d.ts.map +1 -0
- package/dist/clipboard-utils.js +67 -0
- package/dist/clipboard-utils.js.map +1 -0
- package/dist/constants.d.ts +35 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +37 -0
- package/dist/constants.js.map +1 -0
- package/dist/get-status-color.d.ts +32 -0
- package/dist/get-status-color.d.ts.map +1 -0
- package/dist/get-status-color.js +34 -0
- package/dist/get-status-color.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/path-utils.d.ts +23 -0
- package/dist/path-utils.d.ts.map +1 -0
- package/dist/path-utils.js +29 -0
- package/dist/path-utils.js.map +1 -0
- package/dist/request-utils.d.ts +67 -0
- package/dist/request-utils.d.ts.map +1 -0
- package/dist/request-utils.js +177 -0
- package/dist/request-utils.js.map +1 -0
- package/dist/rx-utils.d.ts +9 -0
- package/dist/rx-utils.d.ts.map +1 -0
- package/dist/rx-utils.js +33 -0
- package/dist/rx-utils.js.map +1 -0
- package/dist/use-d3-axis.d.ts +45 -0
- package/dist/use-d3-axis.d.ts.map +1 -0
- package/dist/use-d3-axis.js +83 -0
- package/dist/use-d3-axis.js.map +1 -0
- package/dist/use-deep-compare.d.ts +13 -0
- package/dist/use-deep-compare.d.ts.map +1 -0
- package/dist/use-deep-compare.js +37 -0
- package/dist/use-deep-compare.js.map +1 -0
- package/dist/use-dimensions.d.ts +22 -0
- package/dist/use-dimensions.d.ts.map +1 -0
- package/dist/use-dimensions.js +47 -0
- package/dist/use-dimensions.js.map +1 -0
- package/dist/use-intersection-observer.d.ts +30 -0
- package/dist/use-intersection-observer.d.ts.map +1 -0
- package/dist/use-intersection-observer.js +51 -0
- package/dist/use-intersection-observer.js.map +1 -0
- package/dist/use-on-click-outside.d.ts +9 -0
- package/dist/use-on-click-outside.d.ts.map +1 -0
- package/dist/use-on-click-outside.js +42 -0
- package/dist/use-on-click-outside.js.map +1 -0
- package/dist/use-throttle.d.ts +25 -0
- package/dist/use-throttle.d.ts.map +1 -0
- package/dist/use-throttle.js +49 -0
- package/dist/use-throttle.js.map +1 -0
- package/dist/use-update-effect.d.ts +26 -0
- package/dist/use-update-effect.d.ts.map +1 -0
- package/dist/use-update-effect.js +36 -0
- package/dist/use-update-effect.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { BehaviorSubject } from 'rxjs';
|
|
2
|
+
export interface SortingRule {
|
|
3
|
+
desc?: boolean;
|
|
4
|
+
id: string;
|
|
5
|
+
}
|
|
6
|
+
export interface FilterRule {
|
|
7
|
+
id: string;
|
|
8
|
+
value: string | Array<string>;
|
|
9
|
+
}
|
|
10
|
+
/** Standard interface for any additional request options that are required */
|
|
11
|
+
export interface RequestOptions {
|
|
12
|
+
filter?: Array<FilterRule>;
|
|
13
|
+
limit?: number;
|
|
14
|
+
searchTerm?: string;
|
|
15
|
+
sort?: Array<SortingRule>;
|
|
16
|
+
startIndex?: number;
|
|
17
|
+
}
|
|
18
|
+
/** Error class for request errors that allow them to be caught more easily */
|
|
19
|
+
export declare class RequestError extends Error {
|
|
20
|
+
requestParams: {
|
|
21
|
+
[k: string]: any;
|
|
22
|
+
};
|
|
23
|
+
status: number;
|
|
24
|
+
constructor(status: number, message: string, requestParams?: {
|
|
25
|
+
[k: string]: any;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Request helper for converting an options object into a valid query string
|
|
30
|
+
*
|
|
31
|
+
* @param options The request options object to convert, will handle it being undefined
|
|
32
|
+
*/
|
|
33
|
+
export declare const getQueryStr: (options?: RequestOptions, extras?: {
|
|
34
|
+
[k: string]: string | number | boolean;
|
|
35
|
+
}) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Request helper for checking valid response status
|
|
38
|
+
*
|
|
39
|
+
* @param res The response to check
|
|
40
|
+
*/
|
|
41
|
+
export declare const isValidResponse: (res: Response) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Request helper to check whether a response is valid and throw a Service error instance if it is not.
|
|
44
|
+
*
|
|
45
|
+
* @param res the response to check
|
|
46
|
+
* @param fallbackMessage a fallback error message if one is not provided
|
|
47
|
+
*/
|
|
48
|
+
export declare function validateResponse(res: Response, fallbackMessage: string, requestParams?: {
|
|
49
|
+
[k: string]: any;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
export declare const CHUNK_SIZE = 5000000;
|
|
52
|
+
/**
|
|
53
|
+
* An async function that loops over a file and uploads it chunk by chunk, whilst notifying the calling function of it's
|
|
54
|
+
* progress via an Rx stream.
|
|
55
|
+
*
|
|
56
|
+
* @param url the url to upload too
|
|
57
|
+
* @param file the file to upload
|
|
58
|
+
* @param authHeaders the authHeaders to upload with
|
|
59
|
+
* @param extras a dict of extras to also set on the form
|
|
60
|
+
* @param sub an optional subject notify the calling code of current progress
|
|
61
|
+
*/
|
|
62
|
+
export declare function chunkedFileUpload(url: string, file: File, authHeaders: {
|
|
63
|
+
[k: string]: string;
|
|
64
|
+
}, extras?: {
|
|
65
|
+
[k: string]: string;
|
|
66
|
+
}, sub?: BehaviorSubject<number>): Promise<void>;
|
|
67
|
+
//# sourceMappingURL=request-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-utils.d.ts","sourceRoot":"","sources":["../src/request-utils.tsx"],"names":[],"mappings":"AAiBA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAIvC,MAAM,WAAW,WAAW;IACxB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,UAAU;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,E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ACpC,MAAM,CAAC,EAAE;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,EAChC,GAAG,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,GAC9B,OAAO,CAAC,IAAI,CAAC,CA4Cf"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
12
|
+
*
|
|
13
|
+
*
|
|
14
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+
* you may not use this file except in compliance with the License.
|
|
16
|
+
* You may obtain a copy of the License at
|
|
17
|
+
*
|
|
18
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+
*
|
|
20
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
21
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
22
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
23
|
+
* See the License for the specific language governing permissions and
|
|
24
|
+
* limitations under the License.
|
|
25
|
+
*/
|
|
26
|
+
import { nanoid } from 'nanoid';
|
|
27
|
+
import { HTTP_METHOD } from './constants';
|
|
28
|
+
/** Error class for request errors that allow them to be caught more easily */
|
|
29
|
+
export class RequestError extends Error {
|
|
30
|
+
constructor(status, message, requestParams) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'ServiceError';
|
|
33
|
+
this.status = status;
|
|
34
|
+
this.requestParams = requestParams;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Request helper for converting an options object into a valid query string
|
|
39
|
+
*
|
|
40
|
+
* @param options The request options object to convert, will handle it being undefined
|
|
41
|
+
*/
|
|
42
|
+
export const getQueryStr = (options, extras) => {
|
|
43
|
+
if (!options && !extras) {
|
|
44
|
+
return '';
|
|
45
|
+
}
|
|
46
|
+
let query = '?';
|
|
47
|
+
if (Number.isInteger(options.startIndex)) {
|
|
48
|
+
query += `offset=${options.startIndex}&`;
|
|
49
|
+
}
|
|
50
|
+
if (Number.isInteger(options.limit)) {
|
|
51
|
+
query += `limit=${options.limit}&`;
|
|
52
|
+
}
|
|
53
|
+
if (options.searchTerm) {
|
|
54
|
+
query += `query=${options.searchTerm}&`;
|
|
55
|
+
}
|
|
56
|
+
if (options.sort && options.sort.length > 0) {
|
|
57
|
+
// Handle all items in array of sorting rules
|
|
58
|
+
query += `order_by=${options.sort.map((sort) => `${sort.desc ? '-' : ''}${sort.id}`).join(',')}&`;
|
|
59
|
+
}
|
|
60
|
+
if (options.filter && options.filter.length > 0) {
|
|
61
|
+
for (const filter of options.filter) {
|
|
62
|
+
// Handle single filters or arrays of filter terms
|
|
63
|
+
if (typeof filter.value === 'string') {
|
|
64
|
+
query += `${filter.id}=${filter.value}&`;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
for (const val of filter.value) {
|
|
68
|
+
query += `${filter.id}=${val}&`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
Object.keys(extras).forEach((key) => {
|
|
75
|
+
query += `${key}=${String(extras[key])}&`;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (_a) {
|
|
79
|
+
// Do nothing as it was probably empty
|
|
80
|
+
}
|
|
81
|
+
return query.replace(/&$/, '');
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Request helper for checking valid response status
|
|
85
|
+
*
|
|
86
|
+
* @param res The response to check
|
|
87
|
+
*/
|
|
88
|
+
export const isValidResponse = (res) => {
|
|
89
|
+
return res.status >= 100 && res.status < 400;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Request helper to check whether a response is valid and throw a Service error instance if it is not.
|
|
93
|
+
*
|
|
94
|
+
* @param res the response to check
|
|
95
|
+
* @param fallbackMessage a fallback error message if one is not provided
|
|
96
|
+
*/
|
|
97
|
+
export function validateResponse(res, fallbackMessage, requestParams) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
if (!isValidResponse(res)) {
|
|
100
|
+
let message = fallbackMessage;
|
|
101
|
+
try {
|
|
102
|
+
const json = yield res.json();
|
|
103
|
+
message = (json === null || json === void 0 ? void 0 : json.message) || (json === null || json === void 0 ? void 0 : json.detail) || fallbackMessage;
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
if (fallbackMessage) {
|
|
107
|
+
try {
|
|
108
|
+
message = (yield res.text()) || fallbackMessage;
|
|
109
|
+
}
|
|
110
|
+
catch (_a) {
|
|
111
|
+
// do nothing, use fallback message
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
throw new RequestError(res.status, message, requestParams);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
export const CHUNK_SIZE = 5e6;
|
|
120
|
+
/**
|
|
121
|
+
* An async function that loops over a file and uploads it chunk by chunk, whilst notifying the calling function of it's
|
|
122
|
+
* progress via an Rx stream.
|
|
123
|
+
*
|
|
124
|
+
* @param url the url to upload too
|
|
125
|
+
* @param file the file to upload
|
|
126
|
+
* @param authHeaders the authHeaders to upload with
|
|
127
|
+
* @param extras a dict of extras to also set on the form
|
|
128
|
+
* @param sub an optional subject notify the calling code of current progress
|
|
129
|
+
*/
|
|
130
|
+
export function chunkedFileUpload(url, file, authHeaders, extras, sub) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
|
|
133
|
+
let currentPointer = 0;
|
|
134
|
+
let currentChunk = 0;
|
|
135
|
+
const uploadId = nanoid();
|
|
136
|
+
/* eslint-disable no-await-in-loop */
|
|
137
|
+
// We need to process sequentially thus await in a loop
|
|
138
|
+
while (currentPointer < file.size) {
|
|
139
|
+
const fd = new FormData();
|
|
140
|
+
if (extras) {
|
|
141
|
+
for (const extra of Object.keys(extras)) {
|
|
142
|
+
fd.append(extra, extras[extra]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
fd.append('upload_id', uploadId);
|
|
146
|
+
fd.append('file_total_size', `${file.size}`);
|
|
147
|
+
fd.append('file_chunk_offset', `${currentPointer}`);
|
|
148
|
+
fd.append('file_chunk_index', `${currentChunk}`);
|
|
149
|
+
fd.append('file_total_chunks', `${totalChunks}`);
|
|
150
|
+
fd.append('file', new File([file.slice(currentPointer, currentPointer + CHUNK_SIZE)], file.name));
|
|
151
|
+
try {
|
|
152
|
+
const res = yield fetch(url, {
|
|
153
|
+
body: fd,
|
|
154
|
+
headers: Object.assign(Object.assign({}, authHeaders), { Accept: 'application/json' }),
|
|
155
|
+
method: HTTP_METHOD.POST,
|
|
156
|
+
});
|
|
157
|
+
yield validateResponse(res, `Failed to upload file: ${file.name}`);
|
|
158
|
+
if (sub) {
|
|
159
|
+
sub.next(Math.round(((currentChunk + 1) / totalChunks) * 100));
|
|
160
|
+
}
|
|
161
|
+
currentPointer += CHUNK_SIZE;
|
|
162
|
+
currentChunk += 1;
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
if (sub) {
|
|
166
|
+
sub.error(err.message);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
throw err;
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/* eslint-enable no-await-in-loop */
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=request-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-utils.js","sourceRoot":"","sources":["../src/request-utils.tsx"],"names":[],"mappings":";;;;;;;;;AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAqB1C,8EAA8E;AAC9E,MAAM,OAAO,YAAa,SAAQ,KAAK;IAKnC,YAAY,MAAc,EAAE,OAAe,EAAE,aAAoC;QAC7E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACvC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAwB,EAAE,MAAmD,EAAU,EAAE;IACjH,IAAI,CAAC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACtB,OAAO,EAAE,CAAC;IACd,CAAC;IAED,IAAI,KAAK,GAAG,GAAG,CAAC;IAEhB,IAAI,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACvC,KAAK,IAAI,UAAU,OAAO,CAAC,UAAU,GAAG,CAAC;IAC7C,CAAC;IACD,IAAI,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,KAAK,IAAI,SAAS,OAAO,CAAC,KAAK,GAAG,CAAC;IACvC,CAAC;IACD,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QACrB,KAAK,IAAI,SAAS,OAAO,CAAC,UAAU,GAAG,CAAC;IAC5C,CAAC;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AI,EAAE,CAAC,CAAC;gBACnE,IAAI,GAAG,EAAE,CAAC;oBACN,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,YAAY,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;gBACnE,CAAC;gBACD,cAAc,IAAI,UAAU,CAAC;gBAC7B,YAAY,IAAI,CAAC,CAAC;YACtB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACX,IAAI,GAAG,EAAE,CAAC;oBACN,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBAC3B,CAAC;qBAAM,CAAC;oBACJ,MAAM,GAAG,CAAC;gBACd,CAAC;gBACD,MAAM;YACV,CAAC;QACL,CAAC;QACD,oCAAoC;IACxC,CAAC;CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Subscription } from 'rxjs';
|
|
2
|
+
/**
|
|
3
|
+
* A helper hook that wraps a subscription and makes sure that it is properly cleaned up at the end of the component
|
|
4
|
+
* lifecycle, returns a function that should wrap the call to subscribe
|
|
5
|
+
*
|
|
6
|
+
* @param sub the subscription to cleanup
|
|
7
|
+
*/
|
|
8
|
+
export declare function useSubscription(): (sub: Subscription) => void;
|
|
9
|
+
//# sourceMappingURL=rx-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rx-utils.d.ts","sourceRoot":"","sources":["../src/rx-utils.tsx"],"names":[],"mappings":"AAiBA,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAEpC;;;;;GAKG;AACH,wBAAgB,eAAe,IAAI,CAAC,GAAG,EAAE,YAAY,KAAK,IAAI,CAU7D"}
|
package/dist/rx-utils.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { useEffect, useState } from 'react';
|
|
18
|
+
/**
|
|
19
|
+
* A helper hook that wraps a subscription and makes sure that it is properly cleaned up at the end of the component
|
|
20
|
+
* lifecycle, returns a function that should wrap the call to subscribe
|
|
21
|
+
*
|
|
22
|
+
* @param sub the subscription to cleanup
|
|
23
|
+
*/
|
|
24
|
+
export function useSubscription() {
|
|
25
|
+
const [subscription, setSubscription] = useState();
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (subscription) {
|
|
28
|
+
return () => subscription.unsubscribe();
|
|
29
|
+
}
|
|
30
|
+
}, [subscription]);
|
|
31
|
+
return setSubscription;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=rx-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rx-utils.js","sourceRoot":"","sources":["../src/rx-utils.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG5C;;;;;GAKG;AACH,MAAM,UAAU,eAAe;IAC3B,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,EAAgB,CAAC;IAEjE,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,YAAY,EAAE,CAAC;YACf,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;QAC5C,CAAC;IACL,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAEnB,OAAO,eAAe,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type AxisDomain = string | number | 'auto' | 'dataMin' | 'dataMax';
|
|
2
|
+
interface AxisProps<T> {
|
|
3
|
+
domain?: [T, T];
|
|
4
|
+
tickFormatter: (tick?: number) => string;
|
|
5
|
+
ticks?: Array<number>;
|
|
6
|
+
type: 'number' | 'category';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* An underlying helper hook that uses d3 to create a function for getting the axis properties for an axis based
|
|
10
|
+
* on the pixel size of that axis. This hook should not be used directly, use one of the wrappers for it,
|
|
11
|
+
* e.g. useD3LinearAxis or useD3TimeAxis
|
|
12
|
+
*
|
|
13
|
+
* @param data the underlying data for the axis
|
|
14
|
+
* @param domain the target domain for the axis
|
|
15
|
+
* @param getScale a function to get the d3Scale instance with domain applied
|
|
16
|
+
* @param domainFormatter a function to translate the d3 domain back into a recharts domain
|
|
17
|
+
*/
|
|
18
|
+
export declare function useD3Axis<T>(data: Array<T>, getScale: (data: Array<T>, domain?: [T, T]) => any, domainFormatter: (domain: [any, any]) => [T, T], domain?: [T, T]): (width?: number) => AxisProps<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Hook that returns a function that generates the props for a linearly scaled axis based on the size in pixels of the
|
|
21
|
+
* axis
|
|
22
|
+
*
|
|
23
|
+
* @param data the underlying data for the axis
|
|
24
|
+
* @param domain the target domain for the axis
|
|
25
|
+
*/
|
|
26
|
+
export declare function useD3LinearAxis(data: Array<number>, domain?: [number, number]): (axisSize?: number) => AxisProps<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Hook that returns a function that generates the props for a time scaled axis based on the size in pixels of the
|
|
29
|
+
* axis
|
|
30
|
+
*
|
|
31
|
+
* @param data the underlying data for the axis
|
|
32
|
+
* @param domain the target domain for the axis
|
|
33
|
+
*/
|
|
34
|
+
export declare function useD3TimeAxis(data: Array<Date>, domain?: [Date, Date]): (axisSize?: number) => AxisProps<Date>;
|
|
35
|
+
/**
|
|
36
|
+
* Hook that returns a function that generates the props for an ordinal (categorical) scaled axis, doesn't actually use
|
|
37
|
+
* the ordinal scale as it doesn't need to, but has the same returned api as the others so it can be substituted in.
|
|
38
|
+
*
|
|
39
|
+
* @param mapping a mapping dict that translates the numeric value to a label
|
|
40
|
+
*/
|
|
41
|
+
export declare function useD3OrdinalAxis(mapping: {
|
|
42
|
+
[k: number]: string;
|
|
43
|
+
}): (axisSize?: number) => AxisProps<string>;
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=use-d3-axis.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-d3-axis.d.ts","sourceRoot":"","sources":["../src/use-d3-axis.tsx"],"names":[],"mappings":"AAsBA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;AAE1E,UAAU,SAAS,CAAC,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAChB,aAAa,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACzC,KAAK,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,IAAI,EAAE,QAAQ,GAAG,UAAU,CAAC;CAC/B;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACvB,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,GAAG,EAClD,eAAe,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAC/C,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,GAChB,CAAC,KAAK,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,CAgBlC;AAOD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC3B,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,EACnB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAC1B,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC,MAAM,CAAC,CAE1C;AAOD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC,IAAI,CAAC,CAE9G;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC,MAAM,CAAC,CAK3G"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { scaleLinear, scaleTime } from 'd3-scale';
|
|
18
|
+
import { useCallback, useMemo } from 'react';
|
|
19
|
+
import useDeepCompare from './use-deep-compare';
|
|
20
|
+
/**
|
|
21
|
+
* An underlying helper hook that uses d3 to create a function for getting the axis properties for an axis based
|
|
22
|
+
* on the pixel size of that axis. This hook should not be used directly, use one of the wrappers for it,
|
|
23
|
+
* e.g. useD3LinearAxis or useD3TimeAxis
|
|
24
|
+
*
|
|
25
|
+
* @param data the underlying data for the axis
|
|
26
|
+
* @param domain the target domain for the axis
|
|
27
|
+
* @param getScale a function to get the d3Scale instance with domain applied
|
|
28
|
+
* @param domainFormatter a function to translate the d3 domain back into a recharts domain
|
|
29
|
+
*/
|
|
30
|
+
export function useD3Axis(data, getScale, domainFormatter, domain) {
|
|
31
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
32
|
+
const valueScale = useMemo(() => getScale(data, domain), useDeepCompare([data, domain, getScale]));
|
|
33
|
+
return useCallback((axisSize) => {
|
|
34
|
+
const scale = valueScale.range([0, axisSize]).nice();
|
|
35
|
+
return {
|
|
36
|
+
domain: domainFormatter(scale.domain()),
|
|
37
|
+
tickFormatter: scale.tickFormat(),
|
|
38
|
+
ticks: scale.ticks(),
|
|
39
|
+
type: 'number',
|
|
40
|
+
};
|
|
41
|
+
}, [domainFormatter, valueScale]);
|
|
42
|
+
}
|
|
43
|
+
/** Helper function for getting a linear d3Scale instance, based on data and domain */
|
|
44
|
+
const getLinearScale = (data, domain) => {
|
|
45
|
+
return scaleLinear().domain([data ? Math.min(...data) : domain[0], data ? Math.max(...data) : domain[1]]);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Hook that returns a function that generates the props for a linearly scaled axis based on the size in pixels of the
|
|
49
|
+
* axis
|
|
50
|
+
*
|
|
51
|
+
* @param data the underlying data for the axis
|
|
52
|
+
* @param domain the target domain for the axis
|
|
53
|
+
*/
|
|
54
|
+
export function useD3LinearAxis(data, domain) {
|
|
55
|
+
return useD3Axis(data, getLinearScale, (dm) => dm, domain);
|
|
56
|
+
}
|
|
57
|
+
/** Helper function for getting a time d3Scale instance, based on data and domain */
|
|
58
|
+
const getTimeScale = (data, domain) => {
|
|
59
|
+
return scaleTime().domain([data ? data[0] : domain[0], data ? data[data.length - 1] : domain[1]]);
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Hook that returns a function that generates the props for a time scaled axis based on the size in pixels of the
|
|
63
|
+
* axis
|
|
64
|
+
*
|
|
65
|
+
* @param data the underlying data for the axis
|
|
66
|
+
* @param domain the target domain for the axis
|
|
67
|
+
*/
|
|
68
|
+
export function useD3TimeAxis(data, domain) {
|
|
69
|
+
return useD3Axis(data, getTimeScale, (dm) => dm, domain);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Hook that returns a function that generates the props for an ordinal (categorical) scaled axis, doesn't actually use
|
|
73
|
+
* the ordinal scale as it doesn't need to, but has the same returned api as the others so it can be substituted in.
|
|
74
|
+
*
|
|
75
|
+
* @param mapping a mapping dict that translates the numeric value to a label
|
|
76
|
+
*/
|
|
77
|
+
export function useD3OrdinalAxis(mapping) {
|
|
78
|
+
return () => ({
|
|
79
|
+
tickFormatter: (tick) => mapping[tick],
|
|
80
|
+
type: 'category',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=use-d3-axis.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-d3-axis.js","sourceRoot":"","sources":["../src/use-d3-axis.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAA0B,WAAW,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAE7C,OAAO,cAAc,MAAM,oBAAoB,CAAC;AAYhD;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CACrB,IAAc,EACd,QAAkD,EAClD,eAA+C,E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CAAC,IAAiB,EAAE,MAAoB,EAA6B,EAAE;IACxF,OAAO,SAAS,EAAE,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACtG,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,IAAiB,EAAE,MAAqB;IAClE,OAAO,SAAS,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgC;IAC7D,OAAO,GAAsB,EAAE,CAAC,CAAC;QAC7B,aAAa,EAAE,CAAC,IAAY,EAAU,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;QACtD,IAAI,EAAE,UAAU;KACnB,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A helper hook that allows for objects to be deeply compared rather than being compared by reference.
|
|
3
|
+
*
|
|
4
|
+
* An example usage with useEffect, where deepObject is a complex object, would be:
|
|
5
|
+
*
|
|
6
|
+
* useEffect(() => {
|
|
7
|
+
* doSomethingHeavy(deepObject)
|
|
8
|
+
* }, useDeepCompare([deepObject]))
|
|
9
|
+
*
|
|
10
|
+
* @param value - an array of values to check the equality of
|
|
11
|
+
*/
|
|
12
|
+
export default function useDeepCompare<T>(value: T): T;
|
|
13
|
+
//# sourceMappingURL=use-deep-compare.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-deep-compare.d.ts","sourceRoot":"","sources":["../src/use-deep-compare.tsx"],"names":[],"mappings":"AAmBA;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAQrD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import isEqual from 'lodash/isEqual';
|
|
18
|
+
import { useRef } from 'react';
|
|
19
|
+
/**
|
|
20
|
+
* A helper hook that allows for objects to be deeply compared rather than being compared by reference.
|
|
21
|
+
*
|
|
22
|
+
* An example usage with useEffect, where deepObject is a complex object, would be:
|
|
23
|
+
*
|
|
24
|
+
* useEffect(() => {
|
|
25
|
+
* doSomethingHeavy(deepObject)
|
|
26
|
+
* }, useDeepCompare([deepObject]))
|
|
27
|
+
*
|
|
28
|
+
* @param value - an array of values to check the equality of
|
|
29
|
+
*/
|
|
30
|
+
export default function useDeepCompare(value) {
|
|
31
|
+
const ref = useRef();
|
|
32
|
+
if (!isEqual(value, ref.current)) {
|
|
33
|
+
ref.current = value;
|
|
34
|
+
}
|
|
35
|
+
return ref.current;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=use-deep-compare.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-deep-compare.js","sourceRoot":"","sources":["../src/use-deep-compare.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,OAAO,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAI,KAAQ;IAC9C,MAAM,GAAG,GAAG,MAAM,EAAK,CAAC;IAExB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,OAAO,GAAG,CAAC,OAAO,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface DimensionObject {
|
|
2
|
+
bottom: number;
|
|
3
|
+
height: number;
|
|
4
|
+
left: number;
|
|
5
|
+
right: number;
|
|
6
|
+
top: number;
|
|
7
|
+
width: number;
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
}
|
|
11
|
+
export type UseDimensionsHook<T extends HTMLElement> = [(node: T) => void, {
|
|
12
|
+
[k: string]: number;
|
|
13
|
+
}, T];
|
|
14
|
+
/**
|
|
15
|
+
* The useDimensions hook allows for a component to track the dimensions of a given element by passing the returned ref
|
|
16
|
+
* to it.
|
|
17
|
+
*
|
|
18
|
+
* @param liveMeasure whether to recalculate dimensions when the user scrolls or resizes the page.
|
|
19
|
+
*/
|
|
20
|
+
declare function useDimensions<T extends HTMLElement>(liveMeasure?: boolean): UseDimensionsHook<T>;
|
|
21
|
+
export default useDimensions;
|
|
22
|
+
//# sourceMappingURL=use-dimensions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-dimensions.d.ts","sourceRoot":"","sources":["../src/use-dimensions.tsx"],"names":[],"mappings":"AAkBA,MAAM,WAAW,eAAe;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,WAAW,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,EAAE;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,EAAE,CAAC,CAAC,CAAC;AAEvG;;;;;GAKG;AACH,iBAAS,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,WAAW,UAAO,GAAG,iBAAiB,CAAC,CAAC,CAAC,CA2BtF;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { useCallback, useLayoutEffect, useState } from 'react';
|
|
18
|
+
/**
|
|
19
|
+
* The useDimensions hook allows for a component to track the dimensions of a given element by passing the returned ref
|
|
20
|
+
* to it.
|
|
21
|
+
*
|
|
22
|
+
* @param liveMeasure whether to recalculate dimensions when the user scrolls or resizes the page.
|
|
23
|
+
*/
|
|
24
|
+
function useDimensions(liveMeasure = true) {
|
|
25
|
+
const [dimensions, setDimensions] = useState({});
|
|
26
|
+
const [node, setNode] = useState(null);
|
|
27
|
+
const ref = useCallback((_node) => {
|
|
28
|
+
setNode(_node);
|
|
29
|
+
}, []);
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
if (node) {
|
|
32
|
+
const measure = () => window.requestAnimationFrame(() => setDimensions(node.getBoundingClientRect()));
|
|
33
|
+
measure();
|
|
34
|
+
if (liveMeasure) {
|
|
35
|
+
window.addEventListener('resize', measure);
|
|
36
|
+
window.addEventListener('scroll', measure);
|
|
37
|
+
return () => {
|
|
38
|
+
window.removeEventListener('resize', measure);
|
|
39
|
+
window.removeEventListener('scroll', measure);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, [liveMeasure, node]);
|
|
44
|
+
return [ref, dimensions, node];
|
|
45
|
+
}
|
|
46
|
+
export default useDimensions;
|
|
47
|
+
//# sourceMappingURL=use-dimensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-dimensions.js","sourceRoot":"","sources":["../src/use-dimensions.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAe/D;;;;;GAKG;AACH,SAAS,aAAa,CAAwB,WAAW,GAAG,IAAI;IAC5D,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IACjD,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAI,IAAI,CAAC,CAAC;IAE1C,MAAM,GAAG,GAAG,WAAW,CAAC,CAAC,KAAQ,EAAE,EAAE;QACjC,OAAO,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,eAAe,CAAC,GAAG,EAAE;QACjB,IAAI,IAAI,EAAE,CAAC;YACP,MAAM,OAAO,GAAG,GAAW,EAAE,CACzB,MAAM,CAAC,qBAAqB,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAC;YACpF,OAAO,EAAE,CAAC;YAEV,IAAI,WAAW,EAAE,CAAC;gBACd,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;gBAC3C,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;gBAE3C,OAAO,GAAG,EAAE;oBACR,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;oBAC9C,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;gBAClD,CAAC,CAAC;YACN,CAAC;QACL,CAAC;IACL,CAAC,EAAE,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC;IAExB,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023 Impulse Innovations Limited
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { MutableRefObject } from 'react';
|
|
18
|
+
/**
|
|
19
|
+
* The useIntersectionObserver hook allows a component to track whether an element is intersecting with an ancestor
|
|
20
|
+
* element or with a top-level document's viewport.
|
|
21
|
+
*
|
|
22
|
+
* @param ref the ref to the element that is to be observed
|
|
23
|
+
* @param rootMargin the amount of the element that is to be intersecting for the observer's callback to be executed.
|
|
24
|
+
* @param threshold Either a single number or an array of numbers which indicate at what percentage of the target's
|
|
25
|
+
* visibility the observer's callback should be executed.
|
|
26
|
+
* @returns boolean indicating whether element is intersecting or not
|
|
27
|
+
*/
|
|
28
|
+
declare function useIntersectionObserver<T extends Element>(ref: MutableRefObject<T>, rootMargin?: string, threshold?: number): boolean;
|
|
29
|
+
export default useIntersectionObserver;
|
|
30
|
+
//# sourceMappingURL=use-intersection-observer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intersection-observer.d.ts","sourceRoot":"","sources":["../src/use-intersection-observer.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,gBAAgB,EAAuB,MAAM,OAAO,CAAC;AAE9D;;;;;;;;;GASG;AAEH,iBAAS,uBAAuB,CAAC,CAAC,SAAS,OAAO,EAC9C,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAC,EACxB,UAAU,SAAQ,EAClB,SAAS,SAAM,GAChB,OAAO,CAuBT;AAED,eAAe,uBAAuB,CAAC"}
|