Skip to content

Commit

Permalink
feat: adding in collapsible sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
maria-mcparland committed Mar 25, 2024
1 parent 81303d6 commit 100c8c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
81 changes: 47 additions & 34 deletions packages/elements-core/src/components/Layout/SidebarLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Flex } from '@stoplight/mosaic';
import { Box, Button, Flex } from '@stoplight/mosaic';
import * as React from 'react';
import { useLocation } from 'react-router-dom';

Expand All @@ -8,6 +8,7 @@ type SidebarLayoutProps = {
sidebarWidth?: number;
children?: React.ReactNode;
renderSideBar?: boolean;
layout?: 'sidebar' | 'drawer';
};

const MAX_CONTENT_WIDTH = 1800;
Expand All @@ -16,52 +17,64 @@ const SIDEBAR_MAX_WIDTH = 1.5 * SIDEBAR_MIN_WIDTH;

export const SidebarLayout = React.forwardRef<HTMLDivElement, SidebarLayoutProps>(
(
{ sidebar, children, maxContentWidth = MAX_CONTENT_WIDTH, sidebarWidth = SIDEBAR_MIN_WIDTH, renderSideBar = true },
{
sidebar,
children,
maxContentWidth = MAX_CONTENT_WIDTH,
sidebarWidth = SIDEBAR_MIN_WIDTH,
renderSideBar = true,
layout,
},
ref,
) => {
const scrollRef = React.useRef<HTMLDivElement | null>(null);
const [sidebarRef, currentSidebarWidth, startResizing] = useResizer(sidebarWidth);
const [isSidebarOpen, setSidebarOpenState] = React.useState(true);
const { pathname } = useLocation();

React.useEffect(() => {
// Scroll to top on page change
scrollRef.current?.scrollTo(0, 0);
}, [pathname]);

return (
<Flex ref={ref} className="sl-elements-api" pin h="full">
<Flex
ref={sidebarRef}
onMouseDown={(e: React.MouseEvent<HTMLElement>) => e.preventDefault()}
style={{ maxWidth: `${SIDEBAR_MAX_WIDTH}px` }}
>
{renderSideBar && (
<Flex
direction="col"
bg="canvas-100"
borderR
pt={8}
pos="sticky"
pinY
overflowY="auto"
style={{
paddingLeft: `calc((100% - ${maxContentWidth}px) / 2)`,
width: `${currentSidebarWidth}px`,
minWidth: `${SIDEBAR_MIN_WIDTH}px`,
}}
>
{sidebar}
</Flex>
)}
{!layout || layout !== 'drawer' || isSidebarOpen ? (
<Flex
justifySelf="end"
flexGrow={0}
flexShrink={0}
resize="x"
onMouseDown={startResizing}
style={{ width: '1em', flexBasis: '6px', cursor: 'ew-resize' }}
/>
</Flex>
ref={sidebarRef}
onMouseDown={(e: React.MouseEvent<HTMLElement>) => e.preventDefault()}
style={{ maxWidth: `${SIDEBAR_MAX_WIDTH}px` }}
>
{renderSideBar && (
<Flex
direction="col"
bg="canvas-100"
borderR
pt={8}
pos="sticky"
pinY
overflowY="auto"
style={{
paddingLeft: `calc((100% - ${maxContentWidth}px) / 2)`,
width: `${currentSidebarWidth}px`,
minWidth: `${SIDEBAR_MIN_WIDTH}px`,
}}
>
{sidebar}
</Flex>
)}
<Flex
justifySelf="end"
flexGrow={0}
flexShrink={0}
resize="x"
onMouseDown={startResizing}
style={{ width: '1em', flexBasis: '6px', cursor: 'ew-resize' }}
/>
{layout === 'drawer' && <Button onClick={() => setSidebarOpenState(false)}>Hide Sidebar</Button>}
</Flex>
) : (
<Button onClick={() => setSidebarOpenState(true)}>Open Sidebar</Button>
)}

<Box ref={scrollRef} bg="canvas" px={24} flex={1} w="full" overflowY="auto">
<Box style={{ maxWidth: `${maxContentWidth - currentSidebarWidth}px` }} py={16}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SidebarLayoutProps = {
tryItCorsProxy?: string;
tryItOutDefaultServer?: string;
useCustomNav?: boolean;
layout?: 'sidebar' | 'drawer';
};

export const APIWithSidebarLayout: React.FC<SidebarLayoutProps> = ({
Expand All @@ -42,6 +43,7 @@ export const APIWithSidebarLayout: React.FC<SidebarLayoutProps> = ({
tryItCorsProxy,
tryItOutDefaultServer,
useCustomNav,
layout,
}) => {
const container = React.useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -101,7 +103,7 @@ export const APIWithSidebarLayout: React.FC<SidebarLayoutProps> = ({
);

return (
<SidebarLayout ref={container} sidebar={sidebar} renderSideBar={!useCustomNav}>
<SidebarLayout ref={container} sidebar={sidebar} renderSideBar={!useCustomNav} layout={layout}>
{node && (
<ParsedDocs
key={pathname}
Expand Down
3 changes: 2 additions & 1 deletion packages/elements/src/containers/API.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface CommonAPIProps extends RoutingProps {
*
* @default "sidebar"
*/
layout?: 'sidebar' | 'stacked';
layout?: 'sidebar' | 'stacked' | 'drawer';
logo?: string;

/**
Expand Down Expand Up @@ -197,6 +197,7 @@ export const APIImpl: React.FC<APIProps> = props => {
tryItCorsProxy={tryItCorsProxy}
tryItOutDefaultServer={tryItOutDefaultServer}
useCustomNav={useCustomNav}
layout={layout}
/>
)}
</InlineRefResolverProvider>
Expand Down

0 comments on commit 100c8c7

Please sign in to comment.