Skip to content

Commit

Permalink
feat(esl-carousel): add centered renderer
Browse files Browse the repository at this point in the history
Co-authored-by: ala'n (Alexey Stsefanovich) <[email protected]>
  • Loading branch information
abarmina and ala-n committed Jul 18, 2024
1 parent ef418f1 commit dd26e7e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/modules/esl-carousel/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export * from './plugin/wheel/esl-carousel.wheel.mixin';
import './renderers/esl-carousel.none.renderer';
import './renderers/esl-carousel.default.renderer';
import './renderers/esl-carousel.grid.renderer';
import './renderers/esl-carousel.centered.renderer';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {ESLCarouselRenderer} from '../core/esl-carousel.renderer';
import {ESLDefaultCarouselRenderer} from './esl-carousel.default.renderer';
import {normalize} from '../core/nav/esl-carousel.nav.utils';

@ESLCarouselRenderer.register
export class ESLCenteredCarouselRenderer extends ESLDefaultCarouselRenderer {
public static override is = 'centered';
public static override classes: string[] = ['esl-carousel-centered-renderer', 'esl-carousel-default-renderer'];

/** @returns size of all active slides */
public get activeSlidesSize(): number {
let width = (this.count - 1) * this.gap;
for (let i = 0; i < this.count; i++) {
const position = normalize(i + this.currentIndex, this.size);
const $slide = this.$slides[position];
width += this.vertical ? $slide.offsetHeight : $slide.offsetWidth;
}
return width;
}

/** @returns carousel area size */
public get carouselSize(): number {
return this.vertical ? this.$carousel.clientHeight : this.$carousel.clientWidth;
}

/** @returns carousel padding value */
public get carouselPadding(): number {
const carouselStyles = getComputedStyle(this.$carousel);
return parseFloat(carouselStyles[this.vertical ? 'paddingTop' : 'paddingLeft']);
}

/** @returns slide offset by the slide index */
protected override getOffset(index: number): number {
const offset = super.getOffset(index);
return offset - (this.carouselSize - this.activeSlidesSize) / 2 + this.carouselPadding;
}
}

0 comments on commit dd26e7e

Please sign in to comment.