Skip to content

Commit

Permalink
feat: 🎸 add canStart at hook useTourGuideController
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed Jun 22, 2020
1 parent 63eacc0 commit a7d0798
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
13 changes: 9 additions & 4 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const uri =
// Add <TourGuideProvider/> at the root of you app!
function App() {
return (
<TourGuideProvider {...{ borderRadius: 16, startAtMount: true }}>
<TourGuideProvider {...{ borderRadius: 16 }}>
<AppContent />
</TourGuideProvider>
)
Expand All @@ -31,16 +31,21 @@ const AppContent = () => {
const iconProps = { size: 40, color: '#888' }

// Use Hooks to control!
const { start, stop, eventEmitter } = useTourGuideController()
const { start, canStart, stop, eventEmitter } = useTourGuideController()

React.useEffect(() => {
// start at mount
if (canStart) {
start()
}
}, [canStart]) // wait until everything is registered

React.useEffect(() => {
eventEmitter.on('start', () => console.log('start'))
eventEmitter.on('stop', () => console.log('stop'))
eventEmitter.on('stepChange', () => console.log(`stepChange`))

return () => eventEmitter.off('*', null)
}, [])

return (
<View style={styles.container}>
{/* Use TourGuideZone only to wrap */}
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ const AppContent = () => {
// Use Hooks to control!
const { start, stop, eventEmitter } = useTourGuideController()

// Can start at mount 🎉
React.useEffect(() => {
if (canStart) {
start()
}
}, [canStart]) // but need to wait until everything is registered 😁

React.useEffect(() => {
eventEmitter.on('start', () => console.log('start'))
eventEmitter.on('stop', () => console.log('stop'))
Expand Down Expand Up @@ -159,6 +166,7 @@ interface TourGuideZoneProps {
shape?: Shape // which shape
maskOffset?: number // offset around zone
borderRadius?: number // round corner when rectangle
startAtMount?: boolean // start at mount
children: React.ReactNode
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rn-tourguide",
"version": "2.4.0",
"version": "2.4.1",
"description": "Make an interactive step by step tour guide for your react-native app (a rewrite of react-native-copilot)",
"main": "node_modules/expo/AppEntry.js",
"types": "lib/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/components/TourGuideContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ export interface Emitter {

export interface ITourGuideContext {
eventEmitter?: Emitter
canStart: boolean
registerStep?(step: IStep): void
unregisterStep?(stepName: string): void
getCurrentStep?(): IStep | undefined
start?(fromStep?: number): void
stop?(): void
}
export const TourGuideContext = React.createContext<ITourGuideContext>({})
export const TourGuideContext = React.createContext<ITourGuideContext>({
canStart: false,
})
9 changes: 7 additions & 2 deletions src/components/TourGuideProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const TourGuideProvider = ({
const [visible, setVisible] = useState<boolean | undefined>(undefined)
const [currentStep, updateCurrentStep] = useState<IStep | undefined>()
const [steps, setSteps] = useState<Steps>({})
const [canStart, setCanStart] = useState<boolean>(false)

const startTries = useRef<number>(0)
const mounted = useIsMounted()
Expand All @@ -70,8 +71,11 @@ export const TourGuideProvider = ({
}, [visible, currentStep])

useEffect(() => {
if (startAtMount && mounted && Object.entries(steps).length > 0) {
start()
if (mounted && Object.entries(steps).length > 0) {
setCanStart(true)
if (startAtMount) {
start()
}
}
}, [mounted, steps])

Expand Down Expand Up @@ -172,6 +176,7 @@ export const TourGuideProvider = ({
getCurrentStep,
start,
stop,
canStart,
}}
>
{children}
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useTourGuideController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import * as React from 'react'
import { TourGuideContext } from '../components/TourGuideContext'

export const useTourGuideController = () => {
const { start, stop, eventEmitter, getCurrentStep } = React.useContext(
TourGuideContext,
)
const {
start,
canStart,
stop,
eventEmitter,
getCurrentStep,
} = React.useContext(TourGuideContext)
return {
start,
stop,
eventEmitter,
getCurrentStep,
canStart,
}
}

0 comments on commit a7d0798

Please sign in to comment.