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: add functionality to configure lateral spacing #292

Merged
merged 8 commits into from
Sep 11, 2024
74 changes: 55 additions & 19 deletions src/components/Sliding/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component } from 'pet-dex-utilities';
import { makeSwipable } from '../../utils/swiper';
import { activeSlide } from './utils/activeSlide';
import { activeSlideShuffle } from './utils/activeSlideShuffle';

import './index.scss';

const events = [
Expand All @@ -8,27 +11,29 @@ const events = [
'slide:previous',
'slide:remove',
'slides:clear',
'slides:mode',
];

const activeSlide = (slides, slide) => {
Array.from(slides).forEach((item) => {
item.classList.toggle('sliding__slide--active', item === slide);
});
};

const html = `
<div class="sliding" data-select="sliding">
<div class="sliding__content" data-select="sliding-content">
</div>
</div>`;

export default function Sliding({ slides = [] }) {
export default function Sliding({
slides = [],
shuffleMode = false,
slideSideSpacing = 0,
}) {
Component.call(this, { html, events });

this.slideIndex = 0;

slides.forEach((item) => this.add(item));

this.setShuffleMode(shuffleMode);
if (slides.length > 1 && shuffleMode)
this.setSlideSideSpacing(slideSideSpacing);

const $sliding = this.selected.get('sliding');

makeSwipable($sliding);
Expand All @@ -42,10 +47,15 @@ export default function Sliding({ slides = [] }) {
};

this.listen('mount', () => {
const slidingContent = Array.from(
this.selected.get('sliding-content').children,
);

activeSlide(
Array.from(this.selected.get('sliding-content').children),
slidingContent,
this.selected.get('sliding-content').children[0],
);
activeSlideShuffle(slidingContent, this.slideIndex);
$sliding.addEventListener('swipe-left', this.swipeLeft());
$sliding.addEventListener('swipe-right', this.swipeRight());
});
Expand All @@ -70,31 +80,42 @@ Sliding.prototype = Object.assign(Sliding.prototype, Component.prototype, {
},

next() {
this.slideIndex += 1;
const slides = this.selected.get('sliding-content').children;

this.slideIndex += 1;

if (this.slideIndex > slides.length - 1) this.slideIndex = 0;

const slide = slides[this.slideIndex];
const container = this.selected.get('sliding').clientWidth;
this.selected.get('sliding-content').style.transform =
`translateX(${-this.slideIndex * container}px)`;
activeSlide(slides, slide);

if (!this.shuffleMode) {
const container = this.selected.get('sliding').clientWidth;
this.selected.get('sliding-content').style.transform =
`translateX(${-this.slideIndex * container}px)`;
activeSlide(slides, slide);
}

activeSlideShuffle(slides, this.slideIndex);
this.emit('slide:next', slide);
},

previous() {
this.slideIndex -= 1;
const slides = this.selected.get('sliding-content').children;

this.slideIndex -= 1;

if (this.slideIndex < 0) this.slideIndex = slides.length - 1;

const slide = slides[this.slideIndex];
const container = this.selected.get('sliding').clientWidth;

this.selected.get('sliding-content').style.transform =
`translateX(${-this.slideIndex * container}px)`;
activeSlide(slides, slide);
if (!this.shuffleMode) {
const container = this.selected.get('sliding').clientWidth;
this.selected.get('sliding-content').style.transform =
`translateX(${-this.slideIndex * container}px)`;
activeSlide(slides, slide);
}

activeSlideShuffle(slides, this.slideIndex);
this.emit('slide:previous', slide);
},

Expand All @@ -105,4 +126,19 @@ Sliding.prototype = Object.assign(Sliding.prototype, Component.prototype, {

this.emit('slides:clear');
},

setSlideSideSpacing(slideSideSpacing = 0) {
this.selected.get('sliding-content').style.padding =
`0 ${slideSideSpacing}px`;
},

setShuffleMode(shuffleMode = false) {
const $sliding = this.selected.get('sliding');

this.shuffleMode = shuffleMode;

$sliding.classList.toggle('sliding--shuffle', shuffleMode);

this.emit('slides:mode', shuffleMode);
},
});
49 changes: 49 additions & 0 deletions src/components/Sliding/index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.sliding {
width: 100%;

overflow: hidden;

margin: auto;
Expand All @@ -9,6 +10,8 @@
&__content {
display: flex;

align-items: center;

transform: translateX(0);

transition: transform 0.6s ease-in-out;
Expand All @@ -17,4 +20,50 @@
&__slide {
flex: 0 0 100%;
}

&--shuffle {
.sliding__slide {
width: 100%;

display: none;
flex: unset;

position: relative;

z-index: 0;

transition: all 450ms ease;

&--active {
display: block;

z-index: 2;
}

&--prev,
&--next,
&--unfocused {
width: 50%;

display: block;

position: absolute;

transform: scale(0.9);

opacity: 0.6;
inset: 0;
}

&--next {
left: auto;
}

&--unfocused {
width: 100%;

transform: scale(0.94);
}
}
}
}
5 changes: 5 additions & 0 deletions src/components/Sliding/utils/activeSlide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const activeSlide = (slides, slide) => {
Array.from(slides).forEach((item) => {
item.classList.toggle('sliding__slide--active', item === slide);
});
};
26 changes: 26 additions & 0 deletions src/components/Sliding/utils/activeSlideShuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const activeSlideShuffle = (slides, activeIndex) => {
const totalSlides = slides.length;
const hasMoreThanTwoSlides = totalSlides > 2;
const prevIndex = activeIndex === 0 ? totalSlides - 1 : activeIndex - 1;
const nextIndex = activeIndex === totalSlides - 1 ? 0 : activeIndex + 1;

Array.from(slides).forEach((slide, index) => {
slide.classList.remove(
'sliding__slide--active',
'sliding__slide--unfocused',
'sliding__slide--prev',
'sliding__slide--next',
);

if (index === activeIndex) slide.classList.add('sliding__slide--active');

if (index !== activeIndex && !hasMoreThanTwoSlides)
slide.classList.add('sliding__slide--unfocused');

if (index === prevIndex && hasMoreThanTwoSlides)
slide.classList.add('sliding__slide--prev');

if (index === nextIndex && hasMoreThanTwoSlides)
slide.classList.add('sliding__slide--next');
});
};
4 changes: 4 additions & 0 deletions src/stories/Sliding.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export default {
},
argsTypes: {
slides: { control: 'object', defaultValue: [] },
slideSideSpacing: { control: 'number', defaultValue: 0 },
shuffleMode: { control: 'boolean', defaultValue: false },
},
};

export const Default = {
args: {
slides: [$slide1, $slide2, $slide3],
slideSideSpacing: 60,
shuffleMode: true,
},
};
Loading