Skip to content

Commit 057a1a7

Browse files
authored
Merge pull request #3 from KasselLabs/improve-calbacks
Add onClose callback receiving tour state data.
2 parents b70bb03 + c75dc10 commit 057a1a7

14 files changed

+211
-13
lines changed

packages/mask/src/Mask.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,41 @@ const Mask: React.FC<MaskProps> = ({
107107

108108
export type MaskProps = {
109109
children?: React.ReactNode
110+
111+
/**
112+
* Object containing size and position informations of where to position the Mask.
113+
*/
110114
sizes: RectResult
115+
116+
/**
117+
* Prop to customize styles for the different parts of the Mask using a function that allows to extend the base styles an take advantage of some state props.
118+
*/
111119
styles?: StylesObj
120+
121+
/**
122+
* Class to apply to the Mask wrapper
123+
*/
112124
className?: string
125+
126+
/**
127+
* Class to apply to the Highlighted area rect
128+
*/
113129
highlightedAreaClassName?: string
130+
131+
/**
132+
* Extra space to add in Mask calculations. Useful when calculating space from Element bounding rect and want to add more space.
133+
* Single number sets sema space for x and y, otherwise, an Array sets [x, y].
134+
*/
114135
padding?: number | [number, number]
136+
137+
/**
138+
* Click handler for the Mask except the highlighted area.
139+
*/
115140
onClick?: MouseEventHandler<HTMLDivElement>
141+
142+
/**
143+
* Click handler for the Highlighted area.
144+
*/
116145
onClickHighlighted?: MouseEventHandler<SVGRectElement>
117146
}
118147

packages/playground/styleguide.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const path = require('path')
33
module.exports = {
44
skipComponentsWithoutExample: true,
55
propsParser: require('react-docgen-typescript').withCustomConfig(
6-
'./tsconfig.json'
6+
'./tsconfig.json', {
7+
shouldRemoveUndefinedFromOptional: true
8+
}
79
).parse,
810
styleguideComponents: {
911
LogoRenderer: path.join(__dirname, 'src/styleguide/Logo.js'),
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"files": {
33
"main.css": "/static/css/main.2b8cbd1d.chunk.css",
4-
"main.js": "/build/main.69b57c4e.js",
4+
"main.js": "/build/main.54d07917.js",
55
"runtime-main.js": "/build/bundle.d27f85f7.js",
6-
"build/2.c746b211.js": "/build/2.c746b211.js",
7-
"build/2.c746b211.js.LICENSE.txt": "/build/2.c746b211.js.LICENSE.txt",
6+
"build/2.0e0a0dcd.js": "/build/2.0e0a0dcd.js",
7+
"build/2.0e0a0dcd.js.LICENSE.txt": "/build/2.0e0a0dcd.js.LICENSE.txt",
88
"index.html": "/index.html",
99
"static/css/main.2b8cbd1d.chunk.css.map": "/static/css/main.2b8cbd1d.chunk.css.map"
1010
},
1111
"entrypoints": [
1212
"build/bundle.d27f85f7.js",
13-
"build/2.c746b211.js",
13+
"build/2.0e0a0dcd.js",
1414
"static/css/main.2b8cbd1d.chunk.css",
15-
"build/main.69b57c4e.js"
15+
"build/main.54d07917.js"
1616
]
1717
}

packages/playground/styleguide/build/2.0e0a0dcd.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/playground/styleguide/build/2.c746b211.js

-2
This file was deleted.

packages/playground/styleguide/build/main.54d07917.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/playground/styleguide/build/main.69b57c4e.js

-1
This file was deleted.

packages/playground/styleguide/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<body>
1212
<div id="rsg-root"></div>
1313
<script src="build/bundle.d27f85f7.js"></script>
14-
<script src="build/2.c746b211.js"></script>
15-
<script src="build/main.69b57c4e.js"></script>
14+
<script src="build/2.0e0a0dcd.js"></script>
15+
<script src="build/main.54d07917.js"></script>
1616
</body>
1717
</html>

packages/popover/src/Popover.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,31 @@ const Popover: React.FC<PopoverProps> = ({
154154
export default Popover
155155

156156
export type PopoverProps = {
157+
/**
158+
* Object containing size and position informations of where to position the Popover
159+
*/
157160
sizes: RectResult
161+
158162
children?: React.ReactNode
163+
164+
/**
165+
* The position for the Popover, fixed in case of [number, number], calculated prefered position in case of string
166+
*/
159167
position?: PositionType
168+
169+
/**
170+
* Extra space to add in Popover position calculations. Useful when calculating space from Element bounding rect and want to add more space.
171+
*/
160172
padding?: number | [number, number]
173+
174+
/**
175+
* Prop to customize styles for the different parts of the Mask using a function that allows to extend the base styles an take advantage of some state props.
176+
*/
161177
styles?: StylesObj
178+
179+
/**
180+
* Class to apply to the Popover
181+
*/
162182
className?: string
163183
}
164184

packages/tour/src/Context.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { useContext } from 'react'
1+
import React, { useEffect, useContext } from 'react'
22
import { useState } from 'react'
33
import Tour from './Tour'
44
import { ProviderProps, TourProps } from './types'
5+
import usePrevious from './usePrevious'
56

67
const defaultState = {
78
isOpen: false,
@@ -21,6 +22,7 @@ const TourProvider: React.FC<ProviderProps> = ({
2122
defaultOpen = false,
2223
startAt = 0,
2324
steps: defaultSteps,
25+
onClose,
2426
...props
2527
}) => {
2628
const [isOpen, setIsOpen] = useState(defaultOpen)
@@ -40,6 +42,16 @@ const TourProvider: React.FC<ProviderProps> = ({
4042
...props,
4143
}
4244

45+
const previousIsOpen = usePrevious(isOpen)
46+
47+
useEffect(() => {
48+
const hasClosedTour = previousIsOpen === true && isOpen === false
49+
50+
if (hasClosedTour && onClose && typeof onClose === 'function') {
51+
onClose({ currentStep, steps })
52+
}
53+
}, [isOpen, previousIsOpen])
54+
4355
return (
4456
<TourContext.Provider value={value}>
4557
{children}

packages/tour/src/Tour.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const Tour: React.FC<TourProps> = ({
7272
function maskClickHandler() {
7373
if (!disabledActions) {
7474
if (onClickMask && typeof onClickMask === 'function') {
75-
onClickMask({ setCurrentStep, setIsOpen, currentStep })
75+
onClickMask({ setCurrentStep, setIsOpen, currentStep, steps })
7676
} else {
7777
setIsOpen(false)
7878
}

packages/tour/src/types.tsx

+120
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,138 @@ import { MaskStylesObj } from '@reactour/mask'
44
import { StylesObj } from './styles'
55

66
type SharedProps = {
7+
8+
/**
9+
* Array of elements to highlight with special info and props.
10+
*/
711
steps: StepType[]
12+
13+
/**
14+
* Prop to customize styles for the different parts of the Mask, Popover and Tour using a function that allows to extend the base styles an take advantage of some state props.
15+
*/
816
styles?: StylesObj & PopoverStylesObj & MaskStylesObj
17+
18+
/**
19+
* Extra space to add between the Mask and the Popover and the highlighted element. A single number coordinates both spaces. Otherwise, passing an Object specifying the Component space.
20+
*/
921
padding?: Padding
22+
23+
/**
24+
* Set a global position for the Popover in all steps, fixed in case of [number, number], calculated in case of position string
25+
*/
1026
position?: Position
27+
28+
/**
29+
* Disables the ability to click or interact in any way with the Highlighted element on every step.
30+
* This option could be overrided on specific steps using stepInteraction prop.
31+
*/
1132
disableInteraction?: boolean
33+
34+
/**
35+
* The Tour uses FocusScope in order to lock the focus iteration inside the Popover when Tour is active. This prop allows to disable this behaviour.
36+
*/
1237
disableFocusLock?: boolean
38+
39+
/**
40+
* Disable interactivity with Dot navigation inside Popover.
41+
*/
1342
disableDotsNavigation?: boolean
43+
44+
/**
45+
* Disable all keyboard navigation events when true, disable only selected keys when array.
46+
* default: false
47+
*/
1448
disableKeyboardNavigation?: boolean | KeyboardParts[]
49+
50+
/**
51+
* Class assigned to Popover.
52+
*/
1553
className?: string
54+
55+
/**
56+
* Class assigned to Mask.
57+
*/
1658
maskClassName?: string
59+
60+
/**
61+
* Class assigned to highlighted part of Mask. Useful when using disableInteraction.
62+
*/
1763
highlightedMaskClassName?: string
64+
65+
/**
66+
* Helper function to customize the Next button inside Popover, with useful parameters. It is possible to use the base Button and customize the props.
67+
*/
1868
nextButton?: (props: BtnFnProps) => void
69+
70+
/**
71+
* Helper function to customize the Prev button inside Popover, with useful parameters. It is possible to use the base Button and customize the props.
72+
*/
1973
prevButton?: (props: BtnFnProps) => void
74+
75+
/**
76+
* Action fired just after the Tour is open.
77+
*/
2078
afterOpen?: (target: Element | null) => void
79+
80+
/**
81+
* Action fired just before the Tour is closed.
82+
*/
2183
beforeClose?: (target: Element | null) => void
84+
85+
/**
86+
* Function that overrides the default close behavior of the Mask click handler. Comes with useful parameters to play with.
87+
*/
2288
onClickMask?: (clickProps: ClickProps) => void
89+
90+
/**
91+
* Click handler for highlighted area. Only works when disableInteraction is active. Useful in case is needed to avoid onClickMask when clicking the highlighted element.
92+
*/
2393
onClickHighlighted?: MouseEventHandler<SVGRectElement>
94+
95+
/**
96+
* Function to customize the content of the Badge using helper parameters like the current and total steps and if the Tour is transitioning between steps.
97+
*/
2498
badgeContent?: (badgeProps: BadgeProps) => any
99+
100+
/**
101+
* Show or hide the Navigation (Prev and Next buttons and Dots) inside Popover.
102+
*/
25103
showNavigation?: boolean
104+
105+
/**
106+
* Show or hide Prev and Next buttons inside Popover.
107+
*/
26108
showPrevNextButtons?: boolean
109+
110+
/**
111+
* Show or hide the Close button inside Popover.
112+
*/
27113
showCloseButton?: boolean
114+
115+
/**
116+
* Show or hide the Badge inside Popover.
117+
*/
28118
showBadge?: boolean
119+
120+
/**
121+
* Activate smooth scroll behavior when steps are outside viewport.
122+
* default: false
123+
*/
29124
scrollSmooth?: boolean
125+
126+
/**
127+
* Tolerance in pixels to add when calculating if the step element is outside viewport to scroll into view.
128+
*/
30129
inViewThreshold?: number
130+
131+
/**
132+
* Configure generic accessibility related attributes like aria-labelledby, aria-label for Close button and if show or hide Dot navigation in screen readers.
133+
*/
31134
accessibilityOptions?: A11yOptions
135+
136+
/**
137+
* Option to navigate and show Navigation in right-to-left mode
138+
*/
32139
rtl?: boolean
33140
}
34141

@@ -49,6 +156,7 @@ type ClickProps = {
49156
setIsOpen: Dispatch<React.SetStateAction<Boolean>>
50157
setCurrentStep: Dispatch<React.SetStateAction<number>>
51158
currentStep: number
159+
steps: StepType[]
52160
}
53161

54162
export type TourProps = SharedProps &
@@ -67,8 +175,20 @@ type BadgeProps = {
67175

68176
export type ProviderProps = SharedProps & {
69177
children: React.ReactNode
178+
/**
179+
* Set the initial Open state
180+
*/
70181
defaultOpen?: Boolean
182+
183+
/**
184+
* Set the initial Step number when the tour starts
185+
*/
71186
startAt?: number
187+
188+
/**
189+
* Called when the Tour is closed
190+
*/
191+
onClose?: (props: Object) => void | null
72192
}
73193

74194
export type ContentProps = {

packages/tour/src/usePrevious.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useRef } from 'react'
2+
3+
function usePrevious(value: any) {
4+
// The ref object is a generic container whose current property is mutable ...
5+
// ... and can hold any value, similar to an instance property on a class
6+
const ref = useRef()
7+
// Store current value in ref
8+
useEffect(() => {
9+
ref.current = value
10+
}, [value]) // Only re-run if value changes
11+
// Return previous value (happens before update in useEffect above)
12+
return ref.current
13+
}
14+
15+
export default usePrevious

0 commit comments

Comments
 (0)