settingsLayout.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import React from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Button from 'app/components/button';
  5. import {IconClose, IconMenu} from 'app/icons';
  6. import {t} from 'app/locale';
  7. import {fadeIn, slideInLeft} from 'app/styles/animations';
  8. import space from 'app/styles/space';
  9. import SettingsBreadcrumb from './settingsBreadcrumb';
  10. import SettingsHeader from './settingsHeader';
  11. import SettingsSearch from './settingsSearch';
  12. type Props = {
  13. renderNavigation?: () => React.ReactNode;
  14. children: React.ReactNode;
  15. } & RouteComponentProps<{}, {}>;
  16. type State = {
  17. /**
  18. * This is used when the screen is small enough that the navigation should
  19. * be hidden. On large screens this state will end up unused.
  20. */
  21. navVisible: boolean;
  22. /**
  23. * Offset mobile settings navigation by the height of main navigation,
  24. * settings breadcrumbs and optional warnings.
  25. */
  26. navOffsetTop: number;
  27. };
  28. class SettingsLayout extends React.Component<Props, State> {
  29. state: State = {
  30. navVisible: false,
  31. navOffsetTop: 0,
  32. };
  33. componentDidMount() {
  34. // Close the navigation when navigating.
  35. this.unlisten = browserHistory.listen(() => this.toggleNav(false));
  36. }
  37. componentWillUnmount() {
  38. this.unlisten();
  39. }
  40. unlisten!: () => void;
  41. headerRef = React.createRef<HTMLDivElement>();
  42. toggleNav(navVisible: boolean) {
  43. // when the navigation is opened, body should be scroll-locked
  44. this.toggleBodyScrollLock(navVisible);
  45. this.setState({
  46. navOffsetTop: this.headerRef.current?.getBoundingClientRect().bottom ?? 0,
  47. navVisible,
  48. });
  49. }
  50. toggleBodyScrollLock(lock: boolean) {
  51. const bodyElement = document.getElementsByTagName('body')[0];
  52. if (window.scrollTo) {
  53. window.scrollTo(0, 0);
  54. }
  55. bodyElement.classList[lock ? 'add' : 'remove']('scroll-lock');
  56. }
  57. render() {
  58. const {params, routes, route, renderNavigation, children} = this.props;
  59. const {navVisible, navOffsetTop} = this.state;
  60. // We want child's view's props
  61. const childProps =
  62. children && React.isValidElement(children) ? children.props : this.props;
  63. const childRoutes = childProps.routes || routes || [];
  64. const childRoute = childProps.route || route || {};
  65. const shouldRenderNavigation = typeof renderNavigation === 'function';
  66. return (
  67. <SettingsColumn>
  68. <SettingsHeader ref={this.headerRef}>
  69. <HeaderContent>
  70. {shouldRenderNavigation && (
  71. <NavMenuToggle
  72. priority="link"
  73. label={t('Open the menu')}
  74. icon={navVisible ? <IconClose aria-hidden /> : <IconMenu aria-hidden />}
  75. onClick={() => this.toggleNav(!navVisible)}
  76. />
  77. )}
  78. <StyledSettingsBreadcrumb
  79. params={params}
  80. routes={childRoutes}
  81. route={childRoute}
  82. />
  83. <SettingsSearch />
  84. </HeaderContent>
  85. </SettingsHeader>
  86. <MaxWidthContainer>
  87. {shouldRenderNavigation && (
  88. <SidebarWrapper isVisible={navVisible} offsetTop={navOffsetTop}>
  89. {renderNavigation!()}
  90. </SidebarWrapper>
  91. )}
  92. <NavMask isVisible={navVisible} onClick={() => this.toggleNav(false)} />
  93. <Content>{children}</Content>
  94. </MaxWidthContainer>
  95. </SettingsColumn>
  96. );
  97. }
  98. }
  99. const SettingsColumn = styled('div')`
  100. display: flex;
  101. flex-direction: column;
  102. flex: 1; /* so this stretches vertically so that footer is fixed at bottom */
  103. min-width: 0; /* fixes problem when child content stretches beyond layout width */
  104. footer {
  105. margin-top: 0;
  106. }
  107. `;
  108. const HeaderContent = styled('div')`
  109. display: flex;
  110. align-items: center;
  111. justify-content: space-between;
  112. `;
  113. const NavMenuToggle = styled(Button)`
  114. display: none;
  115. margin: -${space(1)} ${space(1)} -${space(1)} -${space(1)};
  116. padding: ${space(1)};
  117. color: ${p => p.theme.subText};
  118. &:hover,
  119. &:focus,
  120. &:active {
  121. color: ${p => p.theme.textColor};
  122. }
  123. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  124. display: block;
  125. }
  126. `;
  127. const StyledSettingsBreadcrumb = styled(SettingsBreadcrumb)`
  128. flex: 1;
  129. `;
  130. const MaxWidthContainer = styled('div')`
  131. display: flex;
  132. max-width: ${p => p.theme.settings.containerWidth};
  133. flex: 1;
  134. `;
  135. const SidebarWrapper = styled('div')<{isVisible: boolean; offsetTop: number}>`
  136. flex-shrink: 0;
  137. width: ${p => p.theme.settings.sidebarWidth};
  138. background: ${p => p.theme.background};
  139. border-right: 1px solid ${p => p.theme.border};
  140. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  141. display: ${p => (p.isVisible ? 'block' : 'none')};
  142. position: fixed;
  143. top: ${p => p.offsetTop}px;
  144. bottom: 0;
  145. overflow-y: auto;
  146. animation: ${slideInLeft} 100ms ease-in-out;
  147. z-index: ${p => p.theme.zIndex.settingsSidebarNav};
  148. box-shadow: ${p => p.theme.dropShadowHeavy};
  149. }
  150. `;
  151. const NavMask = styled('div')<{isVisible: boolean}>`
  152. display: none;
  153. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  154. display: ${p => (p.isVisible ? 'block' : 'none')};
  155. background: rgba(0, 0, 0, 0.35);
  156. height: 100%;
  157. width: 100%;
  158. position: absolute;
  159. z-index: ${p => p.theme.zIndex.settingsSidebarNavMask};
  160. animation: ${fadeIn} 250ms ease-in-out;
  161. }
  162. `;
  163. /**
  164. * Note: `overflow: hidden` will cause some buttons in `SettingsPageHeader` to be cut off because it has negative margin.
  165. * Will also cut off tooltips.
  166. */
  167. const Content = styled('div')`
  168. flex: 1;
  169. padding: ${space(4)};
  170. min-width: 0; /* keep children from stretching container */
  171. `;
  172. export default SettingsLayout;