-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[change] call 'onLayout' when elements are resized
Uses ResizeObserver to monitor layout changes. Falls back to window.onresize (with initial firing). Fix #60 Close #848
- Loading branch information
Showing
6 changed files
with
152 additions
and
20 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
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
76 changes: 76 additions & 0 deletions
76
website/storybook/1-components/View/examples/PropOnLayout.js
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,76 @@ | ||
/** | ||
* @flow | ||
*/ | ||
|
||
import React from 'react'; | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
|
||
const l1 = { width: '100%', paddingLeft: 0, paddingTop: 0 }; | ||
const l2 = { width: '75%', paddingLeft: 10, paddingTop: 10 }; | ||
|
||
export default class ViewOnLayoutExample extends React.Component { | ||
state = { | ||
layoutInfo: {} | ||
}; | ||
|
||
componentDidMount() { | ||
this._layout = l1; | ||
|
||
this._interval = setInterval(() => { | ||
if (this._ref) { | ||
this._ref.setNativeProps({ style: this._layout }); | ||
this._layout = this._layout.width === '100%' ? l2 : l1; | ||
} | ||
}, 2000); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this._interval); | ||
} | ||
|
||
render() { | ||
const { x, y, width, height } = this.state.layoutInfo; | ||
return ( | ||
<View style={styles.root}> | ||
<View style={styles.left}> | ||
<Text>x: {x}</Text> | ||
<Text>y: {y}</Text> | ||
<Text>width: {width}</Text> | ||
<Text>height: {height}</Text> | ||
</View> | ||
<View style={styles.right}> | ||
<View ref={this._setRef} style={styles.container}> | ||
<View onLayout={this._handleLayout} style={styles.box} /> | ||
</View> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
_handleLayout = ({ nativeEvent }) => { | ||
this.setState(() => ({ layoutInfo: nativeEvent.layout })); | ||
}; | ||
|
||
_setRef = component => { | ||
this._ref = component; | ||
}; | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
root: { | ||
flexDirection: 'row' | ||
}, | ||
container: { | ||
height: 50 | ||
}, | ||
left: { | ||
width: 100 | ||
}, | ||
right: { | ||
flex: 1 | ||
}, | ||
box: { | ||
backgroundColor: '#eee', | ||
flex: 1 | ||
} | ||
}); |
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