Skip to content

Commit 2fefa50

Browse files
selebBrandon Carroll
authored and
Brandon Carroll
committed
feat: export context for typescript (#235)
* export context * Update index.d.ts allows use of carousel context in typescript * Update README.md
1 parent 669b3b7 commit 2fefa50

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Carousels: Love them or hate them. However, if you are a React developer, and y
6363
* [<ButtonLast />](#buttonlast-)
6464
* [<ButtonPlay />](#buttonplay-)
6565
* [WithStore() Higher Order Component](#withstore-higher-order-component)
66+
* [Hooks and `useContext`](#hooks-and-usecontext)
6667
* [TypeScript usage](#typescript-usage)
6768
* [WithStore() Higher Order Component](#withstore-higher-order-component-1)
6869
* [Examples](#examples)
@@ -531,6 +532,32 @@ __unsubscribeMasterSpinner__: 🔥 DON'T USE THIS.
531532

532533
__unsubscribeAllMasterSpinner__: Don't call this manually unless you have some sort of super-customized carousel. This is called internally once all `<Image hasMasterSpinner />` and all `<ImageWithZoom hasMasterSpinner />` components are finished loading their images. Calling this directly will force a "success" state and the master spinner (the spinner that covers the entire carousel while loading) will turn off.
533534

535+
### Hooks and `useContext`
536+
537+
If you'd like to consume the context via hooks rather than using the HoC approach described above, the context is exported as `CarouselContext`.
538+
539+
Note that you will likely need to subscribe/unsubscribe to changes in order to take advantage of the context.
540+
541+
Example:
542+
543+
```js
544+
import React, { useContext } from 'react';
545+
import { CarouselContext } from 'pure-react-carousel';
546+
547+
export function MyComponentUsingContext() {
548+
const carouselContext = useContext(CarouselContext);
549+
const [currentSlide, setCurrentSlide] = useState(carouselContext.state.currentSlide);
550+
useEffect(() => {
551+
function onChange() {
552+
setCurrentSlide(carouselContext.state.currentSlide);
553+
}
554+
carouselContext.subscribe(onChange);
555+
return () => carouselContext.unsubscribe(onChange);
556+
}, [carouselContext]);
557+
return `The current slide is: ${currentSlide}`;
558+
}
559+
```
560+
534561
## TypeScript usage
535562
The current bundled Typescript definitions are mostly complete. Certain edge case could have been not accounted for! Pull requests to improve them are welcome and appreciated.
536563

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { default as ButtonFirst } from './ButtonFirst';
33
export { default as ButtonNext } from './ButtonNext';
44
export { default as ButtonLast } from './ButtonLast';
55
export { default as ButtonPlay } from './ButtonPlay';
6-
export { default as CarouselProvider } from './CarouselProvider';
6+
export { default as CarouselProvider, CarouselContext } from './CarouselProvider';
77
export { default as Dot } from './Dot';
88
export { default as DotGroup } from './DotGroup';
99
export { default as Image } from './Image';

typings/index.d.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ interface CarouselState {
5252
readonly infinite: boolean
5353
}
5454

55+
interface CarouselStoreInterface {
56+
readonly state: CarouselState
57+
readonly setStoreState: (state: Partial<CarouselState>) => void
58+
readonly getStoreState: () => CarouselState
59+
readonly subscribe: (func: () => void) => void
60+
readonly unsubscribe: (func: () => void) => void
61+
readonly updateSubscribers: (cb?: (state: CarouselState) => void) => void
62+
readonly subscribeMasterSpinner: (src) => void
63+
readonly unsubscribeMasterSpinner: (src) => false | object
64+
readonly unsubscribeAllMasterSpinner: () => void
65+
readonly masterSpinnerSuccess: (src) => void
66+
readonly masterSpinnerError: (src) => void
67+
readonly setMasterSpinnerFinished: () => void
68+
readonly isMasterSpinnerFinished: () => boolean
69+
}
70+
71+
declare const CarouselContext: React.Context<CarouselStoreInterface>
72+
5573
interface CarouselProviderProps {
5674
readonly children: React.ReactNode
5775
readonly className?: string
@@ -94,10 +112,7 @@ type CarouselProviderInterface = React.ComponentClass<CarouselProviderProps>
94112
declare const CarouselProvider: CarouselProviderInterface
95113

96114
export interface CarouselInjectedProps {
97-
readonly carouselStore: {
98-
readonly setStoreState: (state: Partial<CarouselState>) => void
99-
readonly unsubscribeAllMasterSpinner: () => void
100-
}
115+
readonly carouselStore: CarouselStoreInterface
101116
}
102117

103118
// Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766
@@ -150,6 +165,8 @@ export {
150165
ButtonLast,
151166
ButtonNext,
152167
ButtonPlay,
168+
CarouselStoreInterface,
169+
CarouselContext,
153170
CarouselProvider,
154171
CarouselProviderProps,
155172
Dot,

0 commit comments

Comments
 (0)