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

Commit ffeee8e

Browse files
committed
make navbar responsive to changes in dimensions
1 parent 303cb78 commit ffeee8e

2 files changed

Lines changed: 67 additions & 29 deletions

File tree

Libraries/Navigator/NavigatorNavigationBar.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import NavigatorNavigationBarStylesIOS from 'ReactNavigatorNavigationBarStylesIO
1515
import Platform from 'ReactStyleSheet';
1616
import StyleSheet from 'ReactStyleSheet';
1717
import View from 'ReactView';
18+
import Dimensions from 'ReactDimensions';
1819
import { Map } from 'immutable';
1920
import autobind from 'autobind-decorator';
2021

@@ -44,6 +45,7 @@ class NavigatorNavigationBar extends Component {
4445
this._descriptors[componentName] = new Map();
4546
});
4647

48+
this._handleResize = this._handleResize.bind(this)
4749
}
4850

4951
static propTypes = {
@@ -132,6 +134,24 @@ class NavigatorNavigationBar extends Component {
132134
}
133135
}
134136

137+
componentWillMount() {
138+
window.addEventListener('resize', this._handleResize)
139+
}
140+
141+
componentWillUnmount() {
142+
window.removeEventListener('resize', this._handleResize)
143+
clearTimeout(this._resizeTimeout)
144+
}
145+
146+
_handleResize() {
147+
const self = this
148+
clearTimeout(this._resizeTimeout)
149+
// debounce
150+
this._resizeTimeout = setTimeout(function () {
151+
self.setState({ dimensions: Dimensions.get('window') })
152+
}, 100)
153+
}
154+
135155
render() {
136156
var navBarStyle = {
137157
height: this.props.navigationStyles.General.TotalNavHeight,

Libraries/Navigator/NavigatorNavigationBarStylesIOS.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Dimensions from 'ReactDimensions';
1313
import buildStyleInterpolator from './polyfills/buildStyleInterpolator';
1414
import merge from './polyfills/merge';
1515

16-
var SCREEN_WIDTH = Dimensions.get('window').width;
1716
var NAV_BAR_HEIGHT = 44;
1817
var STATUS_BAR_HEIGHT = 20;
1918
var NAV_HEIGHT = NAV_BAR_HEIGHT + STATUS_BAR_HEIGHT;
@@ -49,33 +48,49 @@ var BASE_STYLES = {
4948
},
5049
};
5150

51+
var caches = {}
52+
var opacityRatio = 100;
53+
5254
// There are 3 stages: left, center, right. All previous navigation
5355
// items are in the left stage. The current navigation item is in the
5456
// center stage. All upcoming navigation items are in the right stage.
5557
// Another way to think of the stages is in terms of transitions. When
5658
// we move forward in the navigation stack, we perform a
5759
// right-to-center transition on the new navigation item and a
5860
// center-to-left transition on the current navigation item.
59-
var Stages = {
60-
Left: {
61-
Title: merge(BASE_STYLES.Title, { left: - SCREEN_WIDTH / 2, opacity: 0 }),
62-
LeftButton: merge(BASE_STYLES.LeftButton, { left: - SCREEN_WIDTH / 3, opacity: 1 }),
63-
RightButton: merge(BASE_STYLES.RightButton, { left: SCREEN_WIDTH / 3, opacity: 0 }),
64-
},
65-
Center: {
66-
Title: merge(BASE_STYLES.Title, { left: 0, opacity: 1 }),
67-
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 1 }),
68-
RightButton: merge(BASE_STYLES.RightButton, { left: 2 * SCREEN_WIDTH / 3 - 0, opacity: 1 }),
69-
},
70-
Right: {
71-
Title: merge(BASE_STYLES.Title, { left: SCREEN_WIDTH / 2, opacity: 0 }),
72-
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 0 }),
73-
RightButton: merge(BASE_STYLES.RightButton, { left: SCREEN_WIDTH, opacity: 0 }),
74-
},
75-
};
61+
function calcStyles (screenWidth) {
62+
const Stages = {
63+
Left: {
64+
Title: merge(BASE_STYLES.Title, { left: - screenWidth / 2, opacity: 0 }),
65+
LeftButton: merge(BASE_STYLES.LeftButton, { left: - screenWidth / 3, opacity: 1 }),
66+
RightButton: merge(BASE_STYLES.RightButton, { left: screenWidth / 3, opacity: 0 }),
67+
},
68+
Center: {
69+
Title: merge(BASE_STYLES.Title, { left: 0, opacity: 1 }),
70+
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 1 }),
71+
RightButton: merge(BASE_STYLES.RightButton, { left: 2 * screenWidth / 3 - 0, opacity: 1 }),
72+
},
73+
Right: {
74+
Title: merge(BASE_STYLES.Title, { left: screenWidth / 2, opacity: 0 }),
75+
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 0 }),
76+
RightButton: merge(BASE_STYLES.RightButton, { left: screenWidth, opacity: 0 }),
77+
},
78+
}
7679

80+
const Interpolators = {
81+
// Animating *into* the center stage from the right
82+
RightToCenter: buildSceneInterpolators(Stages.Right, Stages.Center),
83+
// Animating out of the center stage, to the left
84+
CenterToLeft: buildSceneInterpolators(Stages.Center, Stages.Left),
85+
// Both stages (animating *past* the center stage)
86+
RightToLeft: buildSceneInterpolators(Stages.Right, Stages.Left),
87+
}
7788

78-
var opacityRatio = 100;
89+
return {
90+
Stages,
91+
Interpolators
92+
}
93+
};
7994

8095
function buildSceneInterpolators(startStyles, endStyles) {
8196
return {
@@ -134,22 +149,25 @@ function buildSceneInterpolators(startStyles, endStyles) {
134149
};
135150
}
136151

137-
var Interpolators = {
138-
// Animating *into* the center stage from the right
139-
RightToCenter: buildSceneInterpolators(Stages.Right, Stages.Center),
140-
// Animating out of the center stage, to the left
141-
CenterToLeft: buildSceneInterpolators(Stages.Center, Stages.Left),
142-
// Both stages (animating *past* the center stage)
143-
RightToLeft: buildSceneInterpolators(Stages.Right, Stages.Left),
144-
};
152+
function getStyles () {
153+
const screenWidth = Dimensions.get('window').width
154+
if (!caches[screenWidth]) {
155+
caches[screenWidth] = calcStyles(screenWidth)
156+
}
145157

158+
return caches[screenWidth]
159+
}
146160

147161
module.exports = {
148162
General: {
149163
NavBarHeight: NAV_BAR_HEIGHT,
150164
StatusBarHeight: STATUS_BAR_HEIGHT,
151165
TotalNavHeight: NAV_HEIGHT,
152166
},
153-
Interpolators,
154-
Stages,
167+
get Interpolators() {
168+
return getStyles().Interpolators
169+
},
170+
get Stages() {
171+
return getStyles().Stages
172+
},
155173
};

0 commit comments

Comments
 (0)