Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useInViewport #171

Merged
merged 12 commits into from
Dec 3, 2019
Merged

feat: add useInViewport #171

merged 12 commits into from
Dec 3, 2019

Conversation

wangtianlun
Copy link
Contributor

No description provided.

function useInViewport<T extends HTMLElement = HTMLElement>(
...args: [Arg] | []
): [boolean, MutableRefObject<T>?] {
const [inViewPort, setInViewport] = useState<boolean>(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初始值是不是 undefined 比较好?这样能给用户区分是 “真的不可见” 还是 “没有初始化完成” 留一些余地。。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果用户传的是一个确定的 dom 进来,这里是不是应该初始化为 当前的 true/false ?

Copy link
Collaborator

@brickspert brickspert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你觉得 useInVewport 和 Intersection Observer API 是什么关系呢?是不是功能可以增强下?
比如:

  1. 可以拿到 div 可视区域的百分比
  2. threshold,root,rootMargin 的设置等等。

不过加上之后会不会有点复杂?建议可以考虑下~
不加问题也不大,以后可以迭代。


### 基本用法

<JackBox tsCode={Demo1Tsx} jsCode={Demo1Jsx} demoName='基本用法' description='使用 ref 监听节点尺寸变化'>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

描述写错了。
可以加上“向上滚动,查看效果”等类似的话术。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我改一下

@brickspert
Copy link
Collaborator

记得从 src/index.ts 导出一下

@vercel
Copy link

vercel bot commented Dec 1, 2019

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/umijs/hooks/q60841tbd
🌍 Preview: https://hooks-git-fork-wangtianlun-feat-useinviewport.umijs.now.sh

@vercel vercel bot temporarily deployed to staging December 1, 2019 14:33 Inactive
@vercel vercel bot temporarily deployed to staging December 1, 2019 14:34 Inactive
@vercel vercel bot temporarily deployed to staging December 1, 2019 14:36 Inactive
@vercel vercel bot temporarily deployed to staging December 2, 2019 15:37 Inactive

const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
const top = el.getBoundingClientRect() && el.getBoundingClientRect().top
return top <= viewPortHeight
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试了一下,这个不太对。

如果元素在窗口上面,isInViewPort 永远返回 true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改了一版

Copy link
Collaborator

@brickspert brickspert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@brickspert brickspert merged commit 7b81711 into alibaba:feature Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants