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

Anchor tags within preview panel not working #5527

Open
benkeen opened this issue Feb 8, 2019 · 12 comments
Open

Anchor tags within preview panel not working #5527

benkeen opened this issue Feb 8, 2019 · 12 comments
Assignees

Comments

@benkeen
Copy link

benkeen commented Feb 8, 2019

Apologies if this isn't a bug or there's standard way to solve it, I couldn't find it.

Describe the bug
An anchor tag within a page in the preview panel iframe doesn't take you to that position within the page, but reloads the top iframe with the page content.

To Reproduce
Steps to reproduce the behavior:

  • Create a story within the preview panel, and add a link like About
  • Add a target for that link further down the page<a id="about"></a>

Expected behavior
When clicking the "About" link it would take you to the target (i.e. scroll down to the appropriate spot).

Result: it reloads the page in to the top frame.

@stale
Copy link

stale bot commented Mar 1, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Mar 1, 2019
@stale stale bot removed the inactive label Mar 2, 2019
@shilman
Copy link
Member

shilman commented Mar 2, 2019

@benkeen What version of storybook?

@benkeen
Copy link
Author

benkeen commented Mar 2, 2019

Thx! 4.1.11.

@stale
Copy link

stale bot commented Mar 23, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Mar 23, 2019
@shilman
Copy link
Member

shilman commented Mar 23, 2019

Is this still broken in v5?

@stale stale bot removed the inactive label Mar 23, 2019
@stale
Copy link

stale bot commented Apr 13, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Apr 13, 2019
@stale
Copy link

stale bot commented May 13, 2019

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

@mourad1081
Copy link

Same issue here.

@renatodeleao
Copy link

renatodeleao commented May 12, 2020

Still experiencing this at v5.3.18, was this fixed or is there any workaround? ☮️

@xurxe
Copy link

xurxe commented Jun 5, 2020

I'm also experiencing this and would love a fix or a workaround 😄

@shilman shilman reopened this Jun 6, 2020
@stale stale bot removed the inactive label Jun 6, 2020
@xurxe
Copy link

xurxe commented Jul 6, 2020

We came up with a workaround for now, which involves:

  1. Using event.preventDefault() to prevent reloading of the page.
  2. Using window.scrollTo() to scroll to the destination element.
  3. Using element.focus() to move focus to the destination element. If the destination element is not focusable, you can make it focusable with tabindex="-1".

Below is a basic version of our React implementation. No idea if this is useful at all towards an actual fix (as I don't know exactly what's causing the bug), but hopefully it's a decent workaround in the meantime! Let me know if you spot any issues :D

import React, { createContext, useRef } from 'react';

export const Anchor = ({ href, toRef, ...rest }) => {
  const go = event => {
    /* Prevent the default, which is, as per the bug, refreshing
    the window to show the iframe alone: */
    event.preventDefault();

    /* Scroll to the destination element: */
    window.scrollTo(0, toRef.current.offsetTop);

    /* Move focus to the destination element, which had to be 
    made focusable with tabindex="-1". Using outline: none on 
    the destination element is ok, since it's not actually a 
    "focusable element", just something we have to make
    focusable in order for this this hack to work for users of 
    assistive technologies (such as keyboards and screen readers): */
    toRef.current.focus();
  };

  return <a href={href} onClick={go} {...rest}></Link>;
};

export const Example = () => {
  /* Create a reference for each element linked to */
  const mainRef = useRef(null);

  /* Put references in the context */
  const RefContext = createContext({
    mainRef,
  });

  return (
    /* Wrap context provider at the top level */
    <RefContext.Provider>
      <nav>
        {/* When the Storybook bug is fixed, use href="#id" prop. 
        In the meantime, use toRef={ref} prop. */}
        <Anchor href="#main" toRef={mainRef}>
          Go to main
        </Anchor>
        <div>
          I'm a navigation bar!
        </div>
        <a href="https://wunder.io/"> Link 1 </a>
        <a href="https://wunder.io/"> Link 2 </a>
        <a href="https://wunder.io/"> Link 3 </a>
      </nav>

      {/* Give an ID to use when the Storybook bug is fixed. 
      In the meantime, as a workaround, give a ref and also 
      make focusable */}
      <main id="main" ref={mainRef} tabIndex="-1">
        <div>I'm the main!</div>
        <button> Button 1 </button>
        <button> Button 2 </button>
        <button> Button 3 </button>
      </main>
    </RefContext.Provider>
  );
};

export default {
  title: 'Example',
};

@MichaelArestad MichaelArestad self-assigned this Jul 26, 2021
@reintroducing
Copy link

Just ran into this and looked for a solution which was referenced in another issue and works great for anyone who lands here from a Google search: #15934 (comment)

@shilman shilman removed the todo label Jun 20, 2023
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

7 participants