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

Draft
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 improved href handling.
    • Added default values for autoFocus, isExternal, and showAnchorIcon properties.
    • Refined 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: 9f13a25

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 [#4624]

📜 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 eff03db and 9f13a25.

📒 Files selected for processing (2)
  • packages/components/link/src/use-link.ts (3 hunks)
  • packages/hooks/use-aria-link/src/index.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/hooks/use-aria-link/src/index.ts
  • packages/components/link/src/use-link.ts

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

@jprosevear
Copy link
Author

may revisit this PR again after #4611

Is the hope that the upgrade will fix this or something else?

@wingkwong
Copy link
Member

@jprosevear RA recently bumped their version and we are still using the second last latest version. The version diff would cause some routing issues. So I wanna double check again after syncing the versions.

@jprosevear
Copy link
Author

@jprosevear RA recently bumped their version and we are still using the second last latest version. The version diff would cause some routing issues. So I wanna double check again after syncing the versions.

Ok, but based on my patch and investigation the issue is that RA was returning the right info already, it just wasn't being used correctly in Hero.

@jprosevear jprosevear force-pushed the fix/routerized-links branch from eff03db to 9f13a25 Compare January 30, 2025 13:19
@jprosevear
Copy link
Author

Rebased.

@wingkwong
Copy link
Member

@jprosevear do you have a simple sandbox that can reproduce the issue? I need it to test the before & after.

@jprosevear
Copy link
Author

@jprosevear do you have a simple sandbox that can reproduce the issue? I need it to test the before & after.

I do not, unless there is a good starting point for heroui / RR (or Tanstack Router, as per Discord I think it has the same problem) its probably a couple hours of work at least is my guess which is possible but painful obviously.

What is the right thing to do from a testing infrastructure standpoint long term?

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

FYI, our use-aria-link mirrors useLink from @react-aria. We modified the deprecated warning part only. I think navigating to routerLinkProps.href isn't expected since it will cover the current path.

@@ -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
Member

Choose a reason for hiding this comment

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

href from linkProps in L117 will override this href in L116 so all changes done in this file don't really do anything.

p.s. href from linkProps in L117 is routerized, i.e. 1 -> /blogs/1, which is expected. What's the reason to put prop.href here?

Copy link
Author

Choose a reason for hiding this comment

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

In case you are not using a routerized link you still need the original href I thought

Copy link
Member

Choose a reason for hiding this comment

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

href will be passed to useLinkProps, and eventually it will be in linkProps anyway.

Copy link
Author

@jprosevear jprosevear Jan 31, 2025

Choose a reason for hiding this comment

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

It still needs to be extracted from otherProps though so mergeProps doesn't overwrite the linkProps href with the otherProps href.

Copy link
Author

Choose a reason for hiding this comment

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

the key for me is that useLinkProps can change href

@@ -73,6 +74,7 @@ export function useLink(originalProps: UseLinkProps) {
const {linkProps} = useAriaLink(
{
...otherProps,
href,
Copy link
Member

Choose a reason for hiding this comment

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

href originally is in otherProps

Copy link
Author

Choose a reason for hiding this comment

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

I know; see below.

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

Choose a reason for hiding this comment

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

by doing so, one couldn't do any absolute paths, e.g. says I'm at /blogs and if i set href to /foo/1, I will be navigated to /blogs//foo/1 instead of /foo/1.

Copy link
Author

Choose a reason for hiding this comment

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

Why not? useHref from RR should produce /foo/1 in that case. I updated https://stackblitz.com/edit/wfdcu9f2 with that case.

Copy link
Member

Choose a reason for hiding this comment

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

RRLink can navigate to /foo/1correctly. With your PR change, I got //blogs/foo/1. Note that even I'm at /blogs originally, it would still add the slash causing // at the end. You may link those components locally to play around.

Copy link
Author

Choose a reason for hiding this comment

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

https://github.com/adobe/react-spectrum/blob/016590a4afc585eea18d5a12853c9d1e2d82a19c/packages/%40react-aria/utils/src/openLink.tsx#L174 for reference - unless the useLinkProps href is used, why call it it at all? Also, if the href is something like "https://www.heroui.com/" i believe the router.open call won't be made on it, a regular link will happen.

Copy link
Author

Choose a reason for hiding this comment

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

RRLink can navigate to /foo/1correctly. With your PR change, I got //blogs/foo/1. Note that even I'm at /blogs originally, it would still add the slash causing // at the end. You may link those components locally to play around.

Can you give more details? All this patch really does is ensure the href produced by useLinkProps from react-aria is actually used in Hero (at least that was my intention)

@jprosevear
Copy link
Author

@jprosevear do you have a simple sandbox that can reproduce the issue? I need it to test the before & after.

I do not, unless there is a good starting point for heroui / RR (or Tanstack Router, as per Discord I think it has the same problem) its probably a couple hours of work at least is my guess which is possible but painful obviously.

What is the right thing to do from a testing infrastructure standpoint long term?

https://stackblitz.com/edit/wfdcu9f2

@wingkwong
Copy link
Member

UPD: was using the outdated stackblitz for my initial review. will take a look again.

@jprosevear jprosevear marked this pull request as draft February 10, 2025 15:08
@jrgarciadev jrgarciadev modified the milestones: v2.7.0, v2.7.1 Feb 11, 2025
@jprosevear
Copy link
Author

Upstream discussion: adobe/react-spectrum#7695

@jprosevear
Copy link
Author

@jrgarciadev I would not include this in 2.7.x until there is some resolution upstream

@wingkwong wingkwong removed this from the v2.7.3 milestone Feb 19, 2025
@wingkwong wingkwong removed their assignment Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] - useLink overwrites router-ized prop
3 participants