Sidebar.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, modeGenerator,
  9. } from '@atlaskit/navigation-next';
  10. import Drawer from '@atlaskit/drawer';
  11. import { createSubscribedElement } from './UnstatedUtils';
  12. import AppContainer from '../services/AppContainer';
  13. import SidebarNav from './Sidebar/SidebarNav';
  14. import History from './Sidebar/History';
  15. import CustomSidebar from './Sidebar/CustomSidebar';
  16. class Sidebar extends React.Component {
  17. static propTypes = {
  18. navigationUIController: PropTypes.any.isRequired,
  19. };
  20. state = {
  21. currentContentsId: 'custom',
  22. isDrawerOpen: false,
  23. };
  24. openDrawer = () => this.setState({ isDrawerOpen: true });
  25. closeDrawer = () => this.setState({ isDrawerOpen: false });
  26. itemSelectedHandler = (contentsId) => {
  27. const { navigationUIController } = this.props;
  28. const { currentContentsId } = this.state;
  29. // already selected
  30. if (currentContentsId === contentsId) {
  31. navigationUIController.toggleCollapse();
  32. }
  33. // switch and expand
  34. else {
  35. this.setState({ currentContentsId: contentsId });
  36. navigationUIController.expand();
  37. }
  38. // if (contentsId === 'drawer') {
  39. // this.openDrawer();
  40. // }
  41. }
  42. renderGlobalNavigation = () => (
  43. <>
  44. <SidebarNav currentContentsId={this.state.currentContentsId} onItemSelected={this.itemSelectedHandler} />
  45. <Drawer onClose={this.closeDrawer} isOpen={this.state.isDrawerOpen} width="wide">
  46. <code>Drawer contents</code>
  47. </Drawer>
  48. </>
  49. );
  50. renderSidebarContents = () => {
  51. let contents = <CustomSidebar></CustomSidebar>;
  52. switch (this.state.currentContentsId) {
  53. case 'history':
  54. contents = <History></History>;
  55. break;
  56. }
  57. return contents;
  58. }
  59. render() {
  60. return (
  61. <ThemeProvider
  62. theme={theme => ({
  63. ...theme,
  64. context: 'product',
  65. mode: modeGenerator({
  66. product: { text: '#ffffff', background: '#334455' },
  67. }),
  68. })}
  69. >
  70. <LayoutManager
  71. globalNavigation={this.renderGlobalNavigation}
  72. productNavigation={() => null}
  73. containerNavigation={this.renderSidebarContents}
  74. experimental_hideNavVisuallyOnCollapse
  75. experimental_flyoutOnHover
  76. experimental_alternateFlyoutBehaviour
  77. // experimental_fullWidthFlyout
  78. shouldHideGlobalNavShadow
  79. showContextualNavigation
  80. topOffset={50}
  81. >
  82. </LayoutManager>
  83. </ThemeProvider>
  84. );
  85. }
  86. }
  87. const SidebarWithNavigationUI = withNavigationUIController(Sidebar);
  88. const SidebarWithNavigationUIAndTranslation = withTranslation()(SidebarWithNavigationUI);
  89. /**
  90. * Wrapper component for using unstated
  91. */
  92. const SidebarWrapper = (props) => {
  93. return createSubscribedElement(SidebarWithNavigationUIAndTranslation, props, [AppContainer]);
  94. };
  95. export default () => (
  96. <NavigationProvider><SidebarWrapper /></NavigationProvider>
  97. );