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(datetime): RTL will no longer infinitely scroll #24475

Merged
merged 6 commits into from
Jan 4, 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
13 changes: 8 additions & 5 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getIonMode } from '../../global/ionic-global';
import { Color, DatetimeChangeEventDetail, DatetimeParts, Mode, StyleEventDetail } from '../../interface';
import { startFocusVisible } from '../../utils/focus-visible';
import { getElementRoot, raf, renderHiddenInput } from '../../utils/helpers';
import { isRTL } from '../../utils/rtl';
import { createColorClasses } from '../../utils/theme';
import { PickerColumnItem } from '../picker-column-internal/picker-column-internal-interfaces';

Expand Down Expand Up @@ -686,7 +687,7 @@ export class Datetime implements ComponentInterface {
* if element is not in viewport. Use scrollLeft instead.
*/
writeTask(() => {
calendarBodyRef.scrollLeft = startMonth.clientWidth;
calendarBodyRef.scrollLeft = startMonth.clientWidth * (isRTL(this.el) ? -1 : 1);

let endIO: IntersectionObserver | undefined;
let startIO: IntersectionObserver | undefined;
Expand Down Expand Up @@ -765,7 +766,7 @@ export class Datetime implements ComponentInterface {
year
});

calendarBodyRef.scrollLeft = workingMonth.clientWidth;
calendarBodyRef.scrollLeft = workingMonth.clientWidth * (isRTL(this.el) ? -1 : 1);
calendarBodyRef.style.removeProperty('overflow');
calendarBodyRef.style.removeProperty('pointer-events');

Expand Down Expand Up @@ -1000,9 +1001,11 @@ export class Datetime implements ComponentInterface {
const nextMonth = calendarBodyRef.querySelector('.calendar-month:last-of-type');
if (!nextMonth) { return; }

const left = (nextMonth as HTMLElement).offsetWidth * 2;

calendarBodyRef.scrollTo({
top: 0,
left: (nextMonth as HTMLElement).offsetWidth * 2,
left: left * (isRTL(this.el) ? -1 : 1),
behavior: 'smooth'
});
}
Expand Down Expand Up @@ -1150,10 +1153,10 @@ export class Datetime implements ComponentInterface {
<div class="calendar-next-prev">
<ion-buttons>
<ion-button onClick={() => this.prevMonth()}>
<ion-icon slot="icon-only" icon={chevronBack} lazy={false}></ion-icon>
<ion-icon slot="icon-only" icon={chevronBack} lazy={false} flipRtl></ion-icon>
</ion-button>
<ion-button onClick={() => this.nextMonth()}>
<ion-icon slot="icon-only" icon={chevronForward} lazy={false}></ion-icon>
<ion-icon slot="icon-only" icon={chevronForward} lazy={false} flipRtl></ion-icon>
</ion-button>
</ion-buttons>
</div>
Expand Down
11 changes: 10 additions & 1 deletion core/src/components/datetime/test/basic/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Footer', () => {
});
});


describe('datetime: selecting a day', () => {

it('should update the active day', async () => {
Expand All @@ -100,4 +99,14 @@ describe('datetime: selecting a day', () => {

expect(newActiveDay.innerText).toEqual('13');
});

});

test('datetime:rtl: basic', async () => {
const page = await newE2EPage({
url: '/src/components/datetime/test/basic?ionic:_testing=true&rtl=true'
});

const compare = await page.compareScreenshot();
expect(compare).toMatchScreenshot();
});
32 changes: 32 additions & 0 deletions core/src/utils/rtl/dir.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isRTL } from './dir';

describe('rtl: dir', () => {

describe('with host element', () => {

it('should return true', () => {
expect(isRTL({ dir: 'rtl' })).toBe(true);
});

it('should return false', () => {
expect(isRTL({ dir: 'ltr' })).toBe(false);
expect(isRTL({ dir: '' })).toBe(false);
});

});

describe('without host element', () => {

it('should return true', () => {
global.document.dir = 'rtl';
expect(isRTL()).toBe(true);
});

it('should return false', () => {
global.document.dir = 'ltr';
expect(isRTL()).toBe(false);
});

});

});
14 changes: 14 additions & 0 deletions core/src/utils/rtl/dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

/**
* Returns `true` if the document or host element
* has a `dir` set to `rtl`. The host value will always
* take priority over the root document value.
*/
export const isRTL = (hostEl?: Pick<HTMLElement, 'dir'>) => {
if (hostEl) {
if (hostEl.dir !== '') {
return hostEl.dir.toLowerCase() === 'rtl';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be converting to lowercase? It looks "Ltr" should be treated as invalid looking at the accepted values: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to swap that to explicit checks. Was siding on Dx, but if the browser will ignore any values that aren't rtl | ltr | auto, that isn't very helpful to be different.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops I take it back. The browser dir is not case sensitive, so what you have currently aligns with how the browser behaves: https://codepen.io/liamdebeasi/pen/Jjrvgrp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, that change is reverted 👍

}
}
return document?.dir.toLowerCase() === 'rtl';
}
1 change: 1 addition & 0 deletions core/src/utils/rtl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dir';