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
17 changed files
with
177 additions
and
7 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useAPI | ||
route: /useAPI | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useAntdTable | ||
route: /useAntdTable | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useAsync | ||
route: /useAsync | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useControlledValue | ||
route: /useControlledValue | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useDynamicList | ||
route: /useDynamicList | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useEventEmitter | ||
route: /useEventEmitter | ||
menu: '高阶 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useLoadMore | ||
route: /useLoadMore | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useResponsive | ||
route: /useResponsive | ||
menu: '基础 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
--- | ||
name: useSearch | ||
route: /useSearch | ||
menu: '中台 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
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,22 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import useSize from '../index'; | ||
|
||
// test about Resize Observer see https://github.com/que-etc/resize-observer-polyfill/tree/master/tests | ||
describe('useSearch', () => { | ||
it('should be defined', () => { | ||
expect(useSize).toBeDefined(); | ||
}); | ||
it('without argument ', () => { | ||
const hook = renderHook(() => useSize()); | ||
expect(hook.result.current.length).toEqual(2); | ||
expect(hook.result.current[0].width).toEqual(0); | ||
expect(hook.result.current[0].height).toEqual(0); | ||
expect(hook.result.current[1].current).toEqual(undefined); | ||
}); | ||
it('with argument', () => { | ||
const hook = renderHook(() => useSize<HTMLBodyElement>(document.body)); | ||
expect(hook.result.current.length).toEqual(1); | ||
expect(hook.result.current[0].width).toEqual(0); | ||
expect(hook.result.current[0].height).toEqual(0); | ||
}); | ||
}); |
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,93 @@ | ||
--- | ||
name: useSize | ||
route: /useSize | ||
menu: '基础 hooks' | ||
edit: false | ||
sidebar: true | ||
--- | ||
|
||
import { Playground } from 'docz'; | ||
import useSize from './index'; | ||
|
||
# useSize | ||
|
||
一个用于监听 dom 节点尺寸变化的 Hook | ||
|
||
## 代码演示 | ||
|
||
### 基本用法 | ||
|
||
<Playground> | ||
{ | ||
() => { | ||
function Demo() { | ||
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> | ||
) | ||
} | ||
return <Demo /> | ||
} | ||
} | ||
</Playground> | ||
|
||
### 懒加载(用于监听同一组件内后渲染节点) | ||
|
||
<Playground> | ||
{ | ||
() => { | ||
function Demo() { | ||
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> | ||
) | ||
} | ||
return <Demo /> | ||
} | ||
} | ||
</Playground> | ||
|
||
### 监听提前渲染节点 | ||
|
||
<Playground> | ||
{ | ||
() => { | ||
function Demo() { | ||
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> | ||
) | ||
} | ||
return <Demo /> | ||
} | ||
} | ||
</Playground> | ||
|
||
## 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,44 @@ | ||
import { useState, useRef, MutableRefObject, useLayoutEffect } from 'react'; | ||
import ResizeObserver from 'resize-observer-polyfill'; | ||
|
||
type Arg = HTMLElement | (() => HTMLElement) | null; | ||
|
||
type Size = { width: number; height: number }; | ||
|
||
function useSize<T extends HTMLElement = HTMLElement>(): [Size, MutableRefObject<T>]; | ||
function useSize<T extends HTMLElement = HTMLElement>(arg: Arg): [Size]; | ||
function useSize<T extends HTMLElement = HTMLElement>( | ||
...args: [Arg] | [] | ||
): [Size, MutableRefObject<T>?] { | ||
const element = useRef<T>(); | ||
const [state, setState] = useState<Size>({ width: 0, height: 0 }); | ||
const hasPassedInElement = args.length === 1; | ||
const arg = args[0]; | ||
|
||
useLayoutEffect(() => { | ||
const passedInElement = typeof arg === 'function' ? arg() : arg; | ||
|
||
const targetElement = hasPassedInElement ? passedInElement : element.current; | ||
if (!targetElement) { | ||
return () => {}; | ||
} | ||
|
||
const resizeObserver = new ResizeObserver(entries => { | ||
entries.forEach(entry => { | ||
setState({ width: entry.target.clientWidth, height: entry.target.clientHeight }); | ||
}); | ||
}); | ||
|
||
resizeObserver.observe(targetElement); | ||
return () => { | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [element.current, typeof arg === 'function' ? undefined : arg]); | ||
|
||
if (hasPassedInElement) { | ||
return [state]; | ||
} | ||
return [state, element as MutableRefObject<T>]; | ||
} | ||
|
||
export default useSize; |