11.0.1 • Published 7 days ago

@dopt/react-tour v11.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

@dopt/react-tour

Overview

A React tour component for building experiences with Dopt.

You can use the tour component out of the box as a pre-built component or break out and use it headlessly with your own UI component.

Learn more about how to use this component with Dopt →

Installation

ℹ️ If you are using a particular React framework like Next.js, please check out our framework specific docs.

# npm
npm install @dopt/react-tour

# Yarn
yarn add @dopt/react-tour

# pnpm
pnpm add @dopt/react-tour

Usage

The default export from @dopt/react-tour is a collection of components that you can use to structure and compose a tour item.

import TourItem, { useTourItem } from '@dopt/react-tour';

function MyTourStep({ children }) {
  const tourItem = useTourItem('tour.shaggy-horses-sniff');

  if (!tourItem) {
    return children;
  }

  return (
    <TourItem.Root active={tourItem.active}>
      <TourItem.Anchor>{children}</TourItem.Anchor>
      <TourItem.Popover>
        <TourItem.Content>
          <TourItem.Header>
            <TourItem.Title>{tourItem.title}</TourItem.Title>
            <TourItem.DismissIcon onClick={tourItem.tour?.dismiss} />
          </TourItem.Header>
          <TourItem.Body>{tourItem.body}</TourItem.Body>
          <TourItem.Footer>
            <TourItem.BackButton>{tourItem.backLabel}</TourItem.BackButton>
            <TourItem.NextButton onClick={tourItem.next}>
              {tourItem.nextLabel}
            </TourItem.NextButton>
          </TourItem.Footer>
          <TourItem.Progress
            count={tourItem.tour?.size || 1}
            index={tourItem.index}
          />
        </TourItem.Content>
      </TourItem.Popover>
    </TourItem.Root>
  );
}

Check out our tour example and our headless tour example for more in-depth usage.

Props

Root

The root element of the tour item.

NameTypeDescription
active?booleanDetermines the visibility of the component (default: false)
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Anchor

The element to anchor the tour item to.

NameTypeDescription
childrenReactElementA React element to anchor to

Popover

The tour item popover. Extends HTMLDivElement.

NameTypeDescription
alignment?AlignmentDetermines how the component should align relative to the anchor element (default: center)
children?ReactNodeThe contents of the component
offset?numberThe distance in px to position the component relative to the anchor element (default: 10)
position?SideThe side that the component should position relative to the anchor element (default: top)
theme?ThemeA theme definition to attach to the component

Content

The content of the tour item popover. Extends HTMLDivElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Header

The header of the tour item popover. Extends HTMLElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Title

The title of the tour item popover. Extends HTMLHeadingElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Content

The content of the tour item popover. Extends HTMLDivElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

DismissIcon

The dismiss icon of the tour item popover. Extends HTMLButtonElement.

NameTypeDescription
theme?ThemeA theme definition to attach to the component

Body

The body of the tour item popover. Extends HTMLDivElement.

NameTypeDescription
children?RichTextThe rich text contents of the component
theme?ThemeA theme definition to attach to the component

Footer

The footer of the tour item popover. Extends HTMLElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

NextButton

The next button of the tour item popover. Extends HTMLButtonElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

BackButton

The back button of the tour item popover. Extends HTMLButtonElement.

NameTypeDescription
children?ReactNodeThe contents of the component
theme?ThemeA theme definition to attach to the component

Progress

The progress indicators of the tour item popover. Extends HTMLOListElement.

NameTypeDescription
countnumberThe total count of items
indexnumberThe current item index
theme?ThemeA theme definition to attach to the component

Hooks

If you are planning to only use the tour headlessly, you can import the hooks alone using @dopt/react-tour/hooks.

useTour

  • useTour(id: string): Tour

A React hook for accessing and updating a Tour's state.

import { useTour } from '@dopt/react-tour';

export function MyTourStep() {
  const {
    id,
    items,
    active,
    completed,
    dismissed,
    complete,
    dismiss,
    filter,
    count,
    size,
  } = useTour('onboarding-tour.tour-component');

  return (
    <div>
      <div id="states">
        <div>tour.active: {active}</div>
        <div>tour.completed: {completed}</div>
        <div>tour.dismissed: {dismissed}</div>
      </div>
      <div id="actions">
        <button onClick={complete}>Complete</button>
        <button onClick={dismiss}>Dismiss</button>
      </div>
      <div id="children">
        <div>tour.items: {JSON.stringify(items.map((item) => item.id))}</div>
      </div>
      <div id="filtering">
        <div id="active-items">{JSON.stringify(filter('active'))}</div>
        <div id="not-active-items">{JSON.stringify(filter('not-active'))}</div>
        <div id="completed-items">{JSON.stringify(filter('completed'))}</div>
        <div id="not-completed-items">
          {JSON.stringify(filter('not-completed'))}
        </div>
      </div>
      <div id="metadata">
        <div>tour.size: {size}</div>
      </div>
    </div>
  );
}

