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: navbar state change #4406

Merged
merged 1 commit into from
Dec 22, 2024
Merged

fix: navbar state change #4406

merged 1 commit into from
Dec 22, 2024

Conversation

macci001
Copy link
Contributor

@macci001 macci001 commented Dec 19, 2024

Closes #4345

📝 Description

The PR fixes the Navbar unable to open issue.
Root cause:

  • It issue emerged when we added usePreventScroll and removed react-rmeove-scroll.
  • The usePreventScroll internally adds the overflow-hidden and paddingRight of {scrollBarWidth} styles to prevent the scrolling behaviour. Due to this the width of the navbar base would be changed. Now, this causes the useResizeObserver to trigger which in-turn closes the Navbar menu.

The pr adds the changes to useResizeObserver callback so that it does not close the Navbar Menu.

Current Behaviour

WhatsApp.Video.2024-12-19.at.19.45.50.1.mp4

New Behaviour

WhatsApp.Video.2024-12-19.at.19.45.50.mp4

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

Summary by CodeRabbit

  • Bug Fixes

    • Resolved an issue preventing the navbar from opening.
  • Improvements

    • Enhanced responsiveness and behavior of the navbar during window resizing and scrolling.

Copy link

linear bot commented Dec 19, 2024

Copy link

changeset-bot bot commented Dec 19, 2024

🦋 Changeset detected

Latest commit: 893681d

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

This PR includes changesets to release 2 packages
Name Type
@nextui-org/navbar Patch
@nextui-org/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 Dec 19, 2024

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

Name Status Preview Comments Updated (UTC)
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 19, 2024 0:52am
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 19, 2024 0:52am

Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

This pull request addresses a bug in the NextUI navbar component that prevented the mobile menu from opening correctly. The changes are focused on the useNavbar hook in the packages/components/navbar/src/use-navbar.ts file. The modifications introduce a new scrollWidth calculation and update the resize observer logic to improve the navbar's responsiveness and prevent unnecessary state updates during window resizing.

Changes

File Change Summary
.changeset/smart-oranges-peel.md Patch for @nextui-org/navbar package
packages/components/navbar/src/use-navbar.ts - Added scrollWidth calculation
- Updated useResizeObserver callback to prevent unnecessary updates
- Modified useEffect to ensure correct initial width rendering
- Updated getBaseProps to dynamically set --navbar-height CSS variable

Assessment against linked issues

Objective Addressed Explanation
Mobile Menu Opening [#4345]
Prevent Menu Flickering [#4345]

Possibly related PRs

Suggested labels

👀 Status: In Review

Suggested reviewers

  • wingkwong
  • jrgarciadev

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 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.

@macci001
Copy link
Contributor Author

macci001 commented Dec 19, 2024

@jrgarciadev @wingkwong
while testing this PR, the first click on the Navbar toggle might not work, it would work fine from the second click onwards.

This occurs because in order to test we would manually change the width of the device, so updateWidth would be called for the first click.
This was the exact same behaviour before the bug was introduced, and I pretty much agree to this behaviour. Hence, avoided extra unwanted patches here.

@macci001 macci001 marked this pull request as ready for review December 19, 2024 12:57
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 (1)
.changeset/smart-oranges-peel.md (1)

1-6: Enhance the changeset description with more details

The changeset description could be more informative by including:

  • The root cause (usePreventScroll width calculation issue)
  • The specific fix implemented (scroll width handling)
  • Impact on existing behavior

Consider updating the description to:

---
"@nextui-org/navbar": patch
---

-Resolving the issue preventing the navbar from opening(#4345)
+Fix navbar menu closing unexpectedly due to usePreventScroll width calculations (#4345)
+
+- Prevents unnecessary width updates when only scroll width changes
+- Maintains existing behavior for initial click functionality
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d152b36 and 893681d.

📒 Files selected for processing (2)
  • .changeset/smart-oranges-peel.md (1 hunks)
  • packages/components/navbar/src/use-navbar.ts (1 hunks)
🔇 Additional comments (2)
packages/components/navbar/src/use-navbar.ts (2)

164-168: Note: Existing first-click behavior preserved as intended

As mentioned in the PR comments, the implementation intentionally maintains the existing behavior where the first click might require a width calculation. This is the correct approach as:

  1. It's not a regression but existing behavior
  2. The fix focuses solely on resolving the unintended width calculation issue

164-168: LGTM! Fix addresses the root cause effectively

The implementation correctly handles the width calculation issue by:

  1. Accounting for scroll width differences when usePreventScroll adds padding
  2. Preventing unnecessary navbar menu closures when only the scroll width changes

This fix directly addresses the issue described in #4345 where usePreventScroll was causing unexpected navbar closures.

Let's verify the scroll width calculation works across different scenarios:

✅ Verification successful

No other instances require similar scroll width fixes

The verification shows that:

  1. The scroll width calculation is unique to the navbar component and isn't used elsewhere in the codebase
  2. While usePreventScroll is used in other components (popover and aria-modal-overlay), they don't require width adjustments as they:
    • Don't rely on resize observations
    • Don't have width-dependent layout behaviors like the navbar

The current fix is correctly scoped to the navbar component where the issue manifests.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other scroll width calculations in the codebase
# to ensure consistent handling

# Look for other scroll width calculations
rg -A 2 "innerWidth.*clientWidth|clientWidth.*innerWidth"

# Check for other usePreventScroll usage that might need similar fixes
ast-grep --pattern 'usePreventScroll({$$$})'

Length of output: 1111

@jrgarciadev
Copy link
Member

@macci001 could you please add the before behavior video vs the new behavior video ?

@wingkwong wingkwong added this to the v2.6.9 milestone Dec 19, 2024
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.

[BUG] - Navbar Mobile Menu Can Not Be Opened
3 participants