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

Commit 71daeda

Browse files
enrico-bardelliEnrico Bardelli
andauthored
updateLayout is only called for components affected by navigation (#114)
Co-authored-by: Enrico Bardelli <enrico.bardelli@skillbill.it>
1 parent 9868d65 commit 71daeda

4 files changed

Lines changed: 17 additions & 5 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+
## [2.12.9]
8+
### Changed
9+
- performance optimization: updateLayout is only called for components affected by navigation
10+
711
## [2.12.8]
812
### Added
913
- onEnterRelease event triggered when the Enter is released from the focused item

package-lock.json

Lines changed: 1 addition & 1 deletion
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.12.8",
3+
"version": "2.12.9",
44
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
55
"main": "dist/index.js",
66
"files": [

src/spatialNavigation.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@ class SpatialNavigation {
569569
this.log('smartNavigate', 'this.focusKey', this.focusKey);
570570

571571
if (!this.nativeMode && !fromParentFocusKey) {
572-
this.updateAllLayouts();
572+
forOwn(this.focusableComponents, (component) => {
573+
component.layoutUpdated = false;
574+
});
573575
}
574576

575577
const currentComponent = this.focusableComponents[fromParentFocusKey || this.focusKey];
@@ -581,6 +583,7 @@ class SpatialNavigation {
581583
);
582584

583585
if (currentComponent) {
586+
this.updateLayout(currentComponent.focusKey);
584587
const {parentFocusKey, focusKey, layout} = currentComponent;
585588

586589
const isVerticalDirection = direction === DIRECTION_DOWN || direction === DIRECTION_UP;
@@ -598,6 +601,7 @@ class SpatialNavigation {
598601
*/
599602
const siblings = filter(this.focusableComponents, (component) => {
600603
if (component.parentFocusKey === parentFocusKey && component.focusable) {
604+
this.updateLayout(component.focusKey);
601605
const siblingCutoffCoordinate = SpatialNavigation.getCutoffCoordinate(
602606
isVerticalDirection,
603607
isIncrementalDirection,
@@ -726,6 +730,7 @@ class SpatialNavigation {
726730
/**
727731
* Otherwise, trying to focus something by coordinates
728732
*/
733+
children.forEach((component) => this.updateLayout(component.focusKey));
729734
const {focusKey: childKey} = getChildClosestToOrigin(children);
730735

731736
this.log('getNextFocusKey', 'childKey will be focused', childKey);
@@ -790,7 +795,8 @@ class SpatialNavigation {
790795
* E.g. used in native environments to lazy-measure the layout on focus
791796
*/
792797
node
793-
}
798+
},
799+
layoutUpdated: false
794800
};
795801

796802
if (this.nativeMode) {
@@ -841,6 +847,8 @@ class SpatialNavigation {
841847
const component = this.focusableComponents[focusKey];
842848

843849
if (component) {
850+
this.updateLayout(component.focusKey);
851+
844852
return component.layout;
845853
}
846854

@@ -1002,7 +1010,7 @@ class SpatialNavigation {
10021010
updateLayout(focusKey) {
10031011
const component = this.focusableComponents[focusKey];
10041012

1005-
if (!component || this.nativeMode) {
1013+
if (!component || this.nativeMode || component.layoutUpdated) {
10061014
return;
10071015
}
10081016

0 commit comments

Comments
 (0)