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

feat(cxl-ui): add snap scrolling in category-accordion #330

Merged
merged 1 commit into from
Sep 21, 2023
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
89 changes: 81 additions & 8 deletions packages/cxl-lumo-styles/scss/themes/cxl-tabs-slider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,85 @@
}
}

// Featured course slider and course-slider themes should always snap scroll
// between cards. can also be set with new attribute "snap-scroll"
:host([theme~="cxl-featured-course-slider"]),
:host([theme~="cxl-course-slider"]),
:host([snap-scroll]) {
[part="tabs"] {
padding: 0;
margin: 0;
overflow-x: auto;
scrollbar-width: none;
-ms-overflow-style: none;

scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
scroll-behavior: smooth;

::slotted(*) {
scroll-snap-align: start;
scroll-snap-stop: always;
/* stylelint-disable-next-line declaration-no-important */
pointer-events: all !important;
}

&::-webkit-scrollbar {
display: none;
}
}
}

// Featured course slider
:host([theme~="cxl-featured-course-slider"][orientation="horizontal"]) {
background-color: var(--cxl-color-light-gray);

[part="back-button"],
[part="forward-button"] {
top: calc(50% - 2 * var(--lumo-space-xl));
width: var(--lumo-size-s);
height: var(--lumo-size-s);
color: var(--lumo-primary-color);
border-radius: 100%;

&::after {
font-size: var(--lumo-font-size-xl);
}

@media #{mq.$medium} {
top: unset;
width: var(--lumo-space-xl);
height: var(--lumo-space-xl);

&::after {
font-size: var(--lumo-font-size-xxl);
}
}
}

[part="back-button"] {
margin-left: var(--lumo-space-s);
}

[part="forward-button"] {
margin-right: var(--lumo-space-s);
}

::slotted(vaadin-tab) {
padding: 0 var(--lumo-space-m);

@media #{mq.$medium} {
padding: 0;
}
}

::slotted(vaadin-tab:not(:first-child)) {
@media #{mq.$medium} {
padding-left: var(--lumo-size-xl);
}
}
}

/**
* Theme "cxl-course-slider"
*/
Expand All @@ -48,13 +127,15 @@
::slotted(vaadin-tab) {
padding: 0 calc(var(--lumo-space-s) / 2) calc(var(--lumo-space-s) / 2);
overflow: visible;
scroll-margin-inline-start: var(--lumo-space-xl);
}

::slotted(vaadin-tab:first-of-type) {
margin-inline-start: calc(var(--lumo-space-m) - var(--lumo-space-xs));

@media #{mq.$small} {
margin-inline-start: calc(var(--lumo-space-l) - var(--lumo-space-xs));
scroll-margin-inline-start: var(--lumo-space-l);
}
}

Expand All @@ -71,15 +152,7 @@

[part="tabs"] {
padding-top: var(--cxl-space-sm);
margin: 0;
overflow-x: auto;
overflow-y: hidden;
scrollbar-width: none;
-ms-overflow-style: none;

&::-webkit-scrollbar {
display: none;
}
}

[part="back-button"],
Expand Down
53 changes: 50 additions & 3 deletions packages/cxl-ui/src/components/cxl-tabs-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { customElement } from 'lit/decorators.js';

@customElement('cxl-tabs-slider')
export class CXLTabsSliderElement extends Tabs {
_updateOverflow() {
this.centeredContainerWidthCheck();
super._updateOverflow();
/**
* @return {number}
* @protected
*/
get _scrollOffset() {
if (this.getAttribute('theme').split(' ').includes('cxl-category-accordion')) {
return 200;
}
return super._scrollOffset;
}

_onResize() {
Expand All @@ -32,6 +38,7 @@ export class CXLTabsSliderElement extends Tabs {
* @see https://github.com/vaadin/web-components/blob/v20.0.0-beta1/packages/vaadin-tabs/src/vaadin-tabs.js#L222-L240
* @see https://lit-and-friends.slack.com/archives/C6ULJ2F7S/p1620125717028800
*/

centeredContainerWidthCheck() {
const themeAttr = 'theme';
const centeredVal = 'centered';
Expand All @@ -50,6 +57,46 @@ export class CXLTabsSliderElement extends Tabs {
this.setAttribute(themeAttr, themes.join(' '));
}

/** Redefining Vaadin-Tabs method to fix bug in scroll position end detection */
_updateOverflow() {
this.centeredContainerWidthCheck();

const scrollPosition = this._vertical
? this._scrollerElement.scrollTop
: this.__getNormalizedScrollLeft(this._scrollerElement);
freudFlintstone marked this conversation as resolved.
Show resolved Hide resolved
const scrollSize = this._vertical
? this._scrollerElement.scrollHeight
: this._scrollerElement.scrollWidth;
freudFlintstone marked this conversation as resolved.
Show resolved Hide resolved
const offset = this._vertical
? this._scrollerElement.offsetHeight
: this._scrollerElement.offsetWidth;

// Note that we are not comparing floored scrollPosition to be greater than zero here, which would make a natural
// sense, but to be greater than one intentionally. There is a known bug in Chromium browsers on Linux/Mac
// (https://bugs.chromium.org/p/chromium/issues/detail?id=1123301), which returns invalid value of scrollLeft when
// text direction is RTL. The value is off by one pixel in that case. Comparing scrollPosition to be greater than
pawelkmpt marked this conversation as resolved.
Show resolved Hide resolved
// one on the following line is a workaround for that bug. Comparing scrollPosition to be greater than one means,
// that the left overflow and left arrow will be displayed "one pixel" later than normal. In other words, if the tab
// scroller element is scrolled just by 1px, the overflow is not yet showing.
let overflow = Math.floor(scrollPosition) > 1 ? 'start' : '';

if (Math.ceil(scrollPosition) + offset < Math.ceil(scrollSize - this._scrollOffset / 10)) {
overflow += ' end';
}

if (this.__direction === 1) {
overflow = overflow.replace(/start|end/gi, (matched) =>
matched === 'start' ? 'end' : 'start'
);
}

if (overflow) {
this.setAttribute('overflow', overflow.trim());
} else {
this.removeAttribute('overflow');
}
}

static get is() {
return 'cxl-tabs-slider';
}
Expand Down
Loading