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

[RFC] useInViewport #2060

Closed
coderluojz opened this issue Feb 14, 2023 · 2 comments · Fixed by #2061
Closed

[RFC] useInViewport #2060

coderluojz opened this issue Feb 14, 2023 · 2 comments · Fixed by #2061
Assignees
Labels
feature New feature or request

Comments

@coderluojz
Copy link
Contributor

用于滚动内容时,根据视口所在位置,需要选中高亮菜单按钮。抛出回调以便于自定义控制。

API

useInViewport(
  target,
  options?: Options
  callback?: (entry: IntersectionObserverEntry) => void
);

Demo

import React, { useRef, useState } from 'react';
import { useInViewport } from 'ahooks';

const menus = ['menu-1', 'menu-2', 'menu-3'];
const content = {
  'menu-1': 'Content for menus 1',
  'menu-2': 'Content for menus 2',
  'menu-3': 'Content for menus 3',
};

export default () => {
  const menuRef = useRef<Element[]>([]);

  const [activeMenu, setActiveMenu] = useState(menus[0]);

  useInViewport(menuRef.current, { threshold: 0.1 }, (entry) => {
    if (entry.isIntersecting) {
      const active = entry.target.getAttribute('id') || '';
      setActiveMenu(active);
    }
  });

  const handleMenuClick = (menu) => {
    setActiveMenu(menu);
  };
  return (
    <div style={{ width: 300, height: 300, border: '1px solid', display: 'flex' }}>
      <ul style={{ listStyle: 'none', padding: 0, margin: 0 }}>
        {menus.map((menu, index) => (
          <li
            key={menu}
            style={{
              padding: '10px',
              backgroundColor: activeMenu === menu ? 'lightgray' : 'white',
            }}
          >
            <span style={{ cursor: 'pointer' }} onClick={() => handleMenuClick(menu)}>
              {menu}
            </span>
          </li>
        ))}
      </ul>
      <div style={{ flex: 1, overflow: 'scroll' }}>
        {menus.map((menu, index) => (
          <div
            ref={(el: any) => {
              menuRef.current[index] = el;
            }}
            key={menu}
            id={menu}
            style={{
              height: 500,
              backgroundColor: 'lightgray',
              padding: 20,
            }}
          >
            {content[menu]}
          </div>
        ))}
      </div>
    </div>
  );
};
coderluojz added a commit to coderluojz/hooks that referenced this issue Feb 14, 2023
@crazylxr
Copy link
Collaborator

https://ahooks.js.org/zh-CN/hooks/use-in-viewport 已经有这个hook 了

@coderluojz
Copy link
Contributor Author

coderluojz commented Feb 15, 2023 via email

@crazylxr crazylxr added this to To do in ahooks tasks Feb 15, 2023
@crazylxr crazylxr moved this from To do to In progress in ahooks tasks Feb 15, 2023
@crazylxr crazylxr moved this from In progress to Review in progress in ahooks tasks Feb 15, 2023
@crazylxr crazylxr self-assigned this Feb 15, 2023
@crazylxr crazylxr moved this from Review in progress to In progress in ahooks tasks Feb 15, 2023
@crazylxr crazylxr moved this from In progress to To do in ahooks tasks Feb 22, 2023
@crazylxr crazylxr removed this from To do in ahooks tasks Feb 22, 2023
@crazylxr crazylxr added the feature New feature or request label Feb 23, 2023
@liuyib liuyib self-assigned this Mar 11, 2023
crazylxr pushed a commit that referenced this issue Jul 12, 2023
#2061)

* feat: useInViewport (#2060)

* fix: 修复滚动聚焦问题

* fix: 修改示例 demo 滚动选中菜单功能

* style: format and remove useless code

* refactor: use optional chaining and change options position

* refactor: revert unnecessary change

* docs: update docs and desc of demo

* docs(useInViewport): update docs target type support array

docs(useInViewport): update docs target type support array

* refactor: code optimization

* feat: add callback array dependencies

* feat: add callback and target array test case

* fix: test case code optimization

* test: remove repeat variable

---------

Co-authored-by: liuyib <1656081615@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants