Skip to content

Commit 11577aa

Browse files
author
katiegoines
committed
mobile toc styling
1 parent 383ee7c commit 11577aa

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

src/components/Layout/Layout.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const Layout = ({
6161
useCustomTitle?: boolean;
6262
}) => {
6363
const [menuOpen, toggleMenuOpen] = useState(false);
64+
const [tocOpen, toggleTocOpen] = useState(false);
6465
const [colorMode, setColorMode] = useState<ColorMode>('system');
6566
const [tocHeadings, setTocHeadings] = useState<HeadingInterface[]>([]);
6667
const mainId = 'pageMain';
@@ -213,7 +214,9 @@ export const Layout = ({
213214
value={{
214215
colorMode,
215216
menuOpen,
217+
tocOpen,
216218
toggleMenuOpen,
219+
toggleTocOpen,
217220
handleColorModeChange
218221
}}
219222
>
@@ -247,6 +250,7 @@ export const Layout = ({
247250
currentPlatform={currentPlatform}
248251
pageType={pageType}
249252
showLastUpdatedDate={showLastUpdatedDate}
253+
tocHeadings={tocHeadings}
250254
></LayoutHeader>
251255
<View key={asPathWithNoHash} className="layout-main">
252256
<Flex

src/components/Layout/LayoutHeader.tsx

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,29 @@ import { PageLastUpdated } from '../PageLastUpdated';
1919
import Feedback from '../Feedback';
2020
import RepoActions from '../Menu/RepoActions';
2121
import { usePathWithoutHash } from '@/utils/usePathWithoutHash';
22+
import { TableOfContents } from '../TableOfContents';
2223

2324
export const LayoutHeader = ({
2425
currentPlatform,
2526
isGen1,
2627
pageType = 'inner',
2728
showLastUpdatedDate = true,
28-
showTOC
29+
showTOC,
30+
tocHeadings
2931
}: {
3032
currentPlatform: Platform;
3133
isGen1: boolean;
3234
pageType?: 'home' | 'inner';
3335
showLastUpdatedDate: boolean;
3436
showTOC?: boolean;
37+
tocHeadings;
3538
}) => {
3639
const { menuOpen, toggleMenuOpen } = useContext(LayoutContext);
3740
const menuButtonRef = useRef<HTMLButtonElement>(null);
3841
const sidebarMenuButtonRef = useRef<HTMLButtonElement>(null);
42+
const { tocOpen, toggleTocOpen } = useContext(LayoutContext);
43+
const tocButtonRef = useRef<HTMLButtonElement>(null);
44+
const sidebarTocButtonRef = useRef<HTMLButtonElement>(null);
3945
const router = useRouter();
4046
const asPathWithNoHash = usePathWithoutHash();
4147

@@ -51,6 +57,18 @@ export const LayoutHeader = ({
5157
}
5258
};
5359

60+
const handleTocToggle = () => {
61+
if (!tocOpen) {
62+
toggleTocOpen(true);
63+
// For keyboard navigators, move focus to the close menu button in the nav
64+
setTimeout(() => sidebarTocButtonRef?.current?.focus(), 0);
65+
} else {
66+
toggleTocOpen(false);
67+
// For keyboard navigators, move focus back to menu button in header
68+
tocButtonRef?.current?.focus();
69+
}
70+
};
71+
5472
// Search result transform function that will strip out the pageMain anchor tag
5573
// Algolia search results include the anchor tag where the content was found but since we
5674
// are aggregating records this ends up always being the pageMain anchor tag which is the
@@ -78,6 +96,16 @@ export const LayoutHeader = ({
7896
Menu
7997
</Button>
8098

99+
<Button
100+
onClick={() => handleTocToggle()}
101+
size="small"
102+
ref={tocButtonRef}
103+
className="search-menu-toggle mobile-toggle"
104+
>
105+
<IconMenu aria-hidden="true" />
106+
On this page
107+
</Button>
108+
81109
<View
82110
className={classNames(
83111
'layout-search__search',
@@ -149,6 +177,41 @@ export const LayoutHeader = ({
149177
</div>
150178
</View>
151179
</View>
180+
181+
{/* toc */}
182+
<View
183+
className={classNames('layout-sidebar', {
184+
'layout-sidebar--expanded': tocOpen
185+
})}
186+
>
187+
<Button
188+
size="small"
189+
colorTheme="overlay"
190+
className={classNames('layout-sidebar__mobile-toggle', 'right-menu', {
191+
'layout-sidebar__mobile-toggle--open': tocOpen
192+
})}
193+
ref={sidebarTocButtonRef}
194+
onClick={() => handleTocToggle()}
195+
>
196+
<IconDoubleChevron />
197+
<VisuallyHidden>Close table of contents</VisuallyHidden>
198+
</Button>
199+
<View
200+
className={classNames('layout-sidebar__backdrop', {
201+
'layout-sidebar__backdrop--expanded': tocOpen
202+
})}
203+
onClick={() => toggleTocOpen(false)}
204+
></View>
205+
<View
206+
className={classNames('layout-sidebar__inner', 'right-menu', {
207+
'layout-sidebar__inner--expanded': tocOpen
208+
})}
209+
>
210+
<div className="layout-sidebar-menu">
211+
<TableOfContents headers={tocHeadings} />
212+
</div>
213+
</View>
214+
</View>
152215
</View>
153216
);
154217
};

src/components/Layout/LayoutProvider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ type LayoutContextType = {
55
colorMode: ColorMode;
66
handleColorModeChange: (value: ColorMode) => ColorMode;
77
menuOpen: boolean;
8+
tocOpen: boolean;
89
toggleMenuOpen: Dispatch<SetStateAction<boolean>>;
10+
toggleTocOpen: Dispatch<SetStateAction<boolean>>;
911
};
1012

1113
export const LayoutContext = createContext<LayoutContextType>({
1214
colorMode: 'system',
1315
handleColorModeChange: (value) => value,
1416
menuOpen: false,
15-
toggleMenuOpen: () => {}
17+
tocOpen: false,
18+
toggleMenuOpen: () => {},
19+
toggleTocOpen: () => {}
1620
});
1721

1822
export function LayoutProvider({ value, children }) {

src/styles/layout.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
gap: var(--amplify-space-small);
6565
padding: var(--amplify-space-medium);
6666
opacity: 0;
67+
&.right-menu {
68+
transform: rotate(180deg);
69+
}
6770
&:focus {
6871
box-shadow: none;
6972
}
@@ -86,6 +89,15 @@
8689
display: grid;
8790
grid-template-rows: auto 1fr;
8891
overflow: visible;
92+
93+
&.right-menu {
94+
position: fixed;
95+
right: 0;
96+
transform: translate(100%, 0);
97+
border-right: none;
98+
border-left: 1px solid var(--amplify-colors-neutral-20);
99+
}
100+
89101
&--expanded {
90102
animation: menu 0.2s ease-out forwards;
91103
animation-delay: 0.1s;

0 commit comments

Comments
 (0)