跳转到内容

Tour 引导

带有聚光灯高亮和步骤导航的引导式导览。

import { Tour } from 'asterui'

基础导览

带有多个步骤的简单引导式导览。

import { Tour, Button, Input, Card } from 'asterui'
import { useState, useRef } from 'react'

function App() {
  const [open, setOpen] = useState(false);
  const searchRef = useRef<HTMLInputElement>(null);
  const profileRef = useRef<HTMLButtonElement>(null);
  const settingsRef = useRef<HTMLButtonElement>(null);
  
  const steps = [
    {
      target: searchRef,
      title: 'Search',
      description: 'Use the search bar to find content quickly.',
      placement: 'bottom' as const,
    },
    {
      target: profileRef,
      title: 'Profile',
      description: 'Click here to view and edit your profile.',
      placement: 'bottom' as const,
    },
    {
      target: settingsRef,
      title: 'Settings',
      description: 'Customize your experience in settings.',
      placement: 'left' as const,
    },
  ];
  
  return (
      <Card className="p-4">
        <div className="flex gap-4 items-center mb-4">
          <Input ref={searchRef} placeholder="Search..." className="flex-1" />
          <Button ref={profileRef}>Profile</Button>
          <Button ref={settingsRef}>Settings</Button>
        </div>
        <Button color="primary" onClick={() => setOpen(true)}>
          Start Tour
        </Button>
        <Tour
          open={open}
          steps={steps}
          onClose={() => setOpen(false)}
          onFinish={() => setOpen(false)}
        />
      </Card>
    )
}

export default App

无目标导览

没有目标的步骤出现在屏幕中央。

import { Tour, Button } from 'asterui'
import { useState } from 'react'

function App() {
  const [open, setOpen] = useState(false);
  
  const steps = [
    {
      title: 'Welcome!',
      description: 'This tour will guide you through the main features.',
    },
    {
      title: 'Getting Started',
      description: 'Follow along to learn how to use this application.',
    },
    {
      title: 'All Done!',
      description: 'You are ready to start using the app.',
    },
  ];
  
  return (
      <Button color="primary" onClick={() => setOpen(true)}>
        Start Intro
      </Button>
      <Tour
        open={open}
        steps={steps}
        onClose={() => setOpen(false)}
        type="primary"
      />
    )
}

export default App

带封面图片

在步骤标题上方添加图片或自定义内容。

import { Tour, Button } from 'asterui'
import { useState, useRef } from 'react'

function App() {
  const [open, setOpen] = useState(false);
  const buttonRef = useRef<HTMLButtonElement>(null);
  
  const steps = [
    {
      target: buttonRef,
      title: 'New Feature',
      description: 'Check out this amazing new feature we just added!',
      cover: (
        <img
          src="https://picsum.photos/320/160"
          alt="Feature preview"
          className="w-full h-40 object-cover"
        />
      ),
      placement: 'right' as const,
    },
  ];
  
  return (
      <Button ref={buttonRef} color="primary" onClick={() => setOpen(true)}>
        View Feature
      </Button>
      <Tour
        open={open}
        steps={steps}
        onClose={() => setOpen(false)}
        showSkip={false}
      />
    )
}

export default App

位置

控制气泡相对于目标出现的位置。

Target
import { Tour, Button, Space } from 'asterui'
import { useState, useRef } from 'react'

function App() {
  const [open, setOpen] = useState(false);
  const targetRef = useRef<HTMLDivElement>(null);
  const placements = ['top', 'right', 'bottom', 'left'] as const;
  const [placementIndex, setPlacementIndex] = useState(0);
  
  const steps = [
    {
      target: targetRef,
      title: `Placement: ${placements[placementIndex]}`,
      description: 'The popover can appear on any side of the target.',
      placement: placements[placementIndex],
    },
  ];
  
  return (
      <div className="flex flex-col items-center gap-4">
        <div
          ref={targetRef}
          className="w-24 h-24 bg-primary text-primary-content flex items-center justify-center rounded-lg"
        >
          Target
        </div>
        <Space>
          <Button onClick={() => setPlacementIndex((i) => (i + 1) % 4)}>
            Change Placement
          </Button>
          <Button color="primary" onClick={() => setOpen(true)}>
            Show Tour
          </Button>
        </Space>
        <Tour
          open={open}
          steps={steps}
          onClose={() => setOpen(false)}
          showIndicators={false}
        />
      </div>
    )
}

export default App
属性描述类型默认值
open导览是否可见booleanfalse
steps导览步骤配置TourStepProps[]-
current当前步骤索引(受控)number-
onChange步骤更改时的回调(current: number) => void-
onClose导览关闭时的回调() => void-
onFinish导览完成时的回调() => void-
mask显示带有聚光灯的遮罩覆盖层booleantrue
type按钮样式类型'default' | 'primary''default'
gap聚光灯与目标之间的间隙 [半径, 偏移] 或数字number | [number, number]8
showSkip显示跳过按钮booleantrue
showIndicators显示步骤指示器booleantrue
closeOnMaskClick点击遮罩时关闭booleantrue
closeOnEscape按 Escape 键时关闭booleantrue
scrollIntoView将目标滚动到视图中booleantrue
prevButtonText上一步按钮的文本React.ReactNode'Previous'
nextButtonText下一步按钮的文本React.ReactNode'Next'
finishButtonText完成按钮的文本React.ReactNode'Finish'
skipButtonText跳过按钮的文本React.ReactNode'Skip'
zIndex导览覆盖层的 z-indexnumber1000
属性描述类型默认值
target目标元素 ref 或返回元素的函数RefObject<HTMLElement> | (() => HTMLElement | null) | null-
title步骤标题React.ReactNode-
description步骤描述React.ReactNode-
cover标题上方的封面图片或内容React.ReactNode-
placement气泡相对于目标的位置TourPlacement'bottom'
className步骤气泡的自定义类string-
onOpen步骤变为活动状态时调用() => void-
onClose离开步骤时调用() => void-
  • 导览对话框具有 role=“dialog” 和 aria-modal=“true”
  • 箭头键在步骤之间导航
  • Escape 键关闭导览
  • 步骤指示器可通过键盘访问
  • 打开时焦点被困在导览内