@folklore/hooks 0.0.6
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/cjs.js +259 -0
- package/dist/es.js +246 -0
- package/package.json +52 -0
package/dist/cjs.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
6
|
+
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
|
7
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
8
|
+
var react = require('react');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
|
|
13
|
+
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
|
|
14
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
15
|
+
|
|
16
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
+
|
|
18
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
+
var observersCache = new Map();
|
|
20
|
+
|
|
21
|
+
var getOptionsKey = function getOptionsKey(_ref) {
|
|
22
|
+
var _ref$root = _ref.root,
|
|
23
|
+
root = _ref$root === void 0 ? null : _ref$root,
|
|
24
|
+
rootMargin = _ref.rootMargin,
|
|
25
|
+
_ref$threshold = _ref.threshold,
|
|
26
|
+
threshold = _ref$threshold === void 0 ? null : _ref$threshold;
|
|
27
|
+
return "root_".concat(root, "_rootMargin_").concat(rootMargin || null, "_threshold_").concat(threshold);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
var createObserver = function createObserver(Observer) {
|
|
31
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
32
|
+
var subscribers = [];
|
|
33
|
+
|
|
34
|
+
var addSubscriber = function addSubscriber(element, callback) {
|
|
35
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
36
|
+
return it.element === element;
|
|
37
|
+
}) || null;
|
|
38
|
+
|
|
39
|
+
if (currentSubscriber !== null) {
|
|
40
|
+
return subscribers.map(function (it) {
|
|
41
|
+
return it.element === element && it.callbacks.indexOf(callback) === -1 ? _objectSpread(_objectSpread({}, it), {}, {
|
|
42
|
+
callbacks: [].concat(_toConsumableArray__default["default"](it.callbacks), [callback])
|
|
43
|
+
}) : it;
|
|
44
|
+
}).filter(function (it) {
|
|
45
|
+
return it.callbacks.length > 0;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return [].concat(_toConsumableArray__default["default"](subscribers), [{
|
|
50
|
+
element: element,
|
|
51
|
+
callbacks: [callback]
|
|
52
|
+
}]);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
var removeSubscriber = function removeSubscriber(element, callback) {
|
|
56
|
+
return subscribers.map(function (it) {
|
|
57
|
+
return it.element === element ? _objectSpread(_objectSpread({}, it), {}, {
|
|
58
|
+
callbacks: it.callbacks.filter(function (subCallback) {
|
|
59
|
+
return subCallback !== callback;
|
|
60
|
+
})
|
|
61
|
+
}) : it;
|
|
62
|
+
}).filter(function (it) {
|
|
63
|
+
return it.callbacks.length > 0;
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
var onUpdate = function onUpdate(entries) {
|
|
68
|
+
entries.forEach(function (entry) {
|
|
69
|
+
subscribers.forEach(function (_ref2) {
|
|
70
|
+
var element = _ref2.element,
|
|
71
|
+
callbacks = _ref2.callbacks;
|
|
72
|
+
|
|
73
|
+
if (element === entry.target) {
|
|
74
|
+
callbacks.forEach(function (callback) {
|
|
75
|
+
callback(entry);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
var observer = new Observer(onUpdate, options);
|
|
83
|
+
|
|
84
|
+
var unsubscribe = function unsubscribe(element) {
|
|
85
|
+
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
86
|
+
subscribers = removeSubscriber(element, callback);
|
|
87
|
+
|
|
88
|
+
if (typeof observer.unobserve === 'undefined') {
|
|
89
|
+
observer.disconnect();
|
|
90
|
+
subscribers.forEach(function (subscriber) {
|
|
91
|
+
observer.observe(subscriber.element);
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
97
|
+
return it.element === element;
|
|
98
|
+
}) || null;
|
|
99
|
+
|
|
100
|
+
if (currentSubscriber === null) {
|
|
101
|
+
observer.unobserve(element);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
var subscribe = function subscribe(element, callback) {
|
|
106
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
107
|
+
return it.element === element;
|
|
108
|
+
}) || null;
|
|
109
|
+
subscribers = addSubscriber(element, callback);
|
|
110
|
+
|
|
111
|
+
if (currentSubscriber === null) {
|
|
112
|
+
observer.observe(element);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
subscribe: subscribe,
|
|
118
|
+
unsubscribe: unsubscribe,
|
|
119
|
+
observer: observer
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
var getObserver = function getObserver(Observer) {
|
|
124
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
125
|
+
var observerKey = getOptionsKey(options);
|
|
126
|
+
|
|
127
|
+
if (!observersCache.has(Observer)) {
|
|
128
|
+
observersCache.set(Observer, {});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
var observers = observersCache.get(Observer);
|
|
132
|
+
|
|
133
|
+
if (typeof observers[observerKey] === 'undefined') {
|
|
134
|
+
observers[observerKey] = createObserver(Observer, options);
|
|
135
|
+
observersCache.set(Observer, observers);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return observers[observerKey];
|
|
139
|
+
};
|
|
140
|
+
var useObserver = function useObserver(Observer) {
|
|
141
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
142
|
+
var initialEntry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
143
|
+
var _opts$root = opts.root,
|
|
144
|
+
root = _opts$root === void 0 ? null : _opts$root,
|
|
145
|
+
_opts$rootMargin = opts.rootMargin,
|
|
146
|
+
rootMargin = _opts$rootMargin === void 0 ? null : _opts$rootMargin,
|
|
147
|
+
_opts$threshold = opts.threshold,
|
|
148
|
+
threshold = _opts$threshold === void 0 ? null : _opts$threshold,
|
|
149
|
+
_opts$disabled = opts.disabled,
|
|
150
|
+
disabled = _opts$disabled === void 0 ? false : _opts$disabled;
|
|
151
|
+
|
|
152
|
+
var _useState = react.useState(initialEntry),
|
|
153
|
+
_useState2 = _slicedToArray__default["default"](_useState, 2),
|
|
154
|
+
entry = _useState2[0],
|
|
155
|
+
setEntry = _useState2[1];
|
|
156
|
+
|
|
157
|
+
var nodeRef = react.useRef(null);
|
|
158
|
+
var currentElement = react.useRef(null);
|
|
159
|
+
var elementChanged = nodeRef.current !== currentElement.current;
|
|
160
|
+
react.useEffect(function () {
|
|
161
|
+
var nodeElement = nodeRef.current;
|
|
162
|
+
|
|
163
|
+
var callback = function callback(newEntry) {
|
|
164
|
+
return setEntry(newEntry);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
var unsubscribe = null;
|
|
168
|
+
|
|
169
|
+
if (nodeElement !== null) {
|
|
170
|
+
var newOpts = {};
|
|
171
|
+
|
|
172
|
+
if (root !== null) {
|
|
173
|
+
newOpts.root = root;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (rootMargin !== null) {
|
|
177
|
+
newOpts.rootMargin = rootMargin;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (threshold !== null) {
|
|
181
|
+
newOpts.threshold = threshold;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
var _getObserver = getObserver(Observer, newOpts),
|
|
185
|
+
subscribe = _getObserver.subscribe,
|
|
186
|
+
localUnsubscribe = _getObserver.unsubscribe;
|
|
187
|
+
|
|
188
|
+
unsubscribe = localUnsubscribe;
|
|
189
|
+
subscribe(nodeElement, callback);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
currentElement.current = nodeElement;
|
|
193
|
+
return function () {
|
|
194
|
+
if (unsubscribe !== null) {
|
|
195
|
+
unsubscribe(nodeElement, callback);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}, [Observer, elementChanged, disabled, root, rootMargin, threshold]);
|
|
199
|
+
return {
|
|
200
|
+
ref: nodeRef,
|
|
201
|
+
entry: entry
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Intersection Observer
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
var intersectionObserverInitialEntry = {
|
|
209
|
+
target: null,
|
|
210
|
+
time: null,
|
|
211
|
+
isVisible: false,
|
|
212
|
+
isIntersecting: false,
|
|
213
|
+
intersectionRatio: 0,
|
|
214
|
+
intersectionRect: null,
|
|
215
|
+
boundingClientRect: null,
|
|
216
|
+
rootBounds: null
|
|
217
|
+
};
|
|
218
|
+
var useIntersectionObserver = function useIntersectionObserver() {
|
|
219
|
+
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
220
|
+
_ref3$root = _ref3.root,
|
|
221
|
+
root = _ref3$root === void 0 ? null : _ref3$root,
|
|
222
|
+
_ref3$rootMargin = _ref3.rootMargin,
|
|
223
|
+
rootMargin = _ref3$rootMargin === void 0 ? '0px' : _ref3$rootMargin,
|
|
224
|
+
_ref3$threshold = _ref3.threshold,
|
|
225
|
+
threshold = _ref3$threshold === void 0 ? [0, 1.0] : _ref3$threshold,
|
|
226
|
+
_ref3$disabled = _ref3.disabled,
|
|
227
|
+
disabled = _ref3$disabled === void 0 ? false : _ref3$disabled;
|
|
228
|
+
|
|
229
|
+
return useObserver(IntersectionObserver, {
|
|
230
|
+
root: root,
|
|
231
|
+
rootMargin: rootMargin,
|
|
232
|
+
threshold: threshold,
|
|
233
|
+
disabled: disabled
|
|
234
|
+
}, intersectionObserverInitialEntry);
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Resize Observer
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
var resizeObserverInitialEntry = {
|
|
241
|
+
target: null,
|
|
242
|
+
contentRect: null,
|
|
243
|
+
contentBoxSize: null,
|
|
244
|
+
borderBoxSize: null
|
|
245
|
+
};
|
|
246
|
+
var useResizeObserver = function useResizeObserver() {
|
|
247
|
+
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
248
|
+
_ref4$disabled = _ref4.disabled,
|
|
249
|
+
disabled = _ref4$disabled === void 0 ? false : _ref4$disabled;
|
|
250
|
+
|
|
251
|
+
return useObserver(ResizeObserver, {
|
|
252
|
+
disabled: disabled
|
|
253
|
+
}, resizeObserverInitialEntry);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
exports.getObserver = getObserver;
|
|
257
|
+
exports.useIntersectionObserver = useIntersectionObserver;
|
|
258
|
+
exports.useObserver = useObserver;
|
|
259
|
+
exports.useResizeObserver = useResizeObserver;
|
package/dist/es.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
|
+
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
3
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
4
|
+
import { useState, useRef, useEffect } from 'react';
|
|
5
|
+
|
|
6
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
7
|
+
|
|
8
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
9
|
+
var observersCache = new Map();
|
|
10
|
+
|
|
11
|
+
var getOptionsKey = function getOptionsKey(_ref) {
|
|
12
|
+
var _ref$root = _ref.root,
|
|
13
|
+
root = _ref$root === void 0 ? null : _ref$root,
|
|
14
|
+
rootMargin = _ref.rootMargin,
|
|
15
|
+
_ref$threshold = _ref.threshold,
|
|
16
|
+
threshold = _ref$threshold === void 0 ? null : _ref$threshold;
|
|
17
|
+
return "root_".concat(root, "_rootMargin_").concat(rootMargin || null, "_threshold_").concat(threshold);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var createObserver = function createObserver(Observer) {
|
|
21
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
22
|
+
var subscribers = [];
|
|
23
|
+
|
|
24
|
+
var addSubscriber = function addSubscriber(element, callback) {
|
|
25
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
26
|
+
return it.element === element;
|
|
27
|
+
}) || null;
|
|
28
|
+
|
|
29
|
+
if (currentSubscriber !== null) {
|
|
30
|
+
return subscribers.map(function (it) {
|
|
31
|
+
return it.element === element && it.callbacks.indexOf(callback) === -1 ? _objectSpread(_objectSpread({}, it), {}, {
|
|
32
|
+
callbacks: [].concat(_toConsumableArray(it.callbacks), [callback])
|
|
33
|
+
}) : it;
|
|
34
|
+
}).filter(function (it) {
|
|
35
|
+
return it.callbacks.length > 0;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return [].concat(_toConsumableArray(subscribers), [{
|
|
40
|
+
element: element,
|
|
41
|
+
callbacks: [callback]
|
|
42
|
+
}]);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
var removeSubscriber = function removeSubscriber(element, callback) {
|
|
46
|
+
return subscribers.map(function (it) {
|
|
47
|
+
return it.element === element ? _objectSpread(_objectSpread({}, it), {}, {
|
|
48
|
+
callbacks: it.callbacks.filter(function (subCallback) {
|
|
49
|
+
return subCallback !== callback;
|
|
50
|
+
})
|
|
51
|
+
}) : it;
|
|
52
|
+
}).filter(function (it) {
|
|
53
|
+
return it.callbacks.length > 0;
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
var onUpdate = function onUpdate(entries) {
|
|
58
|
+
entries.forEach(function (entry) {
|
|
59
|
+
subscribers.forEach(function (_ref2) {
|
|
60
|
+
var element = _ref2.element,
|
|
61
|
+
callbacks = _ref2.callbacks;
|
|
62
|
+
|
|
63
|
+
if (element === entry.target) {
|
|
64
|
+
callbacks.forEach(function (callback) {
|
|
65
|
+
callback(entry);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var observer = new Observer(onUpdate, options);
|
|
73
|
+
|
|
74
|
+
var unsubscribe = function unsubscribe(element) {
|
|
75
|
+
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
76
|
+
subscribers = removeSubscriber(element, callback);
|
|
77
|
+
|
|
78
|
+
if (typeof observer.unobserve === 'undefined') {
|
|
79
|
+
observer.disconnect();
|
|
80
|
+
subscribers.forEach(function (subscriber) {
|
|
81
|
+
observer.observe(subscriber.element);
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
87
|
+
return it.element === element;
|
|
88
|
+
}) || null;
|
|
89
|
+
|
|
90
|
+
if (currentSubscriber === null) {
|
|
91
|
+
observer.unobserve(element);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
var subscribe = function subscribe(element, callback) {
|
|
96
|
+
var currentSubscriber = subscribers.find(function (it) {
|
|
97
|
+
return it.element === element;
|
|
98
|
+
}) || null;
|
|
99
|
+
subscribers = addSubscriber(element, callback);
|
|
100
|
+
|
|
101
|
+
if (currentSubscriber === null) {
|
|
102
|
+
observer.observe(element);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
subscribe: subscribe,
|
|
108
|
+
unsubscribe: unsubscribe,
|
|
109
|
+
observer: observer
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
var getObserver = function getObserver(Observer) {
|
|
114
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
115
|
+
var observerKey = getOptionsKey(options);
|
|
116
|
+
|
|
117
|
+
if (!observersCache.has(Observer)) {
|
|
118
|
+
observersCache.set(Observer, {});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
var observers = observersCache.get(Observer);
|
|
122
|
+
|
|
123
|
+
if (typeof observers[observerKey] === 'undefined') {
|
|
124
|
+
observers[observerKey] = createObserver(Observer, options);
|
|
125
|
+
observersCache.set(Observer, observers);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return observers[observerKey];
|
|
129
|
+
};
|
|
130
|
+
var useObserver = function useObserver(Observer) {
|
|
131
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
132
|
+
var initialEntry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
133
|
+
var _opts$root = opts.root,
|
|
134
|
+
root = _opts$root === void 0 ? null : _opts$root,
|
|
135
|
+
_opts$rootMargin = opts.rootMargin,
|
|
136
|
+
rootMargin = _opts$rootMargin === void 0 ? null : _opts$rootMargin,
|
|
137
|
+
_opts$threshold = opts.threshold,
|
|
138
|
+
threshold = _opts$threshold === void 0 ? null : _opts$threshold,
|
|
139
|
+
_opts$disabled = opts.disabled,
|
|
140
|
+
disabled = _opts$disabled === void 0 ? false : _opts$disabled;
|
|
141
|
+
|
|
142
|
+
var _useState = useState(initialEntry),
|
|
143
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
144
|
+
entry = _useState2[0],
|
|
145
|
+
setEntry = _useState2[1];
|
|
146
|
+
|
|
147
|
+
var nodeRef = useRef(null);
|
|
148
|
+
var currentElement = useRef(null);
|
|
149
|
+
var elementChanged = nodeRef.current !== currentElement.current;
|
|
150
|
+
useEffect(function () {
|
|
151
|
+
var nodeElement = nodeRef.current;
|
|
152
|
+
|
|
153
|
+
var callback = function callback(newEntry) {
|
|
154
|
+
return setEntry(newEntry);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
var unsubscribe = null;
|
|
158
|
+
|
|
159
|
+
if (nodeElement !== null) {
|
|
160
|
+
var newOpts = {};
|
|
161
|
+
|
|
162
|
+
if (root !== null) {
|
|
163
|
+
newOpts.root = root;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (rootMargin !== null) {
|
|
167
|
+
newOpts.rootMargin = rootMargin;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (threshold !== null) {
|
|
171
|
+
newOpts.threshold = threshold;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
var _getObserver = getObserver(Observer, newOpts),
|
|
175
|
+
subscribe = _getObserver.subscribe,
|
|
176
|
+
localUnsubscribe = _getObserver.unsubscribe;
|
|
177
|
+
|
|
178
|
+
unsubscribe = localUnsubscribe;
|
|
179
|
+
subscribe(nodeElement, callback);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
currentElement.current = nodeElement;
|
|
183
|
+
return function () {
|
|
184
|
+
if (unsubscribe !== null) {
|
|
185
|
+
unsubscribe(nodeElement, callback);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}, [Observer, elementChanged, disabled, root, rootMargin, threshold]);
|
|
189
|
+
return {
|
|
190
|
+
ref: nodeRef,
|
|
191
|
+
entry: entry
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Intersection Observer
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
var intersectionObserverInitialEntry = {
|
|
199
|
+
target: null,
|
|
200
|
+
time: null,
|
|
201
|
+
isVisible: false,
|
|
202
|
+
isIntersecting: false,
|
|
203
|
+
intersectionRatio: 0,
|
|
204
|
+
intersectionRect: null,
|
|
205
|
+
boundingClientRect: null,
|
|
206
|
+
rootBounds: null
|
|
207
|
+
};
|
|
208
|
+
var useIntersectionObserver = function useIntersectionObserver() {
|
|
209
|
+
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
210
|
+
_ref3$root = _ref3.root,
|
|
211
|
+
root = _ref3$root === void 0 ? null : _ref3$root,
|
|
212
|
+
_ref3$rootMargin = _ref3.rootMargin,
|
|
213
|
+
rootMargin = _ref3$rootMargin === void 0 ? '0px' : _ref3$rootMargin,
|
|
214
|
+
_ref3$threshold = _ref3.threshold,
|
|
215
|
+
threshold = _ref3$threshold === void 0 ? [0, 1.0] : _ref3$threshold,
|
|
216
|
+
_ref3$disabled = _ref3.disabled,
|
|
217
|
+
disabled = _ref3$disabled === void 0 ? false : _ref3$disabled;
|
|
218
|
+
|
|
219
|
+
return useObserver(IntersectionObserver, {
|
|
220
|
+
root: root,
|
|
221
|
+
rootMargin: rootMargin,
|
|
222
|
+
threshold: threshold,
|
|
223
|
+
disabled: disabled
|
|
224
|
+
}, intersectionObserverInitialEntry);
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Resize Observer
|
|
228
|
+
*/
|
|
229
|
+
|
|
230
|
+
var resizeObserverInitialEntry = {
|
|
231
|
+
target: null,
|
|
232
|
+
contentRect: null,
|
|
233
|
+
contentBoxSize: null,
|
|
234
|
+
borderBoxSize: null
|
|
235
|
+
};
|
|
236
|
+
var useResizeObserver = function useResizeObserver() {
|
|
237
|
+
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
238
|
+
_ref4$disabled = _ref4.disabled,
|
|
239
|
+
disabled = _ref4$disabled === void 0 ? false : _ref4$disabled;
|
|
240
|
+
|
|
241
|
+
return useObserver(ResizeObserver, {
|
|
242
|
+
disabled: disabled
|
|
243
|
+
}, resizeObserverInitialEntry);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
export { getObserver, useIntersectionObserver, useObserver, useResizeObserver };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@folklore/hooks",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"description": "React hooks",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"size",
|
|
8
|
+
"ui",
|
|
9
|
+
"utils"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/folkloreinc/folklore-js",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/folkloreinc/folklore-js.git"
|
|
15
|
+
},
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "Folklore",
|
|
18
|
+
"email": "info@folklore.email"
|
|
19
|
+
},
|
|
20
|
+
"contributors": [
|
|
21
|
+
{
|
|
22
|
+
"name": "David Mongeau-Petitpas",
|
|
23
|
+
"email": "dmp@folklore.email"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"name": "Nicolas Roy-Bourdages",
|
|
27
|
+
"email": "nrb@folklore.email"
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
"main": "dist/cjs.js",
|
|
31
|
+
"module": "dist/es.js",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"clean": "rm -rf dist",
|
|
37
|
+
"build": "rollup --config ../../rollup.config.js",
|
|
38
|
+
"prepare": "npm run clean && npm run build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
42
|
+
"react-dom": "^17.0.0 || ^18.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
46
|
+
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"gitHead": "cf8f69f622f7ce5476723a2dea46157be2fe5833"
|
|
52
|
+
}
|