Sidebar.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. withNavigationUIController,
  5. LayoutManager,
  6. NavigationProvider,
  7. ThemeProvider,
  8. } from '@atlaskit/navigation-next';
  9. import { withUnstatedContainers } from './UnstatedUtils';
  10. import AppContainer from '../services/AppContainer';
  11. import SidebarNav from './Sidebar/SidebarNav';
  12. import RecentChanges from './Sidebar/RecentChanges';
  13. import CustomSidebar from './Sidebar/CustomSidebar';
  14. const sidebarDefaultWidth = 240;
  15. class Sidebar extends React.Component {
  16. static propTypes = {
  17. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  18. navigationUIController: PropTypes.any.isRequired,
  19. isDrawerModeOnInit: PropTypes.bool,
  20. };
  21. state = {
  22. currentContentsId: 'recent',
  23. };
  24. componentWillMount() {
  25. this.hackUIController();
  26. }
  27. componentDidUpdate(prevProps, prevState) {
  28. this.toggleDrawerMode(this.isDrawerMode);
  29. }
  30. /**
  31. * hack and override UIController.storeState
  32. *
  33. * Since UIController is an unstated container, setState() in storeState method should be awaited before writing to cache.
  34. */
  35. hackUIController() {
  36. const { navigationUIController } = this.props;
  37. // see: @atlaskit/navigation-next/dist/esm/ui-controller/UIController.js
  38. const orgStoreState = navigationUIController.storeState;
  39. navigationUIController.storeState = async(state) => {
  40. await navigationUIController.setState(state);
  41. orgStoreState(state);
  42. };
  43. }
  44. /**
  45. * return whether drawer mode or not
  46. */
  47. get isDrawerMode() {
  48. let isDrawerMode = this.props.appContainer.state.isDrawerMode;
  49. if (isDrawerMode == null) {
  50. isDrawerMode = this.props.isDrawerModeOnInit;
  51. }
  52. return isDrawerMode;
  53. }
  54. toggleDrawerMode(bool) {
  55. const { navigationUIController } = this.props;
  56. const isStateModified = navigationUIController.state.isResizeDisabled !== bool;
  57. if (!isStateModified) {
  58. return;
  59. }
  60. // Drawer <-- Dock
  61. if (bool) {
  62. // cache state
  63. this.sidebarCollapsedCached = navigationUIController.state.isCollapsed;
  64. this.sidebarWidthCached = navigationUIController.state.productNavWidth;
  65. // clear transition temporary
  66. if (this.sidebarCollapsedCached) {
  67. this.addCssClassTemporary('grw-sidebar-supress-transitions-to-drawer');
  68. }
  69. navigationUIController.disableResize();
  70. // fix width
  71. navigationUIController.setState({ productNavWidth: sidebarDefaultWidth });
  72. }
  73. // Drawer --> Dock
  74. else {
  75. // clear transition temporary
  76. if (this.sidebarCollapsedCached) {
  77. this.addCssClassTemporary('grw-sidebar-supress-transitions-to-dock');
  78. }
  79. navigationUIController.enableResize();
  80. // restore width
  81. if (this.sidebarWidthCached != null) {
  82. navigationUIController.setState({ productNavWidth: this.sidebarWidthCached });
  83. }
  84. }
  85. }
  86. get sidebarElem() {
  87. return document.querySelector('.grw-sidebar');
  88. }
  89. addCssClassTemporary(className) {
  90. // clear
  91. this.sidebarElem.classList.add(className);
  92. // restore after 300ms
  93. setTimeout(() => {
  94. this.sidebarElem.classList.remove(className);
  95. }, 300);
  96. }
  97. backdropClickedHandler = () => {
  98. const { appContainer } = this.props;
  99. appContainer.setState({ isDrawerOpened: false });
  100. }
  101. itemSelectedHandler = (contentsId) => {
  102. const { navigationUIController } = this.props;
  103. const { currentContentsId } = this.state;
  104. // already selected
  105. if (currentContentsId === contentsId) {
  106. navigationUIController.toggleCollapse();
  107. }
  108. // switch and expand
  109. else {
  110. this.setState({ currentContentsId: contentsId });
  111. navigationUIController.expand();
  112. }
  113. }
  114. renderGlobalNavigation = () => (
  115. <SidebarNav currentContentsId={this.state.currentContentsId} onItemSelected={this.itemSelectedHandler} />
  116. );
  117. renderSidebarContents = () => {
  118. let contents = <CustomSidebar />;
  119. switch (this.state.currentContentsId) {
  120. case 'recent':
  121. contents = <RecentChanges />;
  122. break;
  123. }
  124. return <div className="grw-sidebar-content-container">{contents}</div>;
  125. }
  126. render() {
  127. const { isDrawerOpened } = this.props.appContainer.state;
  128. return (
  129. <>
  130. <div className={`grw-sidebar ${this.isDrawerMode ? 'grw-sidebar-drawer' : ''} ${isDrawerOpened ? 'open' : ''}`}>
  131. <ThemeProvider
  132. theme={theme => ({
  133. ...theme,
  134. context: 'product',
  135. })}
  136. >
  137. <LayoutManager
  138. globalNavigation={this.renderGlobalNavigation}
  139. productNavigation={() => null}
  140. containerNavigation={this.renderSidebarContents}
  141. experimental_hideNavVisuallyOnCollapse
  142. experimental_flyoutOnHover
  143. experimental_alternateFlyoutBehaviour
  144. // experimental_fullWidthFlyout
  145. shouldHideGlobalNavShadow
  146. showContextualNavigation
  147. >
  148. </LayoutManager>
  149. </ThemeProvider>
  150. </div>
  151. { isDrawerOpened && (
  152. <div className="grw-sidebar-backdrop modal-backdrop show" onClick={this.backdropClickedHandler}></div>
  153. ) }
  154. </>
  155. );
  156. }
  157. }
  158. const SidebarWithNavigationUIController = withNavigationUIController(Sidebar);
  159. /**
  160. * Wrapper component for using unstated
  161. */
  162. const SidebarWithNavigation = (props) => {
  163. const { preferDrawerModeByUser: isDrawerModeOnInit } = props.appContainer.state;
  164. const initUICForDrawerMode = isDrawerModeOnInit
  165. // generate initialUIController for Drawer mode
  166. ? {
  167. isCollapsed: false,
  168. isResizeDisabled: true,
  169. productNavWidth: sidebarDefaultWidth,
  170. }
  171. // set undefined (should be initialized by cache)
  172. : undefined;
  173. return (
  174. <NavigationProvider initialUIController={initUICForDrawerMode}>
  175. <SidebarWithNavigationUIController {...props} isDrawerModeOnInit={isDrawerModeOnInit} />
  176. </NavigationProvider>
  177. );
  178. };
  179. SidebarWithNavigation.propTypes = {
  180. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  181. };
  182. export default withUnstatedContainers(SidebarWithNavigation, [AppContainer]);