Components

Tour

A guided walk through steps beside the elements they explain, the page dimmed around each, with its place, next, back and skip.

Previews are drawn in the theme you made.

Edit it
scheme
direction

Three steps #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Recipes, Tour, TourSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Plain = Recipes.Button.style({ variant: 'outline' })

// Each step stands beside the element it explains, the page dimmed around a
// cutout over it; the arrows move on and back and Escape skips. The steps are
// the Bundle's args, kept in its Model. `Tour.focusReturn` gives focus back to
// the button that started it.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Started: {} }),
  Bundle.withChild('tour', Bundles.Tour, {
    args: {
      id: 'basic-tour',
      steps: [
        { title: 'Search', description: 'Find any page by its name.', target: 'basic-search' },
        {
          title: 'Save',
          description: 'Keep your work as you go.',
          target: 'basic-save',
          placement: 'right',
        },
        { title: 'That is all', description: 'Start the tour again from its button.' },
      ],
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Three steps',
  slots: TourSlots,
  recipe: Recipes.Tour,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model => App.children.tour.helpers.start()(model)),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' })],
      [
        Button.view(
          { label: 'Start the tour', onClick: onMessage(App.Message.Started()), style: Primary },
          h,
        ),
        Button.view(
          { label: 'Search', attributes: { button: [h.Id('basic-search')] }, style: Plain },
          h,
        ),
        Button.view(
          { label: 'Save', attributes: { button: [h.Id('basic-save')] }, style: Plain },
          h,
        ),
        Tour.view(
          {
            model: model.tour,
            onMessage: message => onMessage(App.Message.GotTourMessage({ message })),
            style,
            mixins: Tour.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Rich content, in the middle #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Kbd, Recipes, Tour, TourSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// The steps are data in the Model; `content` draws Html in place of a step's
// words, here a key to press. Steps with no target stand in the middle,
// and `words` renames the buttons and the step's place.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Started: {} }),
  Bundle.withChild('tour', Bundles.Tour, {
    args: {
      id: 'content-tour',
      steps: [
        { title: 'Welcome', description: 'A short look around.' },
        { title: 'Search anywhere', description: 'Press K with Control to search.' },
      ],
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Rich content, in the middle',
  slots: TourSlots,
  recipe: Recipes.Tour,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model => App.children.tour.helpers.start()(model)),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          { label: 'Take the tour', onClick: onMessage(App.Message.Started()), style: Primary },
          h,
        ),
        Tour.view(
          {
            model: model.tour,
            onMessage: message => onMessage(App.Message.GotTourMessage({ message })),
            content: (step, index) =>
              index === 1
                ? h.p(
                    [],
                    ['Press ', Kbd.combo({ keys: ['Ctrl', 'K'], joiner: '+' }, h), ' to search.'],
                  )
                : h.p([], [step.description]),
            words: { skip: 'Not now', done: 'Got it', progress: 'Step {step} of {count}' },
            style,
            mixins: Tour.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Go on when the target is pressed #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Recipes, Tour, TourSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Plain = Recipes.Button.style({ variant: 'outline' })

// A step that asks the reader to press something goes on when they do:
// `advanceOnClick: Tour.advanceOnClick` hears a press on the step's target
// through the cutout. It is a value, so a tour without it loads no listener.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Started: {} }),
  Bundle.withChild('tour', Bundles.Tour, {
    args: {
      id: 'advance-tour',
      steps: [
        { title: 'Make a draft', description: 'Press New to go on.', target: 'advance-new' },
        { title: 'Share it', description: 'Press Share to finish.', target: 'advance-share' },
      ],
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Go on when the target is pressed',
  slots: TourSlots,
  recipe: Recipes.Tour,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model => App.children.tour.helpers.start()(model)),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' })],
      [
        Button.view(
          { label: 'Start the tour', onClick: onMessage(App.Message.Started()), style: Primary },
          h,
        ),
        Button.view(
          { label: 'New', attributes: { button: [h.Id('advance-new')] }, style: Plain },
          h,
        ),
        Button.view(
          { label: 'Share', attributes: { button: [h.Id('advance-share')] }, style: Plain },
          h,
        ),
        Tour.view(
          {
            model: model.tour,
            onMessage: message => onMessage(App.Message.GotTourMessage({ message })),
            advanceOnClick: Tour.advanceOnClick,
            style,
            mixins: Tour.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Configure once #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Recipes, Tour, TourSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Plain = Recipes.Button.style({ variant: 'outline' })

// Every tour of an application said once: focus goes back to what started it
// and a press on the dim skips. The `strict` preset keeps the target out of
// reach, so the reader follows the steps and not the page.
const AppTour = Tour.configure({
  base: { mixins: Tour.focusReturn, dismissOnOutside: true },
  presets: { strict: { interactive: false, words: { skip: 'Leave' } } },
})

const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Started: {} }),
  Bundle.withChild('tour', Bundles.Tour, {
    args: {
      id: 'configure-tour',
      steps: [
        { title: 'Export', description: 'Your data, as a file.', target: 'configure-export' },
      ],
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Configure once',
  slots: TourSlots,
  recipe: Recipes.Tour,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model => App.children.tour.helpers.start()(model)),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' })],
      [
        Button.view(
          { label: 'Start the tour', onClick: onMessage(App.Message.Started()), style: Primary },
          h,
        ),
        Button.view(
          { label: 'Export', attributes: { button: [h.Id('configure-export')] }, style: Plain },
          h,
        ),
        AppTour.view(
          {
            preset: 'strict',
            model: model.tour,
            onMessage: message => onMessage(App.Message.GotTourMessage({ message })),
            style,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗

  • Keys of your own Add the application’s keys with the routes option, beside the ones the widget handles; the widget’s conflict(routes) judges a candidate set without drawing it.
  • Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).