@duskmoon-dev/el-datepicker 0.4.0 → 0.6.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.
@@ -1,4 +1,4 @@
1
- import { BaseElement, css as cssTag } from '@duskmoon-dev/el-core';
1
+ import { BaseElement, css as cssTag } from '@duskmoon-dev/el-base';
2
2
  import { css } from '@duskmoon-dev/core/components/datepicker';
3
3
 
4
4
  // Strip @layer components wrapper for Shadow DOM
@@ -20,6 +20,65 @@ const styles = cssTag`
20
20
  .datepicker {
21
21
  width: 100%;
22
22
  }
23
+
24
+ /* Popover API styles for dropdown - fixed width for 7-column calendar */
25
+ .datepicker-dropdown {
26
+ position: fixed;
27
+ margin: 0;
28
+ border: none;
29
+ padding: 0;
30
+ inset: unset;
31
+ overflow: hidden;
32
+ background: var(--color-surface, white);
33
+ border-radius: var(--radius-lg, 0.75rem);
34
+ box-shadow: var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1));
35
+ /* 7 days × 2.5rem = 17.5rem + 1rem padding = 18.5rem ≈ 296px */
36
+ width: 296px;
37
+ }
38
+
39
+ .datepicker-dropdown:popover-open {
40
+ display: block;
41
+ }
42
+
43
+ /* Calendar container - stack weekdays and days vertically */
44
+ .datepicker-dropdown .datepicker-calendar {
45
+ display: flex;
46
+ flex-direction: column;
47
+ width: 100%;
48
+ padding: 0.5rem;
49
+ box-sizing: border-box;
50
+ }
51
+
52
+ /* Override grid to use fixed sizes instead of 1fr */
53
+ .datepicker-dropdown .datepicker-weekdays,
54
+ .datepicker-dropdown .datepicker-days {
55
+ display: grid !important;
56
+ grid-template-columns: repeat(7, 2.5rem) !important;
57
+ gap: 0 !important;
58
+ width: fit-content;
59
+ margin: 0 auto;
60
+ }
61
+
62
+ .datepicker-dropdown .datepicker-day {
63
+ width: 2.5rem !important;
64
+ height: 2.5rem !important;
65
+ padding: 0;
66
+ }
67
+
68
+ .datepicker-dropdown .datepicker-weekday {
69
+ width: 2.5rem;
70
+ height: 2rem;
71
+ }
72
+
73
+ .datepicker-dropdown .datepicker-months,
74
+ .datepicker-dropdown .datepicker-years {
75
+ display: grid !important;
76
+ grid-template-columns: repeat(3, 5.5rem) !important;
77
+ gap: 0.5rem !important;
78
+ width: fit-content;
79
+ margin: 0 auto;
80
+ padding: 0.5rem;
81
+ }
23
82
  `;
24
83
 
25
84
  export type DatepickerSize = 'sm' | 'md' | 'lg';
@@ -54,15 +113,15 @@ export class ElDmDatepicker extends BaseElement {
54
113
  size: { type: String, reflect: true, default: 'md' },
55
114
  };
56
115
 
57
- value!: string;
58
- disabled!: boolean;
59
- placeholder!: string;
60
- format!: string;
61
- minDate!: string;
62
- maxDate!: string;
63
- range!: boolean;
64
- showTime!: boolean;
65
- size!: DatepickerSize;
116
+ declare value: string;
117
+ declare disabled: boolean;
118
+ declare placeholder: string;
119
+ declare format: string;
120
+ declare minDate: string;
121
+ declare maxDate: string;
122
+ declare range: boolean;
123
+ declare showTime: boolean;
124
+ declare size: DatepickerSize;
66
125
 
67
126
  private _isOpen = false;
68
127
  private _viewMode: ViewMode = 'days';
@@ -79,15 +138,124 @@ export class ElDmDatepicker extends BaseElement {
79
138
  this.attachStyles(styles);
80
139
  }
81
140
 
141
+ private _delegatedClickHandler: ((e: Event) => void) | null = null;
142
+ private _delegatedChangeHandler: ((e: Event) => void) | null = null;
143
+ private _scrollHandler: (() => void) | null = null;
144
+
82
145
  connectedCallback() {
83
146
  super.connectedCallback();
84
147
  this._parseValue();
85
148
  document.addEventListener('click', this._handleOutsideClick);
149
+ this._setupEventDelegation();
86
150
  }
87
151
 
