Skip to content

Commit 31c2888

Browse files
Custom Helper
- Add prop tu pass CustomHelper Component - debounced showStep whe resize window
1 parent fd78390 commit 31c2888

File tree

6 files changed

+175
-83
lines changed

6 files changed

+175
-83
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
},
7474
"dependencies": {
7575
"classnames": "2.2.6",
76+
"lodash.debounce": "4.0.8",
7677
"lodash.pick": "4.4.0",
7778
"prop-types": "15.6.2",
7879
"scroll-smooth": "1.0.1",

src/Portal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import { Component } from 'react'
22
import { createPortal } from 'react-dom'
33

44
class Tour extends Component {

src/Tour.js

Lines changed: 103 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
33
import cn from 'classnames'
44
import scrollSmooth from 'scroll-smooth'
55
import Scrollparent from 'scrollparent'
6+
import debounce from 'lodash.debounce'
67
import {
78
Arrow,
89
Close,
@@ -101,6 +102,7 @@ class Tour extends Component {
101102
}
102103
this.helper = createRef()
103104
this.helperElement = null
105+
this.debouncedShowStep = debounce(this.showStep, 70)
104106
}
105107

106108
componentDidMount() {
@@ -162,8 +164,8 @@ class Tour extends Component {
162164
}
163165
}
164166
)
165-
// TODO: debounce it.
166-
window.addEventListener('resize', this.showStep, false)
167+
168+
window.addEventListener('resize', this.debouncedShowStep, false)
167169
window.addEventListener('keydown', this.keyDownHandler, false)
168170
}
169171

@@ -282,7 +284,7 @@ class Tour extends Component {
282284
observer: null,
283285
}
284286
}, this.onBeforeClose)
285-
window.removeEventListener('resize', this.showStep)
287+
window.removeEventListener('resize', this.debouncedShowStep)
286288
window.removeEventListener('keydown', this.keyDownHandler)
287289
}
288290

@@ -405,6 +407,7 @@ class Tour extends Component {
405407
prevStep,
406408
rounded,
407409
accentColor,
410+
CustomHelper,
408411
} = this.props
409412

