All components smmdlg
Tour
A guided walkthrough that spotlights elements one step at a time with an anchored card.
Search… ⌘K
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import { Tour, useTour } from "#components/ui/ark-ui/tour.tsx";import { Button } from "#components/ui/ark-ui/button.tsx";
const steps = [ { id: "search", target: () => document.querySelector<HTMLElement>("#search"), title: "Find anything", description: "Search projects, people, and settings from one place.", placement: "bottom-start", arrow: true, actions: [{ label: "Next", action: "next" }], }, { id: "settings", target: () => document.querySelector<HTMLElement>("#settings"), title: "Make it yours", description: "Theme, notifications, and members live here.", actions: [ { label: "Back", action: "prev" }, { label: "Finish", action: "dismiss" }, ], },];
export function Example() { // The machine scrolls each target into view, spotlights it, and anchors // the card next to it. Title/Description fill in from the current step. const tour = useTour({ steps }); return ( <> <Button onClick={() => tour.start()}>Start tour</Button> <Tour.Root tour={tour} size="md" lazyMount unmountOnExit> <Tour.Portal> <Tour.Backdrop /> <Tour.Spotlight /> <Tour.Positioner> <Tour.Content> <Tour.Arrow> <Tour.ArrowTip /> </Tour.Arrow> <Tour.Header> <Tour.Title /> <Tour.Description /> </Tour.Header> <Tour.Footer> <Tour.ProgressText /> <Tour.Control> <Tour.Actions> {(actions) => actions.map((action) => ( <Tour.ActionTrigger key={action.label} action={action} asChild> <Button size="xs">{action.label}</Button> </Tour.ActionTrigger> )) } </Tour.Actions> </Tour.Control> </Tour.Footer> <Tour.CloseTrigger>×</Tour.CloseTrigger> </Tour.Content> </Tour.Positioner> </Tour.Portal> </Tour.Root> </> );}Sizes
Search… ⌘K
Search… ⌘K
Search… ⌘K
Install
1npx stylexblocks add tourSource
components/ui/ark-ui/tour.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345import * as stylex from "@stylexjs/stylex";import { useEffect, useState } from "react";import { Portal } from "@ark-ui/react/portal";import { Tour as ArkTour, useTour, useTourContext } from "@ark-ui/react/tour";import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";import { defineSlotRecipe } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'
// Every layer stacks on one base: backdrop (+0), spotlight (+1), card (+2).// The engines set `--tour-layer` per part.const tourZIndex = `calc(var(--tour-z-index) + var(--tour-layer, 0))`
const tourBackdrop = stylex.create({ base: { '--tour-z-index': zIndices.modal, '--tour-layer': 0, zIndex: tourZIndex, backgroundColor: semanticColors.bgBackdrop, // the spotlight hole glides between steps, but tracks scroll and resize // instantly (engines flag those updates with data-instant) transitionProperty: 'clip-path', transitionDuration: { default: durations.moderate, ':is([data-instant])': '0ms', }, transitionTimingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)', animationName: { default: null, ':is([data-open], [data-state="open"])': 'fade-in', ':is([data-closed], [data-state="closed"])': 'fade-out', }, animationDuration: { default: null, ':is([data-open], [data-state="open"])': durations.moderate, ':is([data-closed], [data-state="closed"])': durations.faster, }, },})
const tourSpotlight = stylex.create({ base: { '--tour-z-index': zIndices.modal, '--tour-layer': 1, zIndex: tourZIndex, boxShadow: `0 0 0 2px ${colorPalette.focusRing}`, transitionProperty: 'left, top, width, height', transitionDuration: { default: durations.moderate, ':is([data-instant])': '0ms', }, transitionTimingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)', },})
const tourPositioner = stylex.create({ base: { '--tour-z-index': zIndices.modal, '--tour-layer': 2, zIndex: tourZIndex, // a step without a target is a centered dialog position: { default: null, ':is([data-type="dialog"])': 'fixed', }, inset: { default: null, ':is([data-type="dialog"])': 0, }, display: { default: null, ':is([data-type="dialog"])': 'flex', }, alignItems: { default: null, ':is([data-type="dialog"])': 'center', }, justifyContent: { default: null, ':is([data-type="dialog"])': 'center', }, },})
const tourContent = stylex.create({ base: { position: 'relative', display: 'flex', flexDirection: 'column', gap: spacing._3, width: 'var(--tour-size)', maxWidth: 'calc(100vw - 2rem)', padding: 'var(--tour-padding)', fontSize: fontSizes.sm, lineHeight: lineHeights.sm, '--tour-size': sizes.xs, '--tour-radius': semanticRadii.panel, '--tour-bg': semanticColors.bgPanel, backgroundColor: 'var(--tour-bg)', borderRadius: 'var(--tour-radius)', boxShadow: semanticShadows.overlay, outline: 'none', transformOrigin: 'var(--transform-origin)', animationName: { default: null, ':is([data-open], [data-state="open"])': 'scale-in, fade-in', ':is([data-closed], [data-state="closed"])': 'scale-out, fade-out', }, animationDuration: { default: null, ':is([data-open], [data-state="open"])': durations.fast, ':is([data-closed], [data-state="closed"])': durations.faster, }, },})
const tourContentSizes = stylex.create({ sm: { '--tour-padding': spacing._3, '--tour-size': sizes.xs, }, md: { '--tour-padding': spacing._4, '--tour-size': sizes.xs, }, lg: { '--tour-padding': spacing._5, '--tour-size': sizes.sm, },})
const tourHeader = stylex.create({ base: { display: 'flex', flexDirection: 'column', gap: spacing._1, paddingInlineEnd: spacing._6, },})
const tourTitle = stylex.create({ base: { margin: 0, fontSize: fontSizes.md, lineHeight: lineHeights.md, fontWeight: fontWeights.semibold, color: semanticColors.fg, },})
const tourDescription = stylex.create({ base: { margin: 0, color: semanticColors.fgMuted, },})
const tourProgressText = stylex.create({ base: { fontSize: fontSizes.xs, lineHeight: lineHeights.xs, fontVariantNumeric: 'tabular-nums', color: semanticColors.fgMuted, },})
const tourFooter = stylex.create({ base: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: spacing._3, paddingTop: spacing._1, },})
const tourControl = stylex.create({ base: { display: 'flex', alignItems: 'center', gap: spacing._2, marginInlineStart: 'auto', },})
const tourCloseTrigger = stylex.create({ base: { position: 'absolute', top: spacing._2_5, insetInlineEnd: spacing._2_5, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', width: sizes._6, height: sizes._6, padding: 0, borderWidth: 0, borderRadius: semanticRadii.control, color: semanticColors.fgMuted, backgroundColor: { default: 'transparent', ':hover': semanticColors.bgMuted, }, cursor: 'pointer', outlineWidth: { default: null, ':focus-visible': '2px', }, outlineOffset: { default: null, ':focus-visible': '1px', }, outlineStyle: { default: null, ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, },})
const tourArrow = stylex.create({ base: { '--arrow-size': sizes._3, '--arrow-background': 'var(--tour-bg)', },})
const tourArrowTip = stylex.create({ base: { backgroundColor: 'var(--arrow-background)', },})
export type TourSize = keyof typeof tourContentSizes
// defineSlotRecipe validates the shape at compile time: default values must// exist in the axis maps, and slot keys must be `styles` or a declared axisconst tourRecipe = defineSlotRecipe({ slots: { backdrop: { styles: tourBackdrop }, spotlight: { styles: tourSpotlight }, positioner: { styles: tourPositioner }, content: { styles: tourContent, sizes: tourContentSizes }, header: { styles: tourHeader }, title: { styles: tourTitle }, description: { styles: tourDescription }, progressText: { styles: tourProgressText }, footer: { styles: tourFooter }, control: { styles: tourControl }, closeTrigger: { styles: tourCloseTrigger }, arrow: { styles: tourArrow }, arrowTip: { styles: tourArrowTip }, }, defaultVariants: { size: "md" },});
const { withRootProvider, withContext } = createSlotRecipeContext(tourRecipe);
// Ark positions the backdrop `absolute` with `inset: 0` and cuts the// spotlight hole with a clip-path in document coordinates, so the backdrop// must span the whole document, not just the first viewport.const backdropHeight = stylex.create({ document: (height: string) => ({ height }),});
function useDocumentHeight(active: boolean) { const [height, setHeight] = useState<number | null>(null); useEffect(() => { if (!active) return; const update = () => setHeight(document.documentElement.scrollHeight); update(); const observer = new ResizeObserver(update); observer.observe(document.body); window.addEventListener("resize", update); return () => { observer.disconnect(); window.removeEventListener("resize", update); }; }, [active]); return height;}
const StyledBackdrop = withContext(ArkTour.Backdrop, "backdrop", "TourBackdrop");
function Backdrop({ style, ...props }: React.ComponentProps<typeof StyledBackdrop>) { const tour = useTourContext(); const height = useDocumentHeight(tour.open); return ( <StyledBackdrop {...props} // dynamic StyleX styles carry inline values the static prop type omits style={ [height != null && backdropHeight.document(`${height}px`), style] as never } /> );}Backdrop.displayName = "TourBackdrop";
/** * Tour — Ark's tour machine wearing the tour slot recipe. Create the * machine with `useTour({ steps })` and pass it to `<Tour.Root tour size>`; * the machine scrolls each step's target into view, cuts a spotlight hole * in the backdrop, anchors the card next to the target (targetless steps * render as a centered dialog), moves focus into the card, and handles * Escape, outside clicks, and ←/→ between steps. Steps declare their own * `actions` (`next`, `prev`, `dismiss`, `skip`); render them with * `Tour.Actions` + `Tour.ActionTrigger` (compose a Button via `asChild`). * Render the layers inside `Tour.Portal`. */export const Tour = { Root: withRootProvider(ArkTour.Root, "TourRoot"), Context: ArkTour.Context, Portal, Backdrop, Spotlight: withContext(ArkTour.Spotlight, "spotlight", "TourSpotlight"), Positioner: withContext(ArkTour.Positioner, "positioner", "TourPositioner"), Content: withContext(ArkTour.Content, "content", "TourContent"), Arrow: withContext(ArkTour.Arrow, "arrow", "TourArrow"), ArrowTip: withContext(ArkTour.ArrowTip, "arrowTip", "TourArrowTip"), Header: withContext("div", "header", "TourHeader"), Title: withContext(ArkTour.Title, "title", "TourTitle"), Description: withContext(ArkTour.Description, "description", "TourDescription"), ProgressText: withContext(ArkTour.ProgressText, "progressText", "TourProgressText"), Footer: withContext("div", "footer", "TourFooter"), Control: withContext(ArkTour.Control, "control", "TourControl"), CloseTrigger: withContext(ArkTour.CloseTrigger, "closeTrigger", "TourCloseTrigger"), Actions: ArkTour.Actions, ActionTrigger: ArkTour.ActionTrigger,};
export { useTour };