import { dispatch, upgrade } from '../../../../node_modules/@browserkids/dom/index.js'; export default class CalendarNavigation extends HTMLElement { constructor() { super(); upgrade(this, ` `); } onKeyDown(e) { const { key } = e; if (key === 'ArrowRight') { this.goNextMonth(); } else if (key === 'ArrowLeft') { this.goPreviousMonth(); } else if (key === 'ArrowUp') { this.goNextYear(); } else if (key === 'ArrowDown') { this.goPreviousYear(); } else if (key === ' ') { this.goToday(); // Hitting space might as well trigger a focused button (month), // avoid that behaviour. e.preventDefault(); } } async onPostUpdate({ detail }) { const { calendar } = window; const { selectedMonth } = detail; const { $year, $month } = this.$refs; const months = await calendar.getMonths(); $year.textContent = selectedMonth.getFullYear(); $month.forEach(($el, month) => { $el.classList.toggle( '-current', month === selectedMonth.getMonth(), ); // eslint-disable-next-line no-param-reassign $el.textContent = months[month]; }); return this; } goToday() { dispatch(this, 'change'); return this; } goMonth({ currentTarget }) { dispatch(this, 'change', { set: { month: this.$refs.$month.indexOf(currentTarget) } }); return this; } goPreviousYear() { dispatch(this, 'change', { diff: { years: -1 } }); return this; } goNextYear() { dispatch(this, 'change', { diff: { years: 1 } }); return this; } goPreviousMonth() { dispatch(this, 'change', { diff: { months: -1 } }); return this; } goNextMonth() { dispatch(this, 'change', { diff: { months: 1 } }); return this; } }