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: routerized links #4628

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

Conversation

jprosevear
Copy link

@jprosevear jprosevear commented Jan 23, 2025

Closes #4624

📝 Description

Use the routerized href if available for both the onClick event and the href prop on the element

⛳️ Current behavior (updates)

Currently the original href is used (ignoring the application of useHref) on the onClick and so navigate() for react-router acts on the wrong href and fails for relative links. So if you are at /blogs already and you put href="14" it will try to go to /14 instead of /blogs/14 and the href is decorate with href="14"

🚀 New behavior

navigate() for react-router works, going to /blogs/14 and the element is decorated correctly (/blogs/14) .

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

Possible, but I don't think it worked before with relative links in RR; absolute links should still work.

📝 Additional Information

Summary by CodeRabbit

  • Improvements

    • Enhanced link component functionality with more robust href handling
    • Added default values for autoFocus, isExternal, and showAnchorIcon properties
    • Improved routing and navigation logic for link components
  • Accessibility

    • Updated link component to better support external links and routing behavior

Copy link

changeset-bot bot commented Jan 23, 2025

⚠️ No Changeset found

Latest commit: eff03db

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

vercel bot commented Jan 23, 2025

@jprosevear is attempting to deploy a commit to the HeroUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Walkthrough

The pull request addresses a bug in the link component's handling of the href property, specifically focusing on how router-based links are processed. The changes modify the useLink and useAriaLink functions to ensure proper preservation of the href attribute from router-related props. The modifications aim to improve the link navigation behavior, particularly for relative links in React Router environments.

Changes

File Change Summary
packages/components/link/src/use-link.ts - Added href to Props interface
- Included href in getLinkProps function
- Added default values for autoFocus, isExternal, and showAnchorIcon
packages/hooks/use-aria-link/src/index.ts - Updated navigation logic to use routerLinkProps.href
- Modified onClick event handler to reference router link properties

Assessment against linked issues

Objective Addressed Explanation
Fix link navigation for relative links [#4624]
Preserve router-generated href

The changes directly address the issue by ensuring that router-generated href values are not overwritten during link prop processing.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cddba82 and eff03db.

📒 Files selected for processing (2)
  • packages/components/link/src/use-link.ts (3 hunks)
  • packages/hooks/use-aria-link/src/index.ts (1 hunks)
🔇 Additional comments (5)
packages/components/link/src/use-link.ts (3)

55-58: LGTM: Default values are properly initialized.

The addition of href to props destructuring along with proper default values for isExternal, showAnchorIcon, and autoFocus improves the component's predictability.


Line range hint 77-84: LGTM: Href is properly forwarded to useAriaLink.

The href is correctly passed to useAriaLink, allowing it to process router-based links appropriately.


115-116: LGTM: Routerized href is preserved.

The comment clearly explains the intention to preserve the routerized href, and the implementation correctly merges it into the final link props.

packages/hooks/use-aria-link/src/index.ts (2)

121-124: LGTM: Navigation logic now correctly handles relative links.

The changes properly fix the relative link navigation issue by:

  1. Using routerLinkProps.href in the navigation condition
  2. Passing the processed href to router.open

This resolves the issue where clicking a relative link (e.g., "14") on /blogs would navigate to /14 instead of /blogs/14.


121-124: Verify the impact on absolute URLs.

While the changes fix relative link handling, let's verify that absolute URLs continue to work as expected.

✅ Verification successful

Absolute URLs are handled correctly

The implementation preserves native browser behavior for absolute URLs while properly handling relative URLs through the router. External links will work as expected without any special handling needed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for absolute URL usage in link components
rg -l 'href=["'\'']https?://' | while read -r file; do
  echo "=== $file ==="
  rg -A 2 'href=["'\'']https?://' "$file"
done

Length of output: 13514


Script:

#!/bin/bash
# Find router.open implementation
ast-grep --pattern 'open($$$) {
  $$$
}'

# Also search for any URL parsing/validation
rg -A 5 'new URL|URL\.parse|isAbsolute|^[^/]*function.*[Uu][Rr][Ll]'

Length of output: 113


Script:

#!/bin/bash
# Find router-related files
fd router

# Search for router class/interface definitions and URL handling
rg -A 5 "class.*Router|interface.*Router|function.*[Rr]outer"

# Search for URL handling in router files
rg -A 5 "href|url|path" $(fd -t f router)

Length of output: 5850


Script:

#!/bin/bash
# Search in use-aria-link package
cat packages/hooks/use-aria-link/src/index.ts

# Also search for URL handling utilities
fd -t f -e ts -e js url-utils

Length of output: 4175

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

@@ -110,6 +112,8 @@ export function useLink(originalProps: UseLinkProps) {
"data-focus": dataAttr(isFocused),
"data-disabled": dataAttr(originalProps.isDisabled),
"data-focus-visible": dataAttr(isFocusVisible),
// The `href` prop may be routerized by useAriaLink, so we may merge over top of it
href,
Copy link
Author

Choose a reason for hiding this comment

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

This makes sure that the href on the is set properly

) {
e.preventDefault();
router.open(e.currentTarget, e, props.href, props.routerOptions);
router.open(e.currentTarget, e, routerLinkProps.href, props.routerOptions);
Copy link
Author

Choose a reason for hiding this comment

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

This makes sure that we use the useHref'ed href for the navigation

@jprosevear jprosevear changed the title Fix/routerized links (fix): routerized links Jan 24, 2025
@jprosevear jprosevear changed the title (fix): routerized links fix: routerized links Jan 24, 2025
@wingkwong wingkwong self-assigned this Jan 24, 2025
@wingkwong wingkwong modified the milestones: v2.6.15, v2.7.0 Jan 25, 2025
@wingkwong
Copy link
Member

may revisit this PR again after #4611

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] - useLink overwrites router-ized prop
2 participants