Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lastStepNextButton prop to allow customization of the next button in last step of the popover #553

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/web/components/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const tourConfig: StepType[] = [
highlightedSelectors: ['.modaaals-modal'],
mutationObservables: ['#portaaal'],
},
{
selector: '[data-tour="open_modal"]',
content:
"You can also customize the next button in the last step and make it do what you want. Notice the Done button here, it'll close the tour.",
},
]

const tourConfigAlt: StepType[] = [
Expand Down
10 changes: 10 additions & 0 deletions apps/web/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import { isMobile } from 'react-device-detect'
import Home from '../components/Home'
import Portal from '../components/Portal'
import { tourConfig, tourConfigAlt } from '../components/config'
import { Button } from '../components/Button'

function App() {
const disableBody = (target: Element | HTMLElement) =>
disableBodyScroll(target)
const enableBody = (target: Element | HTMLElement) => enableBodyScroll(target)

const closeButton = (props) => {
const { setIsOpen } = props
const clickHandler = () => {
setIsOpen(false)
}
return <Button onClick={clickHandler}>Done</Button>
}

return (
<>
<TourProvider
Expand Down Expand Up @@ -68,6 +77,7 @@ function App() {
}
}}
disableKeyboardNavigation={['esc']}
lastStepNextButton={closeButton}
>
<ModalProvider
modals={modals}
Expand Down
11 changes: 9 additions & 2 deletions packages/tour/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Prop to customize granurally each Component inside the _Popover_.
| `Badge` | `styles` |
| `Close` | `styles`, `onClick`, `disabled` |
| `Content` | `content`,`setCurrentStep`,`transition`, `isHighlightingObserved`,`currentStep`,`setIsOpen` |
| `Navigation` | `styles`,`setCurrentStep`, `steps`, `currentStep`, `disableDots`, `nextButton`, `prevButton`, `setIsOpen`, `hideButtons`, `hideDots`, `disableAll`, `rtl`, `Arrow`, |
| `Navigation` | `styles`,`setCurrentStep`, `steps`, `currentStep`, `disableDots`, `nextButton`, `prevButton`, `lastStepNextButton`, `setIsOpen`, `hideButtons`, `hideDots`, `disableAll`, `rtl`, `Arrow`, |
| `Arrow` | `styles`, `inverted`, `disabled` |

<details>
Expand Down Expand Up @@ -353,6 +353,8 @@ default: `reactour__mask`

### `prevButton?: (props: BtnFnProps) => void`

### `lastStepNextButton?: (props: BtnFnProps) => void`

<details>
<summary><small>Type details</small></summary>

Expand All @@ -374,7 +376,11 @@ type NavButtonProps = {

</details>

Helper functions to customize the _Next_ and _Prev_ buttons inside _Popover_, with useful parameters. It is possible to use the base `Button` and customize the props.
Helper functions to:
- customize the _Next_ and _Prev_ buttons inside _Popover_
- customize the _Next_ button of last step inside _Popover, with useful parameters

It is possible to use the base `Button` and customize the props.

### `afterOpen?: (target: Element | null) => void`

Expand Down Expand Up @@ -618,6 +624,7 @@ type PopoverContentProps = {
showBadge?: boolean
nextButton?: (props: BtnFnProps) => void
prevButton?: (props: BtnFnProps) => void
lastStepNextButton?: (props: BtnFnProps) => void
disableDotsNavigation?: boolean
rtl?: boolean
}
Expand Down
16 changes: 15 additions & 1 deletion packages/tour/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Navigation: React.FC<NavigationProps> = ({
setIsOpen,
nextButton,
prevButton,
lastStepNextButton,
disableDots,
hideDots,
hideButtons,
Expand All @@ -24,6 +25,7 @@ const Navigation: React.FC<NavigationProps> = ({
}) => {
const stepsLength = steps.length
const getStyles = stylesMatcher(styles)
const isLastStep = currentStep === steps.length - 1

const Button: React.FC<PropsWithChildren<NavButtonProps>> = ({
onClick,
Expand Down Expand Up @@ -114,7 +116,18 @@ const Navigation: React.FC<NavigationProps> = ({
</div>
) : null}
{!hideButtons ? (
nextButton && typeof nextButton === 'function' ? (
isLastStep &&
lastStepNextButton &&
typeof lastStepNextButton === 'function' ? (
lastStepNextButton({
Button,
setCurrentStep,
currentStep,
stepsLength,
setIsOpen,
steps,
})
) : nextButton && typeof nextButton === 'function' ? (
nextButton({
Button,
setCurrentStep,
Expand Down Expand Up @@ -142,6 +155,7 @@ export type NavigationProps = BaseProps & {
disableDots?: boolean
nextButton?: (props: BtnFnProps) => ReactNode | null
prevButton?: (props: BtnFnProps) => ReactNode | null
lastStepNextButton?: (props: BtnFnProps) => ReactNode | null
setIsOpen: Dispatch<React.SetStateAction<Boolean>>
hideButtons?: boolean
hideDots?: boolean
Expand Down
2 changes: 2 additions & 0 deletions packages/tour/components/PopoverContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PopoverContent: React.FC<PopoverContentProps> = ({
setIsOpen,
nextButton,
prevButton,
lastStepNextButton,
disableDotsNavigation,
rtl,
showPrevNextButtons = true,
Expand Down Expand Up @@ -88,6 +89,7 @@ const PopoverContent: React.FC<PopoverContentProps> = ({
aria-hidden={!accessibilityOptions?.showNavigationScreenReaders}
nextButton={nextButton}
prevButton={prevButton}
lastStepNextButton={lastStepNextButton}
disableDots={disableDotsNavigation}
hideButtons={!showPrevNextButtons}
hideDots={!showDots}
Expand Down
2 changes: 2 additions & 0 deletions packages/tour/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SharedProps = KeyboardHandler & {
clipId?: string
nextButton?: (props: BtnFnProps) => ReactNode | null
prevButton?: (props: BtnFnProps) => ReactNode | null
lastStepNextButton?: (props: BtnFnProps) => ReactNode | null
afterOpen?: (target: Element | null) => void
beforeClose?: (target: Element | null) => void
onClickMask?: (clickProps: ClickProps) => void
Expand Down Expand Up @@ -68,6 +69,7 @@ export type PopoverContentProps = {
showDots?: boolean
nextButton?: (props: BtnFnProps) => ReactNode | null
prevButton?: (props: BtnFnProps) => ReactNode | null
lastStepNextButton?: (props: BtnFnProps) => ReactNode | null
disableDotsNavigation?: boolean
rtl?: boolean
meta?: string
Expand Down