Skip to content

Commit

Permalink
Get rid of helper DOM shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Sep 30, 2023
1 parent 6e41707 commit b7a72da
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 55 deletions.
26 changes: 10 additions & 16 deletions dist/handy-scroll.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@ https://amphiluke.github.io/handy-scroll/

let slice = Array.prototype.slice;

// Precaution to avoid reference errors when imported for SSR (issue #13)
let isDOMAvailable = typeof document === "object" && !!document.documentElement;

let dom = {
isDOMAvailable,

doc: () => isDOMAvailable ? document : null,
html: () => isDOMAvailable ? document.documentElement : null,
body: () => isDOMAvailable ? document.body : null,
// Precaution to avoid reference errors when imported for SSR (issue #13)
isDOMAvailable: typeof document === "object" && !!document.documentElement,

ready(handler) {
if (dom.doc().readyState === "loading") {
dom.doc().addEventListener("DOMContentLoaded", () => void handler(), {once: true});
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => void handler(), {once: true});
} else {
handler();
}
},

$(ref) {
if (typeof ref === "string") { // ref is a selector
return dom.body().querySelector(ref);
return document.body.querySelector(ref);
}
return ref; // ref is already an element
},
Expand All @@ -44,7 +38,7 @@ https://amphiluke.github.io/handy-scroll/
return [ref];
}
if (typeof ref === "string") { // ref is a selector
return slice.call(dom.body().querySelectorAll(ref));
return slice.call(document.body.querySelectorAll(ref));
}
return slice.call(ref); // ref is an array-like object (NodeList or HTMLCollection)
}
Expand All @@ -69,9 +63,9 @@ https://amphiluke.github.io/handy-scroll/

initWidget() {
let instance = this;
let widget = instance.widget = dom.doc().createElement("div");
let widget = instance.widget = document.createElement("div");
widget.classList.add("handy-scroll");
let strut = dom.doc().createElement("div");
let strut = document.createElement("div");
strut.style.width = `${instance.container.scrollWidth}px`;
widget.appendChild(strut);
instance.container.appendChild(widget);
Expand Down Expand Up @@ -139,7 +133,7 @@ https://amphiluke.github.io/handy-scroll/
let containerRect = container.getBoundingClientRect();
let maxVisibleY = scrollBody ?
scrollBody.getBoundingClientRect().bottom :
window.innerHeight || dom.html.clientHeight;
window.innerHeight || document.documentElement.clientHeight;
mustHide = ((containerRect.bottom <= maxVisibleY) || (containerRect.top > maxVisibleY));
}
if (instance.visible === mustHide) {
Expand Down Expand Up @@ -265,7 +259,7 @@ https://amphiluke.github.io/handy-scroll/
*/
destroyDetached() {
instances = instances.filter(instance => {
if (!dom.body().contains(instance.container)) {
if (!document.body.contains(instance.container)) {
instance.destroy();
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/handy-scroll.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 10 additions & 21 deletions dist/handy-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,12 @@ https://amphiluke.github.io/handy-scroll/
})(this, (function () { 'use strict';

var slice = Array.prototype.slice;

// Precaution to avoid reference errors when imported for SSR (issue #13)
var isDOMAvailable = typeof document === "object" && !!document.documentElement;
var dom = {
isDOMAvailable: isDOMAvailable,
doc: function doc() {
return isDOMAvailable ? document : null;
},
html: function html() {
return isDOMAvailable ? document.documentElement : null;
},
body: function body() {
return isDOMAvailable ? document.body : null;
},
// Precaution to avoid reference errors when imported for SSR (issue #13)
isDOMAvailable: typeof document === "object" && !!document.documentElement,
ready: function ready(handler) {
if (dom.doc().readyState === "loading") {
dom.doc().addEventListener("DOMContentLoaded", function () {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", function () {
return void handler();
}, {
once: true
Expand All @@ -38,7 +27,7 @@ https://amphiluke.github.io/handy-scroll/
$: function $(ref) {
if (typeof ref === "string") {
// ref is a selector
return dom.body().querySelector(ref);
return document.body.querySelector(ref);
}
return ref; // ref is already an element
},
Expand All @@ -53,7 +42,7 @@ https://amphiluke.github.io/handy-scroll/
}
if (typeof ref === "string") {
// ref is a selector
return slice.call(dom.body().querySelectorAll(ref));
return slice.call(document.body.querySelectorAll(ref));
}
return slice.call(ref); // ref is an array-like object (NodeList or HTMLCollection)
}
Expand All @@ -78,9 +67,9 @@ https://amphiluke.github.io/handy-scroll/
},
initWidget: function initWidget() {
var instance = this;
var widget = instance.widget = dom.doc().createElement("div");
var widget = instance.widget = document.createElement("div");
widget.classList.add("handy-scroll");
var strut = dom.doc().createElement("div");
var strut = document.createElement("div");
strut.style.width = instance.container.scrollWidth + "px";
widget.appendChild(strut);
instance.container.appendChild(widget);
Expand Down Expand Up @@ -146,7 +135,7 @@ https://amphiluke.github.io/handy-scroll/
var mustHide = widget.scrollWidth <= widget.offsetWidth;
if (!mustHide) {
var containerRect = container.getBoundingClientRect();
var maxVisibleY = scrollBody ? scrollBody.getBoundingClientRect().bottom : window.innerHeight || dom.html.clientHeight;
var maxVisibleY = scrollBody ? scrollBody.getBoundingClientRect().bottom : window.innerHeight || document.documentElement.clientHeight;
mustHide = containerRect.bottom <= maxVisibleY || containerRect.top > maxVisibleY;
}
if (instance.visible === mustHide) {
Expand Down Expand Up @@ -274,7 +263,7 @@ https://amphiluke.github.io/handy-scroll/
*/
destroyDetached: function destroyDetached() {
instances = instances.filter(function (instance) {
if (!dom.body().contains(instance.container)) {
if (!document.body.contains(instance.container)) {
instance.destroy();
return false;
}
Expand Down
Loading

0 comments on commit b7a72da

Please sign in to comment.