1
- import React , { useState , useReducer , useEffect , createRef } from 'react'
1
+ import React , { useState , useReducer , useEffect , useRef , memo } from 'react'
2
2
import cn from 'classnames'
3
3
import scrollSmooth from 'scroll-smooth'
4
4
import Scrollparent from 'scrollparent'
5
-
5
+ import debounce from 'lodash.debounce'
6
6
import Portal from './Portal'
7
7
import {
8
8
SvgMask ,
@@ -22,11 +22,9 @@ function Tour({
22
22
isOpen,
23
23
startAt,
24
24
steps,
25
-
26
25
scrollDuration,
27
26
inViewThreshold,
28
27
scrollOffset,
29
-
30
28
disableInteraction,
31
29
disableKeyboardNavigation,
32
30
className,
@@ -48,20 +46,24 @@ function Tour({
48
46
} ) {
49
47
const [ current , setCurrent ] = useState ( 0 )
50
48
const [ state , dispatch ] = useReducer ( reducer , initialState )
51
- const helper = createRef ( )
49
+ const helper = useRef ( null )
52
50
53
51
useEffect ( ( ) => {
52
+ const debouncedShowStep = debounce ( showStep , 100 )
54
53
window . addEventListener ( 'keydown' , keyHandler , false )
54
+ window . addEventListener ( 'resize' , debouncedShowStep , false )
55
55
56
56
return ( ) => {
57
57
window . removeEventListener ( 'keydown' , keyHandler )
58
+ window . removeEventListener ( 'resize' , debouncedShowStep )
58
59
}
59
60
} , [ ] )
60
61
62
+ useWhyDidYouUpdate ( 'Counter' , { current, state, isOpen } )
61
63
useEffect ( ( ) => {
62
64
if ( isOpen ) {
63
65
open ( startAt )
64
- showStep ( )
66
+
65
67
if ( helper . current ) {
66
68
helper . current . focus ( )
67
69
}
@@ -70,7 +72,7 @@ function Tour({
70
72
71
73
useEffect ( ( ) => {
72
74
if ( isOpen ) showStep ( )
73
- } , [ current ] )
75
+ } , [ isOpen , current ] )
74
76
75
77
function keyHandler ( e ) {
76
78
e . stopPropagation ( )
@@ -107,7 +109,7 @@ function Tour({
107
109
}
108
110
109
111
function open ( startAt ) {
110
- const firstStep = startAt || 0
112
+ const firstStep = startAt || current
111
113
setCurrent ( firstStep )
112
114
113
115
if ( onAfterOpen ) {
@@ -130,9 +132,9 @@ function Tour({
130
132
function showStep ( ) {
131
133
const step = steps [ current ]
132
134
const node = step . selector ? document . querySelector ( step . selector ) : null
135
+ const { w, h } = getWindow ( )
133
136
if ( node ) {
134
137
// DOM node exists
135
- const { w, h } = getWindow ( )
136
138
const nodeRect = hx . getNodeRect ( node )
137
139
138
140
// step is outside view
@@ -153,9 +155,6 @@ function Tour({
153
155
// No DOM node
154
156
dispatch ( {
155
157
type : 'without_node' ,
156
- // ...nodeRect,
157
- helperWidth,
158
- helperHeight,
159
158
helperPosition : step . position ,
160
159
w,
161
160
h,
@@ -349,11 +348,11 @@ function reducer(state, action) {
349
348
top : state . h + 10 ,
350
349
right : state . w / 2 + 9 ,
351
350
bottom : state . h / 2 + 9 ,
352
- left : w / 2 - state . helperWidth / 2 ,
351
+ left : action . w / 2 - state . helperWidth / 2 ,
353
352
width : 0 ,
354
353
height : 0 ,
355
- w,
356
- h,
354
+ w : action . w ,
355
+ h : action . h ,
357
356
helperPosition : 'center' ,
358
357
}
359
358
default :
@@ -365,4 +364,38 @@ Tour.propTypes = propTypes
365
364
366
365
Tour . defaultProps = defaultProps
367
366
368
- export default Tour
367
+ function useWhyDidYouUpdate ( name , props ) {
368
+ // Get a mutable ref object where we can store props ...
369
+ // ... for comparison next time this hook runs.
370
+ const previousProps = useRef ( )
371
+
372
+ useEffect ( ( ) => {
373
+ if ( previousProps . current ) {
374
+ // Get all keys from previous and current props
375
+ const allKeys = Object . keys ( { ...previousProps . current , ...props } )
376
+ // Use this object to keep track of changed props
377
+ const changesObj = { }
378
+ // Iterate through keys
379
+ allKeys . forEach ( key => {
380
+ // If previous is different from current
381
+ if ( previousProps . current [ key ] !== props [ key ] ) {
382
+ // Add to changesObj
383
+ changesObj [ key ] = {
384
+ from : previousProps . current [ key ] ,
385
+ to : props [ key ] ,
386
+ }
387
+ }
388
+ } )
389
+
390
+ // If changesObj not empty then output to console
391
+ if ( Object . keys ( changesObj ) . length ) {
392
+ console . log ( '[why-did-you-update]' , name , changesObj )
393
+ }
394
+ }
395
+
396
+ // Finally update previousProps with current props for next hook call
397
+ previousProps . current = props
398
+ } )
399
+ }
400
+
401
+ export default memo ( Tour )
0 commit comments