{{state}}
3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useDebounce.vue: -------------------------------------------------------------------------------- 1 | 2 |输入停止3s后才会更新状态: {{state}}
3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useDebounceFn.vue: -------------------------------------------------------------------------------- 1 | 2 |疯狂点击按钮,最后停下3s才会执行回调
3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useEvent.vue: -------------------------------------------------------------------------------- 1 | 2 |target 为 css 选择器
4 | 5 |target 为 Ref
8 | 9 |target 为 DOM 对象
12 | 13 |hash: {{hash}}
3 |search: {{search}}
4 |host: {{host}}
5 |hostname: {{hostname}}
6 |origin: {{origin}}
7 |pathname: {{pathname}}
8 |port: {{port}}
9 |protocol: {{protocol}}
10 | 11 | 12 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useInterval.vue: -------------------------------------------------------------------------------- 1 | 2 |{{count}}
3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useLifecycles.vue: -------------------------------------------------------------------------------- 1 | 2 |请点击F12看log
3 | 4 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useLocalStorage.vue: -------------------------------------------------------------------------------- 1 | 2 |将数据持久化到 localStorage 中
3 |localStorage中的值: {{state}}
4 |刷新页面能拿到存在 localStorage 中的状态
5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useResize.vue: -------------------------------------------------------------------------------- 1 | 2 |width: {{width}}
3 |height: {{height}}
4 | 5 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useScroll.vue: -------------------------------------------------------------------------------- 1 | 2 |x: {{x}}
3 |y: {{y}}
4 |滚动下方粉色块观测x和y变化
5 |x: {{x}}
3 |y: {{y}}
4 |滚动下方粉色块观测x和y变化
5 |将数据持久化到 sessionStorage 中
3 |sessionStorage中的值: {{state}}
4 |刷新页面能拿到存在 sessionStorage 中的状态
5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useStorage.vue: -------------------------------------------------------------------------------- 1 | 2 |将数据持久化到 localStorage 中
4 |localStorage中的值: {{localStorageState}}
5 |刷新页面能拿到存在 localStorage 中的状态
6 | 7 | 8 | 9 |将数据持久化到 sessionStorage 中
12 |sessionStorage中的值: {{sessionStorageState}}
13 |刷新页面能拿到存在 sessionStorage 中的状态
14 | 15 | 16 | 17 |Ready: {{ready}}
3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useTimeoutFn.vue: -------------------------------------------------------------------------------- 1 | 2 |Is callback executed?: {{executed}}
3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useTitle.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /docs/.vitepress/components/useToggle.vue: -------------------------------------------------------------------------------- 1 | 2 |Boolean 切换
4 |{{state}}
5 | 6 | 7 | 8 |任意两值直接切换
11 |{{stringState}}
12 | 13 | 14 | 15 | 16 | 17 |x: {{x}}
3 |y: {{y}}
4 | 5 | 6 | -------------------------------------------------------------------------------- /docs/.vitepress/config.js: -------------------------------------------------------------------------------- 1 | const config = { 2 | title: 'vhook', 3 | base: '/vhook/', 4 | lang: 'ZH-CN', 5 | description: 'Collection of Vue Composition Functions', 6 | header: [], 7 | themeConfig: { 8 | repo: 'lmhcoding/vhook', 9 | docsRepo: 'lmhcoding/vhook', 10 | docsBranch: 'master', 11 | docsDir: 'docs', 12 | editLinks: true, 13 | locales: { 14 | '/': { 15 | nav: [ 16 | { 17 | text: 'Changelog', 18 | link: 'https://github.com/lmhcoding/vhook/blob/master/CHANGELOG.md' 19 | } 20 | ], 21 | sidebar: [ 22 | {text: 'Introduction', link: '/'}, 23 | { 24 | text: 'State', 25 | collapsable: false, 26 | children: [ 27 | {text: 'useTitle', link: '/state/useTitle'}, 28 | {text: 'useToggle', link: '/state/useToggle'}, 29 | {text: 'useBoolean', link: '/state/useBoolean'}, 30 | {text: 'useHash', link: '/state/useHash'}, 31 | {text: 'useHistory', link: '/state/useHistory'} 32 | ] 33 | }, 34 | { 35 | text: 'Dom', 36 | collapsable: false, 37 | children: [ 38 | {text: 'useEvent', link: '/dom/useEvent'}, 39 | {text: 'useEventRef', link: '/dom/useEventRef'}, 40 | ] 41 | }, 42 | { 43 | text: 'Lifecycles', 44 | collapsable: false, 45 | children: [ 46 | {text: 'useLifecycles', link: '/lifecycles/useLifecycles'} 47 | ] 48 | }, 49 | { 50 | text: 'SideEffects', 51 | collapsable: false, 52 | children: [ 53 | {text: 'useDebounce', link: '/effects/useDebounce'}, 54 | {text: 'useDebounceFn', link: '/effects/useDebounceFn'}, 55 | {text: 'useInterval', link: '/effects/useInterval'}, 56 | {text: 'useTimeout', link: '/effects/useTimeout'}, 57 | {text: 'useTimeoutFn', link: '/effects/useTimeoutFn'}, 58 | ] 59 | }, 60 | { 61 | text: 'Storage', 62 | collapsable: false, 63 | children: [ 64 | {text: 'useLocalStorage', link: '/storage/useLocalStorage'}, 65 | {text: 'useSessionStorage', link: '/storage/useSessionStorage'}, 66 | {text: 'useStorage', link: '/storage/useStorage'} 67 | ] 68 | }, 69 | { 70 | text: 'Sensors', 71 | collapsable: false, 72 | children: [ 73 | {text: 'useWindowScroll', link: '/sensors/useWindowScroll'}, 74 | {text: 'useResize', link: '/sensors/useResize'}, 75 | {text: 'useScroll', link: '/sensors/useScroll'}, 76 | {text: 'useScrollRef', link: '/sensors/useScrollRef'} 77 | ] 78 | } 79 | ] 80 | } 81 | } 82 | }, 83 | locales: { 84 | '/': { 85 | lang: 'ZH-CN', 86 | title: 'vhook', 87 | description: 'Collection of Vue Composition Functions' 88 | } 89 | } 90 | } 91 | 92 | module.exports = config -------------------------------------------------------------------------------- /docs/.vitepress/theme/index.js: -------------------------------------------------------------------------------- 1 | import theme from 'vitepress/dist/client/theme-default' 2 | import registerComponents from '../components' 3 | 4 | theme.enhanceApp = (({ app }) => { 5 | registerComponents(app) 6 | }) 7 | 8 | export default theme 9 | -------------------------------------------------------------------------------- /docs/dom/useEvent.md: -------------------------------------------------------------------------------- 1 | # ``useEvent`` 2 | --- 3 | 4 | 用于监听事件的 ``Hook`` 5 | 6 | :::tip 7 | 会在组件卸载时自动解绑事件,当 ``target`` 为 ``Ref`` 时,``target`` 变化时也会自动解绑事件 8 | ::: 9 | 10 | 11 | 12 | ## API 13 | 14 | ```typescript 15 | const [target, clear] = useEvent(event, cb, options, target = window) 16 | ``` 17 | 18 | 19 | 20 | ## Params 21 | 22 | | 参数名 | 描述 | 类型 | 默认值 | 23 | | ------- | ------------------------------------------------------------ | ------------------------------------------------- | ------ | 24 | | event | 事件名 | string | | 25 | | cb | 事件监听回调 | Function | | 26 | | options | 传递给 ``addEventListener`` 和 ``removeEventListener`` 的第三个参数 | bolean \| AddEventListenerOptions | | 27 | | target | 绑定事件的目标 | EventTarget \| Reftarget 为 css 选择器
48 | 49 |target 为 Ref
52 | 53 |target 为 DOM 对象
56 | 57 |疯狂点击按钮,最后停下3s才会执行回调
35 | 36 | 37 | 38 | 51 | ``` 52 | 53 | -------------------------------------------------------------------------------- /docs/effects/useInterval.md: -------------------------------------------------------------------------------- 1 | # useInterval 2 | 3 | --- 4 | 5 | 6 | 7 | 对 ``setInterval`` 的简单封装 8 | 9 | 10 | 11 | :::tip 12 | 13 | 在组件 ``mounted`` 时设置定时器,并在组件 ``unmounted`` 时清除定时器 14 | 15 | ::: 16 | 17 | 18 | 19 | ## API 20 | 21 | 22 | 23 | ```typescript 24 | const [clear, start] = useInterval(fn, delay, immediate) 25 | ``` 26 | 27 | 28 | 29 | ## Params 30 | 31 | | 参数名 | 描述 | 类型 | 默认值 | 32 | | --------- | ---------------------------- | -------- | ------ | 33 | | fn | 定时器回调 | Function | | 34 | | delay | 延时时间 | number | | 35 | | immediate | 是否立即执行回调在开启定时器 | boolean | false | 36 | 37 | 38 | 39 | ## Result 40 | 41 | 42 | 43 | | 参数名 | 描述 | 类型 | 44 | | ------ | -------------- | ---------- | 45 | | clear | 用于清除定时器 | () => void | 46 | | start | 用于启动定时器 | () => void | 47 | | | | | 48 | 49 | ## Example 50 | 51 | 52 | 53 |{{count}}
62 | 63 | 64 | 65 | 66 | 83 | ``` 84 | 85 | -------------------------------------------------------------------------------- /docs/effects/useTimeout.md: -------------------------------------------------------------------------------- 1 | # useTimeout 2 | 3 | --- 4 | 5 | 用于在一段时间后更新值 6 | 7 | 8 | 9 | ## API 10 | 11 | ```typescript 12 | const {ready, start, stop} = useTimeout(delay, immediate) 13 | ``` 14 | 15 | 16 | 17 | ## Params 18 | 19 | 20 | 21 | | 参数名 | 描述 | 类型 | 默认值 | 22 | | --------- | ------------------ | ------- | ------ | 23 | | delay | 延时时间 | number | | 24 | | immediate | 是否立即启动定时器 | boolean | true | 25 | 26 | 27 | 28 | ## Result 29 | 30 | 31 | 32 | | 参数名 | 描述 | 类型 | 33 | | ------ | --------------- | ---------- | 34 | | ready | 定时是否结束 | boolean | 35 | | start | 开启/重启定时器 | () => void | 36 | | stop | 停止定时器 | () => void | 37 | 38 | 39 | 40 | ## Example 41 | 42 | 43 | 44 |Ready: {{ready}}
55 | 56 | 57 | 58 | 59 | 76 | ``` 77 | 78 | -------------------------------------------------------------------------------- /docs/effects/useTimeoutFn.md: -------------------------------------------------------------------------------- 1 | # ``useTimeoutFn`` 2 | 3 | --- 4 | 5 | 6 | 7 | 用于在一段时间后执行回调 8 | 9 | 10 | 11 | ## API 12 | 13 | 14 | 15 | ```typescript 16 | const { start, stop } = useTimeoutFn(fn, delay, immediate, clearEffectWhenStop) 17 | ``` 18 | 19 | 20 | 21 | ## Params 22 | 23 | | 参数名 | 描述 | 类型 | 默认值 | 24 | | ------------------- | --------------------------------------------- | -------- | ------ | 25 | | fn | 回调 | Function | | 26 | | delay | 定时时间 | number | | 27 | | immediate | 是否立即注册定时器 | boolen | true | 28 | | clearEffectWhenStop | 调用 ``stop`` 时是否停止内部的 ``watch`` 监听 | boolean | false | 29 | 30 | 31 | 32 | ## Result 33 | 34 | | 参数名 | 描述 | 类型 | 35 | | ------ | --------------- | ---------- | 36 | | start | 开启/重启定时器 | () => void | 37 | | Stop | 停止定时器 | () => void | 38 | 39 | 40 | 41 | ## Example 42 | 43 | 44 | 45 |Is callback executed?: {{executed}}
54 | 55 | 56 | 57 | 77 | ``` 78 | 79 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # ``vhook`` 2 | 3 | --- 4 | 5 | ``vhook`` 是基于 ``vue3.0`` 的 ``Composition API`` 开发的组合函数库 6 | 7 | 8 | 9 | ## Inspiration 10 | 11 | 这个项目的灵感来源于一些优秀的开源 ``react Hooks``,如: [``react-use``](https://github.com/streamich/react-use)、[``ahooks``](https://github.com/alibaba/hooks)等。 12 | 13 | 14 | 15 | ## Install 16 | 17 | ```bash 18 | npm install vhook 19 | yarn add vhook 20 | ``` 21 | 22 | -------------------------------------------------------------------------------- /docs/lifecycles/useLifecycles.md: -------------------------------------------------------------------------------- 1 | # useLifecycles 2 | --- 3 | 4 | 同时使用 ``onMounted`` 和 ``onUnmounted`` 的 ``Hook`` 5 | 6 | ## API 7 | 8 | ```typescript 9 | useLifecycles(onMountedCb, onUnmountedCb) 10 | ``` 11 | 12 | ## Params 13 | 14 | | 参数名 | 描述 | 类型 | 默认值 | 15 | | ------------- | ------------------------ | --------- | ------ | 16 | | onMountedCb | ``onMounted`` 时的回调 | () => any | | 17 | | onUnmountedCb | ``onUnmounted`` 时的回调 | () => any | | 18 | 19 | 20 | 21 | ## Example 22 | 23 | 24 | 25 |width: {{width}}
46 |height: {{height}}
47 | 48 | 56 | ``` 57 | 58 | -------------------------------------------------------------------------------- /docs/sensors/useScroll.md: -------------------------------------------------------------------------------- 1 | # ``useScroll`` 2 | 3 | --- 4 | 5 | 追踪特定 ``DOM`` 节点的滚动位置 6 | 7 | 8 | 9 | ## API 10 | 11 | ```typescript 12 | const [x, y, clear] = useScroll(target, delay) 13 | ``` 14 | 15 | 16 | 17 | ## Params 18 | 19 | | 参数名 | 描述 | 类型 | 默认值 | 20 | | ------ | ---------------------------------- | ----------------------------------------- | ------ | 21 | | target | 监听滚动的 ``DOM``,可使用css选择器 | String \| Element \| Refx: {{x}}
45 |y: {{y}}
46 |滚动下方粉色块观测x和y变化
47 |x: {{x}}
53 |y: {{y}}
54 |滚动下方粉色块观测x和y变化
55 |x: {{x}}
43 |y: {{y}}
44 | 45 | 46 | 54 | ``` 55 | 56 | -------------------------------------------------------------------------------- /docs/state/useBoolean.md: -------------------------------------------------------------------------------- 1 | # ``useBoolean`` 2 | --- 3 | 4 | 用于管理 ``Boolean`` 状态的 ``Hook`` 5 | 6 | 7 | 8 | ## API 9 | 10 | ```typescript 11 | const { 12 | state, 13 | toggle, 14 | setTrue, 15 | setFalse 16 | } = useBoolean(defaultValue?: boolean) 17 | ``` 18 | 19 | 20 | 21 | ## Params 22 | 23 | | 参数 | 说明 | 类型 | 默认值 | 24 | | ------------ | ------------ | ------- | ------ | 25 | | defaultValue | 初始默认状态 | boolean | false | 26 | 27 | 28 | 29 | ## Methods 30 | 31 | 该 Hook 返回以下函数: 32 | 33 | | 函数名 | 类型 | 描述 | 34 | | -------- | ------------------------ | ----------------- | 35 | | toggle | (next?: boolean) => void | 用于反转状态 | 36 | | setTrue | () => void | 将状态设置为true | 37 | | setFalse | () => void | 将状态设置为false | 38 | 39 | 40 | 41 | ## Example 42 | 43 |{{state}}
50 | 51 | 52 | 53 | 54 | 55 | 66 | ``` 67 | 68 | -------------------------------------------------------------------------------- /docs/state/useHash.md: -------------------------------------------------------------------------------- 1 | # ``useHash`` 2 | 3 | --- 4 | 5 | 6 | 7 | 追踪 ``location.hash`` 的变化 8 | 9 | 10 | 11 | ## API 12 | 13 | ```typescript 14 | const { hash, setHash } = useHash() 15 | ``` 16 | 17 | 18 | 19 | ## Result 20 | 21 | | 参数名 | 描述 | 类型 | 22 | | ------- | --------------------------------------------- | --------------------------- | 23 | | hash | 一个只读状态,对应当前 ``location.hash`` 的值 | DeepReadonly> | 24 | | setHash | 用于设置新的 ``hash`` 值 | (hash: string) => void | 25 | 26 | 27 | 28 | ## Example 29 | 30 |hash: {{hash}}
69 |search: {{search}}
70 |host: {{host}}
71 |hostname: {{hostname}}
72 |origin: {{origin}}
73 |pathname: {{pathname}}
74 |port: {{port}}
75 |protocol: {{protocol}}
76 | 77 | 78 | 86 | ``` 87 | 88 | -------------------------------------------------------------------------------- /docs/state/useTitle.md: -------------------------------------------------------------------------------- 1 | # ``useTitle`` 2 | --- 3 | 4 | 用于设置页面的标签页标题 5 | 6 | ## API 7 | 8 | ```typescript 9 | export declare function useTitle( 10 | title: string, 11 | restoreOnUnMount?: boolean 12 | ): (title: string) => void; 13 | ``` 14 | 15 | ## Params 16 | 17 | | 参数 | 说明 | 类型 | 默认值 | 18 | | ---------------- | ------------------------ | ------- | ------ | 19 | | title | 标签页标题 | string | | 20 | | restoreOnUnMount | 组件卸载时是否恢复原标题 | boolean | false | 21 | 22 | ## Methods 23 | 24 | 该 Hook 返回以下函数: 25 | 26 | | 函数签名 | 描述 | 27 | | ------------------------ | ---------------- | 28 | | (title: string) => void | 用于设置页面标题 | 29 | 30 | ## Example 31 | 32 |Boolean 切换
67 |{{state}}
68 | 69 | 70 | 71 |任意两值直接切换
74 |{{stringState}}
75 | 76 | 77 | 78 | 79 | 80 |将数据持久化到 localStorage 中
40 |刷新页面能拿到存在 localStorage 中的状态
41 | 42 | 45 | 46 | 58 | ``` 59 | 60 | -------------------------------------------------------------------------------- /docs/storage/useSessionStorage.md: -------------------------------------------------------------------------------- 1 | # ``useSessionStorage`` 2 | 3 | 具备响应式功能的 ``sessionStorage`` 状态 4 | 5 | 6 | 7 | ## API 8 | 9 | ```typescript 10 | const [state, setState] = useSessionStorage(key, val) 11 | ``` 12 | 13 | 14 | 15 | ## Params 16 | 17 | | 参数名 | 描述 | 类型 | 默认值 | 18 | | ------ | ------------------------------------------------------------ | ------ | ------ | 19 | | key | 存进 ``sessionStorage`` 中的 ``key`` | string | | 20 | | val | 存进 ``sessionStorage`` 中的 ``val``,会使用 ``JSON.stringify`` 进行序列化 | any | | 21 | 22 | 23 | 24 | ## Result 25 | 26 | | 参数名 | 描述 | 类型 | 27 | | -------- | ------------------------------------------------------------ | ---------------------------- | 28 | | state | 存进 ``sessionStorage`` 中的 ``val``,从 ``sessionStorage`` 中取出时会进行 ``JSON.parse`` 反序列化, 只读 | DeepReadonly> | 29 | | setState | 更新 ``sessionStorage``,同时 ``state`` 也会更新, 不传参数则等同于 ``removeItem`` | (Val?: T) => void | 30 | 31 | ## Example 32 | 33 |将数据持久化到 sessionStorage 中
40 |sessionStorage中的值: {{state}}
41 |刷新页面能拿到存在 sessionStorage 中的状态
42 | 43 |将数据持久化到 localStorage 中
41 |localStorage中的值: {{localStorageState}}
42 |刷新页面能拿到存在 localStorage 中的状态
43 | 44 |将数据持久化到 sessionStorage 中
49 |sessionStorage中的值: {{sessionStorageState}}
50 |刷新页面能拿到存在 sessionStorage 中的状态
51 | 52 |