settingsLayout.tsx 6.0 KB

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