Skip to content

Commit

Permalink
Improve accessibility (keyboard navigation)
Browse files Browse the repository at this point in the history
  • Loading branch information
frocher committed Feb 20, 2022
1 parent b444555 commit d999580
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
41 changes: 37 additions & 4 deletions src/range-date-picker-cell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LitElement, html, css, PropertyValues, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { getTime, startOfDay } from 'date-fns';
import { format, getTime, startOfDay } from 'date-fns';
import { enUS } from 'date-fns/esm/locale';
import { Day } from './day.js';

class RangeDatepickerCell extends LitElement {
Expand All @@ -20,6 +21,15 @@ class RangeDatepickerCell extends LitElement {
margin: 0;
padding: 0;
color: var(--wc-datepicker-cell-text);
border: none;
outline: none;
background-color: transparent;
}
.day:focus {
outline: 1px solid
var(--wc-datepicker-cell-hovered, rgba(0, 150, 136, 0.5));
}
.day:not(.disabled):hover {
Expand Down Expand Up @@ -87,11 +97,23 @@ class RangeDatepickerCell extends LitElement {

@property({ type: Boolean }) protected isCurrentDate = false;

@property({ type: Object })
public get locale() {
return this._locale ? this._locale : enUS;
}

public set locale(value) {
const oldValue = this._locale;
this._locale = value;
this.requestUpdate('locale', oldValue);
}

protected _locale: any | null = null;

render(): TemplateResult {
return html`
<div
<button
@click="${this.handleTap}"
@keydown="${this.handleTap}"
@mouseover="${this.handleHover}"
@focus="${this.handleHover}"
class="day ${this.isCurrentDate ? 'currentDate' : ''} ${this.isSelected(
Expand All @@ -102,9 +124,11 @@ class RangeDatepickerCell extends LitElement {
this.max!,
this.disabledDays
)}"
?disabled="${this.disabled}"
title="${this.getTitle(this.day?.date)}"
>
<div class="currentDayMarker">${this.day?.title}</div>
</div>
</button>
`;
}

Expand Down Expand Up @@ -204,6 +228,15 @@ class RangeDatepickerCell extends LitElement {
}
return '';
}

getTitle(date: number | undefined): string {
if (date === undefined) {
return '';
}
return format(date * 1000, 'PPPP', {
locale: this.locale,
});
}
}

window.customElements.define('wc-range-datepicker-cell', RangeDatepickerCell);
14 changes: 9 additions & 5 deletions src/range-datepicker-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ export class RangeDatepickerCalendar extends LitElement {
return html`
<div class="year-container">
${this.year}
<mwc-icon-button icon="arrow_drop_down" @click="${
this.handleOpenYearSelection
}"></mwc-icon-button>
<mwc-icon-button
icon="arrow_drop_down"
@click="${this.handleOpenYearSelection}"
></mwc-icon-button>
<mwc-menu class="year-change" @selected="${this.handleYearSelected}">
${this.yearsList.map(i => this.renderYearItem(i))}
</mwc-men>
${this.yearsList.map(i => this.renderYearItem(i))}
</mwc-menu>
</div>
`;
}
Expand Down Expand Up @@ -295,6 +296,7 @@ export class RangeDatepickerCalendar extends LitElement {
.hoveredDate="${this.hoveredDate}"
.dateTo="${this.dateTo}"
.dateFrom="${this.dateFrom}"
.locale="${this.locale}"
.day="${day}"
?isCurrentDate="${this.isCurrentDate(day)}"
@date-is-selected="${this.handleDateSelected}"
Expand Down Expand Up @@ -432,6 +434,7 @@ export class RangeDatepickerCalendar extends LitElement {
handleDateSelected(e: CustomEvent): void {
const { detail } = e;
const { date } = detail;

if (!this.noRange) {
if (this.dateFrom && this.dateTo) {
this.dateFrom = date;
Expand All @@ -445,6 +448,7 @@ export class RangeDatepickerCalendar extends LitElement {
} else {
this.dateFrom = date;
}

this.dispatchEvent(
new CustomEvent('date-from-changed', { detail: { value: this.dateFrom } })
);
Expand Down

0 comments on commit d999580

Please sign in to comment.