@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.
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/el-dm-datepicker.ts
2
- import { BaseElement, css as cssTag } from "@duskmoon-dev/el-core";
2
+ import { BaseElement, css as cssTag } from "@duskmoon-dev/el-base";
3
3
  import { css } from "@duskmoon-dev/core/components/datepicker";
4
4
  var strippedCss = css.replace(/@layer\s+components\s*\{/, "").replace(/\}[\s]*$/, "");
5
5
  var styles = cssTag`
@@ -16,6 +16,65 @@ var styles = cssTag`
16
16
  .datepicker {
17
17
  width: 100%;
18
18
  }
19
+
20
+ /* Popover API styles for dropdown - fixed width for 7-column calendar */
21
+ .datepicker-dropdown {
22
+ position: fixed;
23
+ margin: 0;
24
+ border: none;
25
+ padding: 0;
26
+ inset: unset;
27
+ overflow: hidden;
28
+ background: var(--color-surface, white);
29
+ border-radius: var(--radius-lg, 0.75rem);
30
+ box-shadow: var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1));
31
+ /* 7 days × 2.5rem = 17.5rem + 1rem padding = 18.5rem ≈ 296px */
32
+ width: 296px;
33
+ }
34
+
35
+ .datepicker-dropdown:popover-open {
36
+ display: block;
37
+ }
38
+
39
+ /* Calendar container - stack weekdays and days vertically */
40
+ .datepicker-dropdown .datepicker-calendar {
41
+ display: flex;
42
+ flex-direction: column;
43
+ width: 100%;
44
+ padding: 0.5rem;
45
+ box-sizing: border-box;
46
+ }
47
+
48
+ /* Override grid to use fixed sizes instead of 1fr */
49
+ .datepicker-dropdown .datepicker-weekdays,
50
+ .datepicker-dropdown .datepicker-days {
51
+ display: grid !important;
52
+ grid-template-columns: repeat(7, 2.5rem) !important;
53
+ gap: 0 !important;
54
+ width: fit-content;
55
+ margin: 0 auto;
56
+ }
57
+
58
+ .datepicker-dropdown .datepicker-day {
59
+ width: 2.5rem !important;
60
+ height: 2.5rem !important;
61
+ padding: 0;
62
+ }
63
+
64
+ .datepicker-dropdown .datepicker-weekday {
65
+ width: 2.5rem;
66
+ height: 2rem;
67
+ }
68
+
69
+ .datepicker-dropdown .datepicker-months,
70
+ .datepicker-dropdown .datepicker-years {
71
+ display: grid !important;
72
+ grid-template-columns: repeat(3, 5.5rem) !important;
73
+ gap: 0.5rem !important;
74
+ width: fit-content;
75
+ margin: 0 auto;
76
+ padding: 0.5rem;
77
+ }
19
78
  `;
20
79
  var WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
21
80
  var MONTHS = [
@@ -45,15 +104,6 @@ class ElDmDatepicker extends BaseElement {
45
104
  showTime: { type: Boolean, reflect: true, default: false },
46
105
  size: { type: String, reflect: true, default: "md" }
47
106
  };
48
- value;
49
- disabled;
50
- placeholder;
51
- format;
52
- minDate;
53
- maxDate;
54
- range;
55
- showTime;
56
- size;
57
107
  _isOpen = false;
58
108
  _viewMode = "days";
59
109
  _viewDate = new Date;
@@ -67,14 +117,99 @@ class ElDmDatepicker extends BaseElement {
67
117
  super();
68
118
  this.attachStyles(styles);
69
119
  }
120
+ _delegatedClickHandler = null;
121
+ _delegatedChangeHandler = null;
122
+ _scrollHandler = null;
70
123
  connectedCallback() {
71
124
  super.connectedCallback();
72
125
  this._parseValue();
73
126
  document.addEventListener("click", this._handleOutsideClick);
127
+ this._setupEventDelegation();
74
128
  }
75
129
  disconnectedCallback() {
76
130
  super.disconnectedCallback();
77
131
  document.removeEventListener("click", this._handleOutsideClick);
132
+ this._removeEventDelegation();
133
+ if (this._scrollHandler) {
134
+ window.removeEventListener("scroll", this._scrollHandler, true);
135
+ window.removeEventListener("resize", this._scrollHandler);
136
+ this._scrollHandler = null;
137
+ }
138
+ }
139
+ _setupEventDelegation() {
140
+ this._delegatedClickHandler = (e) => {
141
+ const target = e.target;
142
+ if (target.closest(".datepicker-input")) {
143
+ e.stopPropagation();
144
+ this._toggle();
145
+ return;
146
+ }
147
+ if (target.closest('[data-nav="prev"]')) {
148
+ e.stopPropagation();
149
+ this._handlePrev();
150
+ return;
151
+ }
152
+ if (target.closest('[data-nav="next"]')) {
153
+ e.stopPropagation();
154
+ this._handleNext();
155
+ return;
156
+ }
157
+ if (target.closest('[data-action="toggle-view"]')) {
158
+ e.stopPropagation();
159
+ this._handleToggleView();
160
+ return;
161
+ }
162
+ const dayBtn = target.closest(".datepicker-day");
163
+ if (dayBtn && dayBtn.dataset.date) {
164
+ e.stopPropagation();
165
+ this._selectDate(new Date(dayBtn.dataset.date));
166
+ return;
167
+ }
168
+ const monthBtn = target.closest(".datepicker-month");
169
+ if (monthBtn && monthBtn.dataset.month !== undefined) {
170
+ e.stopPropagation();
171
+ this._selectMonth(parseInt(monthBtn.dataset.month, 10));
172
+ return;
173
+ }
174
+ const yearBtn = target.closest(".datepicker-year");
175
+ if (yearBtn && yearBtn.dataset.year !== undefined) {
176
+ e.stopPropagation();
177
+ this._selectYear(parseInt(yearBtn.dataset.year, 10));
178
+ return;
179
+ }
180
+ const periodBtn = target.closest(".datepicker-time-period-btn");
181
+ if (periodBtn && periodBtn.dataset.period) {
182
+ e.stopPropagation();
183
+ this._period = periodBtn.dataset.period;
184
+ this._updateValue();
185
+ this.update();
186
+ return;
187
+ }
188
+ };
189
+ this._delegatedChangeHandler = (e) => {
190
+ const target = e.target;
191
+ if (target.classList.contains("datepicker-time-input")) {
192
+ const type = target.dataset.time;
193
+ const value = parseInt(target.value, 10);
194
+ if (type === "hours") {
195
+ this._hours = Math.max(1, Math.min(12, value || 12));
196
+ } else if (type === "minutes") {
197
+ this._minutes = Math.max(0, Math.min(59, value || 0));
198
+ }
199
+ this._updateValue();
200
+ this.update();
201
+ }
202
+ };
203
+ this.shadowRoot?.addEventListener("click", this._delegatedClickHandler);
204
+ this.shadowRoot?.addEventListener("change", this._delegatedChangeHandler);
205
+ }
206
+ _removeEventDelegation() {
207
+ if (this._delegatedClickHandler) {
208
+ this.shadowRoot?.removeEventListener("click", this._delegatedClickHandler);
209
+ }
210
+ if (this._delegatedChangeHandler) {
211
+ this.shadowRoot?.removeEventListener("change", this._delegatedChangeHandler);
212
+ }
78
213
  }
79
214
  _handleOutsideClick = (e) => {
80
215
  if (!this.contains(e.target)) {
@@ -113,12 +248,36 @@ class ElDmDatepicker extends BaseElement {
113
248
  this._viewMode = "days";
114
249
  this.emit("open");
115
250
  this.update();
251
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
252
+ const trigger = this.shadowRoot?.querySelector(".datepicker-input");
253
+ if (dropdown && trigger) {
254
+ try {
255
+ dropdown.showPopover();
256
+ this._positionDropdown(dropdown, trigger);
257
+ } catch {}
258
+ this._scrollHandler = () => {
259
+ this._positionDropdown(dropdown, trigger);
260
+ };
261
+ window.addEventListener("scroll", this._scrollHandler, true);
262
+ window.addEventListener("resize", this._scrollHandler);
263
+ }
116
264
  }
117
265
  _close() {
118
266
  if (!this._isOpen)
119
267
  return;
120
268
  this._isOpen = false;
121
269
  this.emit("close");
270
+ if (this._scrollHandler) {
271
+ window.removeEventListener("scroll", this._scrollHandler, true);
272
+ window.removeEventListener("resize", this._scrollHandler);
273
+ this._scrollHandler = null;
274
+ }
275
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
276
+ if (dropdown) {
277
+ try {
278
+ dropdown.hidePopover();
279
+ } catch {}
280
+ }
122
281
  this.update();
123
282
  }
124
283
  _toggle() {
@@ -128,6 +287,23 @@ class ElDmDatepicker extends BaseElement {
128
287
  this._open();
129
288
  }
130
289
  }
290
+ _positionDropdown(dropdown, trigger) {
291
+ const triggerRect = trigger.getBoundingClientRect();
292
+ const dropdownRect = dropdown.getBoundingClientRect();
293
+ let top = triggerRect.bottom + 4;
294
+ let left = triggerRect.left;
295
+ if (top + dropdownRect.height > window.innerHeight) {
296
+ top = triggerRect.top - dropdownRect.height - 4;
297
+ }
298
+ if (left + dropdownRect.width > window.innerWidth) {
299
+ left = window.innerWidth - dropdownRect.width - 8;
300
+ }
301
+ if (left < 8) {
302
+ left = 8;
303
+ }
304
+ dropdown.style.top = `${top}px`;
305
+ dropdown.style.left = `${left}px`;
306
+ }
131
307
  _formatDate(date) {
132
308
  if (!date)
133
309
  return "";
@@ -447,7 +623,7 @@ class ElDmDatepicker extends BaseElement {
447
623
  <line x1="3" y1="10" x2="21" y2="10"></line>
448
624
  </svg>
449
625
  </div>
450
- <div class="datepicker-dropdown">
626
+ <div class="datepicker-dropdown" popover="manual">
451
627
  ${this._renderHeader()}
452
628
  <div class="datepicker-calendar">
453
629
  ${calendarContent}
@@ -459,54 +635,16 @@ class ElDmDatepicker extends BaseElement {
459
635
  }
460
636
  update() {
461
637
  super.update();
462
- this._attachEventListeners();
463
- }
464
- _attachEventListeners() {
465
- const input = this.shadowRoot?.querySelector(".datepicker-input");
466
- if (input) {
467
- input.removeEventListener("click", this._toggle.bind(this));
468
- input.addEventListener("click", this._toggle.bind(this));
469
- }
470
- const prevBtn = this.shadowRoot?.querySelector('[data-nav="prev"]');
471
- const nextBtn = this.shadowRoot?.querySelector('[data-nav="next"]');
472
- if (prevBtn) {
473
- prevBtn.removeEventListener("click", this._handlePrev);
474
- prevBtn.addEventListener("click", this._handlePrev);
475
- }
476
- if (nextBtn) {
477
- nextBtn.removeEventListener("click", this._handleNext);
478
- nextBtn.addEventListener("click", this._handleNext);
479
- }
480
- const titleBtn = this.shadowRoot?.querySelector('[data-action="toggle-view"]');
481
- if (titleBtn) {
482
- titleBtn.removeEventListener("click", this._handleToggleView);
483
- titleBtn.addEventListener("click", this._handleToggleView);
638
+ if (this._isOpen) {
639
+ const dropdown = this.shadowRoot?.querySelector(".datepicker-dropdown");
640
+ const trigger = this.shadowRoot?.querySelector(".datepicker-input");
641
+ if (dropdown && trigger) {
642
+ try {
643
+ dropdown.showPopover();
644
+ this._positionDropdown(dropdown, trigger);
645
+ } catch {}
646
+ }
484
647
  }
485
- const dayBtns = this.shadowRoot?.querySelectorAll(".datepicker-day");
486
- dayBtns?.forEach((btn) => {
487
- btn.removeEventListener("click", this._handleDayClick);
488
- btn.addEventListener("click", this._handleDayClick);
489
- });
490
- const monthBtns = this.shadowRoot?.querySelectorAll(".datepicker-month");
491
- monthBtns?.forEach((btn) => {
492
- btn.removeEventListener("click", this._handleMonthClick);
493
- btn.addEventListener("click", this._handleMonthClick);
494
- });
495
- const yearBtns = this.shadowRoot?.querySelectorAll(".datepicker-year");
496
- yearBtns?.forEach((btn) => {
497
- btn.removeEventListener("click", this._handleYearClick);
498
- btn.addEventListener("click", this._handleYearClick);
499
- });
500
- const timeInputs = this.shadowRoot?.querySelectorAll(".datepicker-time-input");
501
- timeInputs?.forEach((input2) => {
502
- input2.removeEventListener("change", this._handleTimeChange);
503
- input2.addEventListener("change", this._handleTimeChange);
504
- });
505
- const periodBtns = this.shadowRoot?.querySelectorAll(".datepicker-time-period-btn");
506
- periodBtns?.forEach((btn) => {
507
- btn.removeEventListener("click", this._handlePeriodClick);
508
- btn.addEventListener("click", this._handlePeriodClick);
509
- });
510
648
  }
511
649
  _handlePrev = () => {
512
650
  if (this._viewMode === "days") {
@@ -537,48 +675,6 @@ class ElDmDatepicker extends BaseElement {
537
675
  this._setViewMode("days");
538
676
  }
539
677
  };
540
- _handleDayClick = (e) => {
541
- const btn = e.currentTarget;
542
- const dateStr = btn.dataset.date;
543
- if (dateStr) {
544
- this._selectDate(new Date(dateStr));
545
- }
546
- };
547
- _handleMonthClick = (e) => {
548
- const btn = e.currentTarget;
549
- const month = btn.dataset.month;
550
- if (month !== undefined) {
551
- this._selectMonth(parseInt(month, 10));
552
- }
553
- };
554
- _handleYearClick = (e) => {
555
- const btn = e.currentTarget;
556
- const year = btn.dataset.year;
557
- if (year !== undefined) {
558
- this._selectYear(parseInt(year, 10));
559
- }
560
- };
561
- _handleTimeChange = (e) => {
562
- const input = e.target;
563
- const type = input.dataset.time;
564
- const value = parseInt(input.value, 10);
565
- if (type === "hours") {
566
- this._hours = Math.max(1, Math.min(12, value || 12));
567
- } else if (type === "minutes") {
568
- this._minutes = Math.max(0, Math.min(59, value || 0));
569
- }
570
- this._updateValue();
571
- this.update();
572
- };
573
- _handlePeriodClick = (e) => {
574
- const btn = e.currentTarget;
575
- const period = btn.dataset.period;
576
- if (period) {
577
- this._period = period;
578
- this._updateValue();
579
- this.update();
580
- }
581
- };
582
678
  }
583
679
 
584
680
  // src/index.ts