@duskmoon-dev/el-datepicker 0.4.0 → 0.5.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.
@@ -52,6 +52,65 @@ var styles = import_el_core.css`
52
52
  .datepicker {
53
53
  width: 100%;
54
54
  }
55
+
56
+ /* Popover API styles for dropdown - fixed width for 7-column calendar */
57
+ .datepicker-dropdown {
58
+ position: fixed;
59
+ margin: 0;
60
+ border: none;
61
+ padding: 0;
62
+ inset: unset;
63
+ overflow: hidden;
64
+ background: var(--color-surface, white);
65
+ border-radius: var(--radius-lg, 0.75rem);
66
+ box-shadow: var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1));
67
+ /* 7 days × 2.5rem = 17.5rem + 1rem padding = 18.5rem ≈ 296px */
68
+ width: 296px;
69
+ }
70
+
71
+ .datepicker-dropdown:popover-open {
72
+ display: block;
73
+ }
74
+
75
+ /* Calendar container - stack weekdays and days vertically */
76
+ .datepicker-dropdown .datepicker-calendar {
77
+ display: flex;
78
+ flex-direction: column;
79
+ width: 100%;
80
+ padding: 0.5rem;
81
+ box-sizing: border-box;
82
+ }
83
+
84
+ /* Override grid to use fixed sizes instead of 1fr */
85
+ .datepicker-dropdown .datepicker-weekdays,
86
+ .datepicker-dropdown .datepicker-days {
87
+ display: grid !important;
88
+ grid-template-columns: repeat(7, 2.5rem) !important;
89
+ gap: 0 !important;
90
+ width: fit-content;
91
+ margin: 0 auto;
92
+ }
93
+
94
+ .datepicker-dropdown .datepicker-day {
95
+ width: 2.5rem !important;
96
+ height: 2.5rem !important;
97
+ padding: 0;
98
+ }
99
+
100
+ .datepicker-dropdown .datepicker-weekday {
101
+ width: 2.5rem;
102
+ height: 2rem;
103
+ }
104
+
105
+ .datepicker-dropdown .datepicker-months,
106
+ .datepicker-dropdown .datepicker-years {
107
+ display: grid !important;
108
+ grid-template-columns: repeat(3, 5.5rem) !important;
109
+ gap: 0.5rem !important;
110
+ width: fit-content;
111
+ margin: 0 auto;
112
+ padding: 0.5rem;
113
+ }
55
114
  `;
56
115
  var WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
