0.0.4 • Published 2 years ago

e-guide v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

e-guide

介绍

用于用户导航的高亮组件

特性

  • 基于原生 JS
  • 只有一个主方法
  • 遮罩层和弹出层功能任意搭配

API

属性/方法描述类型默认值
guide导航高亮主函数(target: string | Element | Array<{ element: string | Element; options?: Options }>, options: Options) => GuideReturn
target导航高亮目标元素, 当为 string 时,该参数需要为元素选择器,当为 Element 时,需要为 DOM 元素, 当为数组时,开启导航步骤功能string | Element | Array<{ element: string | Element; options?: Options }>Object
options导航高亮配置, 分为遮罩层、弹出层及公共配置{ mask?: Mask, popover?: Popover, scrollDisabled?: boolean, beforeClose?: () => void }
options.mask遮罩层配置{ closeable: boolean, bgColor: string, withAnimation: boolean, padding: number | number, number }{ padding: 10, withAnimation: true, closeable: true, bgColor: 'rgb(0, 0, 0)' }
options.mask.padding目标元素高亮外边距,当为数组时,可配置上下边距和左右边距number | number, number 10
options.mask.closeable遮罩层是否可关闭booleantrue
options.mask.highlightDisabled高亮区域是否可以操作, 默认可以操作booleanfalse
options.mask.bgColor遮罩层背景颜色string'rgb(0, 0, 0)'
options.mask.withAnimation是否开启动画booleantrue
options.popover弹出层配置{ title: string, content: string, position: ''bottom' | 'bottomRight' | 'bottomLeft' | 'top' | 'topRight' | 'topLeft' | 'left' | 'leftTop' | 'leftBottom' | 'right' | 'rightTop' | 'rightBottom'', closeable: boolean, withAnimation: boolean }{ position: 'bottom', title: "", content: "", withAnimation: true, closeable: true }
options.popover.title弹出层标题string""
options.popover.content弹出层内容string""
options.popover.control开启步骤时,是否开启内置控制前进后退的按钮booleantrueboolean
options.popover.withAnimation是否开启动画booleantrue
options.popover.closeable弹出层是否可关闭, false 时不会有关闭按钮booleantrue
options.popover.position弹出层定位, 支持 12 个方向'bottom' | 'bottomRight' | 'bottomLeft' | 'top' | 'topRight' | 'topLeft' | 'left' | 'leftTop' | 'leftBottom' | 'right' | 'rightTop' | 'rightBottom''bottom'
options.popover.showStepTip步骤开启时,展示步骤进度提示信息钮booleantrue
options.scrollDisabled弹出层及遮罩层是否禁用滚动booleanfalse
options.beforeClose弹出层及遮罩层关闭前回调,步骤模式下,外层 beforeClose 会覆盖内层 beforeClose,此时会传入当前步骤下标(currentStepIndex?: number) => void
options.onNextStep下一步回调,可执行异步操作(currentStepIndex: number) => void
options.onPrevStep上一步回调,可执行异步操作(currentStepIndex: number) => void
options.stepGuideId手动配置此次步骤演示的唯一 idstring
options.showOnce只展示一次,需要配置 stepGuideId,否则无效,guide内部会将该id进行本地缓存,若清空缓存,则会展示高亮导航booleanfalse
options.noMoreShow是否展示不再提示按钮,需要配置 stepGuideId,否则无效booleanfalse
options.noMoreText不再提示文案string'不再提示'
GuideReturn导航返回对象,该对象提供一些方法控制导航功能object
GuideReturn.close控制弹出层及遮罩层的关闭方法() => void
GuideReturn.setStepTo在步骤功能开启时返回,设置弹出层及遮罩层的当前步骤位置(index: number, mathAdd: boolean) => void
GuideReturn.getCurrentStepIndex获取当前步骤下标() => number

例子

常规使用

import { guide } from 'e-guide'

// 只开启遮罩层
guide('#example', {
  mask: {
    withAnimation: true,
    padding: [20, 10],
  },
  popover: null,
})

// 只开启弹出层
guide('#example', {
  mask: null,
  popover: {
    title: '这是标题',
    content: '你好',
    closeable: false,
    position: 'top',
  },
})

// 同时开启遮罩层和弹出层
guide('#example', {
  mask: {
    withAnimation: true,
    padding: [20, 10],
  },
  popover: {
    title: '这是标题',
    content: '你好',
    closeable: false,
    position: 'top',
  },
})

// 目标DOM元素
guide(document.getElementById('example'), {
  mask: {
    withAnimation: true,
    padding: [20, 10],
  },
  popover: {
    title: '这是标题',
    content: '你好',
    closeable: false,
    position: 'top',
  },
})

// 使用步骤指引
guide([
  {
    element: '#example',
    options: {
      mask: {
        withAnimation: true,
        padding: [20, 10],
      },
      popover: {
        title: '这是标题',
        content: '你好',
        closeable: false,
        position: 'top',
      },
    },
  },
  {
    element: '#example1',
    options: {
      mask: {
        withAnimation: true,
        padding: 20,
      },
      popover: {
        title: '这是标题2',
        content: 'hello',
        closeable: false,
        position: 'bottom',
      },
    },
  },
  {
    element: '#example2',
    options: {
      mask: {
        withAnimation: true,
        padding: 20,
      },
      popover: {
        title: '这是标题3',
        content: 'guide',
        position: 'left',
      },
    },
  },
])

步骤控制

// 步骤上一步、下一步的回调控制, 支持异步操作
guide(
  [
    {
      element: '#example',
      options: {
        mask: {
          withAnimation: true,
          padding: [20, 10],
        },
        popover: {
          title: '这是标题',
          content: '你好',
          closeable: false,
          position: 'top',
        },
      },
    },
    {
      element: '#example2',
      options: {
        mask: {
          withAnimation: true,
          padding: 20,
        },
        popover: {
          title: '这是标题3',
          content: 'guide',
          position: 'left',
        },
      },
    },
  ],
  {
    onNextStep: async (currentStepIndex) => {
      if (currentStepIndex === 0) {
        await wait(1000)
      }
    },
    onPrevStep: async (currentStepIndex) => {
      if (currentStepIndex === 1) {
        await wait(1000)
      }
    },
  },
)

步骤只展示一次配置

guide(
  [
    {
      element: '#example',
      options: {
        mask: {
          withAnimation: true,
          padding: [20, 10],
        },
        popover: {
          title: '这是标题',
          content: '你好',
          closeable: false,
          position: 'top',
        },
      },
    },
    {
      element: '#example2',
      options: {
        mask: {
          withAnimation: true,
          padding: 20,
        },
        popover: {
          title: '这是标题3',
          content: 'guide',
          position: 'left',
        },
      },
    },
  ],
  {
    // 手动配置步骤id作为本次指引的唯一标识,用于只展示一次逻辑
    stepGuideId: 'guide-id',
    showOnce: true,
  },
)

不再提示配置

guide(
  [
    {
      element: '#example',
      options: {
        mask: {
          withAnimation: true,
          padding: [20, 10],
        },
        popover: {
          title: '这是标题',
          content: '你好',
          closeable: false,
          position: 'top',
        },
      },
    },
    {
      element: '#example2',
      options: {
        mask: {
          withAnimation: true,
          padding: 20,
        },
        popover: {
          title: '这是标题3',
          content: 'guide',
          position: 'left',
        },
      },
    },
  ],
  {
    // 手动配置步骤id作为本次指引的唯一标识,用于只展示一次逻辑
    stepGuideId: 'guide-id',
    noMoreShow: true,
    // 可配置文案
    noMoreText: '不再',
  },
)

在 React 中使用

// 可以使用ref获取到目标DOM,也可以使用选择器
import { guide } from 'e-guide'
import React, { useRef } from 'react'

function App() {
  const ref = useRef()

  const onClick = () => {
    guide(ref.current, {
      scrollDisabled: false,
      mask: {
        padding: [10, 20],
      },
    })
  }

  return (
    <button ref={ref} onClick={onClick}>
      指引
    </button>
  )
}

运行示例

git clone git@github.com:Jcanno/e-guide.git

// 使用vscode命令打开项目
code e-guide

// 优先使用pnpm安装依赖
pnpm install
// 也可以使用yarn
yarn install

// 运行项目示例
pnpm dev 或 yarn dev
0.0.3

2 years ago

0.0.4

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago