settingsLayout.tsx 5.8 KB

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