settingsLayout.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 {IconClose, IconMenu} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {fadeIn, slideInLeft} from 'sentry/styles/animations';
  8. import {PageContent} from 'sentry/styles/organization';
  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?: () => React.ReactNode;
  16. } & RouteComponentProps<{}, {}>;
  17. function SettingsLayout(props: Props) {
  18. // This is used when the screen is small enough that the navigation should
  19. // be hidden
  20. //
  21. // [!!] On large screens this state is totally unused!
  22. const [navVisible, setNavVisible] = 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. setNavVisible(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={navVisible ? t('Close the menu') : t('Open the menu')}
  50. icon={navVisible ? <IconClose aria-hidden /> : <IconMenu aria-hidden />}
  51. onClick={() => toggleNav(!navVisible)}
  52. />
  53. )}
  54. <StyledSettingsBreadcrumb
  55. params={params}
  56. routes={childRoutes}
  57. route={childRoute}
  58. />
  59. <SettingsSearch />
  60. </HeaderContent>
  61. </SettingsHeader>
  62. <MaxWidthContainer>
  63. {shouldRenderNavigation && (
  64. <SidebarWrapper isVisible={navVisible} offsetTop={navOffsetTop}>
  65. {renderNavigation!()}
  66. </SidebarWrapper>
  67. )}
  68. <NavMask isVisible={navVisible} onClick={() => toggleNav(false)} />
  69. <Content>{children}</Content>
  70. </MaxWidthContainer>
  71. </SettingsColumn>
  72. );
  73. }
  74. const SettingsColumn = styled('div')`
  75. display: flex;
  76. flex-direction: column;
  77. flex: 1; /* so this stretches vertically so that footer is fixed at bottom */
  78. min-width: 0; /* fixes problem when child content stretches beyond layout width */
  79. footer {
  80. margin-top: 0;
  81. }
  82. `;
  83. const HeaderContent = styled('div')`
  84. display: flex;
  85. align-items: center;
  86. justify-content: space-between;
  87. `;
  88. const NavMenuToggle = styled(Button)`
  89. display: none;
  90. margin: -${space(1)} ${space(1)} -${space(1)} -${space(1)};
  91. padding: ${space(1)};
  92. color: ${p => p.theme.subText};
  93. &:hover,
  94. &:focus,
  95. &:active {
  96. color: ${p => p.theme.textColor};
  97. }
  98. @media (max-width: ${p => p.theme.breakpoints.small}) {
  99. display: block;
  100. }
  101. `;
  102. const StyledSettingsBreadcrumb = styled(SettingsBreadcrumb)`
  103. flex: 1;
  104. `;
  105. const MaxWidthContainer = styled('div')`
  106. display: flex;
  107. max-width: ${p => p.theme.settings.containerWidth};
  108. flex: 1;
  109. `;
  110. const SidebarWrapper = styled('div')<{isVisible: boolean; offsetTop: number}>`
  111. flex-shrink: 0;
  112. width: ${p => p.theme.settings.sidebarWidth};
  113. background: ${p => p.theme.background};
  114. border-right: 1px solid ${p => p.theme.border};
  115. @media (max-width: ${p => p.theme.breakpoints.small}) {
  116. display: ${p => (p.isVisible ? 'block' : 'none')};
  117. position: fixed;
  118. top: ${p => p.offsetTop}px;
  119. bottom: 0;
  120. overflow-y: auto;
  121. animation: ${slideInLeft} 100ms ease-in-out;
  122. z-index: ${p => p.theme.zIndex.settingsSidebarNav};
  123. box-shadow: ${p => p.theme.dropShadowHeavy};
  124. }
  125. `;
  126. const NavMask = styled('div')<{isVisible: boolean}>`
  127. display: none;
  128. @media (max-width: ${p => p.theme.breakpoints.small}) {
  129. display: ${p => (p.isVisible ? 'block' : 'none')};
  130. background: rgba(0, 0, 0, 0.35);
  131. height: 100%;
  132. width: 100%;
  133. position: absolute;
  134. z-index: ${p => p.theme.zIndex.settingsSidebarNavMask};
  135. animation: ${fadeIn} 250ms ease-in-out;
  136. }
  137. `;
  138. /**
  139. * Note: `overflow: hidden` will cause some buttons in `SettingsPageHeader` to be cut off because it has negative margin.
  140. * Will also cut off tooltips.
  141. */
  142. const Content = styled('div')`
  143. flex: 1;
  144. padding: ${space(4)};
  145. min-width: 0; /* keep children from stretching container */
  146. /**
  147. * PageContent is not normally used in settings but <PermissionDenied /> uses it under the hood.
  148. * This prevents double padding.
  149. */
  150. ${PageContent} {
  151. padding: 0;
  152. }
  153. `;
  154. export default SettingsLayout;