-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
164504e
commit 7851fb7
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>` | |