settingsLayout.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {useEffect, useRef, useState} from 'react';
  2. import {browserHistory} 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 space from 'sentry/styles/space';
  9. import SettingsHeader from 'sentry/views/settings/components/settingsHeader';
  10. import SettingsSearch from 'sentry/views/settings/components/settingsSearch';
  11. import SettingsBreadcrumb from './settingsBreadcrumb';
  12. type Props = {
  13. url: string;
  14. children?: React.ReactNode;
  15. renderNavigation?: () => React.ReactNode;
  16. };
  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. console.log({props});
  35. // Close menu when navigating away
  36. useEffect(() => browserHistory.listen(() => toggleNav(false)), []);
  37. const {renderNavigation, children} = props;
  38. // We want child's view's props
  39. const shouldRenderNavigation = typeof renderNavigation === 'function';
  40. return (
  41. <SettingsColumn>
  42. <SettingsHeader ref={headerRef}>
  43. <HeaderContent>
  44. {shouldRenderNavigation && (
  45. <NavMenuToggle
  46. priority="link"
  47. aria-label={navVisible ? t('Close the menu') : t('Open the menu')}
  48. icon={navVisible ? <IconClose aria-hidden /> : <IconMenu aria-hidden />}
  49. onClick={() => toggleNav(!navVisible)}
  50. />
  51. )}
  52. <StyledSettingsBreadcrumb {...props} />
  53. <SettingsSearch />
  54. </HeaderContent>
  55. </SettingsHeader>
  56. <MaxWidthContainer>
  57. {shouldRenderNavigation && (
  58. <SidebarWrapper isVisible={navVisible} offsetTop={navOffsetTop}>
  59. {renderNavigation!()}
  60. </SidebarWrapper>
  61. )}
  62. <NavMask isVisible={navVisible} onClick={() => toggleNav(false)} />
  63. <Content>{children}</Content>
  64. </MaxWidthContainer>
  65. </SettingsColumn>
  66. );
  67. }
  68. const SettingsColumn = styled('div')`
  69. display: flex;
  70. flex-direction: column;
  71. flex: 1; /* so this stretches vertically so that footer is fixed at bottom */
  72. min-width: 0; /* fixes problem when child content stretches beyond layout width */
  73. footer {
  74. margin-top: 0;
  75. }
  76. `;
  77. const HeaderContent = styled('div')`
  78. display: flex;
  79. align-items: center;
  80. justify-content: space-between;
  81. `;
  82. const NavMenuToggle = styled(Button)`
  83. display: none;
  84. margin: -${space(1)} ${space(1)} -${space(1)} -${space(1)};
  85. padding: ${space(1)};
  86. color: ${p => p.theme.subText};
  87. &:hover,
  88. &:focus,
  89. &:active {
  90. color: ${p => p.theme.textColor};
  91. }
  92. @media (max-width: ${p => p.theme.breakpoints.small}) {
  93. display: block;
  94. }
  95. `;
  96. const StyledSettingsBreadcrumb = styled(SettingsBreadcrumb)`
  97. flex: 1;
  98. `;
  99. const MaxWidthContainer = styled('div')`
  100. display: flex;
  101. max-width: ${p => p.theme.settings.containerWidth};
  102. flex: 1;
  103. `;
  104. const SidebarWrapper = styled('div')<{isVisible: boolean; offsetTop: number}>`
  105. flex-shrink: 0;
  106. width: ${p => p.theme.settings.sidebarWidth};
  107. background: ${p => p.theme.background};
  108. border-right: 1px solid ${p => p.theme.border};
  109. @media (max-width: ${p => p.theme.breakpoints.small}) {
  110. display: ${p => (p.isVisible ? 'block' : 'none')};
  111. position: fixed;
  112. top: ${p => p.offsetTop}px;
  113. bottom: 0;
  114. overflow-y: auto;
  115. animation: ${slideInLeft} 100ms ease-in-out;
  116. z-index: ${p => p.theme.zIndex.settingsSidebarNav};
  117. box-shadow: ${p => p.theme.dropShadowHeavy};
  118. }
  119. `;
  120. const NavMask = styled('div')<{isVisible: boolean}>`
  121. display: none;
  122. @media (max-width: ${p => p.theme.breakpoints.small}) {
  123. display: ${p => (p.isVisible ? 'block' : 'none')};
  124. background: rgba(0, 0, 0, 0.35);
  125. height: 100%;
  126. width: 100%;
  127. position: absolute;
  128. z-index: ${p => p.theme.zIndex.settingsSidebarNavMask};
  129. animation: ${fadeIn} 250ms ease-in-out;
  130. }
  131. `;
  132. /**
  133. * Note: `overflow: hidden` will cause some buttons in `SettingsPageHeader` to be cut off because it has negative margin.
  134. * Will also cut off tooltips.
  135. */
  136. const Content = styled('div')`
  137. flex: 1;
  138. padding: ${space(4)};
  139. min-width: 0; /* keep children from stretching container */
  140. `;
  141. export default SettingsLayout;