settingsLayout.tsx 6.3 KB

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