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

use CSS transitions and transforms in horizontal scroll #5193

Merged
merged 9 commits into from
Oct 12, 2021
Prev Previous commit
merge move method into scrollToPosition
- fix playwright test
  • Loading branch information
radium-v committed Oct 12, 2021
commit b6719f4641c2d76b827a71f0a7a1c03457521180
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,33 @@ describe("FASTHorizontalScroll", function () {
"fast-horizontal-scroll"
)) as ElementHandle<fastHorizontalScroll>;

const doubleWidth = componentWidth * 2;

await element.evaluateHandle(node => node.scrollToNext());

await element.waitForElementState("stable");

const firstXPos = await element.evaluate(node => node.scrollContainer.scrollLeft);

await element.evaluateHandle(node => node.scrollToNext());
await element.evaluateHandle(node => node.scrollToPrevious());

await element.waitForElementState("stable");

await this.page.evaluate(
({ element, componentWidth }) => {
element.style.setProperty("width", `${componentWidth * 2}px`);
},
{ element, componentWidth }
);
await element.evaluateHandle((node, doubleWidth) => {
node.style.setProperty("width", `${doubleWidth}px`);
}, doubleWidth);

await element.waitForElementState("stable");

expect(await element.evaluate(node => node.clientWidth)).to.equal(doubleWidth);

await element.evaluateHandle(node => node.scrollToNext());

await element.waitForElementState("stable");

const secondXPos = await element.evaluate(node => node.scrollContainer.scrollLeft);
const secondXPos = await element.evaluate(
node => node.scrollContainer.scrollLeft
);

expect(firstXPos).to.not.equal(secondXPos);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,21 +428,16 @@ export class HorizontalScroll extends FoundationElement {

this.scrolling = true;

const seconds = Math.abs(newPosition - position) / this.speed;
const seconds =
this.duration ?? `${Math.abs(newPosition - position) / this.speed}s`;

this.move(newPosition, seconds);
}
this.content.style.setProperty("transition-duration", seconds);

/**
* Applies the CSS transition and transform to the content, then sets the
* scroll position for the scroll container.
*
* @param newPosition - position to move towards
* @param duration - The default calculated transition duration in seconds
* @internal
*/
private move(newPosition: number, duration: number): void {
const transitionendHandler = (e?: TransitionEvent) => {
const computedDuration = parseFloat(
getComputedStyle(this.content).getPropertyValue("transition-duration")
);

const transitionendHandler = (e?: TransitionEvent): void => {
if (e && e.target !== e.currentTarget) {
return;
}
Expand All @@ -460,15 +455,6 @@ export class HorizontalScroll extends FoundationElement {
this.scrolling = false;
};

this.content.style.setProperty(
"transition-duration",
this.duration ?? `${duration}s`
);

const computedDuration = parseFloat(
getComputedStyle(this.content).getPropertyValue("transition-duration")
);

if (computedDuration === 0) {
transitionendHandler();
return;
Expand Down