Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong position of search input on minimal when it does not fit in the space. #4165

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions assets/js/src/frontend/hgf.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ const toggleAria = (elements, add = true) => {
* @param {boolean} skipSidebar
*/
HFG.prototype.init = function (skipSidebar = false) {
const doc = window.document;
if (skipSidebar === false) {
const closeButtons = document.querySelectorAll(closeNavSelector);
const closeButtons = doc.querySelectorAll(closeNavSelector);
addEvent(closeButtons, 'click', () => {
this.toggleMenuSidebar(false);
});
}

const menuMobileToggleButtons = document.querySelectorAll(
'.menu-mobile-toggle'
);
const menuMobileToggleButtons = doc.querySelectorAll('.menu-mobile-toggle');
addEvent(menuMobileToggleButtons, 'click', (event) => {
this.toggleMenuSidebar(
!event.target.parentElement.classList.contains('is-active'),
Expand All @@ -58,7 +57,7 @@ HFG.prototype.init = function (skipSidebar = false) {
/**
* When click to outside of menu sidebar.
*/
const overlay = document.querySelector('.header-menu-sidebar-overlay');
const overlay = doc.querySelector('.header-menu-sidebar-overlay');
if (overlay) {
addEvent(
overlay,
Expand All @@ -77,36 +76,37 @@ HFG.prototype.init = function (skipSidebar = false) {
* @param {Element} target
*/
HFG.prototype.toggleMenuSidebar = function (toggle, target = null) {
const doc = window.document;
const TOGGLE_CLASS_CONTAINER = '.menu-mobile-toggle';
const buttonsContainer = document.querySelectorAll(TOGGLE_CLASS_CONTAINER);
removeClass(document.body, sidebarClasses[1]);
const buttonsContainer = doc.querySelectorAll(TOGGLE_CLASS_CONTAINER);
removeClass(doc.body, sidebarClasses[1]);

/**
* Elements to apply aria-hidden on
*/
const ariaShowOnToggle = document.querySelectorAll(
const ariaShowOnToggle = doc.querySelectorAll(
'#header-menu-sidebar, .hfg-ov'
);
const ariaHideOnToggle = document.querySelectorAll(
const ariaHideOnToggle = doc.querySelectorAll(
'.neve-skip-link, #content, .scroll-to-top, #site-footer, .header--row'
);

if (
(!NeveProperties.isCustomize &&
document.body.classList.contains(sidebarClasses[0])) ||
doc.body.classList.contains(sidebarClasses[0])) ||
toggle === false
) {
const navClickaway = document.querySelector('.nav-clickaway-overlay');
const navClickaway = doc.querySelector('.nav-clickaway-overlay');
if (navClickaway !== null) {
navClickaway.parentNode.removeChild(navClickaway);
}
addClass(document.body, sidebarClasses[1]);
removeClass(document.body, sidebarClasses[0]);
addClass(doc.body, sidebarClasses[1]);
removeClass(doc.body, sidebarClasses[0]);
removeClass(buttonsContainer, sidebarClasses[2]);
// Remove the hiding class after 1 second.
setTimeout(
function () {
removeClass(document.body, sidebarClasses[1]);
removeClass(doc.body, sidebarClasses[1]);
}.bind(this),
1000
);
Expand All @@ -117,17 +117,15 @@ HFG.prototype.toggleMenuSidebar = function (toggle, target = null) {
toggleAria(ariaHideOnToggle, false);
toggleAria(ariaShowOnToggle);
// Remove focus trap when closing.
document.dispatchEvent(new CustomEvent(NV_FOCUS_TRAP_END));
doc.dispatchEvent(new CustomEvent(NV_FOCUS_TRAP_END));
} else {
addClass(document.body, sidebarClasses[0]);
addClass(doc.body, sidebarClasses[0]);
addClass(buttonsContainer, sidebarClasses[2]);
if (target) {
document.dispatchEvent(
doc.dispatchEvent(
new CustomEvent(NV_FOCUS_TRAP_START, {
detail: {
container: document.getElementById(
'header-menu-sidebar'
),
container: doc.getElementById('header-menu-sidebar'),
close: closeNavSelector,
firstFocus: closeNavSelector + ',.menu-item a',
backFocus: target,
Expand Down
32 changes: 24 additions & 8 deletions assets/js/src/frontend/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ export const initNavigation = () => {
*/
export const repositionDropdowns = () => {
const { isRTL } = NeveProperties;
const dropDowns =
document.querySelectorAll('.sub-menu, .minimal .nv-nav-search') || [];
const dropDowns = document.querySelectorAll(
'.sub-menu, .minimal .nv-nav-search'
);

if (dropDowns.length === 0) return;

const windowWidth = window.innerWidth;

dropDowns.forEach((dropDown) => {
const bounding = dropDown.getBoundingClientRect(),
let bounding = dropDown.getBoundingClientRect(),
rightDist = bounding.left;

if (rightDist < 0) {
Expand All @@ -48,6 +50,19 @@ export const repositionDropdowns = () => {
dropDown.style.right = isRTL ? 0 : '100%';
dropDown.style.left = 'auto';
}

// Recalculate bounding after we've made adjustments.
bounding = dropDown.getBoundingClientRect();
rightDist = bounding.left;

if (rightDist < 0 || rightDist + bounding.width >= windowWidth) {
// Calculate how much should we offset the dropdown to make it fit.
dropDown.style.transform =
'translateX(' +
(isRTL ? '-' : '') +
(Math.abs(rightDist) + 20) +
'px)';
}
});
if (typeof menuCalcEvent !== 'undefined') {
window.dispatchEvent(menuCalcEvent);
Expand Down Expand Up @@ -171,15 +186,16 @@ function startFocusTrap(event) {
* Handle searches.
*/
function handleSearch() {
const navSearch = document.querySelectorAll('.nv-nav-search') || [],
navItem = document.querySelectorAll('.menu-item-nav-search') || [],
close = document.querySelectorAll('.close-responsive-search') || [];
const doc = window.document;
const navSearch = doc.querySelectorAll('.nv-nav-search') || [],
navItem = doc.querySelectorAll('.menu-item-nav-search') || [],
close = doc.querySelectorAll('.close-responsive-search') || [];
addEvent(navItem, 'click', (e, searchItem) => {
e.preventDefault();
e.stopPropagation();
toggleClass(searchItem, strings[1]);
createNavOverlay(searchItem, strings[1]);
document.dispatchEvent(
doc.dispatchEvent(
new CustomEvent(NV_FOCUS_TRAP_START, {
detail: {
container: searchItem.querySelector('.nv-nav-search'),
Expand All @@ -196,7 +212,7 @@ function handleSearch() {
addEvent(close, 'click', (e) => {
e.preventDefault();
removeClass(navItem, strings[1]);
const overlay = document.querySelector(`.${strings[2]}`);
const overlay = doc.querySelector(`.${strings[2]}`);
if (overlay === null) {
return;
}
Expand Down
Loading