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

SkipLink does not work if intended target header is rerendered #256

Open
jacknewberry opened this issue Oct 3, 2024 · 1 comment
Open

Comments

@jacknewberry
Copy link

jacknewberry commented Oct 3, 2024

We are trying to use SkipLink with its default behaviour, but it does not seem to work unless it is rendered after the target is rendered.

The intention is to include SkipLink in a layout that will wrap every page. We want the 'select the first h1' behaviour rather than customising the target.

This does not seem to work:

function SomeComponent() {
  // does something that causes this component to re-render
  
  return <h1>Get selected</h1>
}

function App() {
  return (<div>
    <SkipLink disableDefaultBehaviour />
    <SomeComponent/>
  </div>)
}

I believe that this does not work because SkipLink only searches for a target when it renders; rather than searching in the onClick handler.

I am suggesting moving the call to getFirstHeadingElement(); inside focusTarget.

I am unsure if this is related this minimalist issue: #242.

It might also be possible to simplify focusing the target by using element.focus() rather than adding tabIndex. This would make it easier to remove the useEffect.

Does this seem like a bug to you? We could consider drafting a PR.

@jacknewberry
Copy link
Author

fyi @jonnyroutley

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

No branches or pull requests

1 participant