settingsLayout.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. max-width: ${p => p.theme.settings.containerWidth};
  116. flex: 1;
  117. `;
  118. const SidebarWrapper = styled('nav')<{isVisible: boolean; offsetTop: number}>`
  119. flex-shrink: 0;
  120. width: ${p => p.theme.settings.sidebarWidth};
  121. background: ${p => p.theme.background};
  122. border-right: 1px solid ${p => p.theme.border};
  123. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  124. display: ${p => (p.isVisible ? 'block' : 'none')};
  125. position: fixed;
  126. top: ${p => p.offsetTop}px;
  127. bottom: 0;
  128. overflow-y: auto;
  129. animation: ${slideInLeft} 100ms ease-in-out;
  130. z-index: ${p => p.theme.zIndex.settingsSidebarNav};
  131. box-shadow: ${p => p.theme.dropShadowHeavy};
  132. }
  133. `;
  134. const NavMask = styled('div')<{isVisible: boolean}>`
  135. display: none;
  136. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  137. display: ${p => (p.isVisible ? 'block' : 'none')};
  138. background: rgba(0, 0, 0, 0.35);
  139. height: 100%;
  140. width: 100%;
  141. position: absolute;
  142. z-index: ${p => p.theme.zIndex.settingsSidebarNavMask};
  143. animation: ${fadeIn} 250ms ease-in-out;
  144. }
  145. `;
  146. /**
  147. * Note: `overflow: hidden` will cause some buttons in `SettingsPageHeader` to be cut off because it has negative margin.
  148. * Will also cut off tooltips.
  149. */
  150. const Content = styled('div')`
  151. flex: 1;
  152. padding: ${space(4)};
  153. min-width: 0; /* keep children from stretching container */
  154. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  155. padding: ${space(2)};
  156. }
  157. /**
  158. * Layout.Page is not normally used in settings but <PermissionDenied /> uses
  159. * it under the hood. This prevents double padding.
  160. */
  161. ${Layout.Page} {
  162. padding: 0;
  163. }
  164. `;
  165. export default SettingsLayout;