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

fix(listbox): unexpected scrollShadow on virtualized listbox #4784

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from

Conversation

wingkwong
Copy link
Member

@wingkwong wingkwong commented Feb 2, 2025

📝 Description

virtualization will be enabled when the number of items is greater than 50, which comes with scrollShadow. However, currently there is no way to disable it even passing scrollShadowProps={{isEnabled: false}}.

The reason is that the following data attributes were added to virtualized listbox instead of being added in useScrollShadow.

data-bottom-scroll={scrollState.isTop}
data-top-bottom-scroll={scrollState.isMiddle}
data-top-scroll={scrollState.isBottom}

Checked with Roger, the reason is that in the virtualized-listbox, because of how tanstack virtualizer implementation, it makes that the scrolling component differs from the actual component that render the rows (the one that have very big number of height). Hence the useScrollShadow is not able to "detect" the scrolling component and update the data-overflow accordingly.

This PR is to

  • fix the calculation for the virtualization case
  • fix addEventListener (currently this line does nothing since it is not capturing anything)

⛳️ Current behavior (updates)

With the following code, scroll shadow is visible even though setting isEnabled to false.

<Autocomplete
label={`Search from ${args.numItems} items`}
{...args}
scrollShadowProps={{
  isEnabled: false,
}}
>
{largeDataset.map((item, index) => (
  <AutocompleteItem key={index} value={item.value}>
    {item.label}
  </AutocompleteItem>
))}
</Autocomplete>

image

🚀 New behavior

image

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

Summary by CodeRabbit

  • Bug Fixes
    • Resolved unexpected scroll behavior in listbox components.
  • New Features
    • Enhanced virtual scrolling with improved tracking of content height and scroll position.
    • Updated dependencies to ensure more reliable overflow handling during scrolling.

Copy link

linear bot commented Feb 2, 2025

Copy link

changeset-bot bot commented Feb 2, 2025

🦋 Changeset detected

Latest commit: ae70bfc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@heroui/use-data-scroll-overflow Patch
@heroui/listbox Patch
@heroui/scroll-shadow Patch
@heroui/autocomplete Patch
@heroui/select Patch
@heroui/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Feb 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
heroui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 2, 2025 11:14am
heroui-sb ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 2, 2025 11:14am

Copy link
Contributor

coderabbitai bot commented Feb 2, 2025

Walkthrough

This pull request patches two dependencies and refines the scroll behavior in virtualized listboxes. It introduces a new variable to capture the total virtual scroll height and adds corresponding data attributes to the listbox component. In parallel, the overflow detection hook is updated to reference these new attributes and to employ event listeners in the capture phase. The modifications are implemented to resolve the scroll shadow issue described in issue #4553.

Changes

Files Change Summary
.changeset/ninety-flowers-teach.md Patches dependencies @heroui/use-data-scroll-overflow and @heroui/listbox to address scrollShadow behavior in virtualized listboxes (#4553).
packages/components/…/virtualized-listbox.tsx, packages/hooks/…/use-data-scroll-overflow/src/index.ts In the virtualized listbox: introduces virtualScrollHeight and adds data-virtual-scroll-height and data-virtual-scroll-top attributes; in the hook: refactors overflow checking logic with a new query selector and updates event listener configuration using the capture phase.

Sequence Diagram(s)

sequenceDiagram
    participant Listbox as VirtualizedListbox
    participant Hook as useDataScrollOverflow
    participant Browser as Scroll Event

    Browser->>Listbox: Trigger scroll event
    Listbox->>Listbox: Update data-virtual-scroll-top & data-virtual-scroll-height
    Listbox->>Hook: Invoke overflow check with updated scroll data
    Hook->>Hook: Calculate overflow states based on new scroll attributes
Loading

Assessment against linked issues

Objective Addressed Explanation
[BUG] Autocomplete incorrect scroll shadow for >50 items (#4553)

Possibly related issues

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/hooks/use-data-scroll-overflow/src/index.ts (1)

115-124: LGTM! Consider adding error handling for invalid attribute values.

The changes correctly handle both virtualized and non-virtualized scenarios by using data attributes when available and falling back to element properties.

Consider adding error handling for invalid attribute values:

-      const scrollHeight = +(
-        listbox?.getAttribute("data-virtual-scroll-height") ?? el.scrollHeight
-      );
+      const virtualHeight = listbox?.getAttribute("data-virtual-scroll-height");
+      const scrollHeight = virtualHeight ? Number(virtualHeight) || el.scrollHeight : el.scrollHeight;
-      const scrollTop = +(listbox?.getAttribute("data-virtual-scroll-top") ?? el.scrollTop);
+      const virtualTop = listbox?.getAttribute("data-virtual-scroll-top");
+      const scrollTop = virtualTop ? Number(virtualTop) || el.scrollTop : el.scrollTop;
packages/components/listbox/src/virtualized-listbox.tsx (2)

108-108: Consider memoizing virtualScrollHeight.

The value should be memoized to prevent unnecessary recalculations during renders.

-  const virtualScrollHeight = rowVirtualizer.getTotalSize();
+  const virtualScrollHeight = useMemo(() => rowVirtualizer.getTotalSize(), [rowVirtualizer]);

176-180: Handle potential null values in data attributes.

The current implementation might expose undefined values in data attributes.

       {...getListProps()}
-      data-virtual-scroll-height={virtualScrollHeight}
-      data-virtual-scroll-top={parentRef?.current?.scrollTop}
+      data-virtual-scroll-height={virtualScrollHeight || 0}
+      data-virtual-scroll-top={parentRef?.current?.scrollTop || 0}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92281a6 and ae70bfc.

📒 Files selected for processing (3)
  • .changeset/ninety-flowers-teach.md (1 hunks)
  • packages/components/listbox/src/virtualized-listbox.tsx (3 hunks)
  • packages/hooks/use-data-scroll-overflow/src/index.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build
🔇 Additional comments (3)
packages/hooks/use-data-scroll-overflow/src/index.ts (2)

127-131: LGTM! Accurate overflow detection for virtualized listbox.

The changes correctly utilize the new scroll metrics to determine the overflow state, ensuring accurate detection in both virtualized and non-virtualized scenarios.


145-145: LGTM! Event capture phase ensures reliable scroll detection.

Using the capture phase for scroll event handling ensures reliable detection regardless of the actual scroll target in virtualized scenarios.

Also applies to: 164-164

.changeset/ninety-flowers-teach.md (1)

1-7: LGTM! Clear and concise changeset.

The changeset correctly identifies the affected packages and links to the relevant issue.

@wingkwong wingkwong added this to the v2.7.0 milestone Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants