@codingheads/sticky-header
Version:
A library that allows you to create sticky headers. It uses `position: sticky` and IntersectionObserver
56 lines (46 loc) • 1.59 kB
JavaScript
;
/** @format */
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onReady = exports.onInteractive = exports.onComplete = exports.onReadyState = void 0;
/**
* Run a callback when the document.readyState has a certain value
* @param {string|string[]} state - the state(s) when to run the callback
* @param {Function} callback - the callback to run
*/
var onReadyState = function onReadyState(state, callback) {
var validStates = Array.isArray(state) ? state : [state];
var checkAndRun = function checkAndRun() {
if (validStates.includes(document.readyState)) {
document.removeEventListener('readystatechange', checkAndRun);
callback();
return true;
}
return false;
};
if (!checkAndRun()) {
document.addEventListener('readystatechange', checkAndRun);
}
};
exports.onReadyState = onReadyState;
/**
* Run a callback when the document.readyState is 'complete'
* (DOM + assets are loaded)
* @param {Function} callback - the callback to run
*/
var onComplete = function onComplete(callback) {
return exports.onReadyState(['complete', 'loaded'], callback);
};
exports.onComplete = onComplete;
/**
* Run a callback when the document.readyState is 'interactive'
* (DOM is loaded - equivalent to DOMContentLoaded)
* @param {Function} callback - the callback to run
*/
var onInteractive = function onInteractive(callback) {
return exports.onReadyState('interactive', callback);
};
exports.onInteractive = onInteractive;
exports.onReady = exports.onInteractive;
exports["default"] = exports.onReady;