使用者導覽對於 Web 應用程式來說是一項非常寶貴的可用性功能。它們可以讓您有效地吸引新用戶,並提供逐步指南來幫助他們了解軟體。導覽也可以作為重複任務或進階功能的快速參考。
我們的目標是建立一個解決方案,讓您能夠在 React 應用程式中建立跨多個頁面的入門體驗。這是它的樣子:
Ant Design 提供了 Tour 元件來建立互動式指南。但是,它有一些限制:
這是官方文件中的一個範例,示範了基本的本地實作:
import React, { useRef, useState } from 'react'; import { EllipsisOutlined } from '@ant-design/icons'; import { Button, Divider, Space, Tour } from 'antd'; const App = () => { const ref1 = useRef(null); const ref2 = useRef(null); const ref3 = useRef(null); const [open, setOpen] = useState(false); const steps = [ { title: 'Upload File', description: 'Put your files here.', target: () => ref1.current }, { title: 'Save', description: 'Save your changes.', target: () => ref2.current }, { title: 'Other Actions', description: 'Click to see other actions.', target: () => ref3.current }, ]; return ( <> <Button type="primary" onClick={() => setOpen(true)}>Begin Tour</Button> <Divider /> <Space> <Button ref={ref1}>Upload</Button> <Button ref={ref2} type="primary">Save</Button> <Button ref={ref3} icon={<EllipsisOutlined />} /> </Space> <Tour open={open} onClose={() => setOpen(false)} steps={steps} /> </> ); }; export default App;
雖然此實作對於單頁效果很好,但在 React 應用程式中跨頁面遊覽的場景中卻表現不佳。
我們的實作方式如下:
前置步驟,app.jsx,routes.jsx,routesNames.js :
import { RouterProvider } from "react-router-dom"; import AppRouter from "./routes"; export default function App() { return <RouterProvider router={AppRouter} />; }
export const ROUTE_NAMES = { HOME: "/", ABOUT: "/about", };
import AppLayout from "./AppLayout"; import { createBrowserRouter } from "react-router-dom"; import { ROUTE_NAMES } from "./routeNames"; import { Home } from "./components/Home"; import { About } from "./components/About"; import { Result } from "antd"; import {TourProvider} from "./TourContext"; const GetItem = (label, key, icon, to, children = [], type) => { return !to ? { key, icon, children, label, type, } : { key, icon, to, label, }; }; const GetRoute = (path, element, params = null) => { return { path, element, }; }; const WithAppLayout = (Component) => <TourProvider><AppLayout>{Component}</AppLayout></TourProvider>; export const routeItems = [ GetItem("Home", "home", null, ROUTE_NAMES.HOME), GetItem("About", "about", null, ROUTE_NAMES.ABOUT), ]; const AppRouter = createBrowserRouter([ GetRoute(ROUTE_NAMES.HOME, WithAppLayout(<Home />)), GetRoute(ROUTE_NAMES.ABOUT, WithAppLayout(<About />)), GetRoute( "*", <Result status="404" title="404" subTitle="Sorry, the page you visited does not exist." /> ), ]); export default AppRouter;
我們使用 React Context 來管理遊覽的全域狀態,包括活動的遊覽步驟。
import React, { createContext, useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { APP_TOURS } from "./steps"; const TourContext = createContext(); export const TourProvider = ({ children }) => { const [isTourActive, setTourActive] = useState(false); const navigate = useNavigate(); useEffect(() => { if (isTourActive) { navigate("/home"); // Redirect to the starting point of the tour } }, [isTourActive, navigate]); return ( <TourContext.Provider value={{ isTourActive, setTourActive, steps: APP_TOURS }}> {children} </TourContext.Provider> ); }; export default TourContext;
我們使用 querySelector 透過自訂 data-tour-id 屬性動態取得元素,而不是 React refs。
const getTourStepElement = (id) => document.querySelector(`[data-tour-id="${id}"]`); export const APP_TOURS = { "/home": [ { title: "Upload File", description: "Put your files here.", target: () => getTourStepElement("upload") }, { title: "Save", description: "Save your changes.", target: () => getTourStepElement("save") }, { type: "navigate", to: "/about", title: "About Us", description: "Learn more about us." }, ], "/about": [ { title: "About Us", description: "Here's what we are all about.", target: () => getTourStepElement("about") }, ], };
此元件動態處理跨頁面的導航和步驟。
import React, { useContext } from "react"; import { Tour } from "antd"; import { useNavigate } from "react-router-dom"; import TourContext from "./TourContext"; export const GlobalTour = () => { const { isTourActive, steps, setTourActive } = useContext(TourContext); const navigate = useNavigate(); return ( <Tour open={isTourActive} onClose={() => setTourActive(false)} steps={steps} onChange={(current) => { const step = steps[current]; if (step.type === "navigate") { navigate(step.to); } }} /> ); };
遊覽無縫整合到佈局中,可從任何頁面存取。
import React, { useContext } from "react"; import { Layout, Button } from "antd"; import { Link } from "react-router-dom"; import TourContext from "./TourContext"; import { GlobalTour } from "./GlobalTour"; const { Header, Content, Footer } = Layout; const AppLayout = ({ children }) => { const { setTourActive } = useContext(TourContext); return ( <Layout> <Header> <Link to="/home">Home</Link> <Link to="/about">About</Link> <Button onClick={() => setTourActive(true)}>Start Tour</Button> </Header> <Content>{children}</Content> <Footer>© {new Date().getFullYear()} My App</Footer> <GlobalTour /> </Layout> ); }; export default AppLayout;
由於我們的遊覽跨越多個頁面,我們將為我們想要在步驟中突出顯示的每個組件分配 data-tour-id
import { Button, Space } from "antd"; import { EllipsisOutlined } from "@ant-design/icons"; export const Home = () => { return ( <> <Button data-tour-id="upload" >Upload</Button> <Button data-tour-id="save" type="primary"> Save </Button> <Button data-tour-id="actions" icon={<EllipsisOutlined />} /> </> ); };
export const About = () => { return <div data-tour-id="about">About</div>; };
以上是設計和實現 React 應用程式的 Ant Design 全球應用程式之旅。的詳細內容。更多資訊請關注PHP中文網其他相關文章!