Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react,styles): add Drawer component #1646

Merged
merged 28 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions docs/components/Drawer.css

This file was deleted.

84 changes: 0 additions & 84 deletions docs/components/Drawer.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions docs/components/Navigation.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
--docs-navigation-divider-color: var(--stroke-dark);
}

body.TopBar--thin .NavigationDrawer {
--top-bar-height: var(--top-bar-height-thin);
}

.NavigationDrawer {
--drawer-top: var(--top-bar-height);
--drawer-height: calc(100vh - var(--top-bar-height));
overflow-y: auto;
}

.Navigation {
width: var(--menu-width);
background-color: var(--docs-navigation-background-color);
Expand Down
10 changes: 4 additions & 6 deletions docs/components/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import { Icon, ClickOutsideListener, Scrim } from '@deque/cauldron-react';
import { Icon, Scrim } from '@deque/cauldron-react';
import { Link, useLocation } from 'react-router-dom';
import { components, pages, foundations } from '../collections';
import './Navigation.css';
Expand Down Expand Up @@ -38,12 +38,11 @@ const NavigationLink = ({

interface NavigationProps extends React.HTMLAttributes<HTMLElement> {
contentRef: React.RefObject<HTMLElement>;
active?: boolean;
onClick?: () => void;
onNavigation?: () => void;
}

function Navigation(
{ contentRef, active = true, onClick = () => {}, ...props }: NavigationProps,
{ contentRef, onNavigation = () => {}, ...props }: NavigationProps,
ref: React.Ref<HTMLElement>
) {
const location = useLocation();
Expand All @@ -59,7 +58,7 @@ function Navigation(
}
}

onClick();
onNavigation();
};

const activeComponents = components.filter(
Expand All @@ -78,7 +77,6 @@ function Navigation(
text={name}
active={isActive}
onClick={handleClick}
tabIndex={!active ? -1 : undefined}
/>
</li>
);
Expand Down
58 changes: 20 additions & 38 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { render } from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import { Helmet } from 'react-helmet';
import classNames from 'classnames';
import focusable from 'focusable';
import mdxComponents from './mdx-components';
import Footer from './components/Footer';
import ComponentLayout from './components/ComponentLayout';
import Drawer from './components/Drawer';
import Navigation from './components/Navigation';
import {
Code,
Drawer as DrawerComponent,
TopBar,
MenuBar,
TopBarTrigger,
Expand Down Expand Up @@ -49,30 +48,19 @@ const App = () => {
});
const [topBarMenuItem, setTopBarMenuItem] = useState(null);
const workspaceRef = useRef(null);
const focusReturnRef = useRef(null);
const navigationRef = useRef(null);
const topBarTrigger = useRef();
const [workspaceTabIndex, setWorkspaceTabIndex] = useState(-1);
const { theme, toggleTheme } = useThemeContext();

const focusTopBarMenuItem = () => {
if (!topBarMenuItem) {
return;
}

topBarMenuItem?.focus();
};

const onTriggerClick = (e) => {
const { show } = state;

if (e) {
e.preventDefault();
}

if (show && topBarTrigger?.current) {
topBarTrigger?.current?.focus();
}

setState({ show: !show });
};

Expand Down Expand Up @@ -115,14 +103,6 @@ const App = () => {
);
};

useEffect(() => {
document.addEventListener('focusTopBarMenu', focusTopBarMenuItem);

return () => {
document.removeEventListener('focusTopBarMenu', focusTopBarMenuItem);
};
}, []);

const { show, thin } = state;

const [drawerIsActive, setDrawerIsActive] = useState(false);
Expand All @@ -143,20 +123,15 @@ const App = () => {
}, []);

useEffect(() => {
if (state.show) {
// Focus on the first focusable navigation item
navigationRef.current?.querySelector('a')?.focus();
} else {
workspaceRef.current?.focus();
}
}, [state.show]);

useEffect(() => {
const firstFocusableElement =
workspaceRef.current?.querySelector(focusable);
setWorkspaceTabIndex(!firstFocusableElement ? 0 : -1);
// ensure focus return always "resets" after a navigation
focusReturnRef.current = null;
});

// Only render the collapse-able drawer component when there is space to do so
const Drawer = drawerIsActive
? DrawerComponent
: (props) => <Fragment>{props.children}</Fragment>;

return (
<Router>
<Helmet
Expand Down Expand Up @@ -221,17 +196,24 @@ const App = () => {
</TopBar>
<div className="Content">
<Drawer
active={drawerIsActive}
className="NavigationDrawer"
open={show}
position="left"
focusOptions={{
initialFocus: navigationRef,
returnFocus: focusReturnRef
}}
onClose={() => setState({ show: false })}
>
<Navigation
id="navigation"
active={show || !drawerIsActive}
ref={navigationRef}
contentRef={workspaceRef}
onClick={() => setState({ show: false })}
aria-hidden={!show && drawerIsActive}
tabIndex={-1}
onNavigation={() => {
setState({ show: false });
focusReturnRef.current = workspaceRef.current;
}}
/>
</Drawer>
<Workspace
Expand Down
Loading
Loading