forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve StatusBar/Header Behavior (react-navigation#2669)
* Track orientation change * Add tests for withOrientation * Better variable naming
- Loading branch information
1 parent
c4541f4
commit a8556b0
Showing
7 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/views/__tests__/__snapshots__/withOrientation-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`it adds isLandscape to props 1`] = ` | ||
<View | ||
isLandscape={false} | ||
/> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import renderer from 'react-test-renderer'; | ||
import withOrientation, { isOrientationLandscape } from '../withOrientation'; | ||
|
||
test('it adds isLandscape to props', () => { | ||
const WrappedComponent = withOrientation(View); | ||
const rendered = renderer.create(<WrappedComponent />).toJSON(); | ||
expect(rendered).toMatchSnapshot(); | ||
}); | ||
|
||
test('calculates orientation correctly', () => { | ||
const isLandscape = isOrientationLandscape({ width: 10, height: 1 }); | ||
expect(isLandscape).toBeTruthy(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @flow | ||
|
||
import React from 'react'; | ||
import { Dimensions } from 'react-native'; | ||
import hoistNonReactStatic from 'hoist-non-react-statics'; | ||
|
||
type WindowDimensions = { | ||
width: number, | ||
height: number, | ||
}; | ||
|
||
type InjectedProps = { | ||
isLandscape: boolean, | ||
}; | ||
|
||
type State = { | ||
isLandscape: boolean, | ||
}; | ||
|
||
export const isOrientationLandscape = ({ | ||
width, | ||
height, | ||
}: WindowDimensions): boolean => width > height; | ||
|
||
export default function<T: *>(WrappedComponent: ReactClass<T & InjectedProps>) { | ||
class withOrientation extends React.Component<void, T, State> { | ||
state: State; | ||
|
||
constructor() { | ||
super(); | ||
|
||
const isLandscape = isOrientationLandscape(Dimensions.get('window')); | ||
this.state = { isLandscape }; | ||
} | ||
|
||
componentDidMount() { | ||
Dimensions.addEventListener('change', this.handleOrientationChange); | ||
} | ||
|
||
componentWillUnmount() { | ||
Dimensions.removeEventListener('change', this.handleOrientationChange); | ||
} | ||
|
||
handleOrientationChange = ({ window }: { window: WindowDimensions }) => { | ||
const isLandscape = isOrientationLandscape(window); | ||
this.setState({ isLandscape }); | ||
}; | ||
|
||
render() { | ||
return <WrappedComponent {...this.props} {...this.state} />; | ||
} | ||
} | ||
|
||
return hoistNonReactStatic(withOrientation, WrappedComponent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2287,6 +2287,18 @@ [email protected]: | |
setimmediate "^1.0.5" | ||
ua-parser-js "^0.7.9" | ||
|
||
fbjs@^0.8.16: | ||
version "0.8.16" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.16.tgz#5e67432f550dc41b572bf55847b8aca64e5337db" | ||
dependencies: | ||
core-js "^1.0.0" | ||
isomorphic-fetch "^2.1.1" | ||
loose-envify "^1.0.0" | ||
object-assign "^4.1.0" | ||
promise "^7.1.1" | ||
setimmediate "^1.0.5" | ||
ua-parser-js "^0.7.9" | ||
|
||
fbjs@^0.8.9: | ||
version "0.8.15" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.15.tgz#4f0695fdfcc16c37c0b07facec8cb4c4091685b9" | ||
|
@@ -4587,12 +4599,12 @@ react-proxy@^1.1.7: | |
lodash "^4.6.1" | ||
react-deep-force-update "^1.0.0" | ||
|
||
react-test-renderer@^15.6.1: | ||
version "15.6.1" | ||
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-15.6.1.tgz#026f4a5bb5552661fd2cc4bbcd0d4bc8a35ebf7e" | ||
react-test-renderer@^16.0.0: | ||
version "16.0.0" | ||
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.0.0.tgz#9fe7b8308f2f71f29fc356d4102086f131c9cb15" | ||
dependencies: | ||
fbjs "^0.8.9" | ||
object-assign "^4.1.0" | ||
fbjs "^0.8.16" | ||
object-assign "^4.1.1" | ||
|
||
react-timer-mixin@^0.13.2: | ||
version "0.13.3" | ||
|