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

Commit

Permalink
Added onEnter release event (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrumo authored Jul 7, 2021
1 parent c4ad505 commit 9868d65
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.12.8]
### Added
- onEnterRelease event triggered when the Enter is released from the focused item

## [2.12.7]
### Added
- SSR support, additional checks for `window` object to avoid errors on SSR environments.
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,22 @@ String that is used as a component focus key. Should be **unique**, otherwise it
### `onEnterPress`: function
Callback function that is called when the item is currently focused and Enter (OK) key is pressed.

### `onEnterRelease`: function
Callback function that is called when the item is currently focused and Enter (OK) key is released.

Payload:
1. All the props passed to HOC is passed back to this callback. Useful to avoid creating callback functions during render.
2. [Details](#keydetails-object) - info about pressed keys

```jsx
const onPress = ({prop1, prop2}, details) => {...};

const onRelease = ({prop1, prop2}) => {...};
...
<FocusableItem
prop1={111}
prop2={222}
onEnterPress={onPress}
onEnterRelease={onRelease}
/>
...
```
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noriginmedia/react-spatial-navigation",
"version": "2.12.7",
"version": "2.12.8",
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
"main": "dist/index.js",
"files": [
Expand Down
26 changes: 26 additions & 0 deletions src/spatialNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ class SpatialNavigation {
if (this.throttle && !this.throttleKeypresses) {
this.keyDownEventListener.cancel();
}

if (eventType === KEY_ENTER && this.focusKey) {
this.onEnterRelease();
}
};

window.addEventListener('keyup', this.keyUpEventListener);
Expand Down Expand Up @@ -483,6 +487,26 @@ class SpatialNavigation {
component.onEnterPressHandler && component.onEnterPressHandler(details);
}

onEnterRelease() {
const component = this.focusableComponents[this.focusKey];

/* Guard against last-focused component being unmounted at time of onEnterRelease (e.g due to UI fading out) */
if (!component) {
this.log('onEnterRelease', 'noComponent');

return;
}

/* Suppress onEnterRelease if the last-focused item happens to lose its 'focused' status. */
if (!component.focusable) {
this.log('onEnterRelease', 'componentNotFocusable');

return;
}

component.onEnterReleaseHandler && component.onEnterReleaseHandler();
}

onArrowPress(...args) {
const component = this.focusableComponents[this.focusKey];

Expand Down Expand Up @@ -722,6 +746,7 @@ class SpatialNavigation {
node,
parentFocusKey,
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onBecameFocusedHandler,
onBecameBlurredHandler,
Expand All @@ -739,6 +764,7 @@ class SpatialNavigation {
node,
parentFocusKey,
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onBecameFocusedHandler,
onBecameBlurredHandler,
Expand Down
9 changes: 9 additions & 0 deletions src/withFocusable.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const withFocusable = ({
}) => (details) => {
onEnterPress(rest, details);
},
onEnterReleaseHandler: ({
onEnterRelease = noop,
...rest
}) => () => {
onEnterRelease(rest);
},
onArrowPressHandler: ({
onArrowPress = noop,
...rest
Expand Down Expand Up @@ -106,6 +112,7 @@ const withFocusable = ({
preferredChildFocusKey,
forgetLastFocusedChild = false,
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onBecameFocusedHandler,
onBecameBlurredHandler,
Expand All @@ -125,6 +132,7 @@ const withFocusable = ({
parentFocusKey,
preferredChildFocusKey,
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onBecameFocusedHandler,
onBecameBlurredHandler,
Expand Down Expand Up @@ -169,6 +177,7 @@ const withFocusable = ({
'onBecameFocusedHandler',
'onBecameBlurredHandler',
'onEnterPressHandler',
'onEnterReleaseHandler',
'onArrowPressHandler',
'onUpdateFocus',
'onUpdateHasFocusedChild',
Expand Down

0 comments on commit 9868d65

Please sign in to comment.