Skip to content

Commit

Permalink
Added a common selection clear function
Browse files Browse the repository at this point in the history
Signed-off-by: Hofi <hofione@gmail.com>
  • Loading branch information
HofiOne committed May 22, 2024
1 parent a309968 commit 2d9355d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions _includes/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
};
}

function clearFocus() {
if (document.activeElement instanceof HTMLElement)
document.activeElement.blur();
}

function getElementPositionRelativeToRoot(element) {
var rect = element.getBoundingClientRect();

Expand Down
23 changes: 16 additions & 7 deletions _js/custom/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ $(function () {
if (anchorId)
scrollToAnchor(anchorId);
// Clear any focus (e.g back navigation keeps the previously clicked link focused)
window.trigger('blur');
clearFocus();
hideSearch();
}

function updateContentFromUrl(url) {
Expand Down Expand Up @@ -779,15 +780,21 @@ $(function () {
input.trigger("select");
}, 100);
}
else {
// set focus back via the initial content otherwise the focus will not get back to the search input once again
$(".initial-content").find("input").focus();
}
// else {
// // set focus back via the initial content otherwise the focus will not get back to the search input once again
// $(".initial-content").find("input").focus();
// }

if (tooltipTarget)
hideTooltip(true);
// NOTE: event.target is not always the toggle here, use it directly instead of the event
$("#search-button").trigger('blur');

clearFocus();
}

function hideSearch() {
$(".search-content").removeClass("is--visible");
$(".search-content__form").removeClass("is--visible");
$(".initial-content").removeClass("is--hidden");
}

$("#search-button").on('click', toggleSearch);
Expand All @@ -803,6 +810,8 @@ $(function () {
// (e.g. when an inner embedded page link is opened directly in a new tab, not via the internal navigational links)
finalizeContent();

hideSearch()

// Listen for popstate events and update content accordingly
window.addEventListener('popstate', function () {
updateContentFromUrl(window.location.pathname);
Expand Down
13 changes: 9 additions & 4 deletions _js/main.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -8539,7 +8539,8 @@ $(function() {
if (ClipboardJS.isSupported()) addCodeBlocksTitle();
addContentTooltips();
if (anchorId) scrollToAnchor(anchorId);
window.trigger("blur");
clearFocus();
hideSearch();
}
function updateContentFromUrl(url) {
var currContent = document.querySelector(contentID);
Expand Down Expand Up @@ -8921,16 +8922,20 @@ $(function() {
input.trigger("focus");
input.trigger("select");
}, 100);
} else {
$(".initial-content").find("input").focus();
}
if (tooltipTarget) hideTooltip(true);
$("#search-button").trigger("blur");
clearFocus();
}
function hideSearch() {
$(".search-content").removeClass("is--visible");
$(".search-content__form").removeClass("is--visible");
$(".initial-content").removeClass("is--hidden");
}
$("#search-button").on("click", toggleSearch);
}
stickySideBar();
finalizeContent();
hideSearch();
window.addEventListener("popstate", function() {
updateContentFromUrl(window.location.pathname);
});
Expand Down

0 comments on commit 2d9355d

Please sign in to comment.