Comparing version 3.0.0 to 3.1.0
@@ -18,2 +18,3 @@ /** | ||
@example | ||
``` | ||
@@ -23,3 +24,3 @@ import domLoaded from 'dom-loaded'; | ||
if (domLoaded.hasLoaded) { | ||
console.log('The DOM has already finished loading.') | ||
console.log('The DOM has already finished loading.'); | ||
} | ||
@@ -29,4 +30,16 @@ ``` | ||
readonly hasLoaded: boolean; | ||
/** | ||
An `AbortSignal` that triggers when the DOM finishes loading or immediately if it has already loaded. | ||
@example | ||
``` | ||
import domLoaded from 'dom-loaded'; | ||
showLoader({signal: domLoaded.signal}); | ||
``` | ||
*/ | ||
readonly signal: AbortSignal; | ||
}; | ||
export default domLoaded; |
12
index.js
const hasLoaded = () => document.readyState === 'interactive' || document.readyState === 'complete'; | ||
const controller = new AbortController(); | ||
const domLoaded = new Promise(resolve => { | ||
if (hasLoaded()) { | ||
resolve(); | ||
controller.abort(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', () => { | ||
resolve(); | ||
controller.abort(); | ||
}, { | ||
capture: true, | ||
once: true, | ||
passive: true | ||
passive: true, | ||
}); | ||
@@ -20,3 +24,7 @@ } | ||
Object.defineProperty(domLoaded, 'hasLoaded', { | ||
get: () => hasLoaded() | ||
get: hasLoaded, | ||
}); | ||
Object.defineProperty(domLoaded, 'signal', { | ||
get: () => controller.signal, | ||
}); |
{ | ||
"name": "dom-loaded", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Check when the DOM has loaded like `DOMContentLoaded`", | ||
@@ -15,2 +15,4 @@ "license": "MIT", | ||
"exports": "./index.js", | ||
"types": "./index.d.ts", | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -21,3 +23,3 @@ "node": ">=12" | ||
"// TODO: Enable when JSOM support ESM - test": "xo && ava && tsd", | ||
"test": "xo && tsd" | ||
"test": "xo" | ||
}, | ||
@@ -43,3 +45,3 @@ "files": [ | ||
"tsd": "^0.14.0", | ||
"xo": "^0.39.1" | ||
"xo": "^0.54.0" | ||
}, | ||
@@ -46,0 +48,0 @@ "xo": { |
@@ -9,5 +9,5 @@ # dom-loaded | ||
```sh | ||
npm install dom-loaded | ||
``` | ||
$ npm install dom-loaded | ||
``` | ||
@@ -37,4 +37,10 @@ ## Usage | ||
### domLoaded.signal | ||
Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | ||
An `AbortSignal` that triggers when the DOM finishes loading or immediately if it has already loaded. | ||
## Related | ||
- [element-ready](https://github.com/sindresorhus/element-ready) - Detect when an element is ready in the DOM |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4589
57
45