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

refactor: use inline style for cell transform #3605

Merged
merged 3 commits into from
Mar 25, 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
1 change: 1 addition & 0 deletions packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ export const KeyboardNavigationMixin = (superClass) =>
_detectInteracting(e) {
const isInteracting = e.composedPath().some((el) => el.localName === 'vaadin-grid-cell-content');
this._setInteracting(isInteracting);
this.__updateHorizontalScrollPosition();
}

/** @private */
Expand Down
41 changes: 24 additions & 17 deletions packages/grid/src/vaadin-grid-scroll-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,33 @@ export const ScrollMixin = (superClass) =>
__updateHorizontalScrollPosition() {
const scrollWidth = this.$.table.scrollWidth;
const clientWidth = this.$.table.clientWidth;
const scrollLeft = this.__getNormalizedScrollLeft(this.$.table);
const scrollLeft = Math.max(0, this.$.table.scrollLeft);
const normalizedScrollLeft = this.__getNormalizedScrollLeft(this.$.table);

// Position header, footer and items container
const transform = `translate(${-scrollLeft}px, 0)`;
this.$.header.style.transform = transform;
this.$.footer.style.transform = transform;
this.$.items.style.transform = transform;

// Position frozen cells
const x = this.__isRTL ? normalizedScrollLeft + clientWidth - scrollWidth : scrollLeft;
const transformFrozen = `translate(${x}px, 0)`;
for (let i = 0; i < this._frozenCells.length; i++) {
this._frozenCells[i].style.transform = transformFrozen;
}

// Position cells frozen to end
const remaining = scrollLeft + clientWidth - scrollWidth;

this.$.table.style.setProperty('--_grid-horizontal-scroll-remaining', remaining + 'px');
this.$.table.style.setProperty('--_grid-horizontal-scroll-position', -this._scrollLeft + 'px');

if (this.__isRTL) {
// Translating the sticky sections using a CSS variable works nicely on LTR.
// On RTL, it causes jumpy behavior (on Desktop Safari) so we need to translate manually.
const transformFrozen = `translate(${remaining}px, 0)`;
for (let i = 0; i < this._frozenCells.length; i++) {
this._frozenCells[i].style.transform = transformFrozen;
}
const remaining = this.__isRTL ? normalizedScrollLeft : scrollLeft + clientWidth - scrollWidth;
const transformFrozenToEnd = `translate(${remaining}px, 0)`;
for (let i = 0; i < this._frozenToEndCells.length; i++) {
this._frozenToEndCells[i].style.transform = transformFrozenToEnd;
}

const transformFrozenToEnd = `translate(${scrollLeft}px, 0)`;
for (let i = 0; i < this._frozenToEndCells.length; i++) {
this._frozenToEndCells[i].style.transform = transformFrozenToEnd;
}
// Only update the --_grid-horizontal-scroll-position custom property when navigating
// on row focus mode to avoid performance issues.
if (this.hasAttribute('navigating') && this.__rowFocusMode) {
this.$.table.style.setProperty('--_grid-horizontal-scroll-position', -x + 'px');
}
}
};
14 changes: 1 addition & 13 deletions packages/grid/src/vaadin-grid-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ registerStyles(

#header,
#footer {
transform: translateX(var(--_grid-horizontal-scroll-position));
display: block;
position: -webkit-sticky;
position: sticky;
Expand Down Expand Up @@ -102,7 +101,6 @@ registerStyles(
}

#items {
transform: translateX(var(--_grid-horizontal-scroll-position));
flex-grow: 1;
flex-shrink: 0;
display: block;
Expand Down Expand Up @@ -164,15 +162,9 @@ registerStyles(
display: none !important;
}

[frozen] {
z-index: 2;
transform: translateX(calc(-1 * var(--_grid-horizontal-scroll-position)));
will-change: transform;
}

[frozen],
[frozen-to-end] {
z-index: 2;
transform: translateX(var(--_grid-horizontal-scroll-remaining));
will-change: transform;
}

Expand Down Expand Up @@ -293,10 +285,6 @@ registerStyles(

/* RTL specific styles */

:host([dir='rtl']) *:is(#items, #header, #footer, [frozen], [frozen-to-end]) {
transform: none;
}

:host([dir='rtl']) #items,
:host([dir='rtl']) #header,
:host([dir='rtl']) #footer {
Expand Down
8 changes: 8 additions & 0 deletions packages/grid/test/keyboard-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fixtureSync,
focusin,
isChrome,
isDesktopSafari,
keyboardEventFor,
keyDownOn,
keyUpOn,
Expand Down Expand Up @@ -1063,6 +1064,13 @@ describe('keyboard navigation', () => {

flushGrid(grid);

// Force reflow to workaround a Safari rendering issue
Copy link
Member Author

@tomivirkki tomivirkki Mar 25, 2022

Choose a reason for hiding this comment

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

Needed to workaround a Safari rendering issue in the test case. Seems that the test renders funny on master also (even though it oddly passes):

Kapture.2022-03-25.at.13.27.39.mp4

if (isDesktopSafari) {
grid.style.display = 'flex';
await nextFrame();
grid.style.display = '';
}

expect(grid.$.table.scrollLeft).to.equal(grid.$.table.scrollWidth - grid.$.table.offsetWidth);
});

Expand Down