Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure focused date is visible if overlay is small #809

Merged
merged 4 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 70 additions & 16 deletions src/vaadin-date-picker-overlay-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@
return this.getAttribute('dir') === 'rtl';
}

/**
* Whether to scroll to a sub-month position when scrolling to a date.
* This is active if the month scroller is not large enough to fit a
* full month. In that case we want to scroll to a position between
* two months in order to have the focused date in the visible area.
* @returns {boolean} whether to use sub-month scrolling
* @private
*/
get __useSubMonthScrolling() {
return this.$.monthScroller.clientHeight < this.$.monthScroller.itemHeight + this.$.monthScroller.bufferOffset;
}

ready() {
super.ready();
this.setAttribute('tabindex', 0);
Expand Down Expand Up @@ -364,7 +376,9 @@
* Scrolls the list to the given Date.
*/
scrollToDate(date, animate) {
this._scrollToPosition(this._differenceInMonths(date, this._originDate), animate);
const offset = this.__useSubMonthScrolling ? this._calculateWeekScrollOffset(date) : 0;
this._scrollToPosition(this._differenceInMonths(date, this._originDate) + offset, animate);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This covers the initial scrolling to the focused date when opening the overlay.

this.$.monthScroller.forceUpdate();
}

_focusedDateChanged(focusedDate) {
Expand All @@ -385,23 +399,63 @@
* Scrolls the month and year scrollers enough to reveal the given date.
*/
revealDate(date, animate = true) {
if (date) {
const diff = this._differenceInMonths(date, this._originDate);
const scrolledAboveViewport = this.$.monthScroller.position > diff;

const visibleArea = Math.max(
this.$.monthScroller.itemHeight,
this.$.monthScroller.clientHeight - this.$.monthScroller.bufferOffset * 2
);
const visibleItems = visibleArea / this.$.monthScroller.itemHeight;
const scrolledBelowViewport = this.$.monthScroller.position + visibleItems - 1 < diff;

if (scrolledAboveViewport) {
this._scrollToPosition(diff, animate);
} else if (scrolledBelowViewport) {
this._scrollToPosition(diff - visibleItems + 1, animate);
if (!date) {
return;
}
const diff = this._differenceInMonths(date, this._originDate);
// If scroll area does not fit the full month, then always scroll with an offset to
// approximately display the week of the date
if (this.__useSubMonthScrolling) {
const offset = this._calculateWeekScrollOffset(date);
this._scrollToPosition(diff + offset, animate);
return;
}

// Otherwise determine if we need to scroll to make the month of the date visible
const scrolledAboveViewport = this.$.monthScroller.position > diff;

const visibleArea = Math.max(
this.$.monthScroller.itemHeight,
this.$.monthScroller.clientHeight - this.$.monthScroller.bufferOffset * 2
);
const visibleItems = visibleArea / this.$.monthScroller.itemHeight;
const scrolledBelowViewport = this.$.monthScroller.position + visibleItems - 1 < diff;

if (scrolledAboveViewport) {
this._scrollToPosition(diff, animate);
} else if (scrolledBelowViewport) {
this._scrollToPosition(diff - visibleItems + 1, animate);
}
}

/**
* Calculates an offset to be added to the month scroll position
* when using sub-month scrolling, in order ensure that the week
* that the date is in is visible even for small scroll areas.
* As the month scroller uses a month as minimal scroll unit
* (a value of `1` equals one month), we can not exactly identify
* the position of a specific week. This is a best effort
* implementation based on manual testing.
* @param date the date for which to calculate the offset
* @returns {number} the offset
* @private
*/
_calculateWeekScrollOffset(date) {
// Get first day of month
const temp = new Date(0, 0);
temp.setFullYear(date.getFullYear());
temp.setMonth(date.getMonth());
temp.setDate(1);
// Determine week (=row index) of date within the month
let week = 0;
while (temp.getDate() < date.getDate()) {
temp.setDate(temp.getDate() + 1);
if (temp.getDay() === this.i18n.firstDayOfWeek) {
week += 1;
}
}
// Calculate magic number that approximately keeps the week visible
return week / 6;
}

_onOverlayFocus() {
Expand Down
12 changes: 12 additions & 0 deletions src/vaadin-infinite-scroller.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@
}
}

/**
* Force the scroller to update clones after a reset, without
* waiting for the debouncer to resolve.
*/
forceUpdate() {
if (this._debouncerUpdateClones) {
this._buffers[0].updated = this._buffers[1].updated = false;
this._updateClones();
this._debouncerUpdateClones.cancel();
}
}

_activated(active) {
if (active && !this._initialized) {
this._createPool();
Expand Down
168 changes: 131 additions & 37 deletions test/overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@
}
}

function customizeFixture({initialPosition, monthScrollerItems, monthScrollerOffset}) {
const overlay = fixture('overlay');
const monthScroller = overlay.$.monthScroller;
monthScroller.style.setProperty('--vaadin-infinite-scroller-buffer-offset', monthScrollerOffset);
monthScroller.updateStyles({'--vaadin-infinite-scroller-buffer-offset': monthScrollerOffset});
monthScroller.style.height = `${270 * monthScrollerItems}px`;
overlay.i18n = getDefaultI18n();
overlay.$.monthScroller.bufferSize = 3;
overlay.$.yearScroller.bufferSize = 3;
overlay.initialPosition = initialPosition || new Date();
return overlay;
}

describe('vaadin-date-picker-overlay', () => {
var overlay;

Expand Down Expand Up @@ -411,54 +424,55 @@
describe('revealDate', () => {
let overlay, monthScroller;

function fixtureOverlayContent({monthScrollerItems, monthScrollerOffset}, done) {
overlay = fixture('overlay');
monthScroller = overlay.$.monthScroller;
monthScroller.style.setProperty('--vaadin-infinite-scroller-buffer-offset', monthScrollerOffset);
monthScroller.updateStyles({'--vaadin-infinite-scroller-buffer-offset': monthScrollerOffset});
monthScroller.style.height = `${270 * monthScrollerItems}px`;
overlay.i18n = getDefaultI18n();
overlay.$.monthScroller.bufferSize = 3;
overlay.$.yearScroller.bufferSize = 3;
overlay.initialPosition = new Date(2021, 1, 1);
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
}

describe('height(visible area) < height(item)', () => {
beforeEach((done) => {
fixtureOverlayContent({
overlay = customizeFixture({
initialPosition: new Date(2021, 1, 1),
monthScrollerItems: 0.5,
monthScrollerOffset: 0
}, done);
});

it('should scroll when the month is above the visible area', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old test cases for height(visible area) < height(item) are obsolete with the new mode.

const position = monthScroller.position;
});
monthScroller = overlay.$.monthScroller;
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
});

it('should scroll to a position that approximately shows the week the date is in', () => {
// Starting on first of February
const initialPosition = monthScroller.position;
// Scroll to 15th
overlay.revealDate(new Date(2021, 1, 15), false);
const positionOf15th = monthScroller.position;
expect(positionOf15th).to.be.greaterThan(initialPosition);
expect(positionOf15th).to.be.lessThan(initialPosition + 1);
Comment on lines +444 to +447
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent some time to create a test that checks if the date element is actually visible in the scroll area, but could not find a reliable setup. The scroller hard-codes the height of an item to 270px, while the test does not load the theme, which leads to an item only using about 135px.

So I went with the same position property checks that the other tests use.

// Scroll to 28th
overlay.revealDate(new Date(2021, 1, 28), false);
const positionOf28th = monthScroller.position;
expect(positionOf28th).to.be.greaterThan(initialPosition);
expect(positionOf28th).to.be.greaterThan(positionOf15th);
expect(positionOf28th).to.be.lessThan(initialPosition + 1);
// Scroll to first of previous month
overlay.revealDate(new Date(2021, 0, 1), false);
expect(monthScroller.position).to.equal(position - 1);
});

it('should not scroll when the month is the same', () => {
const position = monthScroller.position;
overlay.revealDate(new Date(2021, 1, 10), false);
expect(monthScroller.position).to.equal(position);
});

it('should scroll when the month is below the visible area', () => {
const position = monthScroller.position;
const firstOfPreviousMonthPosition = monthScroller.position;
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
// Scroll to first of next month
overlay.revealDate(new Date(2021, 2, 1), false);
expect(monthScroller.position).to.equal(position + 1);
const firstOfNextMonthPosition = monthScroller.position;
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
});
});

describe('height(visible area) > height(item)', () => {
beforeEach((done) => {
fixtureOverlayContent({
overlay = customizeFixture({
initialPosition: new Date(2021, 1, 1),
monthScrollerItems: 2,
monthScrollerOffset: 0
}, done);
});
monthScroller = overlay.$.monthScroller;
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
});

it('should scroll when the month is above the visible area', () => {
Expand All @@ -482,10 +496,15 @@

describe('offset', () => {
beforeEach((done) => {
fixtureOverlayContent({
overlay = customizeFixture({
initialPosition: new Date(2021, 1, 1),
monthScrollerItems: 3,
monthScrollerOffset: '10%'
}, done);
});
monthScroller = overlay.$.monthScroller;
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
});

it('should scroll when the month is above the visible area', () => {
Expand All @@ -507,6 +526,81 @@
});
});
});

describe('scrollToDate', () => {
let overlay, monthScroller;

describe('height(visible area) < height(item)', () => {
beforeEach((done) => {
overlay = customizeFixture({
initialPosition: new Date(2021, 1, 1),
monthScrollerItems: 0.5,
monthScrollerOffset: 0
});
monthScroller = overlay.$.monthScroller;
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
});

it('should scroll to a sub-month position that approximately shows the week the date is in', () => {
const initialPosition = monthScroller.position;
// Scroll to 15th
overlay.scrollToDate(new Date(2021, 1, 15), false);
const positionOf15th = monthScroller.position;
expect(positionOf15th).to.be.greaterThan(initialPosition);
expect(positionOf15th).to.be.lessThan(initialPosition + 1);
// Scroll to 28th
overlay.scrollToDate(new Date(2021, 1, 28), false);
const positionOf28th = monthScroller.position;
expect(positionOf28th).to.be.greaterThan(initialPosition);
expect(positionOf28th).to.be.greaterThan(positionOf15th);
expect(positionOf28th).to.be.lessThan(initialPosition + 1);
// Scroll to first of previous month
overlay.scrollToDate(new Date(2021, 0, 1), false);
const firstOfPreviousMonthPosition = monthScroller.position;
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
// Scroll to first of next month
overlay.scrollToDate(new Date(2021, 2, 1), false);
const firstOfNextMonthPosition = monthScroller.position;
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
});
});

describe('height(visible area) > height(item)', () => {
beforeEach((done) => {
overlay = customizeFixture({
initialPosition: new Date(2021, 1, 1),
monthScrollerItems: 3,
monthScrollerOffset: 0
});
monthScroller = overlay.$.monthScroller;
setTimeout(() => {
Polymer.RenderStatus.afterNextRender(overlay, done);
}, 1);
});

it('should always scroll to the exact position of the month that the date is in', () => {
const initialPosition = monthScroller.position;
// Scroll to 15th
overlay.scrollToDate(new Date(2021, 1, 15), false);
const positionOf15th = monthScroller.position;
expect(positionOf15th).to.equal(initialPosition);
// Scroll to 28th
overlay.scrollToDate(new Date(2021, 1, 28), false);
const positionOf28th = monthScroller.position;
expect(positionOf15th).to.equal(initialPosition);
// Scroll to first of previous month
overlay.scrollToDate(new Date(2021, 0, 1), false);
const firstOfPreviousMonthPosition = monthScroller.position;
expect(firstOfPreviousMonthPosition).to.equal(initialPosition - 1);
// Scroll to first of next month
overlay.scrollToDate(new Date(2021, 2, 1), false);
const firstOfNextMonthPosition = monthScroller.position;
expect(firstOfNextMonthPosition).to.equal(initialPosition + 1);
});
});
});
</script>

</body>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion theme/lumo/vaadin-date-picker-overlay-content-styles.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
+ var(--lumo-size-m) * 6
+ var(--lumo-space-s)
);
--vaadin-infinite-scroller-buffer-offset: 20%;
--vaadin-infinite-scroller-buffer-offset: 10%;
-webkit-mask-image: linear-gradient(transparent, #000 10%, #000 85%, transparent);
mask-image: linear-gradient(transparent, #000 10%, #000 85%, transparent);
position: relative;
Expand Down