Header.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // @flow
  2. // Copyright 2017 The go-ethereum Authors
  3. // This file is part of the go-ethereum library.
  4. //
  5. // The go-ethereum library is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // The go-ethereum library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  17. import React, {Component} from 'react';
  18. import withStyles from 'material-ui/styles/withStyles';
  19. import AppBar from 'material-ui/AppBar';
  20. import Toolbar from 'material-ui/Toolbar';
  21. import Transition from 'react-transition-group/Transition';
  22. import IconButton from 'material-ui/IconButton';
  23. import Typography from 'material-ui/Typography';
  24. import ChevronLeftIcon from 'material-ui-icons/ChevronLeft';
  25. import {DURATION} from './Common';
  26. // arrowDefault is the default style of the arrow button.
  27. const arrowDefault = {
  28. transition: `transform ${DURATION}ms`,
  29. };
  30. // arrowTransition is the additional style of the arrow button corresponding to the transition's state.
  31. const arrowTransition = {
  32. entered: {transform: 'rotate(180deg)'},
  33. };
  34. // Styles for the Header component.
  35. const styles = theme => ({
  36. header: {
  37. backgroundColor: theme.palette.background.appBar,
  38. color: theme.palette.getContrastText(theme.palette.background.appBar),
  39. zIndex: theme.zIndex.appBar,
  40. },
  41. toolbar: {
  42. paddingLeft: theme.spacing.unit,
  43. paddingRight: theme.spacing.unit,
  44. },
  45. mainText: {
  46. paddingLeft: theme.spacing.unit,
  47. },
  48. });
  49. export type Props = {
  50. classes: Object,
  51. opened: boolean,
  52. openSideBar: () => {},
  53. closeSideBar: () => {},
  54. };
  55. // Header renders the header of the dashboard.
  56. class Header extends Component<Props> {
  57. shouldComponentUpdate(nextProps) {
  58. return nextProps.opened !== this.props.opened;
  59. }
  60. // changeSideBar opens or closes the sidebar corresponding to the previous state.
  61. changeSideBar = () => {
  62. if (this.props.opened) {
  63. this.props.closeSideBar();
  64. } else {
  65. this.props.openSideBar();
  66. }
  67. };
  68. // arrowButton is connected to the sidebar; changes its state.
  69. arrowButton = (transitionState: string) => (
  70. <IconButton onClick={this.changeSideBar}>
  71. <ChevronLeftIcon
  72. style={{
  73. ...arrowDefault,
  74. ...arrowTransition[transitionState],
  75. }}
  76. />
  77. </IconButton>
  78. );
  79. render() {
  80. const {classes, opened} = this.props; // The classes property is injected by withStyles().
  81. return (
  82. <AppBar position="static" className={classes.header}>
  83. <Toolbar className={classes.toolbar}>
  84. <Transition mountOnEnter in={opened} timeout={{enter: DURATION}}>
  85. {this.arrowButton}
  86. </Transition>
  87. <Typography type="title" color="inherit" noWrap className={classes.mainText}>
  88. Go Ethereum Dashboard
  89. </Typography>
  90. </Toolbar>
  91. </AppBar>
  92. );
  93. }
  94. }
  95. export default withStyles(styles)(Header);