Skip to content

Commit 4e9e4ec

Browse files
authored
call focus() on the DOM node when a focusable component is being focused (#77)
1 parent 3bf3556 commit 4e9e4ec

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [1.3.0]
8+
## Added
9+
- new `init` config option `shouldFocusDOMNode` that focuses the underlying _accessible_ DOM node too.
10+
711
# [1.2.0]
812
## Added
913
- new `init` config option `useGetBoundingClientRect` that affects the measurements of sizes and coordinates.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ For example if you have an element with `translateX(50)`, the X position with th
248248
The choice depends on how often you are using transforms, and whether you want it to result into coordinates shift or not.
249249
Sometimes you would want element to be *visually* translated, but its coordinates to be calculated as it was before the translation.
250250

251+
##### `shouldFocusDOMNode`: boolean (default: false)
252+
This flag makes the underlying _accessible_ DOM node to become focused as well. This is useful for [_accessible_](https://developer.mozilla.org/en-US/docs/Web/Accessibility) web applications.
253+
Note that it is the developer's responsibility to make the elements accessible! There are [many resources](https://www.google.com/search?q=web+accessibility) online on the subject. [HTML Semantics and Accessibility Cheat Sheet](https://webaim.org/resources/htmlcheatsheet/) is perhaps a good start,
254+
as it dives directly into the various html tags and how it complies with accessibility. Non-accessible tags like `<div>` needs to have the [tabindex](https://developer.mozilla.org/en-US/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets) attribute set.
255+
Also consider `role` and `aria-label` attributes. But that depends on the application.
256+
257+
The flag is ignored if `nativeMode` is set.
258+
251259
### `setKeyMap`
252260
Method to set custom key codes. I.e. when the device key codes differ from a standard browser arrow key codes.
253261
```jsx

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@noriginmedia/norigin-spatial-navigation",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "React hooks based Spatial Navigation solution",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/SpatialNavigation.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class SpatialNavigationService {
163163
*/
164164
private focusKey: string;
165165

166+
private shouldFocusDOMNode: boolean;
167+
166168
/**
167169
* This collection contains focus keys of the elements that are having a child focused
168170
* Might be handy for styling of certain parent components if their child is focused.
@@ -526,6 +528,7 @@ class SpatialNavigationService {
526528
this.throttle = 0;
527529
this.throttleKeypresses = false;
528530
this.useGetBoundingClientRect = false;
531+
this.shouldFocusDOMNode = false;
529532

530533
this.pressedKeys = {};
531534

@@ -563,13 +566,15 @@ class SpatialNavigationService {
563566
nativeMode = false,
564567
throttle: throttleParam = 0,
565568
throttleKeypresses = false,
566-
useGetBoundingClientRect = false
569+
useGetBoundingClientRect = false,
570+
shouldFocusDOMNode = false
567571
} = {}) {
568572
if (!this.enabled) {
569573
this.enabled = true;
570574
this.nativeMode = nativeMode;
571575
this.throttleKeypresses = throttleKeypresses;
572576
this.useGetBoundingClientRect = useGetBoundingClientRect;
577+
this.shouldFocusDOMNode = shouldFocusDOMNode && !nativeMode;
573578

574579
this.debug = debug;
575580

@@ -1235,6 +1240,10 @@ class SpatialNavigationService {
12351240
if (this.isFocusableComponent(this.focusKey)) {
12361241
const newComponent = this.focusableComponents[this.focusKey];
12371242

1243+
if (this.shouldFocusDOMNode && newComponent.node) {
1244+
newComponent.node.focus();
1245+
}
1246+
12381247
newComponent.onUpdateFocus(true);
12391248
newComponent.onFocus(
12401249
this.getNodeLayoutByFocusKey(this.focusKey),

0 commit comments

Comments
 (0)