-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esl-carousel): add centered renderer
Co-authored-by: ala'n (Alexey Stsefanovich) <[email protected]>
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/modules/esl-carousel/renderers/esl-carousel.centered.renderer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |