Skip to content

Commit

Permalink
Merge pull request #44 from reedsy/1416-the-ui-jumps-when-datepicker-…
Browse files Browse the repository at this point in the history
…is-open-you-scroll-down-and-click-anywhere

Fix: UI jumps when open calendar then scroll from the view and click outside
  • Loading branch information
zzz1ck authored Oct 22, 2024
2 parents 939b797 + 6fc65c5 commit 3cd6c63
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reedsy/vuejs-datepicker",
"version": "1.6.2-reedsy-2.1.8",
"version": "1.6.2-reedsy-2.1.9",
"description": "A simple Vue.js datepicker component. Supports disabling of dates, inline mode, translations",
"keywords": [
"vue",
Expand Down
2 changes: 2 additions & 0 deletions src/components/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ import PickerYear from './PickerYear.vue';
import utils, { makeDateUtils } from '../utils/DateUtils';
import { ELEMENT_IDS } from '../config/ElementIds';
import { getFocusableChildren } from '../utils/FocusableElements';
import { isElementInViewport } from '../utils/IsElementInViewport';
export default {
name: 'DatePicker',
components: {
Expand Down Expand Up @@ -603,6 +604,7 @@ export default {
if (!input) return;
const inputEl = input.$el.querySelector('input');
if (!inputEl) return;
if (!isElementInViewport(inputEl)) return;
inputEl.focus();
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/IsElementInViewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
38 changes: 38 additions & 0 deletions test/unit/specs/Datepicker/Datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,44 @@ describe('Focus after closing the datepicker', () => {
const input = wrapper.vm.$refs.input.$el.querySelector('input');
expect(document.activeElement).toEqual(input);
});
describe('after scrolling and clicking outside', () => {
let wrapper;
let originalGetBoundingClientRect;
beforeEach(() => {
wrapper = mount(Datepicker, { attachTo: document.body });
originalGetBoundingClientRect = Element.prototype.getBoundingClientRect;
});
afterEach(() => {
Element.prototype.getBoundingClientRect = originalGetBoundingClientRect;
wrapper.unmount();
});

it('should focus on the input when it is in viewport', async () => {
Element.prototype.getBoundingClientRect = jest.fn(() => ({
top: 100,
bottom: 0,
left: 0,
right: 0,
}));
await wrapper.vm.showCalendar();
wrapper.vm.clickOutside({ target: document.body });
const input = wrapper.vm.$refs.input.$el.querySelector('input');
expect(document.activeElement).toEqual(input);
});

it('should not focus on the input when it is out of viewport', async () => {
Element.prototype.getBoundingClientRect = jest.fn(() => ({
top: -100,
bottom: 0,
left: 0,
right: 0,
}));
await wrapper.vm.showCalendar();
wrapper.vm.clickOutside({ target: document.body });
const input = wrapper.vm.$refs.input.$el.querySelector('input');
expect(document.activeElement).not.toEqual(input);
});
});
});

describe('Modal', () => {
Expand Down

0 comments on commit 3cd6c63

Please sign in to comment.