410413
const {
@@ -477,82 +480,108 @@ class Tour extends Component {
477480
})}
478481
accentColor={accentColor}
479482
>
480-
{this.props.children}
481-
{steps[current] &&
482-
(typeof steps[current].content === 'function'
483-
? steps[current].content({
484-
close: onRequestClose,
485-
goTo: this.gotoStep,
486-
inDOM,
487-
step: current + 1,
488-
})
489-
: steps[current].content)}
490-
{showNumber && (
491-
<Badge data-tour-elem="badge">
492-
{typeof badgeContent === 'function'
493-
? badgeContent(current + 1, steps.length)
494-
: current + 1}
495-
</Badge>
496-
)}
497-
{(showButtons || showNavigation) && (
498-
<Controls data-tour-elem="controls">
499-
{showButtons && (
500-
<Arrow
501-
onClick={
502-
typeof prevStep === 'function' ? prevStep : this.prevStep
503-
}
504-
disabled={current === 0}
505-
label={prevButton ? prevButton : null}
506-
/>
483+
{CustomHelper ? (
484+
<CustomHelper
485+
current={current}
486+
totalSteps={steps.length}
487+
gotoStep={this.gotoStep}
488+
close={onRequestClose}
489+
content={
490+
steps[current] &&
491+
(typeof steps[current].content === 'function'
492+
? steps[current].content({
493+
close: onRequestClose,
494+
goTo: this.gotoStep,
495+
inDOM,
496+
step: current + 1,
497+
})
498+
: steps[current].content)
499+
}
500+
>
501+
{this.props.children}
502+
</CustomHelper>
503+
) : (
504+
<>
505+
{this.props.children}
506+
{steps[current] &&
507+
(typeof steps[current].content === 'function'
508+
? steps[current].content({
509+
close: onRequestClose,
510+
goTo: this.gotoStep,
511+
inDOM,
512+
step: current + 1,
513+
})
514+
: steps[current].content)}
515+
{showNumber && (
516+
<Badge data-tour-elem="badge">
517+
{typeof badgeContent === 'function'
518+
? badgeContent(current + 1, steps.length)
519+
: current + 1}
520+
</Badge>
507521
)}
508-
509-
{showNavigation && (
510-
<Navigation data-tour-elem="navigation">
511-
{steps.map((s, i) => (
512-
<Dot
513-
key={`${s.selector ? s.selector : 'undef'}_${i}`}
514-
onClick={() => this.gotoStep(i)}
515-
current={current}
516-
index={i}
517-
disabled={current === i || disableDotsNavigation}
518-
showNumber={showNavigationNumber}
519-
data-tour-elem="dot"
520-
className={cn(CN.dot.base, {
521-
[CN.dot.active]: current === i,
522-
})}
522+
{(showButtons || showNavigation) && (
523+
<Controls data-tour-elem="controls">
524+
{showButtons && (
525+
<Arrow
526+
onClick={
527+
typeof prevStep === 'function'
528+
? prevStep
529+
: this.prevStep
530+
}
531+
disabled={current === 0}
532+
label={prevButton ? prevButton : null}
523533
/>
524-
))}
525-
</Navigation>
534+
)}
535+
536+
{showNavigation && (
537+
<Navigation data-tour-elem="navigation">
538+
{steps.map((s, i) => (
539+
<Dot
540+
key={`${s.selector ? s.selector : 'undef'}_${i}`}
541+
onClick={() => this.gotoStep(i)}
542+
current={current}
543+
index={i}
544+
disabled={current === i || disableDotsNavigation}
545+
showNumber={showNavigationNumber}
546+
data-tour-elem="dot"
547+
className={cn(CN.dot.base, {
548+
[CN.dot.active]: current === i,
549+
})}
550+
/>
551+
))}
552+
</Navigation>
553+
)}
554+
555+
{showButtons && (
556+
<Arrow
557+
onClick={
558+
current === steps.length - 1
559+
? lastStepNextButton
560+
? onRequestClose
561+
: () => {}
562+
: typeof nextStep === 'function'
563+
? nextStep
564+
: this.nextStep
565+
}
566+
disabled={
567+
!lastStepNextButton && current === steps.length - 1
568+
}
569+
inverted
570+
label={
571+
lastStepNextButton && current === steps.length - 1
572+
? lastStepNextButton
573+
: nextButton
574+
? nextButton
575+
: null
576+
}
577+
/>
578+
)}
579+
</Controls>
526580
)}
527581

528-
{showButtons && (
529-
<Arrow
530-
onClick={
531-
current === steps.length - 1
532-
? lastStepNextButton
533-
? onRequestClose
534-
: () => {}
535-
: typeof nextStep === 'function'
536-
? nextStep
537-
: this.nextStep
538-
}
539-
disabled={
540-
!lastStepNextButton && current === steps.length - 1
541-
}
542-
inverted
543-
label={
544-
lastStepNextButton && current === steps.length - 1
545-
? lastStepNextButton
546-
: nextButton
547-
? nextButton
548-
: null
549-
}
550-
/>
551-
)}
552-
</Controls>
582+
{showCloseButton ? <Close onClick={onRequestClose} /> : null}
583+
</>
553584
)}
554-
555-
{showCloseButton ? <Close onClick={onRequestClose} /> : null}
556585
</Guide>
557586
</Portal>
558587
)

src/demo/App.js

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import React, { Component } from 'react'
22
import Demo from './Demo'
3-
import Tour from '../index'
3+
import Tour, { Arrow } from '../index'
44
import Text from './Text'
55
import Glitch from './Glitch'
66
import Tooltip from './Tooltip'
77
import { Link } from './Button'
88
import PropTypes from 'prop-types'
9-
import {
10-
disableBodyScroll,
11-
enableBodyScroll,
12-
clearAllBodyScrollLocks,
13-
} from 'body-scroll-lock'
9+
import { disableBodyScroll, enableBodyScroll } from 'body-scroll-lock'
1410

1511
import './styles.css'
1612

@@ -20,6 +16,7 @@ class App extends Component {
2016
this.state = {
2117
isTourOpen: false,
2218
isShowingMore: false,
19+
customComps: false,
2320
}
2421
}
2522

@@ -36,6 +33,11 @@ class App extends Component {
3633
e.preventDefault()
3734
this.openTour()
3835
}
36+
37+
if (this.state.isTourOpen && e.keyCode === 13) {
38+
e.preventDefault()
39+
this.toggleCustomComps()
40+
}
3941
}
4042

4143
toggleShowMore = () => {
@@ -44,6 +46,12 @@ class App extends Component {
4446
}))
4547
}
4648

49+
toggleCustomComps = () => {
50+
this.setState(prevState => ({
51+
customComps: !prevState.customComps,
52+
}))
53+
}
54+
4755
closeTour = () => {
4856
this.setState({ isTourOpen: false })
4957
}
@@ -56,7 +64,7 @@ class App extends Component {
5664
enableBody = target => enableBodyScroll(target)
5765

5866
render() {
59-
const { isTourOpen, isShowingMore } = this.state
67+
const { isTourOpen, isShowingMore, customComps } = this.state
6068
const accentColor = '#5cb7b7'
6169

6270
return (
@@ -76,12 +84,65 @@ class App extends Component {
7684
className="helper"
7785
rounded={5}
7886
accentColor={accentColor}
87+
CustomHelper={customComps ? MyCustomHelper : null}
7988
/>
8089
</div>
8190
)
8291
}
8392
}
8493

94+
const MyCustomHelper = ({ current, content, totalSteps, gotoStep, close }) => {
95+
return (
96+
<main>
97+
<span
98+
style={{
99+
position: 'absolute',
100+
right: '1em',
101+
bottom: '.5em',
102+
fontSize: '10px',
103+
}}
104+
>
105+
Step: {current + 1} |{' '}
106+
<span style={{ cursor: 'pointer' }} onClick={close}>
107+
108+
</span>
109+
<hr style={{ border: 0, borderBottom: '1px solid rgba(0,0,0,.1)' }} />
110+
<Arrow
111+
onClick={() => gotoStep(current < totalSteps - 1 ? current + 1 : 0)}
112+
inverted={current < totalSteps - 1}
113+
/>
114+
</span>
115+
116+
{content}
117+
<ul
118+
style={{
119+
display: 'flex',
120+
flexWrap: 'wrap',
121+
justifyContent: 'center',
122+
listStyle: 'none',
123+
}}
124+
>
125+
{Array.from(Array(totalSteps).keys()).map((li, i) => (
126+
<li key={li}>
127+
<button
128+
onClick={() => current !== i && gotoStep(i)}
129+
style={{
130+
color: current === i ? 'red' : 'initial',
131+
border: 0,
132+
backgroundColor: '#f7f7f7',
133+
padding: '.5em',
134+
margin: '1px',
135+
}}
136+
>
137+
{li + 1}
138+
</button>
139+
</li>
140+
))}
141+
</ul>
142+
</main>
143+
)
144+
}
145+
85146
const tourConfig = [
86147
{
87148
selector: '[data-tut="reactour__iso"]',

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Tour from './Tour'
22
export default Tour
3+
export { Arrow, Close, Badge, Controls, Navigation, Dot } from './components'

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3452,7 +3452,7 @@ locate-path@^3.0.0:
34523452
p-locate "^3.0.0"
34533453
path-exists "^3.0.0"
34543454

3455-
lodash.debounce@^4.0.8:
3455+
lodash.debounce@4.0.8, lodash.debounce@^4.0.8:
34563456
version "4.0.8"
34573457
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
34583458
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=

0 commit comments

Comments
 (0)