Skip to content

Commit a63b41f

Browse files
fix: ensure focused date is visible if overlay is small (#4759) (#4761)
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent 76ba39b commit a63b41f

9 files changed

Lines changed: 198 additions & 49 deletions

File tree

‎integration/tests/dialog-date-picker.test.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '@vaadin/date-picker';
77
import '@vaadin/dialog';
8-
import { getOverlayContent, open } from '@vaadin/date-picker/test/common.js';
8+
import { getOverlayContent, open, waitForScrollToFinish } from '@vaadin/date-picker/test/common.js';
99

1010
describe('date-picker in dialog', () => {
1111
let dialog, datepicker;
@@ -38,6 +38,7 @@ describe('date-picker in dialog', () => {
3838
await sendKeys({ press: 'Tab' });
3939

4040
await nextRender();
41+
await waitForScrollToFinish(getOverlayContent(datepicker));
4142

4243
// Focus the Today button
4344
await sendKeys({ press: 'Tab' });
@@ -59,6 +60,7 @@ describe('date-picker in dialog', () => {
5960
await sendKeys({ press: 'Tab' });
6061

6162
await nextRender();
63+
await waitForScrollToFinish(getOverlayContent(datepicker));
6264

6365
const spy = sinon.spy(datepicker.inputElement, 'focus');
6466

‎packages/date-picker/src/vaadin-date-picker-overlay-content.js‎

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ class DatePickerOverlayContent extends ControllerMixin(ThemableMixin(DirMixin(Po
297297
return this.getAttribute('dir') === 'rtl';
298298
}
299299

300+
/**
301+
* Whether to scroll to a sub-month position when scrolling to a date.
302+
* This is active if the month scroller is not large enough to fit a
303+
* full month. In that case we want to scroll to a position between
304+
* two months in order to have the focused date in the visible area.
305+
* @returns {boolean} whether to use sub-month scrolling
306+
* @private
307+
*/
308+
get __useSubMonthScrolling() {
309+
return this.$.monthScroller.clientHeight < this.$.monthScroller.itemHeight + this.$.monthScroller.bufferOffset;
310+
}
311+
300312
get calendars() {
301313
return [...this.shadowRoot.querySelectorAll('vaadin-month-calendar')];
302314
}
@@ -354,7 +366,9 @@ class DatePickerOverlayContent extends ControllerMixin(ThemableMixin(DirMixin(Po
354366
* Scrolls the list to the given Date.
355367
*/
356368
scrollToDate(date, animate) {
357-
this._scrollToPosition(this._differenceInMonths(date, this._originDate), animate);
369+
const offset = this.__useSubMonthScrolling ? this._calculateWeekScrollOffset(date) : 0;
370+
this._scrollToPosition(this._differenceInMonths(date, this._originDate) + offset, animate);
371+
this.$.monthScroller.forceUpdate();
358372
}
359373

360374
/**
@@ -386,23 +400,63 @@ class DatePickerOverlayContent extends ControllerMixin(ThemableMixin(DirMixin(Po
386400
* Scrolls the month and year scrollers enough to reveal the given date.
387401
*/
388402
revealDate(date, animate = true) {
389-
if (date) {
390-
const diff = this._differenceInMonths(date, this._originDate);
391-
const scrolledAboveViewport = this.$.monthScroller.position > diff;
392-
393-
const visibleArea = Math.max(
394-
this.$.monthScroller.itemHeight,
395-
this.$.monthScroller.clientHeight - this.$.monthScroller.bufferOffset * 2,
396-
);
397-
const visibleItems = visibleArea / this.$.monthScroller.itemHeight;
398-
const scrolledBelowViewport = this.$.monthScroller.position + visibleItems - 1 < diff;
399-
400-
if (scrolledAboveViewport) {
401-
this._scrollToPosition(diff, animate);
402-
} else if (scrolledBelowViewport) {
403-
this._scrollToPosition(diff - visibleItems + 1, animate);
403+
if (!date) {
404+
return;
405+
}
406+
const diff = this._differenceInMonths(date, this._originDate);
407+
// If scroll area does not fit the full month, then always scroll with an offset to
408+
// approximately display the week of the date
409+
if (this.__useSubMonthScrolling) {
410+
const offset = this._calculateWeekScrollOffset(date);
411+
this._scrollToPosition(diff + offset, animate);
412+
return;
413+
}
414+
415+
// Otherwise determine if we need to scroll to make the month of the date visible
416+
const scrolledAboveViewport = this.$.monthScroller.position > diff;
417+
418+
const visibleArea = Math.max(
419+
this.$.monthScroller.itemHeight,
420+
this.$.monthScroller.clientHeight - this.$.monthScroller.bufferOffset * 2,
421+
);
422+
const visibleItems = visibleArea / this.$.monthScroller.itemHeight;
423+
const scrolledBelowViewport = this.$.monthScroller.position + visibleItems - 1 < diff;
424+
425+
if (scrolledAboveViewport) {
426+
this._scrollToPosition(diff, animate);
427+
} else if (scrolledBelowViewport) {
428+
this._scrollToPosition(diff - visibleItems + 1, animate);
429+
}
430+
}
431+
432+
/**
433+
* Calculates an offset to be added to the month scroll position
434+
* when using sub-month scrolling, in order ensure that the week
435+
* that the date is in is visible even for small scroll areas.
436+
* As the month scroller uses a month as minimal scroll unit
437+
* (a value of `1` equals one month), we can not exactly identify
438+
* the position of a specific week. This is a best effort
439+
* implementation based on manual testing.
440+
* @param date the date for which to calculate the offset
441+
* @returns {number} the offset
442+
* @private
443+
*/
444+
_calculateWeekScrollOffset(date) {
445+
// Get first day of month
446+
const temp = new Date(0, 0);
447+
temp.setFullYear(date.getFullYear());
448+
temp.setMonth(date.getMonth());
449+
temp.setDate(1);
450+
// Determine week (=row index) of date within the month
451+
let week = 0;
452+
while (temp.getDate() < date.getDate()) {
453+
temp.setDate(temp.getDate() + 1);
454+
if (temp.getDay() === this.i18n.firstDayOfWeek) {
455+
week += 1;
404456
}
405457
}
458+
// Calculate magic number that approximately keeps the week visible
459+
return week / 6;
406460
}
407461

408462
_initialPositionChanged(initialPosition) {

‎packages/date-picker/src/vaadin-infinite-scroller.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ class InfiniteScroller extends PolymerElement {
143143
}
144144
}
145145

146+
/**
147+
* Force the scroller to update clones after a reset, without
148+
* waiting for the debouncer to resolve.
149+
*/
150+
forceUpdate() {
151+
if (this._debouncerUpdateClones) {
152+
this._buffers[0].updated = this._buffers[1].updated = false;
153+
this._updateClones();
154+
this._debouncerUpdateClones.cancel();
155+
}
156+
}
157+
146158
_activated(active) {
147159
if (active && !this._initialized) {
148160
this._createPool();

‎packages/date-picker/test/overlay.test.js‎

Lines changed: 112 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ function waitUntilScrolledTo(overlay, date, callback) {
1717
}
1818
}
1919

20+
async function customizeFixture({ initialPosition, monthScrollerItems, monthScrollerOffset }) {
21+
const overlay = fixtureSync(`<vaadin-date-picker-overlay-content></vaadin-date-picker-overlay-content>`);
22+
const monthScroller = overlay.$.monthScroller;
23+
monthScroller.style.setProperty('--vaadin-infinite-scroller-buffer-offset', monthScrollerOffset);
24+
monthScroller.style.height = `${270 * monthScrollerItems}px`;
25+
overlay.i18n = getDefaultI18n();
26+
overlay.$.monthScroller.bufferSize = 3;
27+
overlay.$.yearScroller.bufferSize = 3;
28+
overlay.initialPosition = initialPosition || new Date();
29+
await nextRender();
30+
31+
return overlay;
32+
}
33+
2034
describe('overlay', () => {
2135
let overlay;
2236

@@ -315,58 +329,123 @@ describe('overlay', () => {
315329
overlay.$.yearScroller._debouncerUpdateClones.flush();
316330
expect(getFirstVisibleItem(overlay.$.yearScroller, offset).firstElementChild.textContent).to.contain('2000');
317331
});
332+
333+
describe('height(visible area) < height(item)', () => {
334+
let overlay, monthScroller;
335+
336+
beforeEach(async () => {
337+
overlay = await customizeFixture({
338+
initialPosition: new Date(2021, 1, 1),
339+
monthScrollerItems: 0.5,
340+
monthScrollerOffset: 0,
341+
});
342+
monthScroller = overlay.$.monthScroller;
343+
});
344+
345+
it('should scroll to a sub-month position that approximately shows the week the date is in', () => {
346+
const initialPosition = monthScroller.position;
347+
// Scroll to 15th
348+
overlay.scrollToDate(new Date(2021, 1, 15), false);
349+
const positionOf15th = monthScroller.position;
350+
expect(positionOf15th).to.be.greaterThan(initialPosition);
351+
expect(positionOf15th).to.be.lessThan(initialPosition + 1);
352+
// Scroll to 28th
353+
overlay.scrollToDate(new Date(2021, 1, 28), false);
354+
const positionOf28th = monthScroller.position;
355+
expect(positionOf28th).to.be.greaterThan(initialPosition);
356+
expect(positionOf28th).to.be.greaterThan(positionOf15th);
357+
expect(positionOf28th).to.be.lessThan(initialPosition + 1);
358+
// Scroll to first of previous month
359+
overlay.scrollToDate(new Date(2021, 0, 1), false);
360+
const firstOfPreviousMonthPosition = monthScroller.position;
361+
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
362+
// Scroll to first of next month
363+
overlay.scrollToDate(new Date(2021, 2, 1), false);
364+
const firstOfNextMonthPosition = monthScroller.position;
365+
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
366+
});
367+
});
368+
369+
describe('height(visible area) > height(item)', () => {
370+
let overlay, monthScroller;
371+
372+
beforeEach(async () => {
373+
overlay = await customizeFixture({
374+
initialPosition: new Date(2021, 1, 1),
375+
monthScrollerItems: 3,
376+
monthScrollerOffset: 0,
377+
});
378+
monthScroller = overlay.$.monthScroller;
379+
});
380+
381+
it('should always scroll to the exact position of the month that the date is in', () => {
382+
const initialPosition = monthScroller.position;
383+
// Scroll to 15th
384+
overlay.scrollToDate(new Date(2021, 1, 15), false);
385+
const positionOf15th = monthScroller.position;
386+
expect(positionOf15th).to.equal(initialPosition);
387+
// Scroll to 28th
388+
overlay.scrollToDate(new Date(2021, 1, 28), false);
389+
const positionOf28th = monthScroller.position;
390+
expect(positionOf28th).to.equal(initialPosition);
391+
// Scroll to first of previous month
392+
overlay.scrollToDate(new Date(2021, 0, 1), false);
393+
const firstOfPreviousMonthPosition = monthScroller.position;
394+
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
395+
// Scroll to first of next month
396+
overlay.scrollToDate(new Date(2021, 2, 1), false);
397+
const firstOfNextMonthPosition = monthScroller.position;
398+
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
399+
});
400+
});
318401
});
319402

320403
describe('revealDate', () => {
321404
let overlay, monthScroller;
322405

323-
async function fixtureOverlayContent({ monthScrollerItems, monthScrollerOffset }) {
324-
overlay = fixtureSync(`
325-
<vaadin-date-picker-overlay-content></vaadin-date-picker-overlay-content>
326-
`);
327-
monthScroller = overlay.$.monthScroller;
328-
monthScroller.style.setProperty('--vaadin-infinite-scroller-buffer-offset', monthScrollerOffset);
329-
monthScroller.style.height = `calc(var(--vaadin-infinite-scroller-item-height) * ${monthScrollerItems})`;
330-
overlay.i18n = getDefaultI18n();
331-
overlay.$.monthScroller.bufferSize = 3;
332-
overlay.$.yearScroller.bufferSize = 3;
333-
overlay.initialPosition = new Date(2021, 1, 1);
334-
await nextRender();
335-
}
336-
337406
describe('height(visible area) < height(item)', () => {
338407
beforeEach(async () => {
339-
await fixtureOverlayContent({
408+
overlay = await customizeFixture({
409+
initialPosition: new Date(2021, 1, 1),
340410
monthScrollerItems: 0.5,
341411
monthScrollerOffset: 0,
342412
});
413+
monthScroller = overlay.$.monthScroller;
343414
});
344415

345-
it('should scroll when the month is above the visible area', () => {
346-
const position = monthScroller.position;
416+
it('should scroll to a position that approximately shows the week the date is in', () => {
417+
// Starting on first of February
418+
const initialPosition = monthScroller.position;
419+
// Scroll to 15th
420+
overlay.revealDate(new Date(2021, 1, 15), false);
421+
const positionOf15th = monthScroller.position;
422+
expect(positionOf15th).to.be.greaterThan(initialPosition);
423+
expect(positionOf15th).to.be.lessThan(initialPosition + 1);
424+
// Scroll to 28th
425+
overlay.revealDate(new Date(2021, 1, 28), false);
426+
const positionOf28th = monthScroller.position;
427+
expect(positionOf28th).to.be.greaterThan(initialPosition);
428+
expect(positionOf28th).to.be.greaterThan(positionOf15th);
429+
expect(positionOf28th).to.be.lessThan(initialPosition + 1);
430+
// Scroll to first of previous month
347431
overlay.revealDate(new Date(2021, 0, 1), false);
348-
expect(monthScroller.position).to.equal(position - 1);
349-
});
350-
351-
it('should not scroll when the month is the same', () => {
352-
const position = monthScroller.position;
353-
overlay.revealDate(new Date(2021, 1, 10), false);
354-
expect(monthScroller.position).to.equal(position);
355-
});
356-
357-
it('should scroll when the month is below the visible area', () => {
358-
const position = monthScroller.position;
432+
const firstOfPreviousMonthPosition = monthScroller.position;
433+
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
434+
// Scroll to first of next month
359435
overlay.revealDate(new Date(2021, 2, 1), false);
360-
expect(monthScroller.position).to.equal(position + 1);
436+
const firstOfNextMonthPosition = monthScroller.position;
437+
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
361438
});
362439
});
363440

364441
describe('height(visible area) > height(item)', () => {
365442
beforeEach(async () => {
366-
await fixtureOverlayContent({
443+
overlay = await customizeFixture({
444+
initialPosition: new Date(2021, 1, 1),
367445
monthScrollerItems: 2,
368446
monthScrollerOffset: 0,
369447
});
448+
monthScroller = overlay.$.monthScroller;
370449
});
371450

372451
it('should scroll when the month is above the visible area', () => {
@@ -390,10 +469,12 @@ describe('overlay', () => {
390469

391470
describe('offset', () => {
392471
beforeEach(async () => {
393-
await fixtureOverlayContent({
472+
overlay = await customizeFixture({
473+
initialPosition: new Date(2021, 1, 1),
394474
monthScrollerItems: 3,
395475
monthScrollerOffset: '10%',
396476
});
477+
monthScroller = overlay.$.monthScroller;
397478
});
398479

399480
it('should scroll when the month is above the visible area', () => {
-471 Bytes
Loading
-768 Bytes
Loading
-478 Bytes
Loading
-695 Bytes
Loading

‎packages/date-picker/theme/lumo/vaadin-date-picker-overlay-content-styles.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ registerStyles(
3636
+ var(--lumo-size-m) * 6
3737
+ var(--lumo-space-s)
3838
);
39-
--vaadin-infinite-scroller-buffer-offset: 20%;
39+
--vaadin-infinite-scroller-buffer-offset: 10%;
4040
-webkit-mask-image: linear-gradient(transparent, #000 10%, #000 85%, transparent);
4141
mask-image: linear-gradient(transparent, #000 10%, #000 85%, transparent);
4242
position: relative;

0 commit comments

Comments
 (0)