Skip to content

Commit

Permalink
refactor: simplify sidebar animation
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Oct 22, 2024
1 parent c1bd9eb commit d4f7f39
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
23 changes: 9 additions & 14 deletions _javascript/modules/components/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
* Expand or close the sidebar in mobile screens.
*/

const ATTR_DISPLAY = 'sidebar-display';
const $sidebar = document.getElementById('sidebar');
const $trigger = document.getElementById('sidebar-trigger');
const $mask = document.getElementById('mask');

class SidebarUtil {
static isExpanded = false;
static #isExpanded = false;

static toggle() {
if (SidebarUtil.isExpanded === false) {
document.body.setAttribute(ATTR_DISPLAY, '');
} else {
document.body.removeAttribute(ATTR_DISPLAY);
}

SidebarUtil.isExpanded = !SidebarUtil.isExpanded;
this.#isExpanded = !this.#isExpanded;
document.body.toggleAttribute('sidebar-display', this.#isExpanded);
$sidebar.classList.toggle('z-2', this.#isExpanded);
$mask.classList.toggle('d-none', !this.#isExpanded);
}
}

export function sidebarExpand() {
document
.getElementById('sidebar-trigger')
.addEventListener('click', SidebarUtil.toggle);

document.getElementById('mask').addEventListener('click', SidebarUtil.toggle);
$trigger.onclick = $mask.onclick = () => SidebarUtil.toggle();
}
2 changes: 1 addition & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</aside>
</div>

<div id="mask"></div>
<div id="mask" class="d-none position-fixed w-100 h-100 z-1"></div>

{% if site.pwa.enabled %}
{% include_cached notification.html lang=lang %}
Expand Down
10 changes: 0 additions & 10 deletions _sass/addon/commons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ $btn-mb: 0.5rem;
height: 100%;
overflow-y: auto;
width: $sidebar-width;
z-index: 99;
background: var(--sidebar-bg);
border-right: 1px solid var(--sidebar-border-color);

Expand Down Expand Up @@ -1091,16 +1090,7 @@ search {
}

#mask {
display: none;
position: fixed;
inset: 0 0 0 0;
height: 100%;
width: 100%;
z-index: 1;

@at-root [#{$sidebar-display}] & {
display: block !important;
}
}

/* --- basic wrappers --- */
Expand Down

0 comments on commit d4f7f39

Please sign in to comment.