Skip to content

Commit

Permalink
Incito: Add carousel layout
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenbo committed Nov 28, 2024
1 parent fa05f75 commit 397ef3c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
30 changes: 29 additions & 1 deletion lib/incito-browser/incito.styl
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,32 @@
position relative

> *
position absolute
position absolute

.tjek-incito__carousel-layout-view
display flex
flex-direction row
justify-content flex-start
align-items center
overflow-x auto
overflow-y hidden
scroll-snap-type x mandatory
-webkit-overflow-scrolling touch

.tjek-incito__carousel-layout-view > *
flex 0 0 auto
scroll-snap-align start
display flex
justify-content center
align-items center
box-sizing border-box

.tjek-incito__carousel-layout-view::-webkit-scrollbar
opacity 0
display none

.tjek-incito__carousel-layout-view::-webkit-scrollbar-track
opacity 0

.tjek-incito__carousel-layout-view::-webkit-scrollbar-thumb
opacity 0
60 changes: 56 additions & 4 deletions lib/incito-browser/incito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ function renderView(view, canLazyload: boolean) {
view.layout_flex_direction;
}

break;
}
case 'CarouselLayout': {
classNames.push('tjek-incito__carousel-layout-view');

break;
}
}
Expand Down Expand Up @@ -788,13 +793,60 @@ export default class Incito extends MicroEvent<{

start() {
this.el.addEventListener('click', (e) => {
const el = closest(
const linkEl = closest(
e.target as HTMLElement,
'.tjek-incito__view [data-link]'
);
const carouselEl = closest(
e.target as HTMLElement,
'.tjek-incito__carousel-layout-view'
);
const carouselPrevEl = closest(
e.target as HTMLElement,
'.incito__view [data-link]'
'[data-role=carousel-prev]'
);
const link = el ? el.dataset.link : null;
const carouselNextEl = closest(
e.target as HTMLElement,
'[data-role=carousel-next]'
);
const link = linkEl ? linkEl.dataset.link : null;

if (isDefinedStr(link)) {
window.open(link!, '_blank');
}

if (isDefinedStr(link)) window.open(link!, '_blank');
if (carouselEl) {
const scrollWidth = carouselEl.scrollWidth;
const clientWidth = carouselEl.clientWidth;
const scrollLeft = carouselEl.scrollLeft;

if (carouselPrevEl) {
const newScrollLeft = scrollLeft - clientWidth;

if (newScrollLeft < 0) {
carouselEl.scrollTo({
left: scrollWidth - clientWidth,
behavior: 'smooth'
});
} else {
carouselEl.scrollTo({
left: newScrollLeft,
behavior: 'smooth'
});
}
} else if (carouselNextEl) {
const newScrollLeft = scrollLeft + clientWidth;

if (newScrollLeft >= scrollWidth) {
carouselEl.scrollTo({left: 0, behavior: 'smooth'});
} else {
carouselEl.scrollTo({
left: newScrollLeft,
behavior: 'smooth'
});
}
}
}
});

if (this.canLazyload) {
Expand Down

0 comments on commit 397ef3c

Please sign in to comment.