61 lines
2.8 KiB
JavaScript
61 lines
2.8 KiB
JavaScript
|
"use strict";
|
||
|
'use client';
|
||
|
|
||
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = useScrollTrigger;
|
||
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||
|
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
||
|
var React = _interopRequireWildcard(require("react"));
|
||
|
const _excluded = ["getTrigger", "target"];
|
||
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||
|
function defaultTrigger(store, options) {
|
||
|
const {
|
||
|
disableHysteresis = false,
|
||
|
threshold = 100,
|
||
|
target
|
||
|
} = options;
|
||
|
const previous = store.current;
|
||
|
if (target) {
|
||
|
// Get vertical scroll
|
||
|
store.current = target.pageYOffset !== undefined ? target.pageYOffset : target.scrollTop;
|
||
|
}
|
||
|
if (!disableHysteresis && previous !== undefined) {
|
||
|
if (store.current < previous) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return store.current > threshold;
|
||
|
}
|
||
|
const defaultTarget = typeof window !== 'undefined' ? window : null;
|
||
|
function useScrollTrigger(options = {}) {
|
||
|
const {
|
||
|
getTrigger = defaultTrigger,
|
||
|
target = defaultTarget
|
||
|
} = options,
|
||
|
other = (0, _objectWithoutPropertiesLoose2.default)(options, _excluded);
|
||
|
const store = React.useRef();
|
||
|
const [trigger, setTrigger] = React.useState(() => getTrigger(store, other));
|
||
|
React.useEffect(() => {
|
||
|
const handleScroll = () => {
|
||
|
setTrigger(getTrigger(store, (0, _extends2.default)({
|
||
|
target
|
||
|
}, other)));
|
||
|
};
|
||
|
handleScroll(); // Re-evaluate trigger when dependencies change
|
||
|
target.addEventListener('scroll', handleScroll, {
|
||
|
passive: true
|
||
|
});
|
||
|
return () => {
|
||
|
target.removeEventListener('scroll', handleScroll, {
|
||
|
passive: true
|
||
|
});
|
||
|
};
|
||
|
// See Option 3. https://github.com/facebook/react/issues/14476#issuecomment-471199055
|
||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
|
}, [target, getTrigger, JSON.stringify(other)]);
|
||
|
return trigger;
|
||
|
}
|