Skip to content

Commit

Permalink
feat(vue): provide - inject swiper and swiperSlide context (#5377)
Browse files Browse the repository at this point in the history
* feat(vue): provider - inject

* chore(vue):  slideData -> swiperSlide

* chore(vue): provide should be ref type

* chore(vue): update playground

* chore: ignore eslint cache

Co-authored-by: Vladimir Kharlampidi <nolimits4web@gmail.com>
  • Loading branch information
vltansky and nolimits4web authored Jan 24, 2022
1 parent 8297421 commit f3145b1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ cypress/screenshots
.env.development.local
.env.test.local
.env.production.local
.eslintcache
4 changes: 3 additions & 1 deletion playground/vue/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:pagination="{ clickable: true }"
:modules="modules"
>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide><innerComp>Slide 1</innerComp></swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 4</swiper-slide>
Expand All @@ -27,11 +27,13 @@
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
// eslint-disable-next-line
import { Swiper, SwiperSlide } from 'swiper/vue/swiper-vue.js';
import innerComp from './innerComp.vue';
export default {
components: {
Swiper,
SwiperSlide,
innerComp,
},
setup() {
Expand Down
19 changes: 19 additions & 0 deletions playground/vue/innerComp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
test - {{ swiperSlide.isActive }} - {{ swiperSlide.isNext }}
<button @click="swiper.slideNext()">Next</button>
</template>
<script>
import { inject } from 'vue';
export default {
setup() {
const swiper = inject('swiper');
const swiperSlide = inject('swiperSlide');
console.log({ swiper, swiperSlide });
return {
swiper,
swiperSlide,
};
// console.log(swiper, slideData);
},
};
</script>
12 changes: 11 additions & 1 deletion src/vue/swiper-slide.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { h, ref, onMounted, onUpdated, onBeforeUpdate, computed, onBeforeUnmount } from 'vue';
import {
h,
ref,
onMounted,
onUpdated,
onBeforeUpdate,
computed,
onBeforeUnmount,
provide,
} from 'vue';
import { uniqueClasses } from './utils.js';

const SwiperSlide = {
Expand Down Expand Up @@ -66,6 +75,7 @@ const SwiperSlide = {
slideClasses.value.indexOf('swiper-slide-next') >= 0 ||
slideClasses.value.indexOf('swiper-slide-duplicate-next') >= 0,
}));
provide('swiperSlide', slideData);
return () => {
return h(
props.tag,
Expand Down
4 changes: 3 additions & 1 deletion src/vue/swiper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, ref, onMounted, onUpdated, onBeforeUnmount, watch, nextTick } from 'vue';
import { h, ref, onMounted, onUpdated, onBeforeUnmount, watch, nextTick, provide } from 'vue';
import { getParams } from './get-params.js';
import { initSwiper, mountSwiper } from './init-swiper.js';
import {
Expand Down Expand Up @@ -319,6 +319,8 @@ const Swiper = {
breakpointChanged.value = false;
});

provide('swiper', swiperRef);

// update on virtual update
watch(virtualData, () => {
nextTick(() => {
Expand Down

0 comments on commit f3145b1

Please sign in to comment.