88
152
  disconnectedCallback() {
89
153
  super.disconnectedCallback();
90
154
  document.removeEventListener('click', this._handleOutsideClick);
155
+ this._removeEventDelegation();
156
+ // Clean up scroll/resize handlers
157
+ if (this._scrollHandler) {
158
+ window.removeEventListener('scroll', this._scrollHandler, true);
159
+ window.removeEventListener('resize', this._scrollHandler);
160
+ this._scrollHandler = null;
161
+ }
162
+ }
163
+
164
+ private _setupEventDelegation() {
165
+ // Click handler for all interactive elements
166
+ this._delegatedClickHandler = (e: Event) => {
167
+ const target = e.target as HTMLElement;
168
+
169
+ // Input click - toggle dropdown
170
+ if (target.closest('.datepicker-input')) {
171
+ e.stopPropagation();
172
+ this._toggle();
173
+ return;
174
+ }
175
+
176
+ // Navigation buttons
177
+ if (target.closest('[data-nav="prev"]')) {
178
+ e.stopPropagation();
179
+ this._handlePrev();
180
+ return;
181
+ }
182
+ if (target.closest('[data-nav="next"]')) {
183
+ e.stopPropagation();
184
+ this._handleNext();
185
+ return;
186
+ }
187
+
188
+ // Title toggle view
189
+ if (target.closest('[data-action="toggle-view"]')) {
190
+ e.stopPropagation();
191
+ this._handleToggleView();
192
+ return;
193
+ }
194
+
195
+ // Day selection
196
+ const dayBtn = target.closest('.datepicker-day') as HTMLElement;
197
+ if (dayBtn && dayBtn.dataset.date) {
198
+ e.stopPropagation();
199
+ this._selectDate(new Date(dayBtn.dataset.date));
200
+ return;
201
+ }
202
+
203
+ // Month selection
204
+ const monthBtn = target.closest('.datepicker-month') as HTMLElement;
205
+ if (monthBtn && monthBtn.dataset.month !== undefined) {
206
+ e.stopPropagation();
207
+ this._selectMonth(parseInt(monthBtn.dataset.month, 10));
208
+ return;
209
+ }
210
+
211
+ // Year selection
212
+ const yearBtn = target.closest('.datepicker-year') as HTMLElement;
213
+ if (yearBtn && yearBtn.dataset.year !== undefined) {
214
+ e.stopPropagation();
215
+ this._selectYear(parseInt(yearBtn.dataset.year, 10));
216
+ return;
217
+ }
218
+
219
+ // Period buttons (AM/PM)
220
+ const periodBtn = target.closest('.datepicker-time-period-btn') as HTMLElement;
221
+ if (periodBtn && periodBtn.dataset.period) {
222
+ e.stopPropagation();
223
+ this._period = periodBtn.dataset.period as 'AM' | 'PM';
224
+ this._updateValue();
225
+ this.update();
226
+ return;
227
+ }
228
+ };
229
+
230
+ // Change handler for time inputs
231
+ this._delegatedChangeHandler = (e: Event) => {
232
+ const target = e.target as HTMLInputElement;
233
+ if (target.classList.contains('datepicker-time-input')) {
234
+ const type = target.dataset.time;
235
+ const value = parseInt(target.value, 10);
236
+
237
+ if (type === 'hours') {
238
+ this._hours = Math.max(1, Math.min(12, value || 12));
239
+ } else if (type === 'minutes') {
240
+ this._minutes = Math.max(0, Math.min(59, value || 0));
241
+ }
242
+
243
+ this._updateValue();
244
+ this.update();
245
+ }
246
+ };
247
+
248
+ this.shadowRoot?.addEventListener('click', this._delegatedClickHandler);
249
+ this.shadowRoot?.addEventListener('change', this._delegatedChangeHandler);
250
+ }
251
+
252
+ private _removeEventDelegation() {
253
+ if (this._delegatedClickHandler) {
254
+ this.shadowRoot?.removeEventListener('click', this._delegatedClickHandler);
255
+ }
256
+ if (this._delegatedChangeHandler) {
257
+ this.shadowRoot?.removeEventListener('change', this._delegatedChangeHandler);
258
+ }
91
259
  }
92
260
 
