Skip to content

Instantly share code, notes, and snippets.

@sbalci
Created December 25, 2021 17:50
Show Gist options
  • Select an option

  • Save sbalci/07c75ea616a71bf6c5dfe4a1f4fc2573 to your computer and use it in GitHub Desktop.

Select an option

Save sbalci/07c75ea616a71bf6c5dfe4a1f4fc2573 to your computer and use it in GitHub Desktop.
Simple Sidebar
<div id="sidebar1" class="sidebar" aria-label="Main sidebar containing navigation links and some information" aria-hidden="true">
<div class="sidebar__content">
<span>whatever you want</span>
</div>
</div>
<button data-toggle-sidebar="sidebar1">Toggle Sidebar</button>
document.querySelectorAll('[data-toggle-sidebar]').forEach(toggle => {
toggle.addEventListener('click', e => {
const sidebarID = toggle.dataset.toggleSidebar;
const sidebarElement = sidebarID ? document.getElementById(sidebarID) : undefined;
if (sidebarElement) {
let sidebarState = sidebarElement.getAttribute('aria-hidden');
sidebarElement.setAttribute('aria-hidden', sidebarState == 'true' ? false : true);
}
});
});
body {
min-height: 100vh;
padding: 10px;
}
/* SIDEBAR POSITION */
.sidebar {
background-color: #f1f1f1;
height: 100vh;
position: fixed;
top: 0;
right: 0;
min-width: 300px;
}
.sidebar__content {
padding: 8px;
}
/* SIDEBAR HIDDEN STATE */
.sidebar[aria-hidden="true"] {
transition: 200ms;
transform: translateX(100%);
}
/* SIDEBAR VISIBLE STATE */
.sidebar:not([aria-hidden]),
.sidebar[aria-hidden="false"] {
transition: 200ms;
transform: translateX(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment