Created
August 2, 2018 12:44
-
-
Save smashercosmo/26c28e612b0fa9bbe018ad2e983adc5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component, Fragment } from 'react' | |
| import { Popper } from 'react-popper' | |
| import { TutorialContext } from './TutorialProvider' | |
| class Tutorial extends Component { | |
| state = { | |
| stepIndex: 0, | |
| } | |
| next = () => { | |
| this.setState(state => ({ | |
| stepIndex: state.stepIndex + 1, | |
| })) | |
| } | |
| render() { | |
| const { stepIndex } = this.state | |
| return ( | |
| <TutorialContext.Consumer> | |
| {({ steps }) => { | |
| const step = steps.get(stepIndex) | |
| if (!step || !step.ref) return null | |
| return ( | |
| <Popper | |
| referenceElement={step.ref} | |
| placement={step.placement}> | |
| {() => ( | |
| ... | |
| )} | |
| </Popper> | |
| </Fragment> | |
| ) | |
| }} | |
| </TutorialContext.Consumer> | |
| ) | |
| } | |
| } | |
| export { Tutorial } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| export const TutorialContext = React.createContext({}) | |
| export class TutorialProvider extends React.Component { | |
| constructor(props) { | |
| super(props) | |
| this.steps = new Map() | |
| this.state = { | |
| steps: new Map(), | |
| createStep: this.createStep, | |
| } | |
| } | |
| componentDidMount() { | |
| this.setState({ | |
| steps: this.steps, | |
| }) | |
| } | |
| createStep = ({ order, placement = 'bottom' }) => { | |
| const existingStep = this.steps.get(order) | |
| if (existingStep) return existingStep.getRef | |
| const step = { | |
| placement, | |
| ref: null, | |
| getRef: ref => { | |
| step.ref = ref | |
| }, | |
| } | |
| this.steps.set(order, step) | |
| return step.getRef | |
| } | |
| render() { | |
| return ( | |
| <TutorialContext.Provider value={this.state}> | |
| {this.props.children} | |
| </TutorialContext.Provider> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment