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(react): context #5364

Merged
merged 2 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion playground/react/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
// eslint-disable-next-line
import { Swiper, SwiperSlide } from 'swiper/react/swiper-react';

import InnerComp from './InnerComp.jsx';

const App = () => {
return (
<main>
Expand All @@ -18,7 +20,14 @@ const App = () => {
scrollbar={{ draggable: true }}
pagination={{ clickable: true }}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>
<InnerComp>Slide 1</InnerComp>
</SwiperSlide>
{[1, 2, 3, 4, 5].map((v) => (
<SwiperSlide key={v}>
<InnerComp>Slide {v}</InnerComp>
</SwiperSlide>
))}
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
Expand Down
25 changes: 25 additions & 0 deletions playground/react/InnerComp.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { Swiper, SwiperSlide, useSwiperSlide, useSwiper } from 'swiper/react/swiper-react';

export default function InnerComp({ children }) {
const { swiper } = useSwiper();
const { isActive, isDuplicate, isNext, isPrev, isVisible } = useSwiperSlide();
return (
<div style={{ lineHeight: 1, marginTop: '100px' }}>
<>
isActive: {isActive.toString()} isDuplicate: {isDuplicate.toString()} isNext:{' '}
{isNext.toString()} isPrev: {isPrev.toString()} isVisible: {isVisible.toString()}
</>
<br />
{children}
<br />
<button
onClick={() => {
swiper.slideNext(500);
}}
>
Slide next
</button>
</div>
);
}
13 changes: 13 additions & 0 deletions src/react/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createContext, useContext } from 'react';

export const SwiperSlideContext = createContext(null);

export const useSwiperSlide = () => {
return useContext(SwiperSlideContext);
};

export const SwiperContext = createContext(null);

export const useSwiper = () => {
return useContext(SwiperContext);
};
2 changes: 2 additions & 0 deletions src/react/swiper-react.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Swiper } from './swiper.js';
import { SwiperSlide } from './swiper-slide.js';

export { useSwiperSlide, useSwiper } from './context.js';

export { Swiper, SwiperSlide };
52 changes: 26 additions & 26 deletions src/react/swiper-slide.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useRef, useState, forwardRef } from 'react';
import { uniqueClasses } from './utils.js';
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js';
import { SwiperSlideContext } from './context.js';

const SwiperSlide = forwardRef(
(
Expand Down Expand Up @@ -40,22 +41,19 @@ const SwiperSlide = forwardRef(
}
}, [swiper]);

let slideData;
if (typeof children === 'function') {
slideData = {
isActive:
slideClasses.indexOf('swiper-slide-active') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-active') >= 0,
isVisible: slideClasses.indexOf('swiper-slide-visible') >= 0,
isDuplicate: slideClasses.indexOf('swiper-slide-duplicate') >= 0,
isPrev:
slideClasses.indexOf('swiper-slide-prev') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-prev') >= 0,
isNext:
slideClasses.indexOf('swiper-slide-next') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-next') >= 0,
};
}
const slideData = {
isActive:
slideClasses.indexOf('swiper-slide-active') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-active') >= 0,
isVisible: slideClasses.indexOf('swiper-slide-visible') >= 0,
isDuplicate: slideClasses.indexOf('swiper-slide-duplicate') >= 0,
isPrev:
slideClasses.indexOf('swiper-slide-prev') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-prev') >= 0,
isNext:
slideClasses.indexOf('swiper-slide-next') >= 0 ||
slideClasses.indexOf('swiper-slide-duplicate-next') >= 0,
};

const renderChildren = () => {
return typeof children === 'function' ? children(slideData) : children;
Expand All @@ -68,16 +66,18 @@ const SwiperSlide = forwardRef(
data-swiper-slide-index={virtualIndex}
{...rest}
>
{zoom ? (
<div
className="swiper-zoom-container"
data-swiper-zoom={typeof zoom === 'number' ? zoom : undefined}
>
{renderChildren()}
</div>
) : (
renderChildren()
)}
<SwiperSlideContext.Provider value={slideData}>
{zoom ? (
<div
className="swiper-zoom-container"
data-swiper-zoom={typeof zoom === 'number' ? zoom : undefined}
>
{renderChildren()}
</div>
) : (
renderChildren()
)}
</SwiperSlideContext.Provider>
</Tag>
);
},
Expand Down
47 changes: 30 additions & 17 deletions src/react/swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getChildren } from './get-children.js';
import { updateSwiper } from './update-swiper.js';
import { renderVirtual, updateOnVirtualData } from './virtual.js';
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js';
import { SwiperContext } from './context.js';

const Swiper = forwardRef(
(
Expand Down Expand Up @@ -192,23 +193,35 @@ const Swiper = forwardRef(
className={uniqueClasses(`${containerClasses}${className ? ` ${className}` : ''}`)}
{...restProps}
>
{slots['container-start']}
{needsNavigation(swiperParams) && (
<>
<div ref={prevElRef} className="swiper-button-prev" />
<div ref={nextElRef} className="swiper-button-next" />
</>
)}
{needsScrollbar(swiperParams) && <div ref={scrollbarElRef} className="swiper-scrollbar" />}
{needsPagination(swiperParams) && (
<div ref={paginationElRef} className="swiper-pagination" />
)}
<WrapperTag className="swiper-wrapper">
{slots['wrapper-start']}
{renderSlides()}
{slots['wrapper-end']}
</WrapperTag>
{slots['container-end']}
<SwiperContext.Provider
value={{
swiper: swiperRef.current,
nextEl: nextElRef.current,
prevEl: prevElRef.current,
scrollbarEl: scrollbarElRef.current,
paginationEl: paginationElRef.current,
}}
>
{slots['container-start']}
{needsNavigation(swiperParams) && (
<>
<div ref={prevElRef} className="swiper-button-prev" />
<div ref={nextElRef} className="swiper-button-next" />
</>
)}
{needsScrollbar(swiperParams) && (
<div ref={scrollbarElRef} className="swiper-scrollbar" />
)}
{needsPagination(swiperParams) && (
<div ref={paginationElRef} className="swiper-pagination" />
)}
<WrapperTag className="swiper-wrapper">
{slots['wrapper-start']}
{renderSlides()}
{slots['wrapper-end']}
</WrapperTag>
{slots['container-end']}
</SwiperContext.Provider>
</Tag>
);
},
Expand Down