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

header links navigates to unknown page #16918

Closed
sag1v opened this issue Dec 6, 2021 · 10 comments
Closed

header links navigates to unknown page #16918

sag1v opened this issue Dec 6, 2021 · 10 comments

Comments

@sag1v
Copy link

sag1v commented Dec 6, 2021

After migrating from storybook v5 to v6 i have noticed a strange behavior regarding auto generated header links. After a quick search i found out why it is happening (but not sure how it worked until now with previous version).

Given this check in addons-links and the fact that all header anchors are actually an SVG elements, this check always evaluates to true (SVG element != HTML Element).

Because we return we are not triggering the preventDefault method of the event hence the link navigates to a different page (a none existing page).

Here is a screenshot of the gist of it

image

To Reproduce
I just created a fresh repo with npx sb@next repro and this bug shows up, i didn't change anything.

System
Environment Info:

System:
OS: macOS 11.6
Binaries:
Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node
Yarn: 1.22.5 - ~/.y/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v12.16.3/bin/npm
Browsers:
Chrome: 96.0.4664.55
Firefox: 85.0.2
Safari: 15.0

@cgreene-st
Copy link

I'm also seeing this in the latest version 6.4.0. Thanks for flagging the bug 👍

You can also see it live here by clicking the headings in the docs page - https://storybookjs.netlify.app/official-storybook/?path=/story/addons-docs-markdown-docs--page

@sag1v
Copy link
Author

sag1v commented Dec 7, 2021

I'm not sure whats the correct fix for this (should we test against SVG elements as well?) nor what could be a valid workaround until this is fixed.

Maybe @shilman can advise?

@sag1v
Copy link
Author

sag1v commented Dec 12, 2021

Hi @shilman Do you know if this is being worked on

@shilman
Copy link
Member

shilman commented Dec 12, 2021

Not yet. Please feel free to take a crack at it if it's blocking you!

@sag1v
Copy link
Author

sag1v commented Dec 27, 2021

OK I think i found the issue and a way to fix it. I'm not sure how good is this fix though.

It turns out that in mdx.tsx we are emitting NAVIGATE_URL with only the hash as the url value instead of appending it to the entire location.search string.

Because we are passing options.plain: true (somewhere, not sure exactly where) then we just call react-router's navigate with the hash. see router.tsx.

My suggested solution is to bring back generateHrefWithHash (who got deleted a year ago) with some modifications:

function generateHrefWithHash(hash: string): string {
  const location = window.parent ? window.parent.location : window.location;
  const href = `${location.search}${hash}`;
  return href;
}

function navigate(url: string) {
  const urlWithHash = generateHrefWithHash(url);
  addons.getChannel().emit(NAVIGATE_URL, urlWithHash);
}

I'm not sure this is the best solution or naming but anyway it seems to work fine.

@shilman @ndelangen WDYT?

PR - #17080

@shilman
Copy link
Member

shilman commented Jan 5, 2022

Jiminy cricket!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.5.0-alpha.9 containing PR #17134 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jan 5, 2022
@StanislavPonomarenko
Copy link

StanislavPonomarenko commented Jan 10, 2022

I've updated to prerelease.
I use tsx component for navigation via mdx file

<NavBanner
title="Colors"
desc="Primary, Secondary and Support colors"
links={[
{ path: '#identity-colors', text: 'Identity Colors' },
{ path: '#colors', text: 'Colors' },
]}

and in the same file I've used anchor link <a href="#identity-colors">Test</a>

In result links in component do not work but test anchor link works. https://www.screencast.com/t/JStshkoy
here is component code https://www.screencast.com/t/VEfhp3wy

@shilman
Copy link
Member

shilman commented Jan 10, 2022

w00t!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.4.10 containing PR #17134 that references this issue. Upgrade today to the @latest NPM tag to try it out!

npx sb upgrade

@StanislavPonomarenko
Copy link

Thank you for your comment but issue is still there after update to 6.4.10
here is a short video file https://www.screencast.com/t/PFo1KJ44covD

so anchor links in mdx file works, but if I use custom component with such links in mdx file those links won't work.
If you need some info from my side I would give it to you.

@shilman
Copy link
Member

shilman commented Jan 10, 2022

@StanislavPonomarenko we don't support links from custom components. please open a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants