forked from alibaba/hooks
-
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
Showing
11 changed files
with
256 additions
and
56 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
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
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
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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state, ref] = useSize(); | ||
return ( | ||
<div ref={ref}> | ||
try to resize the preview window <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state, ref] = useSize<HTMLDivElement>(); | ||
return ( | ||
<div ref={ref}> | ||
try to resize the preview window <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state] = useSize(() => document.querySelector('#demo2')); | ||
return ( | ||
<div id="demo2"> | ||
try to resize the preview window <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state] = useSize(() => document.querySelector('#demo2')); | ||
return ( | ||
<div id="demo2"> | ||
try to resize the preview window <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state] = useSize(document.querySelector('body')); | ||
return ( | ||
<div> | ||
this demo is listening to body size change, try to resize the window instead <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,12 @@ | ||
import React from 'react'; | ||
import useSize from '..'; | ||
|
||
export default () => { | ||
const [state] = useSize(document.querySelector('body')); | ||
return ( | ||
<div> | ||
this demo is listening to body size change, try to resize the window instead <br /> | ||
dimensions -- width: {state.width} px, height: {state.height} px | ||
</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,65 @@ | ||
--- | ||
name: useSize | ||
route: /useSize | ||
menu: '基础 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
||
import JackBox from 'jackbox'; | ||
|
||
import Demo1 from './demo/demo1'; | ||
import Demo1Tsx from '!raw-loader!./demo/demo1.tsx'; | ||
import Demo1Jsx from '!raw-loader!./demo/demo1.jsx'; | ||
|
||
import Demo2 from './demo/demo2'; | ||
import Demo2Tsx from '!raw-loader!./demo/demo2.tsx'; | ||
import Demo2Jsx from '!raw-loader!./demo/demo2.jsx'; | ||
|
||
import Demo3 from './demo/demo3'; | ||
import Demo3Tsx from '!raw-loader!./demo/demo3.tsx'; | ||
import Demo3Jsx from '!raw-loader!./demo/demo3.jsx'; | ||
|
||
# useSize | ||
|
||
一个用于监听 dom 节点尺寸变化的 Hook | ||
|
||
## 代码演示 | ||
|
||
### 基本用法 | ||
|
||
<JackBox tsCode={Demo1Tsx} jsCode={Demo1Jsx} demoName='基本用法' description='使用 ref 监听节点尺寸变化'> | ||
<Demo1 /> | ||
</JackBox> | ||
|
||
### 懒加载(用于监听同一组件内后渲染节点) | ||
|
||
<JackBox tsCode={Demo2Tsx} jsCode={Demo2Jsx} demoName='懒加载(用于监听同一组件内后渲染节点)' description='传入 function 来监听 dom 节点'> | ||
<Demo2 /> | ||
</JackBox> | ||
|
||
### 监听提前渲染节点 | ||
|
||
<JackBox tsCode={Demo3Tsx} jsCode={Demo3Jsx} demoName='监听提前渲染节点' description='直接传入 dom 节点'> | ||
<Demo3 /> | ||
</JackBox> | ||
|
||
## API | ||
|
||
``` | ||
const [ state, ref? ] = useSize(dom); | ||
dom?: HTMLElement | (() => HTMLElement) | ||
``` | ||
|
||
### 结果 | ||
|
||
| 参数 | 说明 | 类型 | | ||
|----------|------------------------------------------|------------| | ||
| state | dom 节点的尺寸 | { width: number, height: number } | | ||
| ref | 当未传入任何参数时,将 ref 绑定给需监听的节点 | - | | ||
|
||
### 参数 | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
|---------|----------------------------------------------|------------------------|--------| | ||
| dom? | 可选项,如果未传入则会监听返回结果中的 ref,否则会监听传入的节点 | HTMLElement \| (() => HTMLElement) \| undefined | - | |
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,65 @@ | ||
--- | ||
name: useSize | ||
route: /useSize | ||
menu: 'Basic Hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
||
import JackBox from 'jackbox'; | ||
|
||
import Demo1 from './demo/demo1'; | ||
import Demo1Tsx from '!raw-loader!./demo/demo1.tsx'; | ||
import Demo1Jsx from '!raw-loader!./demo/demo1.jsx'; | ||
|
||
import Demo2 from './demo/demo2'; | ||
import Demo2Tsx from '!raw-loader!./demo/demo2.tsx'; | ||
import Demo2Jsx from '!raw-loader!./demo/demo2.jsx'; | ||
|
||
import Demo3 from './demo/demo3'; | ||
import Demo3Tsx from '!raw-loader!./demo/demo3.tsx'; | ||
import Demo3Jsx from '!raw-loader!./demo/demo3.jsx'; | ||
|
||
# useSize | ||
|
||
A hook to subscribe DOM element size change | ||
|
||
## Examples | ||
|
||
### Default usage | ||
|
||
<JackBox tsCode={Demo1Tsx} jsCode={Demo1Jsx} demoName='Default usage' description='using ref to listen to size change'> | ||
<Demo1 /> | ||
</JackBox> | ||
|
||
### Lazy load DOM element(used to subscibe to DOM element renders after the hook) | ||
|
||
<JackBox tsCode={Demo2Tsx} jsCode={Demo2Jsx} demoName='Lazy load DOM element(used to subscibe to dom element renders after the hook)' description='pass in a function that returns the DOM element'> | ||
<Demo2 /> | ||
</JackBox> | ||
|
||
### Listen to pre-rendered DOM | ||
|
||
<JackBox tsCode={Demo3Tsx} jsCode={Demo3Jsx} demoName='Listen to pre-rendered DOM' description='pass in the DOM element itself'> | ||
<Demo3 /> | ||
</JackBox> | ||
|
||
## API | ||
|
||
``` | ||
const [ state, ref? ] = useSize(dom); | ||
dom?: HTMLElement | (() => HTMLElement) | ||
``` | ||
|
||
### Result | ||
|
||
| Property | Description | Type | | ||
|----------|------------------------------------------|------------| | ||
| state | size of the DOM | { width: number, height: number } | | ||
| ref | when no param is passed, this ref will be listened | - | | ||
|
||
### Params | ||
|
||
| Property | Description | Type | Default | | ||
|---------|----------------------------------------------|------------------------|--------| | ||
| dom? | optional, if none is passed, this hook will subscibe to the ref that it returns | HTMLElement \| (() => HTMLElement) \| undefined | - | |