93
261
  private _handleOutsideClick = (e: MouseEvent) => {
@@ -127,12 +295,49 @@ export class ElDmDatepicker extends BaseElement {
127
295
  this._viewMode = 'days';
128
296
  this.emit('open');
129
297
  this.update();
298
+
299
+ // Show popover and position it
300
+ const dropdown = this.shadowRoot?.querySelector('.datepicker-dropdown') as HTMLElement;
301
+ const trigger = this.shadowRoot?.querySelector('.datepicker-input') as HTMLElement;
302
+ if (dropdown && trigger) {
303
+ try {
304
+ dropdown.showPopover();
305
+ this._positionDropdown(dropdown, trigger);
306
+ } catch {
307
+ // Ignore if already shown
308
+ }
309
+
310
+ // Add scroll listener to reposition dropdown
311
+ this._scrollHandler = () => {
312
+ this._positionDropdown(dropdown, trigger);
313
+ };
314
+ window.addEventListener('scroll', this._scrollHandler, true);
315
+ window.addEventListener('resize', this._scrollHandler);
316
+ }
130
317
  }
131
318
 
132
319
  private _close() {
133
320
  if (!this._isOpen) return;
134
321
  this._isOpen = false;
135
322
  this.emit('close');
323
+
324
+ // Remove scroll/resize listeners
325
+ if (this._scrollHandler) {
326
+ window.removeEventListener('scroll', this._scrollHandler, true);
327
+ window.removeEventListener('resize', this._scrollHandler);
328
+ this._scrollHandler = null;
329
+ }
330
+
331
+ // Hide popover
332
+ const dropdown = this.shadowRoot?.querySelector('.datepicker-dropdown') as HTMLElement;
333
+ if (dropdown) {
334
+ try {
335
+ dropdown.hidePopover();
336
+ } catch {
337
+ // Ignore if already hidden
338
+ }
339
+ }
340
+
136
341
  this.update();
137
342
  }
138
343
 
@@ -144,6 +349,34 @@ export class ElDmDatepicker extends BaseElement {
144
349
  }
145
350
  }
146
351
 
