2015-07-07 20:34:09 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-07-07 20:34:09 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-07-07 20:34:09 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
var {Animated, LayoutAnimation, PanResponder, StyleSheet, View} = ReactNative;
|
2015-07-07 20:34:09 +00:00
|
|
|
|
|
|
|
var AnExSet = require('AnExSet');
|
|
|
|
|
|
|
|
var CIRCLE_SIZE = 80;
|
|
|
|
var CIRCLE_MARGIN = 18;
|
|
|
|
var NUM_CIRCLES = 30;
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class Circle extends React.Component<any, any> {
|
2016-02-25 10:44:58 +00:00
|
|
|
longTimer: number;
|
|
|
|
|
2015-07-07 20:34:09 +00:00
|
|
|
_onLongPress: () => void;
|
|
|
|
_toggleIsActive: () => void;
|
|
|
|
constructor(props: Object): void {
|
|
|
|
super();
|
|
|
|
this._onLongPress = this._onLongPress.bind(this);
|
|
|
|
this._toggleIsActive = this._toggleIsActive.bind(this);
|
2015-07-30 03:19:40 +00:00
|
|
|
this.state = {
|
|
|
|
isActive: false,
|
|
|
|
pan: new Animated.ValueXY(), // Vectors reduce boilerplate. (step1: uncomment)
|
2018-05-11 20:32:37 +00:00
|
|
|
pop: new Animated.Value(0), // Initial value. (step2a: uncomment)
|
2015-07-30 03:19:40 +00:00
|
|
|
};
|
2015-07-07 20:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onLongPress(): void {
|
|
|
|
var config = {tension: 40, friction: 3};
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.pan.addListener(value => {
|
|
|
|
// Async listener for state changes (step1: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
this.props.onMove && this.props.onMove(value);
|
|
|
|
});
|
|
|
|
Animated.spring(this.state.pop, {
|
2018-05-11 20:32:37 +00:00
|
|
|
toValue: 1, // Pop to larger size. (step2b: uncomment)
|
|
|
|
...config, // Reuse config for convenient consistency (step2b: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
}).start();
|
2018-05-11 20:32:37 +00:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
panResponder: PanResponder.create({
|
|
|
|
onPanResponderMove: Animated.event([
|
|
|
|
null, // native event - ignore (step1: uncomment)
|
|
|
|
{dx: this.state.pan.x, dy: this.state.pan.y}, // links pan to gestureState (step1: uncomment)
|
|
|
|
]),
|
|
|
|
onPanResponderRelease: (e, gestureState) => {
|
|
|
|
LayoutAnimation.easeInEaseOut(); // @flowfixme animates layout update as one batch (step3: uncomment)
|
|
|
|
Animated.spring(this.state.pop, {
|
|
|
|
toValue: 0, // Pop back to 0 (step2c: uncomment)
|
|
|
|
...config,
|
|
|
|
}).start();
|
|
|
|
this.setState({panResponder: undefined});
|
|
|
|
this.props.onMove &&
|
|
|
|
this.props.onMove({
|
|
|
|
x: gestureState.dx + this.props.restLayout.x,
|
|
|
|
y: gestureState.dy + this.props.restLayout.y,
|
|
|
|
});
|
|
|
|
this.props.onDeactivate();
|
|
|
|
this.state.pan.removeAllListeners();
|
|
|
|
},
|
|
|
|
}),
|
2015-07-07 20:34:09 +00:00
|
|
|
},
|
2018-05-11 20:32:37 +00:00
|
|
|
() => {
|
|
|
|
this.props.onActivate();
|
|
|
|
},
|
|
|
|
);
|
2015-07-07 20:34:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
2015-07-07 20:34:09 +00:00
|
|
|
if (this.state.panResponder) {
|
|
|
|
var handlers = this.state.panResponder.panHandlers;
|
2018-05-11 20:32:37 +00:00
|
|
|
var dragStyle = {
|
|
|
|
// Used to position while dragging
|
|
|
|
position: 'absolute', // Hoist out of layout (step1: uncomment)
|
|
|
|
...this.state.pan.getLayout(), // Convenience converter (step1: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
handlers = {
|
|
|
|
onStartShouldSetResponder: () => !this.state.isActive,
|
|
|
|
onResponderGrant: () => {
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.pan.setValue({x: 0, y: 0}); // reset (step1: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
this.state.pan.setOffset(this.props.restLayout); // offset from onLayout (step1: uncomment)
|
2018-01-08 20:45:38 +00:00
|
|
|
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses
|
|
|
|
* an error found when Flow v0.63 was deployed. To see the error
|
|
|
|
* delete this comment and run Flow. */
|
2015-07-07 20:34:09 +00:00
|
|
|
this.longTimer = setTimeout(this._onLongPress, 300);
|
|
|
|
},
|
|
|
|
onResponderRelease: () => {
|
|
|
|
if (!this.state.panResponder) {
|
2018-01-08 20:45:38 +00:00
|
|
|
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment
|
|
|
|
* suppresses an error found when Flow v0.63 was deployed. To see
|
|
|
|
* the error delete this comment and run Flow. */
|
2015-07-07 20:34:09 +00:00
|
|
|
clearTimeout(this.longTimer);
|
|
|
|
this._toggleIsActive();
|
|
|
|
}
|
2018-05-11 20:32:37 +00:00
|
|
|
},
|
2015-07-07 20:34:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
var animatedStyle: Object = {
|
2018-05-11 20:32:37 +00:00
|
|
|
shadowOpacity: this.state.pop, // no need for interpolation (step2d: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
transform: [
|
2018-05-11 20:32:37 +00:00
|
|
|
{
|
|
|
|
scale: this.state.pop.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [1, 1.3], // scale up from 1 to 1.3 (step2d: uncomment)
|
|
|
|
}),
|
|
|
|
},
|
2015-07-07 20:34:09 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
var openVal = this.props.openVal;
|
|
|
|
if (this.props.dummy) {
|
|
|
|
animatedStyle.opacity = 0;
|
|
|
|
} else if (this.state.isActive) {
|
2018-05-11 20:32:37 +00:00
|
|
|
var innerOpenStyle = [
|
|
|
|
styles.open,
|
|
|
|
{
|
|
|
|
// (step4: uncomment)
|
|
|
|
left: openVal.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [this.props.restLayout.x, 0],
|
|
|
|
}),
|
|
|
|
top: openVal.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [this.props.restLayout.y, 0],
|
|
|
|
}),
|
|
|
|
width: openVal.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [CIRCLE_SIZE, this.props.containerLayout.width],
|
|
|
|
}),
|
|
|
|
height: openVal.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [CIRCLE_SIZE, this.props.containerLayout.height],
|
|
|
|
}),
|
|
|
|
margin: openVal.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [CIRCLE_MARGIN, 0],
|
|
|
|
}),
|
|
|
|
borderRadius: openVal.interpolate({
|
|
|
|
inputRange: [-0.15, 0, 0.5, 1],
|
|
|
|
outputRange: [0, CIRCLE_SIZE / 2, CIRCLE_SIZE * 1.3, 0],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
];
|
2015-07-07 20:34:09 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Animated.View
|
|
|
|
onLayout={this.props.onLayout}
|
2018-05-11 20:32:37 +00:00
|
|
|
style={[
|
|
|
|
styles.dragView,
|
|
|
|
dragStyle,
|
|
|
|
animatedStyle,
|
|
|
|
this.state.isActive ? styles.open : null,
|
|
|
|
]}
|
2015-07-07 20:34:09 +00:00
|
|
|
{...handlers}>
|
|
|
|
<Animated.View style={[styles.circle, innerOpenStyle]}>
|
|
|
|
<AnExSet
|
|
|
|
containerLayout={this.props.containerLayout}
|
|
|
|
id={this.props.id}
|
|
|
|
isActive={this.state.isActive}
|
|
|
|
openVal={this.props.openVal}
|
|
|
|
onDismiss={this._toggleIsActive}
|
|
|
|
/>
|
|
|
|
</Animated.View>
|
|
|
|
</Animated.View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_toggleIsActive(velocity) {
|
|
|
|
var config = {tension: 30, friction: 7};
|
|
|
|
if (this.state.isActive) {
|
2018-05-11 20:32:37 +00:00
|
|
|
Animated.spring(this.props.openVal, {toValue: 0, ...config}).start(() => {
|
|
|
|
// (step4: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
this.setState({isActive: false}, this.props.onDeactivate);
|
2018-05-11 20:32:37 +00:00
|
|
|
}); // (step4: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
} else {
|
|
|
|
this.props.onActivate();
|
|
|
|
this.setState({isActive: true, panResponder: undefined}, () => {
|
|
|
|
// this.props.openVal.setValue(1); // (step4: comment)
|
2018-05-11 20:32:37 +00:00
|
|
|
Animated.spring(this.props.openVal, {toValue: 1, ...config}).start(); // (step4: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class AnExApp extends React.Component<any, any> {
|
2016-02-25 10:44:58 +00:00
|
|
|
static title = 'Animated - Gratuitous App';
|
2018-06-06 12:20:40 +00:00
|
|
|
static description =
|
|
|
|
'Bunch of Animations - tap a circle to ' +
|
|
|
|
'open a view with more animations, or longPress and drag to reorder circles.';
|
2016-02-25 10:44:58 +00:00
|
|
|
|
2015-07-07 20:34:09 +00:00
|
|
|
_onMove: (position: Point) => void;
|
|
|
|
constructor(props: any): void {
|
|
|
|
super(props);
|
|
|
|
var keys = [];
|
|
|
|
for (var idx = 0; idx < NUM_CIRCLES; idx++) {
|
|
|
|
keys.push('E' + idx);
|
|
|
|
}
|
|
|
|
this.state = {
|
|
|
|
keys,
|
|
|
|
restLayouts: [],
|
|
|
|
openVal: new Animated.Value(0),
|
|
|
|
};
|
|
|
|
this._onMove = this._onMove.bind(this);
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
2015-07-07 20:34:09 +00:00
|
|
|
var circles = this.state.keys.map((key, idx) => {
|
|
|
|
if (key === this.state.activeKey) {
|
|
|
|
return <Circle key={key + 'd'} dummy={true} />;
|
|
|
|
} else {
|
|
|
|
if (!this.state.restLayouts[idx]) {
|
|
|
|
var onLayout = function(index, e) {
|
|
|
|
var layout = e.nativeEvent.layout;
|
2018-05-11 20:32:37 +00:00
|
|
|
this.setState(state => {
|
2015-07-07 20:34:09 +00:00
|
|
|
state.restLayouts[index] = layout;
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
}.bind(this, idx);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Circle
|
|
|
|
key={key}
|
|
|
|
id={key}
|
|
|
|
openVal={this.state.openVal}
|
|
|
|
onLayout={onLayout}
|
|
|
|
restLayout={this.state.restLayouts[idx]}
|
|
|
|
onActivate={this.setState.bind(this, {
|
|
|
|
activeKey: key,
|
|
|
|
activeInitialLayout: this.state.restLayouts[idx],
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.state.activeKey) {
|
|
|
|
circles.push(
|
2018-05-11 20:32:37 +00:00
|
|
|
<Animated.View
|
|
|
|
key="dark"
|
|
|
|
style={[styles.darkening, {opacity: this.state.openVal}]}
|
|
|
|
/>,
|
2015-07-07 20:34:09 +00:00
|
|
|
);
|
|
|
|
circles.push(
|
|
|
|
<Circle
|
|
|
|
openVal={this.state.openVal}
|
|
|
|
key={this.state.activeKey}
|
|
|
|
id={this.state.activeKey}
|
|
|
|
restLayout={this.state.activeInitialLayout}
|
|
|
|
containerLayout={this.state.layout}
|
|
|
|
onMove={this._onMove}
|
2018-05-11 20:32:37 +00:00
|
|
|
onDeactivate={() => {
|
|
|
|
this.setState({activeKey: undefined});
|
|
|
|
}}
|
|
|
|
/>,
|
2015-07-07 20:34:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<View
|
|
|
|
style={styles.grid}
|
|
|
|
onLayout={e => this.setState({layout: e.nativeEvent.layout})}>
|
2015-07-07 20:34:09 +00:00
|
|
|
{circles}
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMove(position: Point): void {
|
|
|
|
var newKeys = moveToClosest(this.state, position);
|
|
|
|
if (newKeys !== this.state.keys) {
|
2018-05-11 20:32:37 +00:00
|
|
|
LayoutAnimation.easeInEaseOut(); // animates layout update as one batch (step3: uncomment)
|
2015-07-07 20:34:09 +00:00
|
|
|
this.setState({keys: newKeys});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Point = {x: number, y: number};
|
|
|
|
function distance(p1: Point, p2: Point): number {
|
|
|
|
var dx = p1.x - p2.x;
|
|
|
|
var dy = p1.y - p2.y;
|
|
|
|
return dx * dx + dy * dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveToClosest({activeKey, keys, restLayouts}, position) {
|
|
|
|
var activeIdx = -1;
|
|
|
|
var closestIdx = activeIdx;
|
|
|
|
var minDist = Infinity;
|
|
|
|
var newKeys = [];
|
|
|
|
keys.forEach((key, idx) => {
|
|
|
|
var dist = distance(position, restLayouts[idx]);
|
|
|
|
if (key === activeKey) {
|
|
|
|
idx = activeIdx;
|
|
|
|
} else {
|
|
|
|
newKeys.push(key);
|
|
|
|
}
|
|
|
|
if (dist < minDist) {
|
|
|
|
minDist = dist;
|
|
|
|
closestIdx = idx;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (closestIdx === activeIdx) {
|
|
|
|
return keys; // nothing changed
|
|
|
|
} else {
|
|
|
|
newKeys.splice(closestIdx, 0, activeKey);
|
|
|
|
return newKeys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
flexDirection: 'row',
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
},
|
|
|
|
circle: {
|
|
|
|
width: CIRCLE_SIZE,
|
|
|
|
height: CIRCLE_SIZE,
|
|
|
|
borderRadius: CIRCLE_SIZE / 2,
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: 'black',
|
|
|
|
margin: CIRCLE_MARGIN,
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
dragView: {
|
|
|
|
shadowRadius: 10,
|
|
|
|
shadowColor: 'rgba(0,0,0,0.7)',
|
|
|
|
shadowOffset: {height: 8},
|
|
|
|
alignSelf: 'flex-start',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
},
|
|
|
|
open: {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
width: undefined, // unset value from styles.circle
|
|
|
|
height: undefined, // unset value from styles.circle
|
|
|
|
borderRadius: 0, // unset value from styles.circle
|
|
|
|
},
|
|
|
|
darkening: {
|
|
|
|
backgroundColor: 'black',
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = AnExApp;
|