57
116
  var MONTHS = [
@@ -81,15 +140,6 @@ class ElDmDatepicker extends import_el_core.BaseElement {
81
140
  showTime: { type: Boolean, reflect: true, default: false },
82
141
  size: { type: String, reflect: true, default: "md" }
83
142
  };
84
- value;
85
- disabled;
86
- placeholder;
87
- format;
88
- minDate;
89
- maxDate;
90
- range;
91
- showTime;
92
- size;
93
143
  _isOpen = false;
94
144
  _viewMode = "days";
95
145
  _viewDate = new Date;
@@ -103,14 +153,99 @@ class ElDmDatepicker extends import_el_core.BaseElement {
103
153
  super();
104
154
  this.attachStyles(styles);
105
155
  }
156
+ _delegatedClickHandler = null;
157
+ _delegatedChangeHandler = null;
158
+ _scrollHandler = null;
106
159
  connectedCallback() {
107
160
  super.connectedCallback();
108
161
  this._parseValue();
109
162
  document.addEventListener("click", this._handleOutsideClick);
163
+ this._setupEventDelegation();
110
164
  }
111
165
  disconnectedCallback() {
112
166
  super.disconnectedCallback();
113
167
  document.removeEventListener("click", this._handleOutsideClick);
168
+ this._removeEventDelegation();
169
+ if (this._scrollHandler) {
170
+ window.removeEventListener("scroll", this._scrollHandler, true);
171
+ window.removeEventListener("resize", this._scrollHandler);
172
+ this._scrollHandler = null;
173
+ }
174
+ }
175
+ _setupEventDelegation() {
176
+ this._delegatedClickHandler = (e) => {
177
+ const target = e.target;
178
+ if (target.closest(".datepicker-input")) {
179
+ e.stopPropagation();
180
+ this._toggle();
181
+ return;
182
+ }
183
+ if (target.closest('[data-nav="prev"]')) {
184
+ e.stopPropagation();
185
+ this._handlePrev();
186
+ return;
187
+ }
188
+ if (target.closest('[data-nav="next"]')) {
189
+ e.stopPropagation();
190
+ this._handleNext();
191
+ return;
192
+ }
193
+ if (target.closest('[data-action="toggle-view"]')) {
194
+ e.stopPropagation();
195
+ this._handleToggleView();
196
+ return;
197
+ }
198
+ const dayBtn = target.closest(".datepicker-day");
199
+ if (dayBtn && dayBtn.dataset.date) {
200
+ e.stopPropagation();
201
+ this._selectDate(new Date(dayBtn.dataset.date));
202
+ return;
203
+ }
204
+ const monthBtn = target.closest(".datepicker-month");
205
+ if (monthBtn && monthBtn.dataset.month !== undefined) {
206
+ e.stopPropagation();
207
+ this._selectMonth(parseInt(monthBtn.dataset.month, 10));
208
+ return;
209
+ }
210
+ const yearBtn = target.closest(".datepicker-year");
211
+ if (yearBtn && yearBtn.dataset.year !== undefined) {
212
+ e.stopPropagation();
213
+ this._selectYear(parseInt(yearBtn.dataset.year, 10));
214
+ return;
215
+ }
216
+ const periodBtn = target.closest(".datepicker-time-period-btn");
217
+ if (periodBtn && periodBtn.dataset.period) {
218
+ e.stopPropagation();
219
+ this._period = periodBtn.dataset.period;
220
+ this._updateValue();
221
+ this.update();
222
+ return;
223
+ }
224
+ };
225
+ this._delegatedChangeHandler = (e) => {
226
+ const target = e.target;
227
+ if (target.classList.contains("datepicker-time-input")) {
228
+ const type = target.dataset.time;
229
+ const value = parseInt(target.value, 10);
230
+ if (type === "hours") {
231
+ this._hours = Math.max(1, Math.min(12, value || 12));
232
+ } else if (type === "minutes") {
233
+ this._minutes = Math.max(0, Math.min(59, value || 0));
234
+ }
235
+ this._updateValue();
236
+ this.update();
237
+ }
238
+ };
239
+ this.shadowRoot?.addEventListener("click", this._delegatedClickHandler);
240
+ this.shadowRoot?.addEventListener("change", this._delegatedChangeHandler);
241
+ }
242
+ _removeEventDelegation() {
243
+ if (this._delegatedClickHandler) {
244
+ this.shadowRoot?.removeEventListener("click", this._delegatedClickHandler);
245
+ }
246
+ if (this._delegatedChangeHandler) {
247
+ this.shadowRoot?.removeEventListener("change", this._delegatedChangeHandler);
248
+ }
114
249
  }
115
250
  _handleOutsideClick = (e) => {
116
251
  if (!this.contains(e.target)) {
@@ -149,12 +284,36 @@ class ElDmDatepicker extends import_el_core.BaseElement {
149
284
  this._viewMode = "days";
150
285
  this.emit("open");
151
286
  this.update();
287
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
288
+ const trigger = this.shadowRoot?.querySelector(".datepicker-input");
289
+ if (dropdown && trigger) {
290
+ try {
291
+ dropdown.showPopover();
292
+ this._positionDropdown(dropdown, trigger);
293
+ } catch {}
294
+ this._scrollHandler = () => {
295
+ this._positionDropdown(dropdown, trigger);
296
+ };
297
+ window.addEventListener("scroll", this._scrollHandler, true);
298
+ window.addEventListener("resize", this._scrollHandler);
299
+ }
152
300
  }
153
301
  _close() {
154
302
  if (!this._isOpen)
155
303
  return;
156
304
  this._isOpen = false;
157
305
  this.emit("close");
306
+ if (this._scrollHandler) {
307
+ window.removeEventListener("scroll", this._scrollHandler, true);
308
+ window.removeEventListener("resize", this._scrollHandler);
309
+ this._scrollHandler = null;
310
+ }
311
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
312
+ if (dropdown) {
313
+ try {
314
+ dropdown.hidePopover();
315
+ } catch {}
316
+ }
158
317
  this.update();
159
318
  }
160
319
  _toggle() {
@@ -164,6 +323,23 @@ class ElDmDatepicker extends import_el_core.BaseElement {
164
323
  this._open();
165
324
  }
166
325
  }
326
+ _positionDropdown(dropdown, trigger) {
327
+ const triggerRect = trigger.getBoundingClientRect();
328
+ const dropdownRect = dropdown.getBoundingClientRect();
329
+ let top = triggerRect.bottom + 4;
330
+ let left = triggerRect.left;
331
+ if (top + dropdownRect.height > window.innerHeight) {
332
+ top = triggerRect.top - dropdownRect.height - 4;
333
+ }
334
+ if (left + dropdownRect.width > window.innerWidth) {
335
+ left = window.innerWidth - dropdownRect.width - 8;
336
+ }
337
+ if (left < 8) {
338
+ left = 8;
339
+ }
340
+ dropdown.style.top = `${top}px`;
341
+ dropdown.style.left = `${left}px`;
342
+ }
167
343
  _formatDate(date) {
168
344
  if (!date)
169
345
  return "";
@@ -483,7 +659,7 @@ class ElDmDatepicker extends import_el_core.BaseElement {
483
659
  <line x1="3" y1="10" x2="21" y2="10"></line>
484
660
  </svg>
485
661
  </div>
486
- <div class="datepicker-dropdown">
662
+ <div class="datepicker-dropdown" popover="manual">
487
663
  ${this._renderHeader()}
488
664
  <div class="datepicker-calendar">
489
665
  ${calendarContent}
@@ -495,54 +671,16 @@ class ElDmDatepicker extends import_el_core.BaseElement {
495
671
  }
496
672
  update() {
497
673
  super.update();
498
- this._attachEventListeners();
499
- }
500
- _attachEventListeners() {
501
- const input = this.shadowRoot?.querySelector(".datepicker-input");
502
- if (input) {
503
- input.removeEventListener("click", this._toggle.bind(this));
504
- input.addEventListener("click", this._toggle.bind(this));
505
- }
506
- const prevBtn = this.shadowRoot?.querySelector('[data-nav="prev"]');
507
- const nextBtn = this.shadowRoot?.querySelector('[data-nav="next"]');
508
- if (prevBtn) {
509
- prevBtn.removeEventListener("click", this._handlePrev);
510
- prevBtn.addEventListener("click", this._handlePrev);
511
- }
512
- if (nextBtn) {
513
- nextBtn.removeEventListener("click", this._handleNext);
514
- nextBtn.addEventListener("click", this._handleNext);
515
- }
516
- const titleBtn = this.shadowRoot?.querySelector('[data-action="toggle-view"]');
517
- if (titleBtn) {
518
- titleBtn.removeEventListener("click", this._handleToggleView);
519
- titleBtn.addEventListener("click", this._handleToggleView);
674
+ if (this._isOpen) {
675
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
676
+ const trigger = this.shadowRoot?.querySelector(".datepicker-input");
677
+ if (dropdown && trigger) {
678
+ try {
679
+ dropdown.showPopover();
680
+ this._positionDropdown(dropdown, trigger);
681
+ } catch {}
682
+ }
520
683
  }
521
- const dayBtns = this.shadowRoot?.querySelectorAll(".datepicker-day");
522
- dayBtns?.forEach((btn) => {
523
- btn.removeEventListener("click", this._handleDayClick);
524
- btn.addEventListener("click", this._handleDayClick);
525
- });
526
- const monthBtns = this.shadowRoot?.querySelectorAll(".datepicker-month");
527
- monthBtns?.forEach((btn) => {
528
- btn.removeEventListener("click", this._handleMonthClick);
529
- btn.addEventListener("click", this._handleMonthClick);
530
- });
531
- const yearBtns = this.shadowRoot?.querySelectorAll(".datepicker-year");
532
- yearBtns?.forEach((btn) => {
533
- btn.removeEventListener("click", this._handleYearClick);
534
- btn.addEventListener("click", this._handleYearClick);
535
- });
536
- const timeInputs = this.shadowRoot?.querySelectorAll(".datepicker-time-input");
537
- timeInputs?.forEach((input2) => {
538
- input2.removeEventListener("change", this._handleTimeChange);
539
- input2.addEventListener("change", this._handleTimeChange);
540
- });
541
- const periodBtns = this.shadowRoot?.querySelectorAll(".datepicker-time-period-btn");
542
- periodBtns?.forEach((btn) => {
543
- btn.removeEventListener("click", this._handlePeriodClick);
544
- btn.addEventListener("click", this._handlePeriodClick);
545
- });
546
684
  }
547
685
  _handlePrev = () => {
548
686
  if (this._viewMode === "days") {
@@ -573,48 +711,6 @@ class ElDmDatepicker extends import_el_core.BaseElement {
573
711
  this._setViewMode("days");
574
712
  }
575
713
  };
576
- _handleDayClick = (e) => {
577
- const btn = e.currentTarget;
578
- const dateStr = btn.dataset.date;
579
- if (dateStr) {
580
- this._selectDate(new Date(dateStr));
581
- }
582
- };
583
- _handleMonthClick = (e) => {
584
- const btn = e.currentTarget;
585
- const month = btn.dataset.month;
586
- if (month !== undefined) {
587
- this._selectMonth(parseInt(month, 10));
588
- }
589
- };
590
- _handleYearClick = (e) => {
591
- const btn = e.currentTarget;
592
- const year = btn.dataset.year;
593
- if (year !== undefined) {
594
- this._selectYear(parseInt(year, 10));
595
- }
596
- };
597
- _handleTimeChange = (e) => {
598
- const input = e.target;
599
- const type = input.dataset.time;
600
- const value = parseInt(input.value, 10);
601
- if (type === "hours") {
602
- this._hours = Math.max(1, Math.min(12, value || 12));
603
- } else if (type === "minutes") {
604
- this._minutes = Math.max(0, Math.min(59, value || 0));
605
- }
606
- this._updateValue();
607
- this.update();
608
- };
609
- _handlePeriodClick = (e) => {
610
- const btn = e.currentTarget;
611
- const period = btn.dataset.period;
612
- if (period) {
613
- this._period = period;
614
- this._updateValue();
615
- this.update();
616
- }
617
- };
618
714
  }
619
715
 
620
716
  // src/index.ts