Skip to content

Commit

Permalink
docs: add useHover docs & demo
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Oct 28, 2023
1 parent 164504e commit 7851fb7
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/useHover/demo/demo1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* title: Basic usage
* desc: Use ref to set element that needs monitoring.
*
* title.zh-CN: 基础用法
* desc.zh-CN: 使用 ref 设置需要监听的元素。
*/

import { useHover, useRef } from '@any-hooks/solid'

export default () => {
const [ref, setRef] = useRef()
const isHovering = useHover(ref)

return <div ref={setRef}>{isHovering() ? 'hover' : 'leaveHover'}</div>
}
25 changes: 25 additions & 0 deletions src/useHover/demo/demo2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* title: Pass in DOM element
* desc: Pass in a function that returns the DOM element.
*
* title.zh-CN: 传入 DOM 元素
* desc.zh-CN: 传入 function 并返回一个 dom 元素。
*/

import { useHover } from '@any-hooks/solid'

export default () => {
const isHovering = useHover(() => document.getElementById('hover-div'), {
onEnter: () => {
console.log('onEnter')
},
onLeave: () => {
console.log('onLeave')
},
onChange: (isHover) => {
console.log('onChange', isHover)
},
})

return <div id="hover-div">{isHovering() ? 'hover' : 'leaveHover'}</div>
}
47 changes: 47 additions & 0 deletions src/useHover/index.en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# useHover

A hook that tracks whether the element is being hovered.

## Examples

### Default usage

<code src="./demo/demo1.tsx" />

### Pass in DOM element

<code src="./demo/demo2.tsx" />

## API

```javascript
const isHovering = useHover(
target,
{
onEnter,
onLeave,
onChange
}
);
```

### Params

| Property | Description | Type | Default |
| -------- | ------------------ | ----------------------------- | ------- |
| target | DOM element or ref | `() => Element` \| `Element` | - |
| options | More config | `Options` | - |

### Options

| Property | Description | Type | Default |
| -------- | --------------------------------------- | ------------------------------- | ------- |
| onEnter | Callback to be executed on mouse hover | `() => void` | - |
| onLeave | Callback to be executed on mouse leave | `() => void` | - |
| onChange | Callback to be executed on hover change | `(isHovering: boolean) => void` | - |

### Result

| Property | Description | Type |
| ---------- | ------------------------------------ | ------------------- |
| isHovering | Whether the element is being hovered | `Accessor<boolean>` |
47 changes: 47 additions & 0 deletions src/useHover/index.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# useHover

监听 DOM 元素是否有鼠标悬停。

## 代码演示

### 基础用法

<code src="./demo/demo1.tsx" />

### 传入 DOM 元素

<code src="./demo/demo2.tsx" />

## API

```javascript
const isHovering = useHover(
target,
{
onEnter,
onLeave,
onChange
}
);
```

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ------- | --------------------- | ----------------------------- | ------ |
| target | DOM 节点或者 Ref 对象 | `() => Element` \| `Element` | - |
| options | 额外的配置项 | `Options` | - |

### Options

| 参数 | 说明 | 类型 | 默认值 |
| -------- | -------------------- | ------------------------------- | ------ |
| onEnter | hover 时触发 | `() => void` | - |
| onLeave | 取消 hover 时触发 | `() => void` | - |
| onChange | hover 状态变化时触发 | `(isHovering: boolean) => void` | - |

### Result

| 参数 | 说明 | 类型 |
| ---------- | ---------------------- | ------------------- |
| isHovering | 鼠标元素是否处于 hover | `Accessor<boolean>` |

0 comments on commit 7851fb7

Please sign in to comment.