352
+ private _positionDropdown(dropdown: HTMLElement, trigger: HTMLElement) {
353
+ const triggerRect = trigger.getBoundingClientRect();
354
+ const dropdownRect = dropdown.getBoundingClientRect();
355
+
356
+ // Position below the trigger
357
+ let top = triggerRect.bottom + 4;
358
+ let left = triggerRect.left;
359
+
360
+ // Check if dropdown would go off the bottom of the screen
361
+ if (top + dropdownRect.height > window.innerHeight) {
362
+ // Position above the trigger
363
+ top = triggerRect.top - dropdownRect.height - 4;
364
+ }
365
+
366
+ // Check if dropdown would go off the right of the screen
367
+ if (left + dropdownRect.width > window.innerWidth) {
368
+ left = window.innerWidth - dropdownRect.width - 8;
369
+ }
370
+
371
+ // Ensure it doesn't go off the left
372
+ if (left < 8) {
373
+ left = 8;
374
+ }
375
+
376
+ dropdown.style.top = `${top}px`;
377
+ dropdown.style.left = `${left}px`;
378
+ }
379
+
147
380
  private _formatDate(date: Date | null): string {
148
381
  if (!date) return '';
149
382
 
@@ -538,7 +771,7 @@ export class ElDmDatepicker extends BaseElement {
538
771
  <line x1="3" y1="10" x2="21" y2="10"></line>
539
772
  </svg>
540
773
  </div>
541
- <div class="datepicker-dropdown">
774
+ <div class="datepicker-dropdown" popover="manual">
542
775
  ${this._renderHeader()}
543
776
  <div class="datepicker-calendar">
544
777
  ${calendarContent}
@@ -549,73 +782,23 @@ export class ElDmDatepicker extends BaseElement {
549
782
  `;
550
783
  }
551
784
 
552
- update() {
785
+ protected update(): void {
553
786
  super.update();
554
- this._attachEventListeners();
555
- }
787
+ // Event delegation is set up once in connectedCallback
556
788
 
557
- private _attachEventListeners() {
558
- const input = this.shadowRoot?.querySelector('.datepicker-input');
559
- if (input) {
560
- input.removeEventListener('click', this._toggle.bind(this));
561
- input.addEventListener('click', this._toggle.bind(this));
562
- }
563
-
564
- // Navigation
565
- const prevBtn = this.shadowRoot?.querySelector('[data-nav="prev"]');
566
- const nextBtn = this.shadowRoot?.querySelector('[data-nav="next"]');
567
-
568
- if (prevBtn) {
569
- prevBtn.removeEventListener('click', this._handlePrev);
570
- prevBtn.addEventListener('click', this._handlePrev);
571
- }
572
-
573
- if (nextBtn) {
574
- nextBtn.removeEventListener('click', this._handleNext);
575
- nextBtn.addEventListener('click', this._handleNext);
576
- }
577
-
578
- // Title toggle view
579
- const titleBtn = this.shadowRoot?.querySelector('[data-action="toggle-view"]');
580
- if (titleBtn) {
581
- titleBtn.removeEventListener('click', this._handleToggleView);
582
- titleBtn.addEventListener('click', this._handleToggleView);
789
+ // If dropdown is open, re-show the popover after DOM update
790
+ if (this._isOpen) {
791
+ const dropdown = this.shadowRoot?.querySelector('.datepicker-dropdown') as HTMLElement;
792
+ const trigger = this.shadowRoot?.querySelector('.datepicker-input') as HTMLElement;
793
+ if (dropdown && trigger) {
794
+ try {
795
+ dropdown.showPopover();
796
+ this._positionDropdown(dropdown, trigger);
797
+ } catch {
798
+ // Ignore if already shown or other errors
799
+ }
800
+ }
583
801
  }
584
-
585
- // Day selection
586
- const dayBtns = this.shadowRoot?.querySelectorAll('.datepicker-day');
587
- dayBtns?.forEach((btn) => {
588
- btn.removeEventListener('click', this._handleDayClick);
589
- btn.addEventListener('click', this._handleDayClick);
590
- });
591
-
592
- // Month selection
593
- const monthBtns = this.shadowRoot?.querySelectorAll('.datepicker-month');
594
- monthBtns?.forEach((btn) => {
595
- btn.removeEventListener('click', this._handleMonthClick);
596
- btn.addEventListener('click', this._handleMonthClick);
597
- });
598
-
599
- // Year selection
600
- const yearBtns = this.shadowRoot?.querySelectorAll('.datepicker-year');
601
- yearBtns?.forEach((btn) => {
602
- btn.removeEventListener('click', this._handleYearClick);
603
- btn.addEventListener('click', this._handleYearClick);
604
- });
605
-
606
- // Time inputs
607
- const timeInputs = this.shadowRoot?.querySelectorAll('.datepicker-time-input');
608
- timeInputs?.forEach((input) => {
609
- input.removeEventListener('change', this._handleTimeChange);
610
- input.addEventListener('change', this._handleTimeChange);
611
- });
612
-
613
- // Period buttons
614
- const periodBtns = this.shadowRoot?.querySelectorAll('.datepicker-time-period-btn');
615
- periodBtns?.forEach((btn) => {
616
- btn.removeEventListener('click', this._handlePeriodClick);
617
- btn.addEventListener('click', this._handlePeriodClick);
618
- });
619
802
  }
620
803
 
621
804
  private _handlePrev = () => {
@@ -649,53 +832,4 @@ export class ElDmDatepicker extends BaseElement {
649
832
  this._setViewMode('days');
650
833
  }
651
834
  };
652
-
653
- private _handleDayClick = (e: Event) => {
654
- const btn = e.currentTarget as HTMLElement;
655
- const dateStr = btn.dataset.date;
656
- if (dateStr) {
657
- this._selectDate(new Date(dateStr));
658
- }
659
- };
660
-
661
- private _handleMonthClick = (e: Event) => {
662
- const btn = e.currentTarget as HTMLElement;
663
- const month = btn.dataset.month;
664
- if (month !== undefined) {
665
- this._selectMonth(parseInt(month, 10));
666
- }
667
- };
668
-
669
- private _handleYearClick = (e: Event) => {
670
- const btn = e.currentTarget as HTMLElement;
671
- const year = btn.dataset.year;
672
- if (year !== undefined) {
673
- this._selectYear(parseInt(year, 10));
674
- }
675
- };
676
-
677
- private _handleTimeChange = (e: Event) => {
678
- const input = e.target as HTMLInputElement;
679
- const type = input.dataset.time;
680
- const value = parseInt(input.value, 10);
681
-
682
- if (type === 'hours') {
683
- this._hours = Math.max(1, Math.min(12, value || 12));
684
- } else if (type === 'minutes') {
685
- this._minutes = Math.max(0, Math.min(59, value || 0));
686
- }
687
-
688
- this._updateValue();
689
- this.update();
690
- };
691
-
692
- private _handlePeriodClick = (e: Event) => {
693
- const btn = e.currentTarget as HTMLElement;
694
- const period = btn.dataset.period as 'AM' | 'PM';
695
- if (period) {
696
- this._period = period;
697
- this._updateValue();
698
- this.update();
699
- }
700
- };
701
835
  }