-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,106 @@ | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const triggers = document.querySelectorAll('.wp-block-kadence-off-canvas-trigger'); | ||
triggers.forEach(function (trigger) { | ||
trigger.addEventListener('click', function () { | ||
const headerContainer = trigger.closest('.wp-block-kadence-header'); | ||
const offCanvas = headerContainer.querySelector('.wp-block-kadence-off-canvas'); | ||
|
||
if (offCanvas) { | ||
offCanvas.classList.toggle('active'); | ||
/** | ||
* File kb-off-canvas-trigger.js. | ||
* | ||
* Handles toggling the off canvas navigation. | ||
*/ | ||
(function () { | ||
const handleOffCanvas = function (triggerButtons, offCanvasArea, closeButton) { | ||
const focusableElementsString = | ||
'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]'; | ||
let focusableElements = offCanvasArea.querySelectorAll(focusableElementsString); | ||
focusableElements = Array.prototype.slice.call(focusableElements); | ||
let firstFocusableElement = focusableElements[0]; | ||
let lastFocusableElement = focusableElements[focusableElements.length - 1]; | ||
|
||
const openOffCanvas = function (event) { | ||
event.target.classList.add('triggered'); | ||
offCanvasArea.classList.add('show-off-canvas'); | ||
setTimeout(function () { | ||
offCanvasArea.classList.add('active'); | ||
triggerButtons.forEach((button) => button.setAttribute('aria-expanded', 'true')); | ||
firstFocusableElement.focus(); | ||
}, 10); | ||
}; | ||
|
||
const closeOffCanvas = function () { | ||
offCanvasArea.classList.remove('active'); | ||
triggerButtons.forEach((button) => button.setAttribute('aria-expanded', 'false')); | ||
for (let i = 0; i < triggerButtons.length; i++) { | ||
if (triggerButtons[i].classList.contains('triggered')) { | ||
triggerButtons[i].focus(); | ||
triggerButtons[i].classList.remove('triggered'); | ||
} | ||
} | ||
setTimeout(function () { | ||
offCanvasArea.classList.remove('show-off-canvas'); | ||
}, 250); | ||
}; | ||
|
||
triggerButtons.forEach((button) => { | ||
button.addEventListener('click', openOffCanvas); | ||
}); | ||
}); | ||
|
||
const overlays = document.querySelectorAll('.kb-off-canvas-overlay'); | ||
overlays.forEach(function (overlay) { | ||
overlay.addEventListener('click', function () { | ||
const uniqueId = overlay.getAttribute('data-unique-id'); | ||
closeButton.addEventListener('click', closeOffCanvas); | ||
|
||
if (uniqueId) { | ||
const offCanvas = document.querySelector('.wp-block-kadence-off-canvas' + uniqueId); | ||
function handleKeyDown(event) { | ||
const isTabPressed = event.key === 'Tab' || event.keyCode === 9; | ||
|
||
if (offCanvas) { | ||
offCanvas.classList.toggle('active'); | ||
if (!isTabPressed) { | ||
return; | ||
} | ||
|
||
if (event.shiftKey) { | ||
if (document.activeElement === firstFocusableElement) { | ||
event.preventDefault(); | ||
lastFocusableElement.focus(); | ||
} | ||
} else { | ||
if (document.activeElement === lastFocusableElement) { | ||
event.preventDefault(); | ||
firstFocusableElement.focus(); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
const closeTriggers = document.querySelectorAll('.kb-off-canvas-close'); | ||
closeTriggers.forEach(function (trigger) { | ||
trigger.addEventListener('click', function () { | ||
const offCanvas = trigger.closest('.wp-block-kadence-off-canvas'); | ||
function handleEscape(event) { | ||
if (event.key === 'Escape' || event.keyCode === 27) { | ||
closeOffCanvas(); | ||
} | ||
} | ||
|
||
if (offCanvas) { | ||
offCanvas.classList.toggle('active'); | ||
// Close modal on outside click. | ||
document.addEventListener('click', function (event) { | ||
var target = event.target; | ||
var modal = document.querySelector('.kb-off-canvas-overlay'); | ||
if (target === modal) { | ||
closeOffCanvas(); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
offCanvasArea.addEventListener('keydown', handleKeyDown); | ||
document.addEventListener('keydown', handleEscape); | ||
// Handle closing off canvas when a link is clicked. | ||
var menuLinks = offCanvasArea.querySelectorAll('a:not(.kt-tab-title)'); | ||
// No point if no links. | ||
if (menuLinks.length) { | ||
for (let i = 0; i < menuLinks.length; i++) { | ||
menuLinks[i].addEventListener('click', function (event) { | ||
closeOffCanvas(); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const initOffCanvas = function () { | ||
const triggerButtons = document.querySelectorAll('.wp-block-kadence-off-canvas-trigger'); | ||
const offCanvasArea = document.querySelector('.wp-block-kadence-off-canvas'); | ||
const closeButton = offCanvasArea.querySelector('.kb-off-canvas-close'); | ||
|
||
if (triggerButtons.length > 0 && offCanvasArea && closeButton) { | ||
handleOffCanvas(triggerButtons, offCanvasArea, closeButton); | ||
} | ||
}; | ||
|
||
// Initialize immediately for already loaded DOM | ||
initOffCanvas(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,7 @@ | |
background: none; | ||
box-shadow: none; | ||
} | ||
svg { | ||
pointer-events: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.