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

Make it possible to opt out of focus handling #90

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ To recreate this `react-router-hash-link` does the following:
- For focusable elements, it calls `element.focus()` and leaves focus on the target element.

Note that you may find it useful to leave focus on non-interactive elements (by adding a `tabindex` of `-1`) to augment the navigation action with a visual focus indicator.

### `preventFocusHandling: boolean`

- Optionally prevent the default focus handling
37 changes: 22 additions & 15 deletions src/HashLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ let hashFragment = '';
let observer = null;
let asyncTimerId = null;
let scrollFunction = null;
let preventFocusHandling = false;

function reset() {
hashFragment = '';
preventFocusHandling = false;
if (observer !== null) observer.disconnect();
if (asyncTimerId !== null) {
window.clearTimeout(asyncTimerId);
Expand All @@ -21,7 +23,8 @@ function isInteractiveElement(element) {
const linkTags = ['A', 'AREA'];
return (
(formTags.includes(element.tagName) && !element.hasAttribute('disabled')) ||
(linkTags.includes(element.tagName) && element.hasAttribute('href'))
(linkTags.includes(element.tagName) && element.hasAttribute('href')) ||
element.isContentEditable
);
}

Expand All @@ -47,19 +50,21 @@ function getElAndScroll() {
if (element !== null) {
scrollFunction(element);

// update focus to where the page is scrolled to
// unfortunately this doesn't work in safari (desktop and iOS) when blur() is called
let originalTabIndex = element.getAttribute('tabindex');
if (originalTabIndex === null && !isInteractiveElement(element)) {
element.setAttribute('tabindex', -1);
}
element.focus({ preventScroll: true });
if (originalTabIndex === null && !isInteractiveElement(element)) {
// for some reason calling blur() in safari resets the focus region to where it was previously,
// if blur() is not called it works in safari, but then are stuck with default focus styles
// on an element that otherwise might never had focus styles applied, so not an option
element.blur();
element.removeAttribute('tabindex');
if (!preventFocusHandling) {
// update focus to where the page is scrolled to
// unfortunately this doesn't work in safari (desktop and iOS) when blur() is called
let originalTabIndex = element.getAttribute('tabindex');
if (originalTabIndex === null && !isInteractiveElement(element)) {
element.setAttribute('tabindex', -1);
}
element.focus({ preventScroll: true });
if (originalTabIndex === null && !isInteractiveElement(element)) {
// for some reason calling blur() in safari resets the focus region to where it was previously,
// if blur() is not called it works in safari, but then are stuck with default focus styles
// on an element that otherwise might never had focus styles applied, so not an option
element.blur();
element.removeAttribute('tabindex');
}
}

reset();
Expand Down Expand Up @@ -109,6 +114,7 @@ export function genericHashLink(As) {
function handleClick(e) {
reset();
hashFragment = props.elementId ? `#${props.elementId}` : linkHash;
preventFocusHandling = !!props.preventFocusHandling;
if (props.onClick) props.onClick(e);
if (
hashFragment !== '' &&
Expand All @@ -128,7 +134,7 @@ export function genericHashLink(As) {
hashLinkScroll(props.timeout);
}
}
const { scroll, smooth, timeout, elementId, ...filteredProps } = props;
const { scroll, smooth, timeout, elementId, preventFocusHandling, ...filteredProps } = props;
return (
<As {...passDownProps} {...filteredProps} onClick={handleClick} ref={ref}>
{props.children}
Expand All @@ -152,6 +158,7 @@ if (process.env.NODE_ENV !== 'production') {
timeout: PropTypes.number,
elementId: PropTypes.string,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
preventFocusHandling: PropTypes.bool,
};

HashLink.propTypes = propTypes;
Expand Down