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

Commit fd82f25

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

2 files changed

Lines changed: 66 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: 46 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,48 @@ var BASE_STYLES = {
4948
},
5049
};
5150

51+
var opacityRatio = 100;
52+
5253
// There are 3 stages: left, center, right. All previous navigation
5354
// items are in the left stage. The current navigation item is in the
5455
// center stage. All upcoming navigation items are in the right stage.
5556
// Another way to think of the stages is in terms of transitions. When
5657
// we move forward in the navigation stack, we perform a
5758
// right-to-center transition on the new navigation item and a
5859
// 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-
};
60+
function calcStyles (screenWidth) {
61+
const Stages = {
62+
Left: {
63+
Title: merge(BASE_STYLES.Title, { left: - screenWidth / 2, opacity: 0 }),
64+
LeftButton: merge(BASE_STYLES.LeftButton, { left: - screenWidth / 3, opacity: 1 }),
65+
RightButton: merge(BASE_STYLES.RightButton, { left: screenWidth / 3, opacity: 0 }),
66+
},
67+
Center: {
68+
Title: merge(BASE_STYLES.Title, { left: 0, opacity: 1 }),
69+
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 1 }),
70+
RightButton: merge(BASE_STYLES.RightButton, { left: 2 * screenWidth / 3 - 0, opacity: 1 }),
71+
},
72+
Right: {
73+
Title: merge(BASE_STYLES.Title, { left: screenWidth / 2, opacity: 0 }),
74+
LeftButton: merge(BASE_STYLES.LeftButton, { left: 0, opacity: 0 }),
75+
RightButton: merge(BASE_STYLES.RightButton, { left: screenWidth, opacity: 0 }),
76+
},
77+
}
7678

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

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

8094
function buildSceneInterpolators(startStyles, endStyles) {
8195
return {
@@ -134,22 +148,25 @@ function buildSceneInterpolators(startStyles, endStyles) {
134148
};
135149
}
136150

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-
};
151+
function getStyles () {
152+
const screenWidth = Dimensions.get('window').width
153+
if (!caches[screenWidth]) {
154+
caches[screenWidth] = calcStyles(screenWidth)
155+
}
145156

157+
return caches[screenWidth]
158+
}
146159

147160
module.exports = {
148161
General: {
149162
NavBarHeight: NAV_BAR_HEIGHT,
150163
StatusBarHeight: STATUS_BAR_HEIGHT,
151164
TotalNavHeight: NAV_HEIGHT,
152165
},
153-
Interpolators,
154-
Stages,
166+
get Interpolators() {
167+
return getStyles().Interpolators
168+
},
169+
get Stages() {
170+
return getStyles().Stages
171+
},
155172
};

0 commit comments

Comments
 (0)