Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit 5e5bb5c

Browse files
feat: improve context logic
1 parent e8b3ca8 commit 5e5bb5c

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

example/.eslintcache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/index.tsx":"1","/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/App.tsx":"2"},{"size":164,"mtime":1607363608000,"results":"3","hashOfConfig":"4"},{"size":2291,"mtime":1616847947465,"results":"5","hashOfConfig":"4"},{"filePath":"6","messages":"7","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"bpxiyg",{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/index.tsx",[],"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/App.tsx",[]]
1+
[{"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/index.tsx":"1","/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/App.tsx":"2"},{"size":164,"mtime":1607363608000,"results":"3","hashOfConfig":"4"},{"size":2091,"mtime":1616848806743,"results":"5","hashOfConfig":"4"},{"filePath":"6","messages":"7","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"8"},"bpxiyg",{"filePath":"9","messages":"10","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/index.tsx",[],["11","12"],"/Users/emiliano/Desktop/projects/react-spring-carousel/example/src/App.tsx",[],{"ruleId":"13","replacedBy":"14"},{"ruleId":"15","replacedBy":"16"},"no-native-reassign",["17"],"no-negated-in-lhs",["18"],"no-global-assign","no-unsafe-negation"]

example/src/App.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const Item: React.FC<HTMLAttributes<HTMLDivElement>> = ({
5151
)
5252
}
5353

54-
function MyApp() {
54+
const App = () => {
5555
const [, setActiveItem] = useState(0)
5656
const {
5757
carouselFragment,
@@ -77,7 +77,7 @@ function MyApp() {
7777
setActiveItem(data.nextItem)
7878
})
7979
useListenToCustomEvent('onSlideChange', (data) => {
80-
console.log('onSlideChange', data)
80+
console.log('onDrag', data)
8181
})
8282

8383
return (
@@ -106,15 +106,4 @@ function MyApp() {
106106
)
107107
}
108108

109-
const App = () => {
110-
const [render, setRender] = useState(true)
111-
112-
return render ? (
113-
<>
114-
<button onClick={() => setRender(false)}>change</button>
115-
<MyApp />
116-
</>
117-
) : null
118-
}
119-
120109
export default App

src/carouselTypes/useSpringCarousel.tsx

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, createContext, useCallback } from 'react'
1+
import React, { useRef, createContext, useCallback, useContext } from 'react'
22
import { useSpring, config, animated, AnimationResult } from 'react-spring'
33
import { useDrag } from 'react-use-gesture'
44
import {
@@ -19,26 +19,20 @@ import {
1919
SlideActionType
2020
} from '../types'
2121

22-
export const UseSpringCarouselContext = createContext<TransformCarouselContextProps>(
23-
{
24-
getIsFullscreen: () => false,
25-
getIsPrevItem: () => false,
26-
getIsNextItem: () => false,
27-
slideToItem: () => {},
28-
getIsAnimating: () => false,
29-
getIsDragging: () => false,
30-
getIsActiveItem: () => false,
31-
enterFullscreen: () => {},
32-
exitFullscreen: () => {},
33-
slideToPrevItem: () => {},
34-
slideToNextItem: () => {},
35-
useListenToCustomEvent: () => {},
36-
getCurrentActiveItem: () => ({
37-
id: '',
38-
index: 0
39-
})
22+
const UseSpringCarouselContext = createContext<
23+
TransformCarouselContextProps | undefined
24+
>(undefined)
25+
26+
export function useSpringCarouselContext() {
27+
const context = useContext(UseSpringCarouselContext)
28+
29+
if (!context) {
30+
throw new Error(`useSpringCarouselContext isn't being used within the useSringCarousel context;
31+
use the context only inside a component that is rendered within the Carousel.`)
4032
}
41-
)
33+
34+
return context
35+
}
4236

4337
export function useSpringCarousel({
4438
items,

src/carouselTypes/useTransitionCarousel.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useRef, useState } from 'react'
1+
import React, { createContext, useRef, useState, useContext } from 'react'
22
import { useTransition, animated, config } from 'react-spring'
33
import { useDrag } from 'react-use-gesture'
44
import { prepareDataForCustomEvent } from '../index.utils'
@@ -13,20 +13,20 @@ import {
1313
SlideActionType
1414
} from '../types'
1515

16-
const UseTransitionCarouselContext = createContext<TransitionCarouselContextProps>(
17-
{
18-
activeItem: 0,
19-
slideToNextItem: () => {},
20-
slideToPrevItem: () => {},
21-
enterFullscreen: () => {},
22-
exitFullscreen: () => {},
23-
slideToItem: () => {},
24-
getIsAnimating: () => false,
25-
getIsPrevItem: () => false,
26-
getIsNextItem: () => false,
27-
useListenToCustomEvent: () => {}
16+
const UseTransitionCarouselContext = createContext<
17+
TransitionCarouselContextProps | undefined
18+
>(undefined)
19+
20+
export function useTransitionCarouselContext() {
21+
const context = useContext(UseTransitionCarouselContext)
22+
23+
if (!context) {
24+
throw new Error(`useTransitionCarouselContext isn't being used within the useTransitionCarousel context;
25+
use the context only inside a component that is rendered within the Carousel.`)
2826
}
29-
)
27+
28+
return context
29+
}
3030

3131
export function useTransitionCarousel({
3232
items,
@@ -249,7 +249,6 @@ export function useTransitionCarousel({
249249
}
250250

251251
setActiveItem(itemIndex)
252-
253252
emitCustomEvent(
254253
'onSlideStartChange',
255254
prepareDataForCustomEvent<OnSlideStartChange>({

0 commit comments

Comments
 (0)