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 sidebar open/close on mobile #2016

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
27 changes: 17 additions & 10 deletions assets/js/sidebar/sidebar-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ import { isEmbedded } from '../globals'

const ANIMATION_DURATION = 300

const CONTENT_SELECTOR = '.content'
const SIDEBAR_TOGGLE_SELECTOR = '.sidebar-toggle'

const smallScreenQuery = window.matchMedia(`screen and (max-width: ${SMALL_SCREEN_BREAKPOINT}px)`)

if (!isEmbedded) {
update()
setDefaultSidebarState()

window.addEventListener('swup:page:view', update)
window.addEventListener('swup:page:view', setDefaultSidebarState)

qs(SIDEBAR_TOGGLE_SELECTOR).addEventListener('click', toggleSidebar)
const sidebar = document.getElementById('sidebar')
const sidebarToggle = qs(SIDEBAR_TOGGLE_SELECTOR)

sidebarToggle.addEventListener('click', toggleSidebar)

// Clicks outside small screen open sidebar should close it.
qs(CONTENT_SELECTOR).addEventListener('click', () => {
if (smallScreenQuery.matches && isSidebarOpen()) {
document.body.addEventListener('click', (event) => {
if (
smallScreenQuery.matches &&
isSidebarOpen() &&
!sidebar.contains(event.target) &&
!sidebarToggle.contains(event.target)
) {
toggleSidebar()
}
})
Expand All @@ -31,7 +38,7 @@ if (!isEmbedded) {
window.addEventListener('resize', throttle(() => {
if (lastWindowWidth === window.innerWidth) return
lastWindowWidth = window.innerWidth
update()
setDefaultSidebarState()
}, 100))

// Save sidebar width changes on user resize only.
Expand All @@ -43,15 +50,13 @@ if (!isEmbedded) {
document.body.style.setProperty('--sidebarWidth', `${width}px`)
})
// We observe on mousedown because we only care about user resize.
const sidebar = document.getElementById('sidebar')
sidebar.addEventListener('mousedown', () => resizeObserver.observe(sidebar))
sidebar.addEventListener('mouseup', () => resizeObserver.unobserve(sidebar))
}

export function update () {
function setDefaultSidebarState () {
const pref = sessionStorage.getItem(SIDEBAR_STATE_KEY)
const open = pref !== SIDEBAR_PREF_CLOSED && !smallScreenQuery.matches
initializeList()
updateSidebar(open)
}

Expand Down Expand Up @@ -79,6 +84,8 @@ export function isSidebarOpened () {
}

function updateSidebar (open) {
// Lazy init list. Only needed when open.
if (open) initializeList()
document.body.classList.toggle(SIDEBAR_CLASS_OPEN, open)
qs(SIDEBAR_TOGGLE_SELECTOR).setAttribute('aria-expanded', open ? 'true' : 'false')
}
Expand Down