Skip to content

Commit

Permalink
🐛 fix(search): close modal when clicking current page result (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo authored Jul 1, 2024
1 parent 0f18ac2 commit f72f454
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions static/js/searchElasticlunr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,24 @@ window.onload = function () {
}
});

results.addEventListener('click', function(event) {
const clickedElement = event.target.closest('a');
if (clickedElement) {
const clickedHref = clickedElement.getAttribute('href');
const currentPageUrl = window.location.href;

// Normalise URLs by removing the text fragment and trailing slash.
const normalizeUrl = (url) => url.split('#')[0].replace(/\/$/, '');

// Check if the clicked link matches the current page.
// If using Ctrl+click or Cmd+click, don't close the modal.
if (normalizeUrl(clickedHref) === normalizeUrl(currentPageUrl) &&
!event.ctrlKey && !event.metaKey) {
closeModal();
}
}
});

// Add touch events to the results.
setupTouchEvents();
},
Expand Down
Loading

0 comments on commit f72f454

Please sign in to comment.