Sidebar.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. withNavigationUIController,
  6. LayoutManager,
  7. NavigationProvider,
  8. ThemeProvider,
  9. } from '@atlaskit/navigation-next';
  10. import { createSubscribedElement } from './UnstatedUtils';
  11. import AppContainer from '../services/AppContainer';
  12. import SidebarNav from './Sidebar/SidebarNav';
  13. import RecentChanges from './Sidebar/RecentChanges';
  14. import CustomSidebar from './Sidebar/CustomSidebar';
  15. const sidebarDefaultWidth = 240;
  16. class Sidebar extends React.Component {
  17. static propTypes = {
  18. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  19. navigationUIController: PropTypes.any.isRequired,
  20. };
  21. state = {
  22. currentContentsId: 'recent',
  23. };
  24. componentWillMount() {
  25. this.initBreakpointEvents();
  26. }
  27. initBreakpointEvents() {
  28. const { appContainer, navigationUIController } = this.props;
  29. const mdOrAvobeHandler = (mql) => {
  30. // sm -> md
  31. if (mql.matches) {
  32. appContainer.setState({ isDrawerOpened: false });
  33. navigationUIController.enableResize();
  34. // restore width
  35. if (this.sidebarWidthCached != null) {
  36. navigationUIController.setState({ productNavWidth: this.sidebarWidthCached });
  37. }
  38. }
  39. // md -> sm
  40. else {
  41. // cache width
  42. this.sidebarWidthCached = navigationUIController.state.productNavWidth;
  43. appContainer.setState({ isDrawerOpened: false });
  44. navigationUIController.disableResize();
  45. navigationUIController.expand();
  46. // fix width
  47. navigationUIController.setState({ productNavWidth: sidebarDefaultWidth });
  48. }
  49. };
  50. appContainer.addBreakpointListener('md', mdOrAvobeHandler, true);
  51. }
  52. backdropClickedHandler = () => {
  53. const { appContainer } = this.props;
  54. appContainer.setState({ isDrawerOpened: false });
  55. }
  56. itemSelectedHandler = (contentsId) => {
  57. const { navigationUIController } = this.props;
  58. const { currentContentsId } = this.state;
  59. // already selected
  60. if (currentContentsId === contentsId) {
  61. navigationUIController.toggleCollapse();
  62. }
  63. // switch and expand
  64. else {
  65. this.setState({ currentContentsId: contentsId });
  66. navigationUIController.expand();
  67. }
  68. }
  69. renderGlobalNavigation = () => (
  70. <SidebarNav currentContentsId={this.state.currentContentsId} onItemSelected={this.itemSelectedHandler} />
  71. );
  72. renderSidebarContents = () => {
  73. let contents = <CustomSidebar />;
  74. switch (this.state.currentContentsId) {
  75. case 'recent':
  76. contents = <RecentChanges />;
  77. break;
  78. }
  79. return contents;
  80. }
  81. render() {
  82. const { isDrawerOpened } = this.props.appContainer.state;
  83. return (
  84. <>
  85. <div className={`grw-sidebar ${isDrawerOpened ? 'open' : ''}`}>
  86. <ThemeProvider
  87. theme={theme => ({
  88. ...theme,
  89. context: 'product',
  90. })}
  91. >
  92. <LayoutManager
  93. globalNavigation={this.renderGlobalNavigation}
  94. productNavigation={() => null}
  95. containerNavigation={this.renderSidebarContents}
  96. experimental_hideNavVisuallyOnCollapse
  97. experimental_flyoutOnHover
  98. experimental_alternateFlyoutBehaviour
  99. // experimental_fullWidthFlyout
  100. shouldHideGlobalNavShadow
  101. showContextualNavigation
  102. >
  103. </LayoutManager>
  104. </ThemeProvider>
  105. </div>
  106. { isDrawerOpened && (
  107. <div className="grw-sidebar-backdrop modal-backdrop show" onClick={this.backdropClickedHandler}></div>
  108. ) }
  109. </>
  110. );
  111. }
  112. }
  113. const SidebarWithNavigationUI = withNavigationUIController(Sidebar);
  114. const SidebarWithNavigationUIAndTranslation = withTranslation()(SidebarWithNavigationUI);
  115. /**
  116. * Wrapper component for using unstated
  117. */
  118. const SidebarWrapper = (props) => {
  119. return createSubscribedElement(SidebarWithNavigationUIAndTranslation, props, [AppContainer]);
  120. };
  121. export default () => (
  122. <NavigationProvider><SidebarWrapper /></NavigationProvider>
  123. );