React Joyride

React Joyride 使用案例:

1. 組件封裝

import { useMemo, useState } from 'react';
import Joyride from 'react-joyride';

interface IProps {
  run: boolean;
  CLASS_CONTENT_LIST: { class: string; content: string }[];
  onSetStatus: any;
}

const Tour = (props: IProps) => {
  const { run = true, CLASS_CONTENT_LIST, onSetStatus } = props;
  const [isFirstStep, setIsFirstStep] = useState<boolean>(true); // 防止閃爍

  const steps = useMemo(() => {
    return CLASS_CONTENT_LIST.map((item, index) => {
      return {
        target: item.class,
        content: item.content,
        disableBeacon: index === 0 && isFirstStep ? true : false,
      };
    });
  }, [CLASS_CONTENT_LIST, isFirstStep]);

  const handleJoyrideCallback = ({ action }: any) => {
    if (action == 'next' && isFirstStep) {
      setIsFirstStep(false);
    }
    if (action == 'reset') {
      onSetStatus(false);
    }
  };

  return (
    <Joyride
      steps={steps}
      continuous={true}
      locale={{ back: '上一步', close: '關(guān)閉', last: '完成', next: '下一步', skip: '跳過(guò)' }}
      styles={{
        options: {
          primaryColor: '#2F54EB',
          textColor: '#000',
        },
      }}
      disableScrolling={true} // 禁用滾動(dòng)
      showSkipButton={true} // 顯示跳過(guò)按鈕
      disableOverlayClose={true} // 禁用遮罩層關(guān)閉
      hideCloseButton={true} // 隱藏關(guān)閉按鈕
      run={run}
      callback={handleJoyrideCallback}
    />
  );
};

export default Tour;

2. 使用

import Tour from '@/components/reactJoyride';
import { useDispatch, useSelector } from 'umi';

const dispatch = useDispatch();
const tourStatus = useSelector((state: any) => state.app.tourStatus);

...
<Tour
  run={tourStatus}
  CLASS_CONTENT_LIST={[
    { class: '.tour-class-first', content: '這里是第一步操作' },
    { class: '.tour-class-second', content: '這里是第二步操作' },
    { class: '.tour-class-third', content: '這里是第三步操作' },
  ]}
  onSetStatus={(status: boolean) => {
    // 這里是為了只需要引導(dǎo)用戶一次,把 run 存儲(chǔ)到 localStorage 中
    dispatch({ type: 'app/setTourStatus', payload: status });
  }}
/>
...

想必大家看完后也注意到了 "防止閃爍" 這個(gè)注釋,不知道各位是否在使用時(shí)也發(fā)現(xiàn)了這樣一個(gè)小問(wèn)題,那就是從第一步到第二步后,再返回第一步時(shí)頁(yè)面會(huì)閃一下,就是元素會(huì)從左上角閃到目標(biāo)元素位置。

簡(jiǎn)單修改一下,就能避免閃爍,給用戶良好的使用體驗(yàn)??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容