Skip to content

Instantly share code, notes, and snippets.

@vadym1930
Last active May 11, 2019 15:41
Show Gist options
  • Save vadym1930/645b3d0ba2d26675c65786a692ebba4e to your computer and use it in GitHub Desktop.
Save vadym1930/645b3d0ba2d26675c65786a692ebba4e to your computer and use it in GitHub Desktop.
Example of slider component in class pattern, jquery + slick-carousel with Drupal.behaviors
import $ from 'jquery';
import 'slick-carousel';
class Slider {
constructor(props) {
this.selector = props.selector;
this.slide = props.slide;
this.key = props.key;
this.slickOptions = props.slickOptions;
this.Drupal = Drupal;
}
init() {
const _this = this;
this.Drupal.behaviors[this.key] = {
attach(context) {
const $sliders = $(_this.selector, context);
_this.carousel($sliders);
}
}
}
carousel = $sliders => {
if (!$sliders.length) {
return;
}
const _this = this;
$sliders.each(function () {
$(this).slick({
slide: _this.slide,
// here general slick options go across all sliders
..._this.slickOptions
});
});
}
}
export default Slider;
import Slider from '../shared/Slider';
const sliderOptions = {
selector: '.js-showcase-carousel',
slide: '.js-showcase-slide',
key: 'showcasesCarousel',
slickOptions: {
slidesToShow: 1,
slidesToScroll: 1,
arrows: false,
dots: true,
rows: 0,
mobileFirst: true,
responsive: [
{
breakpoint: 1279,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots: false,
infinite: true,
useTransform: false,
}
}
]
}
};
const showcaseSlider = new Slider(sliderOptions);
showcaseSlider.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment