@joralvmel/cosmos-elements 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/dist/components/badge/cosm-badge.d.ts +64 -0
- package/dist/components/badge/cosm-badge.d.ts.map +1 -0
- package/dist/components/badge/cosm-badge.js +162 -0
- package/dist/components/badge/cosm-badge.js.map +1 -0
- package/dist/components/button/cosm-button.d.ts.map +1 -1
- package/dist/components/button/cosm-button.js +2 -0
- package/dist/components/button/cosm-button.js.map +1 -1
- package/dist/components/calendar/cosm-calendar.d.ts +77 -0
- package/dist/components/calendar/cosm-calendar.d.ts.map +1 -0
- package/dist/components/calendar/cosm-calendar.js +696 -0
- package/dist/components/calendar/cosm-calendar.js.map +1 -0
- package/dist/components/checkbox/cosm-checkbox.d.ts +122 -0
- package/dist/components/checkbox/cosm-checkbox.d.ts.map +1 -0
- package/dist/components/checkbox/cosm-checkbox.js +433 -0
- package/dist/components/checkbox/cosm-checkbox.js.map +1 -0
- package/dist/components/dialog/cosm-dialog.d.ts +103 -0
- package/dist/components/dialog/cosm-dialog.d.ts.map +1 -0
- package/dist/components/dialog/cosm-dialog.js +497 -0
- package/dist/components/dialog/cosm-dialog.js.map +1 -0
- package/dist/components/icon/cosm-icon.d.ts +7 -15
- package/dist/components/icon/cosm-icon.d.ts.map +1 -1
- package/dist/components/icon/cosm-icon.js +29 -30
- package/dist/components/icon/cosm-icon.js.map +1 -1
- package/dist/components/icon-button/cosm-icon-button.d.ts.map +1 -1
- package/dist/components/icon-button/cosm-icon-button.js +6 -0
- package/dist/components/icon-button/cosm-icon-button.js.map +1 -1
- package/dist/components/listbox/cosm-listbox.d.ts +1 -5
- package/dist/components/listbox/cosm-listbox.d.ts.map +1 -1
- package/dist/components/listbox/cosm-listbox.js +3 -18
- package/dist/components/listbox/cosm-listbox.js.map +1 -1
- package/dist/components/pagination/cosm-pagination.d.ts +118 -0
- package/dist/components/pagination/cosm-pagination.d.ts.map +1 -0
- package/dist/components/pagination/cosm-pagination.js +315 -0
- package/dist/components/pagination/cosm-pagination.js.map +1 -0
- package/dist/components/popover/cosm-popover.d.ts +0 -22
- package/dist/components/popover/cosm-popover.d.ts.map +1 -1
- package/dist/components/popover/cosm-popover.js +21 -54
- package/dist/components/popover/cosm-popover.js.map +1 -1
- package/dist/components/radio-group/cosm-radio-group.d.ts +111 -0
- package/dist/components/radio-group/cosm-radio-group.d.ts.map +1 -0
- package/dist/components/radio-group/cosm-radio-group.js +478 -0
- package/dist/components/radio-group/cosm-radio-group.js.map +1 -0
- package/dist/components/select/cosm-select.d.ts +126 -0
- package/dist/components/select/cosm-select.d.ts.map +1 -0
- package/dist/components/select/cosm-select.js +682 -0
- package/dist/components/select/cosm-select.js.map +1 -0
- package/dist/components/switch/cosm-switch.d.ts +103 -0
- package/dist/components/switch/cosm-switch.d.ts.map +1 -0
- package/dist/components/switch/cosm-switch.js +359 -0
- package/dist/components/switch/cosm-switch.js.map +1 -0
- package/dist/components/text-field/cosm-text-field.d.ts +8 -18
- package/dist/components/text-field/cosm-text-field.d.ts.map +1 -1
- package/dist/components/text-field/cosm-text-field.js +30 -67
- package/dist/components/text-field/cosm-text-field.js.map +1 -1
- package/dist/components/textarea/cosm-textarea.d.ts +131 -0
- package/dist/components/textarea/cosm-textarea.d.ts.map +1 -0
- package/dist/components/textarea/cosm-textarea.js +445 -0
- package/dist/components/textarea/cosm-textarea.js.map +1 -0
- package/dist/custom-elements.json +5915 -1330
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/current-page.d.ts +7 -0
- package/dist/internal/current-page.d.ts.map +1 -0
- package/dist/internal/current-page.js +18 -0
- package/dist/internal/current-page.js.map +1 -0
- package/llms.md +306 -20
- package/package.json +39 -3
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
import { addCalendarMonths, calendarContract, canNavigateCalendarMonth, formatCalendarDate, formatCalendarMonth, getCalendarLocaleInfo, getCalendarMonth, getCalendarMonthDates, getCalendarToday, getCalendarTodayLabel, getCalendarWeekdayNames, isCalendarDateSelectable, parseCalendarDate, tryAddCalendarDays, tryAddCalendarMonths, validateCalendarBounds, } from '@joralvmel/cosmos-core';
|
|
2
|
+
import { chevronLeft } from '@joralvmel/cosmos-icons/chevron-left';
|
|
3
|
+
import { chevronRight } from '@joralvmel/cosmos-icons/chevron-right';
|
|
4
|
+
import { css, html, LitElement, nothing } from 'lit';
|
|
5
|
+
import { CosmElement } from '../../internal/cosm-element.js';
|
|
6
|
+
import { define } from '../../internal/define.js';
|
|
7
|
+
import { boxSizing, focusRing } from '../../internal/styles.js';
|
|
8
|
+
import '../button/cosm-button.js';
|
|
9
|
+
import '../icon/cosm-icon.js';
|
|
10
|
+
const styles = css `
|
|
11
|
+
:host {
|
|
12
|
+
--cosm-calendar-focus: var(--cosm-primary, oklch(0.55 0.2 268));
|
|
13
|
+
display: block;
|
|
14
|
+
min-inline-size: 0;
|
|
15
|
+
inline-size: 100%;
|
|
16
|
+
color: var(--cosm-calendar-color, var(--cosm-fg, currentColor));
|
|
17
|
+
background: var(--cosm-calendar-background, var(--cosm-bg, transparent));
|
|
18
|
+
font-family: var(--cosm-font-sans, ui-sans-serif, system-ui, sans-serif);
|
|
19
|
+
}
|
|
20
|
+
.base {
|
|
21
|
+
--cosm-focus-ring: var(--cosm-calendar-focus, var(--cosm-primary, oklch(0.55 0.2 268)));
|
|
22
|
+
display: grid;
|
|
23
|
+
gap: var(--cosm-space-sm, 0.5rem);
|
|
24
|
+
color: var(--cosm-calendar-color, var(--cosm-fg, currentColor));
|
|
25
|
+
background: var(--cosm-calendar-background, var(--cosm-bg, transparent));
|
|
26
|
+
}
|
|
27
|
+
.header {
|
|
28
|
+
display: grid;
|
|
29
|
+
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
30
|
+
align-items: center;
|
|
31
|
+
gap: var(--cosm-space-xs, 0.25rem);
|
|
32
|
+
}
|
|
33
|
+
.heading {
|
|
34
|
+
margin: 0;
|
|
35
|
+
text-align: center;
|
|
36
|
+
font: inherit;
|
|
37
|
+
font-size: var(--cosm-text-md, 1rem);
|
|
38
|
+
font-weight: var(--cosm-weight-semibold, 600);
|
|
39
|
+
}
|
|
40
|
+
.heading-trigger {
|
|
41
|
+
border: 0;
|
|
42
|
+
border-radius: var(--cosm-radius-sm, 0.25rem);
|
|
43
|
+
padding: 0.125rem;
|
|
44
|
+
background: transparent;
|
|
45
|
+
color: inherit;
|
|
46
|
+
font: inherit;
|
|
47
|
+
cursor: pointer;
|
|
48
|
+
}
|
|
49
|
+
.heading-trigger:hover,
|
|
50
|
+
.picker-option:hover:not(:disabled) {
|
|
51
|
+
background: var(--cosm-neutral-3, color-mix(in oklab, currentColor 10%, transparent));
|
|
52
|
+
}
|
|
53
|
+
.heading-trigger:disabled {
|
|
54
|
+
color: var(--cosm-calendar-muted-color, var(--cosm-fg-muted, currentColor));
|
|
55
|
+
cursor: not-allowed;
|
|
56
|
+
}
|
|
57
|
+
.heading-trigger:focus-visible,
|
|
58
|
+
.picker-option:focus-visible {
|
|
59
|
+
outline: 2px solid var(--cosm-calendar-focus);
|
|
60
|
+
outline-offset: 2px;
|
|
61
|
+
}
|
|
62
|
+
.picker-grid {
|
|
63
|
+
display: grid;
|
|
64
|
+
grid-template-columns: repeat(4, minmax(0, 1fr));
|
|
65
|
+
gap: var(--cosm-calendar-gap, var(--cosm-space-2xs, 0.125rem));
|
|
66
|
+
}
|
|
67
|
+
.month-grid {
|
|
68
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
69
|
+
}
|
|
70
|
+
.picker-option {
|
|
71
|
+
min-inline-size: 44px;
|
|
72
|
+
min-block-size: 44px;
|
|
73
|
+
border: 1px solid transparent;
|
|
74
|
+
border-radius: var(--cosm-calendar-radius, var(--cosm-radius-md, 0.5rem));
|
|
75
|
+
background: transparent;
|
|
76
|
+
color: var(--cosm-calendar-color, var(--cosm-fg, currentColor));
|
|
77
|
+
font: inherit;
|
|
78
|
+
cursor: pointer;
|
|
79
|
+
}
|
|
80
|
+
.picker-option[aria-pressed='true'] {
|
|
81
|
+
border-color: var(--cosm-calendar-focus);
|
|
82
|
+
font-weight: var(--cosm-weight-semibold, 600);
|
|
83
|
+
}
|
|
84
|
+
.picker-option:disabled {
|
|
85
|
+
color: var(--cosm-calendar-muted-color, var(--cosm-fg-muted, currentColor));
|
|
86
|
+
cursor: not-allowed;
|
|
87
|
+
}
|
|
88
|
+
.picker-range {
|
|
89
|
+
display: grid;
|
|
90
|
+
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
91
|
+
align-items: center;
|
|
92
|
+
gap: var(--cosm-space-xs, 0.25rem);
|
|
93
|
+
margin-block-end: var(--cosm-space-xs, 0.25rem);
|
|
94
|
+
text-align: center;
|
|
95
|
+
}
|
|
96
|
+
.weekdays,
|
|
97
|
+
.week {
|
|
98
|
+
display: grid;
|
|
99
|
+
grid-template-columns: repeat(
|
|
100
|
+
7,
|
|
101
|
+
minmax(max(44px, var(--cosm-calendar-day-size, 2.75rem)), 1fr)
|
|
102
|
+
);
|
|
103
|
+
gap: var(--cosm-calendar-gap, var(--cosm-space-2xs, 0.125rem));
|
|
104
|
+
}
|
|
105
|
+
.calendar-scroll {
|
|
106
|
+
inline-size: 100%;
|
|
107
|
+
max-inline-size: 100%;
|
|
108
|
+
min-inline-size: 0;
|
|
109
|
+
overflow-x: auto;
|
|
110
|
+
}
|
|
111
|
+
.weekdays {
|
|
112
|
+
color: var(--cosm-calendar-muted-color, var(--cosm-fg-muted, currentColor));
|
|
113
|
+
font-size: var(--cosm-text-sm, 0.875rem);
|
|
114
|
+
text-align: center;
|
|
115
|
+
}
|
|
116
|
+
.weekday,
|
|
117
|
+
.empty {
|
|
118
|
+
display: grid;
|
|
119
|
+
min-block-size: max(44px, var(--cosm-calendar-day-size, 2.75rem));
|
|
120
|
+
place-items: center;
|
|
121
|
+
}
|
|
122
|
+
.weekday {
|
|
123
|
+
overflow: hidden;
|
|
124
|
+
font-weight: var(--cosm-weight-medium, 500);
|
|
125
|
+
text-overflow: ellipsis;
|
|
126
|
+
white-space: nowrap;
|
|
127
|
+
}
|
|
128
|
+
.day {
|
|
129
|
+
display: grid;
|
|
130
|
+
inline-size: 100%;
|
|
131
|
+
min-inline-size: max(44px, var(--cosm-calendar-day-size, 2.75rem));
|
|
132
|
+
min-block-size: max(44px, var(--cosm-calendar-day-size, 2.75rem));
|
|
133
|
+
aspect-ratio: 1;
|
|
134
|
+
place-items: center;
|
|
135
|
+
border: 1px solid transparent;
|
|
136
|
+
border-radius: var(--cosm-calendar-radius, var(--cosm-radius-md, 0.5rem));
|
|
137
|
+
background: transparent;
|
|
138
|
+
color: var(--cosm-calendar-color, var(--cosm-fg, currentColor));
|
|
139
|
+
font: inherit;
|
|
140
|
+
font-size: var(--cosm-text-sm, 0.875rem);
|
|
141
|
+
cursor: pointer;
|
|
142
|
+
}
|
|
143
|
+
.day:hover:not([data-disabled='true']) {
|
|
144
|
+
background: var(--cosm-neutral-3, color-mix(in oklab, currentColor 10%, transparent));
|
|
145
|
+
}
|
|
146
|
+
.day:active:not([data-disabled='true']) {
|
|
147
|
+
background: var(--cosm-neutral-4, color-mix(in oklab, currentColor 16%, transparent));
|
|
148
|
+
}
|
|
149
|
+
.day[data-outside-month='true'] {
|
|
150
|
+
color: var(--cosm-calendar-muted-color, var(--cosm-fg-muted, currentColor));
|
|
151
|
+
}
|
|
152
|
+
.day[data-disabled='true'] {
|
|
153
|
+
color: var(--cosm-calendar-muted-color, var(--cosm-fg-muted, currentColor));
|
|
154
|
+
cursor: not-allowed;
|
|
155
|
+
}
|
|
156
|
+
.day[data-selected='true'] {
|
|
157
|
+
background: var(
|
|
158
|
+
--cosm-calendar-selected-background,
|
|
159
|
+
var(--cosm-primary, oklch(0.55 0.2 268))
|
|
160
|
+
);
|
|
161
|
+
color: var(
|
|
162
|
+
--cosm-calendar-selected-color,
|
|
163
|
+
var(--cosm-primary-fg, oklch(1 0 0))
|
|
164
|
+
);
|
|
165
|
+
font-weight: var(--cosm-weight-semibold, 600);
|
|
166
|
+
}
|
|
167
|
+
.day[aria-current='date'] {
|
|
168
|
+
box-shadow: inset 0 0 0 1px
|
|
169
|
+
var(--cosm-calendar-today-border, var(--cosm-calendar-color, currentColor));
|
|
170
|
+
}
|
|
171
|
+
:host([disabled]) .base {
|
|
172
|
+
opacity: 0.6;
|
|
173
|
+
}
|
|
174
|
+
@media (forced-colors: active) {
|
|
175
|
+
.day[data-selected='true'] {
|
|
176
|
+
outline: 1px solid Highlight;
|
|
177
|
+
}
|
|
178
|
+
.day[aria-current='date'] {
|
|
179
|
+
border-color: ButtonText;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
@media (prefers-reduced-motion: reduce) {
|
|
183
|
+
.day {
|
|
184
|
+
transition: none;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
`;
|
|
188
|
+
let instance = 0;
|
|
189
|
+
/**
|
|
190
|
+
* An inline, localized Gregorian calendar for selecting one date with direct month and year navigation.
|
|
191
|
+
*
|
|
192
|
+
* @tag cosm-calendar
|
|
193
|
+
* @csspart base - Calendar root.
|
|
194
|
+
* @csspart header - Month heading and navigation controls.
|
|
195
|
+
* @csspart previous - Previous-month navigation button.
|
|
196
|
+
* @csspart heading - Localized month and year heading.
|
|
197
|
+
* @csspart next - Next-month navigation button.
|
|
198
|
+
* @csspart month-trigger - Button that opens direct month selection.
|
|
199
|
+
* @csspart year-trigger - Button that opens direct year selection.
|
|
200
|
+
* @csspart month-option - Month choice in the month selection view.
|
|
201
|
+
* @csspart year-option - Year choice in the year selection view.
|
|
202
|
+
* @csspart weekday - Localized weekday heading.
|
|
203
|
+
* @csspart grid - Seven-column date grid.
|
|
204
|
+
* @csspart day - Date button, including adjacent-month dates.
|
|
205
|
+
* @fires {CustomEvent<CalendarChangeDetail>} cosm-change - Proposed selected date.
|
|
206
|
+
* @cssprop --cosm-calendar-background - Calendar surface background.
|
|
207
|
+
* @cssprop --cosm-calendar-color - Primary calendar text color.
|
|
208
|
+
* @cssprop --cosm-calendar-muted-color - Adjacent-month and weekday text color.
|
|
209
|
+
* @cssprop --cosm-calendar-selected-background - Selected-date background.
|
|
210
|
+
* @cssprop --cosm-calendar-selected-color - Selected-date text color.
|
|
211
|
+
* @cssprop --cosm-calendar-day-size - Minimum date target dimensions.
|
|
212
|
+
* @cssprop --cosm-calendar-gap - Space between date targets.
|
|
213
|
+
* @cssprop --cosm-calendar-radius - Date target corner radius.
|
|
214
|
+
* @cssprop --cosm-calendar-focus - Visible keyboard focus indicator color.
|
|
215
|
+
* @cssprop --cosm-calendar-today-border - Non-color-only today marker.
|
|
216
|
+
*/
|
|
217
|
+
export class CosmCalendar extends CosmElement {
|
|
218
|
+
static shadowRootOptions = {
|
|
219
|
+
...LitElement.shadowRootOptions,
|
|
220
|
+
delegatesFocus: true,
|
|
221
|
+
};
|
|
222
|
+
static properties = {
|
|
223
|
+
value: { attribute: false, noAccessor: true },
|
|
224
|
+
defaultValue: { attribute: 'default-value' },
|
|
225
|
+
min: {},
|
|
226
|
+
max: {},
|
|
227
|
+
locale: {},
|
|
228
|
+
disabled: { type: Boolean, reflect: true },
|
|
229
|
+
};
|
|
230
|
+
static styles = [boxSizing, focusRing, styles];
|
|
231
|
+
valueControlled = false;
|
|
232
|
+
#value;
|
|
233
|
+
#valueEdited = false;
|
|
234
|
+
#activeDate;
|
|
235
|
+
#visibleMonth;
|
|
236
|
+
#pendingFocusDate;
|
|
237
|
+
#navigationView = 'days';
|
|
238
|
+
#yearPageStart = 1;
|
|
239
|
+
#headingId = `cosm-calendar-heading-${++instance}`;
|
|
240
|
+
constructor() {
|
|
241
|
+
super();
|
|
242
|
+
this.defaultValue = undefined;
|
|
243
|
+
this.min = undefined;
|
|
244
|
+
this.max = undefined;
|
|
245
|
+
this.locale = undefined;
|
|
246
|
+
this.disabled = calendarContract.props.disabled.default;
|
|
247
|
+
this.#activeDate = getCalendarToday();
|
|
248
|
+
this.#visibleMonth = getCalendarMonth(this.#activeDate);
|
|
249
|
+
}
|
|
250
|
+
get value() {
|
|
251
|
+
return this.#value;
|
|
252
|
+
}
|
|
253
|
+
set value(value) {
|
|
254
|
+
this.valueControlled = value !== undefined;
|
|
255
|
+
this.#setValue(value);
|
|
256
|
+
}
|
|
257
|
+
willUpdate(changed) {
|
|
258
|
+
validateCalendarBounds(this.min, this.max);
|
|
259
|
+
if (this.defaultValue !== undefined)
|
|
260
|
+
parseCalendarDate(this.defaultValue);
|
|
261
|
+
if (this.value !== undefined)
|
|
262
|
+
parseCalendarDate(this.value);
|
|
263
|
+
if (changed.has('defaultValue') && !this.valueControlled && !this.#valueEdited) {
|
|
264
|
+
this.#setValue(this.defaultValue);
|
|
265
|
+
this.#syncViewTo(this.defaultValue ?? getCalendarToday());
|
|
266
|
+
}
|
|
267
|
+
if (changed.has('value') && this.valueControlled && this.value !== undefined) {
|
|
268
|
+
this.#syncViewTo(this.value);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
updated() {
|
|
272
|
+
const date = this.#pendingFocusDate;
|
|
273
|
+
if (!date)
|
|
274
|
+
return;
|
|
275
|
+
this.#pendingFocusDate = undefined;
|
|
276
|
+
const button = [...this.renderRoot.querySelectorAll('[data-date]')].find(candidate => candidate.dataset.date === date);
|
|
277
|
+
button?.focus();
|
|
278
|
+
}
|
|
279
|
+
#setValue(value) {
|
|
280
|
+
const old = this.#value;
|
|
281
|
+
this.#value = value;
|
|
282
|
+
this.requestUpdate('value', old);
|
|
283
|
+
}
|
|
284
|
+
#syncViewTo(value) {
|
|
285
|
+
this.#activeDate = value;
|
|
286
|
+
this.#visibleMonth = getCalendarMonth(value);
|
|
287
|
+
}
|
|
288
|
+
#canNavigate(months, localeInfo) {
|
|
289
|
+
if (this.#navigationView === 'years') {
|
|
290
|
+
const start = this.#yearPageStart + months * 12;
|
|
291
|
+
return start >= 1 && start <= 9999 && this.#hasSelectableYearInRange(start, start + 11);
|
|
292
|
+
}
|
|
293
|
+
if (this.#navigationView === 'months') {
|
|
294
|
+
const year = parseCalendarDate(this.#visibleMonth).year + months;
|
|
295
|
+
return year >= 1 && year <= 9999 && this.#hasSelectableYear(year);
|
|
296
|
+
}
|
|
297
|
+
return canNavigateCalendarMonth(this.#visibleMonth, months, localeInfo.firstDay, this.min, this.max, this.disabled);
|
|
298
|
+
}
|
|
299
|
+
#navigate(months, localeInfo) {
|
|
300
|
+
if (!this.#canNavigate(months, localeInfo))
|
|
301
|
+
return;
|
|
302
|
+
if (this.#navigationView === 'years') {
|
|
303
|
+
this.#yearPageStart += months * 12;
|
|
304
|
+
this.requestUpdate();
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (this.#navigationView === 'months') {
|
|
308
|
+
const { year, month } = parseCalendarDate(this.#visibleMonth);
|
|
309
|
+
const targetYear = year + months;
|
|
310
|
+
const targetMonth = this.#monthForYear(targetYear, month);
|
|
311
|
+
if (targetMonth !== undefined)
|
|
312
|
+
this.#setVisibleMonth(targetMonth, localeInfo);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
this.#activeDate = addCalendarMonths(this.#activeDate, months);
|
|
316
|
+
this.#visibleMonth = addCalendarMonths(this.#visibleMonth, months);
|
|
317
|
+
this.requestUpdate();
|
|
318
|
+
}
|
|
319
|
+
#hasSelectableMonth(month) {
|
|
320
|
+
const dates = getCalendarMonthDates(month, 0).filter((date) => date !== undefined && date.slice(0, 7) === month.slice(0, 7));
|
|
321
|
+
const first = dates[0];
|
|
322
|
+
const last = dates.at(-1);
|
|
323
|
+
return (!this.disabled &&
|
|
324
|
+
first !== undefined &&
|
|
325
|
+
last !== undefined &&
|
|
326
|
+
(this.min === undefined || last >= this.min) &&
|
|
327
|
+
(this.max === undefined || first <= this.max));
|
|
328
|
+
}
|
|
329
|
+
#hasSelectableYear(year) {
|
|
330
|
+
const first = `${String(year).padStart(4, '0')}-01-01`;
|
|
331
|
+
const last = `${String(year).padStart(4, '0')}-12-31`;
|
|
332
|
+
return (!this.disabled &&
|
|
333
|
+
year >= 1 &&
|
|
334
|
+
year <= 9999 &&
|
|
335
|
+
(this.min === undefined || last >= this.min) &&
|
|
336
|
+
(this.max === undefined || first <= this.max));
|
|
337
|
+
}
|
|
338
|
+
#hasSelectableYearInRange(start, end) {
|
|
339
|
+
for (let year = start; year <= Math.min(end, 9999); year++) {
|
|
340
|
+
if (this.#hasSelectableYear(year))
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
#monthForYear(year, preferredMonth) {
|
|
346
|
+
const month = Array.from({ length: 12 }, (_, index) => index + 1)
|
|
347
|
+
.filter(value => this.#hasSelectableMonth(`${String(year).padStart(4, '0')}-${String(value).padStart(2, '0')}-01`))
|
|
348
|
+
.sort((left, right) => Math.abs(left - preferredMonth) - Math.abs(right - preferredMonth))[0];
|
|
349
|
+
return month === undefined
|
|
350
|
+
? undefined
|
|
351
|
+
: `${String(year).padStart(4, '0')}-${String(month).padStart(2, '0')}-01`;
|
|
352
|
+
}
|
|
353
|
+
#setVisibleMonth(month, localeInfo) {
|
|
354
|
+
if (!this.#hasSelectableMonth(month))
|
|
355
|
+
return;
|
|
356
|
+
const current = parseCalendarDate(this.#visibleMonth);
|
|
357
|
+
const target = parseCalendarDate(month);
|
|
358
|
+
const monthDelta = (target.year - current.year) * 12 + target.month - current.month;
|
|
359
|
+
const candidate = tryAddCalendarMonths(this.#activeDate, monthDelta);
|
|
360
|
+
const selectableDates = getCalendarMonthDates(month, localeInfo.firstDay).filter((date) => date !== undefined &&
|
|
361
|
+
date.slice(0, 7) === month.slice(0, 7) &&
|
|
362
|
+
isCalendarDateSelectable(date, this.min, this.max));
|
|
363
|
+
this.#visibleMonth = month;
|
|
364
|
+
this.#activeDate =
|
|
365
|
+
candidate && selectableDates.includes(candidate) ? candidate : (selectableDates[0] ?? month);
|
|
366
|
+
this.#pendingFocusDate = this.#activeDate;
|
|
367
|
+
this.requestUpdate();
|
|
368
|
+
}
|
|
369
|
+
#onMonthTrigger = () => {
|
|
370
|
+
this.#navigationView = 'months';
|
|
371
|
+
this.requestUpdate();
|
|
372
|
+
};
|
|
373
|
+
#onYearTrigger = () => {
|
|
374
|
+
const { year } = parseCalendarDate(this.#visibleMonth);
|
|
375
|
+
this.#yearPageStart = Math.max(1, Math.floor((year - 1) / 10) * 10);
|
|
376
|
+
this.#navigationView = 'years';
|
|
377
|
+
this.requestUpdate();
|
|
378
|
+
};
|
|
379
|
+
#onMonthSelect = (event, localeInfo) => {
|
|
380
|
+
const button = event.currentTarget;
|
|
381
|
+
const month = button.dataset.month;
|
|
382
|
+
if (!month)
|
|
383
|
+
return;
|
|
384
|
+
const year = parseCalendarDate(this.#visibleMonth).year;
|
|
385
|
+
this.#setVisibleMonth(`${String(year).padStart(4, '0')}-${month}-01`, localeInfo);
|
|
386
|
+
this.#navigationView = 'days';
|
|
387
|
+
this.requestUpdate();
|
|
388
|
+
};
|
|
389
|
+
#onYearSelect = (event, localeInfo) => {
|
|
390
|
+
const button = event.currentTarget;
|
|
391
|
+
const year = Number(button.dataset.year);
|
|
392
|
+
if (!Number.isInteger(year) || !this.#hasSelectableYear(year))
|
|
393
|
+
return;
|
|
394
|
+
const month = this.#monthForYear(year, parseCalendarDate(this.#visibleMonth).month);
|
|
395
|
+
if (month)
|
|
396
|
+
this.#setVisibleMonth(month, localeInfo);
|
|
397
|
+
this.#navigationView = 'days';
|
|
398
|
+
this.requestUpdate();
|
|
399
|
+
};
|
|
400
|
+
#moveActiveDate(value) {
|
|
401
|
+
this.#activeDate = value;
|
|
402
|
+
this.#visibleMonth = getCalendarMonth(value);
|
|
403
|
+
this.#pendingFocusDate = value;
|
|
404
|
+
this.requestUpdate();
|
|
405
|
+
}
|
|
406
|
+
#onPrevious = () => this.#navigate(-1, getCalendarLocaleInfo(this.locale));
|
|
407
|
+
#onNext = () => this.#navigate(1, getCalendarLocaleInfo(this.locale));
|
|
408
|
+
#onDayFocus = (event) => {
|
|
409
|
+
const button = event.currentTarget;
|
|
410
|
+
const date = button.dataset.date;
|
|
411
|
+
if (date && date !== this.#activeDate) {
|
|
412
|
+
this.#activeDate = date;
|
|
413
|
+
this.requestUpdate();
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
#onDayClick = (event) => {
|
|
417
|
+
const button = event.currentTarget;
|
|
418
|
+
const date = button.dataset.date;
|
|
419
|
+
if (!date || this.disabled || !isCalendarDateSelectable(date, this.min, this.max))
|
|
420
|
+
return;
|
|
421
|
+
const changed = this.value !== date;
|
|
422
|
+
this.#activeDate = date;
|
|
423
|
+
this.#visibleMonth = getCalendarMonth(date);
|
|
424
|
+
if (!changed) {
|
|
425
|
+
this.requestUpdate();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (!this.valueControlled) {
|
|
429
|
+
this.#valueEdited = true;
|
|
430
|
+
this.#setValue(date);
|
|
431
|
+
}
|
|
432
|
+
this.emit('cosm-change', { value: date });
|
|
433
|
+
this.requestUpdate();
|
|
434
|
+
};
|
|
435
|
+
#onGridKeydown = (event, localeInfo) => {
|
|
436
|
+
if (event.defaultPrevented || this.disabled)
|
|
437
|
+
return;
|
|
438
|
+
const rtlDelta = localeInfo.direction === 'rtl' ? -1 : 1;
|
|
439
|
+
const movement = {
|
|
440
|
+
ArrowLeft: -rtlDelta,
|
|
441
|
+
ArrowRight: rtlDelta,
|
|
442
|
+
ArrowUp: -7,
|
|
443
|
+
ArrowDown: 7,
|
|
444
|
+
};
|
|
445
|
+
let next;
|
|
446
|
+
const day = parseCalendarDate(this.#activeDate);
|
|
447
|
+
const delta = movement[event.key];
|
|
448
|
+
if (delta !== undefined) {
|
|
449
|
+
next = tryAddCalendarDays(this.#activeDate, delta);
|
|
450
|
+
}
|
|
451
|
+
else if (event.key === 'Home' || event.key === 'End') {
|
|
452
|
+
const offset = (day.weekday - localeInfo.firstDay + 7) % 7;
|
|
453
|
+
next = tryAddCalendarDays(this.#activeDate, event.key === 'Home' ? -offset : 6 - offset);
|
|
454
|
+
}
|
|
455
|
+
else if (event.key === 'PageUp' || event.key === 'PageDown') {
|
|
456
|
+
next = tryAddCalendarMonths(this.#activeDate, (event.key === 'PageUp' ? -1 : 1) * (event.shiftKey ? 12 : 1));
|
|
457
|
+
}
|
|
458
|
+
if (!next)
|
|
459
|
+
return;
|
|
460
|
+
event.preventDefault();
|
|
461
|
+
event.stopPropagation();
|
|
462
|
+
this.#moveActiveDate(next);
|
|
463
|
+
};
|
|
464
|
+
render() {
|
|
465
|
+
const localeInfo = getCalendarLocaleInfo(this.locale);
|
|
466
|
+
const weekdayNames = getCalendarWeekdayNames(localeInfo.locale, localeInfo.firstDay);
|
|
467
|
+
const today = getCalendarToday();
|
|
468
|
+
const dates = getCalendarMonthDates(this.#visibleMonth, localeInfo.firstDay);
|
|
469
|
+
const currentDate = parseCalendarDate(this.#visibleMonth);
|
|
470
|
+
const monthName = new Intl.DateTimeFormat(localeInfo.locale, {
|
|
471
|
+
calendar: 'gregory',
|
|
472
|
+
month: 'long',
|
|
473
|
+
timeZone: 'UTC',
|
|
474
|
+
}).format(new Date(Date.UTC(currentDate.year, currentDate.month - 1, 1, 12)));
|
|
475
|
+
const yearName = new Intl.NumberFormat(localeInfo.locale, { useGrouping: false }).format(currentDate.year);
|
|
476
|
+
const monthLabel = formatCalendarMonth(this.#visibleMonth, localeInfo.locale);
|
|
477
|
+
const relative = new Intl.RelativeTimeFormat(localeInfo.locale, { numeric: 'auto' });
|
|
478
|
+
const relativeUnit = this.#navigationView === 'days' ? 'month' : 'year';
|
|
479
|
+
const relativeStep = this.#navigationView === 'years' ? 12 : 1;
|
|
480
|
+
const previousLabel = relative.format(-relativeStep, relativeUnit);
|
|
481
|
+
const nextLabel = relative.format(relativeStep, relativeUnit);
|
|
482
|
+
const formatDay = new Intl.NumberFormat(localeInfo.locale, { useGrouping: false });
|
|
483
|
+
const yearPageEnd = Math.min(9999, this.#yearPageStart + 11);
|
|
484
|
+
const yearPageLabel = `${new Intl.NumberFormat(localeInfo.locale, {
|
|
485
|
+
useGrouping: false,
|
|
486
|
+
}).format(this.#yearPageStart)}–${new Intl.NumberFormat(localeInfo.locale, { useGrouping: false }).format(yearPageEnd)}`;
|
|
487
|
+
const monthOptions = Array.from({ length: 12 }, (_, index) => {
|
|
488
|
+
const month = String(index + 1).padStart(2, '0');
|
|
489
|
+
const monthStart = `${String(currentDate.year).padStart(4, '0')}-${month}-01`;
|
|
490
|
+
return {
|
|
491
|
+
month,
|
|
492
|
+
label: new Intl.DateTimeFormat(localeInfo.locale, {
|
|
493
|
+
calendar: 'gregory',
|
|
494
|
+
month: 'long',
|
|
495
|
+
timeZone: 'UTC',
|
|
496
|
+
}).format(new Date(Date.UTC(currentDate.year, index, 1, 12))),
|
|
497
|
+
selected: currentDate.month === index + 1,
|
|
498
|
+
disabled: !this.#hasSelectableMonth(monthStart),
|
|
499
|
+
};
|
|
500
|
+
});
|
|
501
|
+
const yearOptions = Array.from({ length: yearPageEnd - this.#yearPageStart + 1 }, (_, index) => {
|
|
502
|
+
const year = this.#yearPageStart + index;
|
|
503
|
+
return {
|
|
504
|
+
year,
|
|
505
|
+
label: new Intl.NumberFormat(localeInfo.locale, { useGrouping: false }).format(year),
|
|
506
|
+
selected: currentDate.year === year,
|
|
507
|
+
disabled: !this.#hasSelectableYear(year),
|
|
508
|
+
};
|
|
509
|
+
});
|
|
510
|
+
const monthPickerId = `${this.#headingId}-month-picker`;
|
|
511
|
+
const yearPickerId = `${this.#headingId}-year-picker`;
|
|
512
|
+
return html `<div
|
|
513
|
+
part="base"
|
|
514
|
+
class="base"
|
|
515
|
+
dir=${localeInfo.direction}
|
|
516
|
+
aria-disabled=${this.disabled ? 'true' : 'false'}
|
|
517
|
+
>
|
|
518
|
+
<div part="header" class="header">
|
|
519
|
+
<cosm-button
|
|
520
|
+
part="previous"
|
|
521
|
+
size="sm"
|
|
522
|
+
variant="ghost"
|
|
523
|
+
tone="neutral"
|
|
524
|
+
.label=${previousLabel}
|
|
525
|
+
.disabled=${!this.#canNavigate(-1, localeInfo)}
|
|
526
|
+
@cosm-press=${this.#onPrevious}
|
|
527
|
+
>
|
|
528
|
+
<cosm-icon
|
|
529
|
+
.icon=${localeInfo.direction === 'rtl' ? chevronRight : chevronLeft}
|
|
530
|
+
size="sm"
|
|
531
|
+
></cosm-icon>
|
|
532
|
+
</cosm-button>
|
|
533
|
+
<h2 part="heading" class="heading" id=${this.#headingId} aria-live="polite" aria-atomic="true">
|
|
534
|
+
<button
|
|
535
|
+
class="heading-trigger"
|
|
536
|
+
type="button"
|
|
537
|
+
part="month-trigger"
|
|
538
|
+
aria-expanded=${this.#navigationView === 'months' ? 'true' : 'false'}
|
|
539
|
+
aria-controls=${monthPickerId}
|
|
540
|
+
?disabled=${this.disabled}
|
|
541
|
+
@click=${this.#onMonthTrigger}
|
|
542
|
+
>
|
|
543
|
+
${monthName}
|
|
544
|
+
</button>
|
|
545
|
+
<button
|
|
546
|
+
class="heading-trigger"
|
|
547
|
+
type="button"
|
|
548
|
+
part="year-trigger"
|
|
549
|
+
aria-expanded=${this.#navigationView === 'years' ? 'true' : 'false'}
|
|
550
|
+
aria-controls=${yearPickerId}
|
|
551
|
+
?disabled=${this.disabled}
|
|
552
|
+
@click=${this.#onYearTrigger}
|
|
553
|
+
>
|
|
554
|
+
${yearName}
|
|
555
|
+
</button>
|
|
556
|
+
</h2>
|
|
557
|
+
<cosm-button
|
|
558
|
+
part="next"
|
|
559
|
+
size="sm"
|
|
560
|
+
variant="ghost"
|
|
561
|
+
tone="neutral"
|
|
562
|
+
.label=${nextLabel}
|
|
563
|
+
.disabled=${!this.#canNavigate(1, localeInfo)}
|
|
564
|
+
@cosm-press=${this.#onNext}
|
|
565
|
+
>
|
|
566
|
+
<cosm-icon
|
|
567
|
+
.icon=${localeInfo.direction === 'rtl' ? chevronLeft : chevronRight}
|
|
568
|
+
size="sm"
|
|
569
|
+
></cosm-icon>
|
|
570
|
+
</cosm-button>
|
|
571
|
+
</div>
|
|
572
|
+
${this.#navigationView === 'months'
|
|
573
|
+
? html `<div
|
|
574
|
+
id=${monthPickerId}
|
|
575
|
+
class="picker-grid month-grid"
|
|
576
|
+
role="group"
|
|
577
|
+
aria-label=${monthLabel}
|
|
578
|
+
>
|
|
579
|
+
${monthOptions.map(option => html `<button
|
|
580
|
+
class="picker-option"
|
|
581
|
+
part="month-option"
|
|
582
|
+
type="button"
|
|
583
|
+
data-month=${option.month}
|
|
584
|
+
aria-pressed=${option.selected ? 'true' : 'false'}
|
|
585
|
+
?disabled=${option.disabled}
|
|
586
|
+
@click=${(event) => this.#onMonthSelect(event, localeInfo)}
|
|
587
|
+
>
|
|
588
|
+
${option.label}
|
|
589
|
+
</button>`)}
|
|
590
|
+
</div>`
|
|
591
|
+
: this.#navigationView === 'years'
|
|
592
|
+
? html `<div
|
|
593
|
+
id=${yearPickerId}
|
|
594
|
+
role="group"
|
|
595
|
+
aria-label=${yearPageLabel}
|
|
596
|
+
>
|
|
597
|
+
<div class="picker-range">
|
|
598
|
+
<cosm-button
|
|
599
|
+
size="sm"
|
|
600
|
+
variant="ghost"
|
|
601
|
+
tone="neutral"
|
|
602
|
+
.label=${previousLabel}
|
|
603
|
+
.disabled=${!this.#canNavigate(-1, localeInfo)}
|
|
604
|
+
@cosm-press=${() => this.#navigate(-1, localeInfo)}
|
|
605
|
+
>
|
|
606
|
+
<cosm-icon
|
|
607
|
+
.icon=${localeInfo.direction === 'rtl' ? chevronRight : chevronLeft}
|
|
608
|
+
size="sm"
|
|
609
|
+
></cosm-icon>
|
|
610
|
+
</cosm-button>
|
|
611
|
+
<span aria-live="polite">${yearPageLabel}</span>
|
|
612
|
+
<cosm-button
|
|
613
|
+
size="sm"
|
|
614
|
+
variant="ghost"
|
|
615
|
+
tone="neutral"
|
|
616
|
+
.label=${nextLabel}
|
|
617
|
+
.disabled=${!this.#canNavigate(1, localeInfo)}
|
|
618
|
+
@cosm-press=${() => this.#navigate(1, localeInfo)}
|
|
619
|
+
>
|
|
620
|
+
<cosm-icon
|
|
621
|
+
.icon=${localeInfo.direction === 'rtl' ? chevronLeft : chevronRight}
|
|
622
|
+
size="sm"
|
|
623
|
+
></cosm-icon>
|
|
624
|
+
</cosm-button>
|
|
625
|
+
</div>
|
|
626
|
+
<div class="picker-grid">
|
|
627
|
+
${yearOptions.map(option => html `<button
|
|
628
|
+
class="picker-option"
|
|
629
|
+
part="year-option"
|
|
630
|
+
type="button"
|
|
631
|
+
data-year=${option.year}
|
|
632
|
+
aria-pressed=${option.selected ? 'true' : 'false'}
|
|
633
|
+
?disabled=${option.disabled}
|
|
634
|
+
@click=${(event) => this.#onYearSelect(event, localeInfo)}
|
|
635
|
+
>
|
|
636
|
+
${option.label}
|
|
637
|
+
</button>`)}
|
|
638
|
+
</div>
|
|
639
|
+
</div>`
|
|
640
|
+
: html `<div class="calendar-scroll">
|
|
641
|
+
<div class="weekdays" role="row">
|
|
642
|
+
${weekdayNames.map(weekday => html `<div part="weekday" class="weekday" role="columnheader" aria-label=${weekday.long}>
|
|
643
|
+
${weekday.short}
|
|
644
|
+
</div>`)}
|
|
645
|
+
</div>
|
|
646
|
+
<div
|
|
647
|
+
part="grid"
|
|
648
|
+
role="grid"
|
|
649
|
+
aria-labelledby=${this.#headingId}
|
|
650
|
+
aria-disabled=${this.disabled ? 'true' : 'false'}
|
|
651
|
+
@keydown=${(event) => this.#onGridKeydown(event, localeInfo)}
|
|
652
|
+
>
|
|
653
|
+
${Array.from({ length: dates.length / 7 }, (_, weekIndex) => {
|
|
654
|
+
const week = dates.slice(weekIndex * 7, weekIndex * 7 + 7);
|
|
655
|
+
return html `<div class="week" role="row">
|
|
656
|
+
${week.map(date => {
|
|
657
|
+
if (!date)
|
|
658
|
+
return html `<div class="empty" role="gridcell" aria-hidden="true"></div>`;
|
|
659
|
+
const parts = parseCalendarDate(date);
|
|
660
|
+
const selectable = isCalendarDateSelectable(date, this.min, this.max);
|
|
661
|
+
const todayDate = date === today;
|
|
662
|
+
const label = `${formatCalendarDate(date, localeInfo.locale)}${todayDate ? `, ${getCalendarTodayLabel(localeInfo.locale)}` : ''}`;
|
|
663
|
+
return html `<div
|
|
664
|
+
role="gridcell"
|
|
665
|
+
aria-selected=${this.value === date ? 'true' : 'false'}
|
|
666
|
+
aria-disabled=${selectable && !this.disabled ? nothing : 'true'}
|
|
667
|
+
>
|
|
668
|
+
<button
|
|
669
|
+
part="day"
|
|
670
|
+
class="day"
|
|
671
|
+
type="button"
|
|
672
|
+
data-date=${date}
|
|
673
|
+
data-selected=${this.value === date ? 'true' : 'false'}
|
|
674
|
+
data-disabled=${selectable && !this.disabled ? 'false' : 'true'}
|
|
675
|
+
data-outside-month=${date.slice(0, 7) === this.#visibleMonth.slice(0, 7) ? 'false' : 'true'}
|
|
676
|
+
aria-label=${label}
|
|
677
|
+
aria-disabled=${selectable && !this.disabled ? nothing : 'true'}
|
|
678
|
+
aria-current=${todayDate ? 'date' : nothing}
|
|
679
|
+
tabindex=${!this.disabled && this.#activeDate === date ? 0 : -1}
|
|
680
|
+
?disabled=${this.disabled}
|
|
681
|
+
@focus=${this.#onDayFocus}
|
|
682
|
+
@click=${this.#onDayClick}
|
|
683
|
+
>
|
|
684
|
+
${formatDay.format(parts.day)}
|
|
685
|
+
</button>
|
|
686
|
+
</div>`;
|
|
687
|
+
})}
|
|
688
|
+
</div>`;
|
|
689
|
+
})}
|
|
690
|
+
</div>
|
|
691
|
+
</div>`}
|
|
692
|
+
</div>`;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
define('cosm-calendar', CosmCalendar);
|
|
696
|
+
//# sourceMappingURL=cosm-calendar.js.map
|