settingsLayout.tsx 5.7 KB

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