Skip to content

Commit

Permalink
Fixes eslint issues in back-to-top.js (#685)
Browse files Browse the repository at this point in the history
* fixes eslint issues in back-to-top.js

* fix: change quotes to single

---------

Co-authored-by: Lee Mills <lee@leemills.dev>
  • Loading branch information
markconroy and millnut authored Jan 28, 2025
1 parent d48b653 commit 80f4eca
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions js/back-to-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file Drupal behavior for 'back to top' link.
*/

(function (Drupal) {
(function localgovBackToTopScript(Drupal) {
Drupal.behaviors.localgovBackToTop = {
attach(context) {
/**
Expand All @@ -19,28 +19,29 @@
* past the top of the viewport. In practices, there's only one of
* these since we've only targeted one element.
*/
const [backToTop] = once('back-to-top', '.back-to-top', context);
const [backToTopTarget] = once(
'back-to-top-target',
'.back-to-top-target',
context,
);

function observerCallback(entries) {
entries.forEach((entry) => {
backToTop.hidden = (
backToTop.hidden =
entry.isIntersecting ||
(!entry.isIntersecting && entry.boundingClientRect.top <= 0)
) ? false : 'until-hidden';
? false
: 'until-hidden';
});
}

const [backToTop] = once('back-to-top', '.back-to-top', context);
const [backToTopTarget] = once(
'back-to-top-target',
'.back-to-top-target',
context,
);
const minContentViewportRatio = parseFloat(
backToTop?.dataset?.minContentViewportRatio ?? 1.5,
10,
);
const viewportHeight = window.innerHeight;
const documentHeight = document.documentElement.offsetHeight;
let intersectionObserver;

if (
!backToTop ||
Expand All @@ -53,10 +54,12 @@
// Create an element absolutely positioned at our threshold.
backToTopTarget.style.position = 'absolute';
backToTopTarget.style.top = `${viewportHeight * minContentViewportRatio}px`;
backToTop.addEventListener('click', (event) => event.target.hidden = 'until-found');
backToTop.addEventListener('click', (event) => {
event.target.hidden = 'until-found';
});

// Create an IntersectionObserver.
intersectionObserver = new IntersectionObserver(observerCallback, {
const intersectionObserver = new IntersectionObserver(observerCallback, {
rootMargin: '16px',
});
intersectionObserver.observe(backToTopTarget);
Expand Down

0 comments on commit 80f4eca

Please sign in to comment.