@dile/ui 3.22.0 → 3.22.1

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.
@@ -80,6 +80,7 @@ export class DileMenuOverlay extends DileOverlay(DileCloseDocumentClick(LitEleme
80
80
  class="${this._overlayClass}"
81
81
  ?inert="${!this._overlayClass && !this._opening}"
82
82
  @click="${this.clickHandler}"
83
+ @keydown="${this._onOverlayKeydown}"
83
84
  >
84
85
  <slot name="content"></slot>
85
86
  </div>
@@ -105,6 +106,22 @@ export class DileMenuOverlay extends DileOverlay(DileCloseDocumentClick(LitEleme
105
106
  if (e.key !== 'Escape' || this._overlayClass !== 'opened') {
106
107
  return;
107
108
  }
109
+ // A key pressed inside the menu is handled when it bubbles up to the overlay,
110
+ // so the content can consume it first by calling preventDefault().
111
+ if (e.composedPath().includes(this.overlay)) {
112
+ return;
113
+ }
114
+ this._closeOnEsc(e);
115
+ }
116
+
117
+ _onOverlayKeydown(e) {
118
+ if (e.key !== 'Escape' || this._overlayClass !== 'opened' || e.defaultPrevented) {
119
+ return;
120
+ }
121
+ this._closeOnEsc(e);
122
+ }
123
+
124
+ _closeOnEsc(e) {
108
125
  // Only the menu closes: a modal or drawer that contains it stays opened until the next Escape.
109
126
  e.stopPropagation();
110
127
  this.close();
@@ -0,0 +1 @@
1
+ export { DileWheelPicker } from './src/DileWheelPicker.js';
@@ -0,0 +1,364 @@
1
+ import { LitElement, html, css } from 'lit';
2
+ import { DileEmmitChange } from '../../../mixins/form/index.js';
3
+
4
+ const supportsScrollEnd = typeof window !== 'undefined' && 'onscrollend' in window;
5
+
6
+ export class DileWheelPicker extends DileEmmitChange(LitElement) {
7
+ static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
8
+
9
+ static styles = css`
10
+ :host {
11
+ display: inline-block;
12
+ width: var(--dile-wheel-picker-width, auto);
13
+ --_item-height: var(--dile-wheel-picker-item-height, 40px);
14
+ }
15
+ :host([hidden]) {
16
+ display: none;
17
+ }
18
+ .picker {
19
+ position: relative;
20
+ height: calc(var(--_item-height) * var(--_visible-items));
21
+ }
22
+ .band {
23
+ position: absolute;
24
+ left: 0;
25
+ right: 0;
26
+ top: calc(var(--_item-height) * (var(--_visible-items) - 1) / 2);
27
+ height: var(--_item-height);
28
+ background-color: var(--dile-wheel-picker-band-background-color, rgba(90, 90, 90, 0.1));
29
+ border-radius: var(--dile-wheel-picker-band-border-radius, 8px);
30
+ pointer-events: none;
31
+ }
32
+ .scroller {
33
+ position: relative;
34
+ height: 100%;
35
+ overflow-y: auto;
36
+ overscroll-behavior: contain;
37
+ scroll-snap-type: y mandatory;
38
+ scrollbar-width: none;
39
+ -webkit-mask-image: linear-gradient(to bottom, transparent 0%, #000 35%, #000 65%, transparent 100%);
40
+ mask-image: linear-gradient(to bottom, transparent 0%, #000 35%, #000 65%, transparent 100%);
41
+ border-radius: var(--dile-wheel-picker-band-border-radius, 8px);
42
+ }
43
+ .scroller::-webkit-scrollbar {
44
+ display: none;
45
+ }
46
+ .scroller:focus {
47
+ outline: none;
48
+ }
49
+ .picker:has(.scroller:focus-visible) .band {
50
+ outline: 2px solid var(--dile-wheel-picker-focus-color, var(--dile-focus-border-color, #6af));
51
+ outline-offset: -2px;
52
+ }
53
+ .spacer {
54
+ height: calc(var(--_item-height) * (var(--_visible-items) - 1) / 2);
55
+ }
56
+ .item {
57
+ box-sizing: border-box;
58
+ height: var(--_item-height);
59
+ display: flex;
60
+ align-items: center;
61
+ justify-content: var(--dile-wheel-picker-item-justify-content, center);
62
+ padding: 0 1rem;
63
+ scroll-snap-align: center;
64
+ white-space: nowrap;
65
+ user-select: none;
66
+ -webkit-user-select: none;
67
+ cursor: pointer;
68
+ font-size: var(--dile-wheel-picker-font-size, 1.25rem);
69
+ color: var(--dile-wheel-picker-text-color, #888);
70
+ transition: color 0.15s ease;
71
+ }
72
+ .item.active {
73
+ color: var(--dile-wheel-picker-selected-text-color, var(--dile-on-background-color, #303030));
74
+ }
75
+ .item[aria-disabled="true"] {
76
+ opacity: 0.35;
77
+ cursor: default;
78
+ }
79
+ :host([disabled]) .picker {
80
+ opacity: 0.5;
81
+ pointer-events: none;
82
+ }
83
+ `;
84
+
85
+ static get properties() {
86
+ return {
87
+ items: { type: Array },
88
+ value: {},
89
+ visibleItems: { type: Number },
90
+ label: { type: String },
91
+ disabled: { type: Boolean, reflect: true },
92
+ name: { type: String, reflect: true },
93
+ _activeIndex: { state: true },
94
+ };
95
+ }
96
+
97
+ constructor() {
98
+ super();
99
+ this.items = [];
100
+ this.value = undefined;
101
+ this.visibleItems = 5;
102
+ this.label = '';
103
+ this.disabled = false;
104
+ this._activeIndex = -1;
105
+ this._normalizedItems = [];
106
+ this._scrollTimer = null;
107
+ this._lastItemHeight = 0;
108
+ }
109
+
110
+ get scroller() {
111
+ return this.shadowRoot?.querySelector('.scroller');
112
+ }
113
+
114
+ get selectedItem() {
115
+ return this._normalizedItems[this._indexOfValue()];
116
+ }
117
+
118
+ connectedCallback() {
119
+ super.connectedCallback();
120
+ this._resizeObserver = new ResizeObserver(() => this._handleResize());
121
+ if (this.hasUpdated) {
122
+ this._resizeObserver.observe(this.scroller);
123
+ }
124
+ }
125
+
126
+ disconnectedCallback() {
127
+ super.disconnectedCallback();
128
+ this._resizeObserver?.disconnect();
129
+ clearTimeout(this._scrollTimer);
130
+ }
131
+
132
+ willUpdate(changed) {
133
+ if (changed.has('items')) {
134
+ this._normalizedItems = (this.items || []).map((item) => this._normalizeItem(item));
135
+ }
136
+ if (changed.has('items') || changed.has('value')) {
137
+ this._activeIndex = this._indexOfValue();
138
+ }
139
+ }
140
+
141
+ firstUpdated() {
142
+ this._resizeObserver.observe(this.scroller);
143
+ }
144
+
145
+ updated(changed) {
146
+ if (changed.has('items') || changed.has('visibleItems')) {
147
+ this._scrollToIndex(this._indexOfValue(), 'instant');
148
+ } else if (changed.has('value')) {
149
+ this._scrollToIndex(this._indexOfValue(), 'smooth');
150
+ }
151
+ }
152
+
153
+ render() {
154
+ const visible = this._oddVisibleItems();
155
+ return html`
156
+ <div class="picker" style="--_visible-items: ${visible}">
157
+ <div class="band" part="band"></div>
158
+ <div
159
+ class="scroller"
160
+ role="listbox"
161
+ tabindex="${this.disabled ? '-1' : '0'}"
162
+ aria-label="${this.label || ''}"
163
+ aria-disabled="${this.disabled ? 'true' : 'false'}"
164
+ aria-activedescendant="${this._activeIndex >= 0 ? `option-${this._activeIndex}` : ''}"
165
+ @scroll="${this._onScroll}"
166
+ @scrollend="${this._onScrollEnd}"
167
+ @keydown="${this._onKeydown}"
168
+ >
169
+ <div class="spacer" aria-hidden="true"></div>
170
+ ${this._normalizedItems.map((item, index) => html`
171
+ <div
172
+ id="option-${index}"
173
+ class="item ${index === this._activeIndex ? 'active' : ''}"
174
+ part="item${index === this._activeIndex ? ' active-item' : ''}"
175
+ role="option"
176
+ aria-selected="${index === this._indexOfValue() ? 'true' : 'false'}"
177
+ aria-disabled="${item.disabled ? 'true' : 'false'}"
178
+ @click="${() => this._onItemClick(index)}"
179
+ >${item.label}</div>
180
+ `)}
181
+ <div class="spacer" aria-hidden="true"></div>
182
+ </div>
183
+ </div>
184
+ `;
185
+ }
186
+
187
+ _normalizeItem(item) {
188
+ if (item !== null && typeof item === 'object') {
189
+ const value = 'value' in item ? item.value : item.label;
190
+ return {
191
+ ...item,
192
+ value,
193
+ label: item.label ?? String(value),
194
+ disabled: Boolean(item.disabled),
195
+ };
196
+ }
197
+ return { value: item, label: String(item), disabled: false };
198
+ }
199
+
200
+ _oddVisibleItems() {
201
+ let visible = Math.max(1, Math.round(Number(this.visibleItems) || 5));
202
+ return visible % 2 === 0 ? visible + 1 : visible;
203
+ }
204
+
205
+ _indexOfValue() {
206
+ if (this.value === undefined || this.value === null) {
207
+ return -1;
208
+ }
209
+ return this._normalizedItems.findIndex((item) => String(item.value) === String(this.value));
210
+ }
211
+
212
+ _itemHeight() {
213
+ const item = this.shadowRoot?.querySelector('.item');
214
+ return item ? item.getBoundingClientRect().height : 0;
215
+ }
216
+
217
+ _indexFromScroll() {
218
+ const height = this._itemHeight();
219
+ if (!height || !this._normalizedItems.length) {
220
+ return -1;
221
+ }
222
+ const index = Math.round(this.scroller.scrollTop / height);
223
+ return Math.min(Math.max(index, 0), this._normalizedItems.length - 1);
224
+ }
225
+
226
+ _scrollToIndex(index, behavior = 'smooth') {
227
+ const scroller = this.scroller;
228
+ const height = this._itemHeight();
229
+ if (!scroller || index < 0 || !height) {
230
+ return;
231
+ }
232
+ const top = index * height;
233
+ if (Math.abs(scroller.scrollTop - top) < 1) {
234
+ return;
235
+ }
236
+ scroller.scrollTo({ top, behavior });
237
+ }
238
+
239
+ _handleResize() {
240
+ const height = this._itemHeight();
241
+ if (height && height !== this._lastItemHeight) {
242
+ this._lastItemHeight = height;
243
+ this._scrollToIndex(this._indexOfValue(), 'instant');
244
+ }
245
+ }
246
+
247
+ _onScroll() {
248
+ const index = this._indexFromScroll();
249
+ if (index >= 0) {
250
+ this._activeIndex = index;
251
+ }
252
+ if (!supportsScrollEnd) {
253
+ clearTimeout(this._scrollTimer);
254
+ this._scrollTimer = setTimeout(() => this._onScrollEnd(), 150);
255
+ }
256
+ }
257
+
258
+ _onScrollEnd() {
259
+ clearTimeout(this._scrollTimer);
260
+ const index = this._indexFromScroll();
261
+ if (index < 0) {
262
+ return;
263
+ }
264
+ const enabledIndex = this._nearestEnabledIndex(index);
265
+ if (enabledIndex !== index) {
266
+ this._scrollToIndex(enabledIndex, 'smooth');
267
+ return;
268
+ }
269
+ this._selectIndex(index, false);
270
+ }
271
+
272
+ _onItemClick(index) {
273
+ if (this.disabled || this._normalizedItems[index]?.disabled) {
274
+ return;
275
+ }
276
+ this._selectIndex(index);
277
+ }
278
+
279
+ _onKeydown(e) {
280
+ if (this.disabled || !this._normalizedItems.length) {
281
+ return;
282
+ }
283
+ const current = this._indexOfValue() >= 0 ? this._indexOfValue() : Math.max(this._activeIndex, 0);
284
+ const last = this._normalizedItems.length - 1;
285
+ const page = this._oddVisibleItems();
286
+ let target;
287
+ switch (e.key) {
288
+ case 'ArrowDown':
289
+ target = this._nextEnabledIndex(current, 1);
290
+ break;
291
+ case 'ArrowUp':
292
+ target = this._nextEnabledIndex(current, -1);
293
+ break;
294
+ case 'PageDown':
295
+ target = this._nearestEnabledIndex(Math.min(current + page, last));
296
+ break;
297
+ case 'PageUp':
298
+ target = this._nearestEnabledIndex(Math.max(current - page, 0));
299
+ break;
300
+ case 'Home':
301
+ target = this._nextEnabledIndex(-1, 1);
302
+ break;
303
+ case 'End':
304
+ target = this._nextEnabledIndex(last + 1, -1);
305
+ break;
306
+ default:
307
+ return;
308
+ }
309
+ e.preventDefault();
310
+ if (target >= 0) {
311
+ this._selectIndex(target);
312
+ }
313
+ }
314
+
315
+ _nextEnabledIndex(from, direction) {
316
+ for (let i = from + direction; i >= 0 && i < this._normalizedItems.length; i += direction) {
317
+ if (!this._normalizedItems[i].disabled) {
318
+ return i;
319
+ }
320
+ }
321
+ return this._normalizedItems[from] && !this._normalizedItems[from].disabled ? from : -1;
322
+ }
323
+
324
+ _nearestEnabledIndex(index) {
325
+ const items = this._normalizedItems;
326
+ if (items[index] && !items[index].disabled) {
327
+ return index;
328
+ }
329
+ for (let offset = 1; offset < items.length; offset++) {
330
+ if (items[index - offset] && !items[index - offset].disabled) {
331
+ return index - offset;
332
+ }
333
+ if (items[index + offset] && !items[index + offset].disabled) {
334
+ return index + offset;
335
+ }
336
+ }
337
+ return -1;
338
+ }
339
+
340
+ _selectIndex(index, scroll = true) {
341
+ const item = this._normalizedItems[index];
342
+ if (!item || item.disabled) {
343
+ return;
344
+ }
345
+ this._activeIndex = index;
346
+ if (scroll) {
347
+ this._scrollToIndex(index, 'smooth');
348
+ }
349
+ if (String(item.value) === String(this.value)) {
350
+ return;
351
+ }
352
+ this.value = item.value;
353
+ this.emmitChange();
354
+ this.dispatchEvent(new CustomEvent('dile-wheel-picker-changed', {
355
+ bubbles: true,
356
+ composed: true,
357
+ detail: {
358
+ name: this.name,
359
+ value: item.value,
360
+ item,
361
+ },
362
+ }));
363
+ }
364
+ }
@@ -0,0 +1,5 @@
1
+ import { DileWheelPicker } from './src/DileWheelPicker.js';
2
+
3
+ if (!customElements.get('dile-wheel-picker')) {
4
+ customElements.define('dile-wheel-picker', DileWheelPicker);
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "3.22.0",
3
+ "version": "3.22.1",
4
4
  "description": "UI Core components from dile-components.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -28,5 +28,5 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "707eaedea1d20e5e5bb081d9a05fab7acc1d698c"
31
+ "gitHead": "cef9349e18ff89a24a206a541f8129fd30ccb553"
32
32
  }