1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-08-25 06:50:45 +02:00

refactor state

This commit is contained in:
Morris Brodersen
2023-11-26 11:54:04 +01:00
parent 9343da1693
commit 6a640515b2
12 changed files with 209 additions and 203 deletions

View File

@@ -2,32 +2,33 @@
* @param {HTMLElement} el
*/
export function AppCollapsible(el) {
const state = {
show: true,
};
let show = true;
setTimeout(() => el.classList.add('-animated'), 200);
el.addEventListener('collapse', (e) => {
update({ show: typeof e.detail === 'boolean' ? !e.detail : state.show });
if (typeof e.detail === 'boolean') {
show = e.detail;
}
update();
});
el.querySelector('.bar > .toggle').addEventListener('click', () => {
update({ show: !state.show });
show = !show;
update();
});
update();
function update(next) {
Object.assign(state, next);
function update() {
el.querySelector('.bar > .toggle > .app-icon').classList.toggle(
'-r180',
state.show,
show,
);
el.querySelectorAll('.body').forEach((el) => {
el.style.height = state.show ? `${el.children[0].offsetHeight}px` : '0';
el.style.height = show ? `${el.children[0].offsetHeight}px` : '0';
});
}
}