useTourItem

A React hook for accessing and updating a tour item's state.

import { useTourItem } from '@dopt/react-tour';
import RichText from '@dopt/react-rich-text';

export function Application() {
  const {
    id,
    tour,
    index,
    title,
    body,
    nextLabel,
    backLabel,
    active,
    completed,
    next,
    back,
  } = useTourItem('onboarding-tour.step-1');

  return (
    <div>
      <div id="states">
        <div>tourItem.active: {active}</div>
        <div>tourItem.completed: {completed}</div>
      </div>
      <div id="actions">
        <button onClick={next}>{nextLabel}</button>
        <button onClick={back}>{backLabel}</button>
      </div>
      <div id="content">
        <div>tourItem.title: {title}</div>
        <div>
          tourItem.body: <RichText>{body}</RichText>
        </div>
        <div>tourItem.nextLabel: {nextLabel}</div>
        <div>tourItem.backLabel: {backLabel}</div>
      </div>
      <div id="parent">
        <div>tourItem.tour: {JSON.stringify(tour)}</div>
      </div>
      <div id="metadata">
        <div>tourItem.index: {tourItem.index}</div>
      </div>
    </div>
  );
}

Styling API

Learn more about styling and theming →

NameSelectorDescription
popover.dopt-tour-itemPopover element
content.dopt-tour-item__contentContent container
header.dopt-tour-item__headerHeader containing title and dismiss icon
title.dopt-tour-item__titleTitle heading
dismissIcon.dopt-tour-item__dismiss-iconDisiss icon button
body.dopt-tour-item__bodyBody content
footer.dopt-tour-item__footerFooter containing back and next buttons
backButton.dopt-tour-item__back-buttonBack button
nextButton.dopt-tour-item__next-buttonNext button
progress.dopt-tour-item__progressProgress indicators
progressItem.dopt-tour-item-progress__itemProgress indicator item

Popover position

NameSelectorDescription
top.dopt-tour-item--topPositioned top
top[data-position="top"]Positioned top
right.dopt-tour-item--rightPositioned right
right[data-position="right"]Positioned right
bottom.dopt-tour-item--bottomPositioned bottom
bottom[data-position="bottom"]Positioned bottom
left.dopt-tour-item--leftPositioned left
left[data-position="left"]Positioned left

Popover alignment

NameSelectorDescription
start.dopt-tour-item--startAligned start
start[data-alignment="start"]Aligned start
center.dopt-tour-item--centerAligned center
center[data-alignment="center"]Aligned center
end.dopt-tour-item--endAligned end
end[data-alignment="end"]Aligned end

Progress item state

NameSelectorDescription
active.dopt-tour-item-progress__item--activeActive progress item

Types

Tour

A stateful container for tour items.

interface Tour {
  id: string;

  items: TourItem[];

  active: boolean;

  completed: boolean;
  dismissed: boolean;

  complete: () => void;
  dismiss: () => void;

  size: number;

  filter(on: FilterableField): TourItem[];
  count(where: CountableField): number;
}

TourItem

A child of the tour. Includes state accessors and methods for updating state along with content configured in Dopt.

interface TourItem {
  id: string;

  tour: Tour | undefined;

  index: number | null | undefined;

  title: string | null | undefined;
  body: RichText | null | undefined;

  nextLabel: string | null | undefined;
  backLabel: string | null | undefined;

  active: boolean;

  completed: boolean;

  next: () => void;
  back: () => void;
}

FilterableField

type FilterableField = 'completed' | 'not-completed' | 'active' | 'not-active';

CountableField

type CountableField = FilterableField;

Alignment

type Alignment = 'start' | 'center' | 'end';

Position

type Side = 'top' | 'right' | 'bottom' | 'left';
11.0.1

7 days ago

11.0.0

14 days ago

10.0.1

20 days ago

9.0.5

2 months ago

9.0.4

2 months ago

9.0.3

2 months ago

9.0.2

3 months ago

9.0.1

3 months ago

9.0.0

6 months ago

8.1.0

7 months ago

8.0.1

7 months ago

8.0.0

7 months ago

7.1.4

7 months ago

7.1.3

8 months ago

7.1.2

8 months ago

7.1.1

8 months ago

7.1.0

8 months ago

7.0.0

8 months ago

6.2.0

9 months ago

6.1.0

9 months ago

6.0.0

9 months ago

5.0.2

9 months ago

5.0.1

9 months ago

5.0.0

9 months ago

4.1.0

9 months ago

4.0.2

9 months ago

4.0.1

9 months ago

4.0.0

9 months ago

3.0.0

10 months ago

2.0.0

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago