Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit 43a6b34

Browse files
event propagation (#60)
* added event object parameter to setFocus and navigateByDirection added event object parameter to onBecameBlurred and onBecameFocused onBecameBlurred and onBecameFocused called synchronously with navigation * fix condition for setCurrentFocusedKey * changed specific event parameter to generic details object
1 parent 177f1c9 commit 43a6b34

File tree

6 files changed

+61
-76
lines changed

6 files changed

+61
-76
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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+
## [2.11.0]
8+
### Changed
9+
- `onBecameBlurred` and `onBecameFocused` are always invoked synchonously with focus change and not on componentDidUpdate
10+
### Added
11+
- `setFocus` and `navigateByDirection` accept an details object, this object is passed back on `onBecameBlurred` and `onBecameFocused` callbacks
12+
713
## [2.10.0]
814
### Changed
915
- Changed behaviour of `onBecameFocused`, now it's invoked also in case of stealFocus

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ const onPress = (direction, {prop1, prop2}) => {
306306
Callback function that is called when the item becomes focused directly or when any of the children components become focused. For example when you have nested tree of 5 focusable components, this callback will be called on every level of down-tree focus change.
307307

308308
Payload:
309-
Component layout object is passed as a first param. All the component props passed back to this callback. Useful to avoid creating callback functions during render. `x` and `y` are relative coordinates to parent DOM (**not the Focusable parent**) element. `left` and `top` are absolute coordinates on the screen.
309+
The first parameter is the component layout object. The second paramter is an object containing all the component props. The third parameter is a details object that was used when triggering the focus change, for example it contains the key event in case of arrow navigation. Useful to avoid creating callback functions during render. `x` and `y` are relative coordinates to parent DOM (**not the Focusable parent**) element. `left` and `top` are absolute coordinates on the screen.
310310

311311
```jsx
312-
const onFocused = ({width, height, x, y, top, left, node}, {prop1, prop2}) => {...};
312+
const onFocused = ({width, height, x, y, top, left, node}, {prop1, prop2}, {event, other}) => {...};
313313

314314
...
315315
<FocusableItem
@@ -324,10 +324,10 @@ const onFocused = ({width, height, x, y, top, left, node}, {prop1, prop2}) => {.
324324
Callback function that is called when the item loses focus or when all the children components lose focus. For example when you have nested tree of 5 focusable components, this callback will be called on every level of down-tree focus change.
325325

326326
Payload:
327-
Component layout object is passed as a first param. All the component props passed back to this callback. Useful to avoid creating callback functions during render. `x` and `y` are relative coordinates to parent DOM (**not the Focusable parent**) element. `left` and `top` are absolute coordinates on the screen.
327+
The first parameter is the component layout object. The second paramter is an object containing all the component props. The third parameter is a details object that was used when triggering the focus change, for example it contains the key event in case of arrow navigation. Useful to avoid creating callback functions during render. `x` and `y` are relative coordinates to parent DOM (**not the Focusable parent**) element. `left` and `top` are absolute coordinates on the screen.
328328

329329
```jsx
330-
const onBlur = ({width, height, x, y, top, left, node}, {prop1, prop2}) => {...};
330+
const onBlur = ({width, height, x, y, top, left, node}, {prop1, prop2}, {event, other}) => {...};
331331

332332
...
333333
<FocusableItem
@@ -362,19 +362,21 @@ This method sets the focus to another component (when focus key is passed as par
362362
This preemptive setting of the focus might be useful when rendering lists of data.
363363
You can assign focus key with the item index and set it to e.g. first item, then as soon as it will be rendered, that item will get focused.
364364
In Native mode this method is ignored (`noop`).
365+
This method accepts a second parameter as a details object that will be passed back to the `onBecameFocused` and `onBecameBlurred` callbacks.
365366

366367
```jsx
367368
setFocus(); // set focus to self
368-
setFocus('SOME_COMPONENT'); // set focus to another component if you know its focus key
369+
setFocus('SOME_COMPONENT', {event: keyEvent}); // set focus to another component if you know its focus key
369370
```
370371
### `navigateByDirection`: function
371372
Move the focus by direction, if you can't use buttons or focusing by key.
373+
This method accepts a second parameter as a details object that will be passed back to the `onBecameFocused` and `onBecameBlurred` callbacks.
372374

373375
```jsx
374376
navigateByDirection('left'); // The focus is moved to left
375377
navigateByDirection('right'); // The focus is moved to right
376378
navigateByDirection('up'); // The focus is moved to up
377-
navigateByDirection('down'); // The focus is moved to down
379+
navigateByDirection('down', {event: keyEvent}); // The focus is moved to down
378380
```
379381

380382

package-lock.json

Lines changed: 12 additions & 31 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/react-spatial-navigation",
3-
"version": "2.10.0",
3+
"version": "2.11.0",
44
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
55
"main": "dist/index.js",
66
"files": [

0 commit comments

Comments
 (0)