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 active link highlighting when using custom href formatting #444

Merged
merged 1 commit into from
May 10, 2024

Conversation

neoeno
Copy link
Contributor

@neoeno neoeno commented May 9, 2024

See #417 (comment) for details on the problem.

Previously when we formatted the href attribute (for example, to prefix a # when using useHashLocation) this would make it not match the current path, which would not have the hash prefix.

This patch preserves the target (href, or to) of the Link component and then compares this value to the active path. This should then match.

I have also altered some variable names to improve clarity given we now have four different various 'paths' in this component.

I have tested this: in this repo, in my previous reproduction, and in the application I am building — so I feel reasonably confident it fixes my issue at least :)

Fixes #417

Copy link

stackblitz bot commented May 9, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cf9ba61) to head (5e7c06c).

Additional details and impacted files
@@            Coverage Diff            @@
##                v3      #444   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines            1         1           
=========================================
  Hits             1         1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -236,7 +236,7 @@ export const Link = forwardRef((props, ref) => {
onClick,
href,
// `className` can be a function to apply the class if this link is active
className: cls?.call ? cls(path === href) : cls,
className: cls?.call ? cls(currentPath === (targetPath)) : cls,
Copy link
Owner

Choose a reason for hiding this comment

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

Everything looks good! One small question: do we need extra braces around targetPath here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely not :) No idea what I was thinking. Thanks for the catch — fix pushed.

Copy link
Owner

@molefrog molefrog left a comment

Choose a reason for hiding this comment

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

Looks and works well! Left one minor comment, but other than that it is ready to be merged.

Previously when we formatted the `href` attribute (for example,
to prefix a `#` when using useHashLocation) this would make it
not match the current path, which would not have the hash prefix.

This patch preserves the target (href, or to) of the Link component
and then compares this value to the active path. This should then
match.

I have also altered some variable names to improve clarity given
we now have four different various 'paths' in this component.

Fixes molefrog#417
@molefrog molefrog merged commit 6677c50 into molefrog:v3 May 10, 2024
4 checks passed
@molefrog
Copy link
Owner

Released in v3.1.3

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] useHashLocation not syncing with state
2 participants