import React, { Component } from "react"; import { Link, Switch, Route } from "react-router-dom"; import Animated from "animated/lib/targets/react-dom"; import ChevronLeft from "react-icons/lib/md/chevron-left"; import PropTypes from "prop-types"; import { Block, Col } from "jsxstyle"; import EnvironmentHeader from "./EnvironmentHeader.js"; import { RED } from "../Theme.js"; import Example from "./Example.js"; import API from "./APISmall.js"; import Guide from "./Guide.js"; const paths = { api: match => `${match.path}/api/:mod`, example: match => `${match.path}/example/:example`, guide: match => `${match.path}/guides/:mod/:header?` }; export default class EnvironmentSmall extends Component { static propTypes = { data: PropTypes.object, match: PropTypes.object, location: PropTypes.object, history: PropTypes.object }; state = { // 0 = parent is active // 1 = child is active anim: new Animated.Value(this.props.match.isExact ? 0 : 1), animating: false }; componentDidMount() { this.preloadExamples(); } preloadExamples() { const { data } = this.props; data.examples.forEach(example => { // native doesn't have `load` if (example.load) example.load(() => {}); // all have `loadSource` if (example.loadSource) example.loadSource(() => {}); }); } componentWillReceiveProps(nextProps) { const { anim } = this.state; // only animate if we're going from here to direct child. // child to child we'll ignore const goingToChild = nextProps.match.isExact === false && this.props.match.isExact === true; const comingFromChild = nextProps.match.isExact === true && this.props.match.isExact === false; if (goingToChild || comingFromChild) { this.setState( { animating: true }, () => { Animated.timing(anim, { toValue: goingToChild ? 1 : 0, duration: 350 }).start(() => { this.setState({ animating: false }); }); } ); } } render() { const { data, match, location } = this.props; const { anim, animating } = this.state; return ( (
{getApiTitle(data, mod)}
)} /> (
{getExampleTitle(data, example)}
)} /> (
{getGuideTitle(data, mod)}
)} />