Skip to content

Commit

Permalink
feat: update aside panels to use react portal
Browse files Browse the repository at this point in the history
Signed-off-by: Mason Hu <[email protected]>
  • Loading branch information
mas-who committed Apr 10, 2024
1 parent 595a167 commit 6e07531
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 272 deletions.
8 changes: 5 additions & 3 deletions src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FC } from "react";
import Navigation from "components/Navigation";
import { QueuedNotification } from "@canonical/react-components";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import Panels from "components/Panels";
import { AuthProvider } from "context/auth";
import { EventQueueProvider } from "context/eventQueue";
import { InstanceLoadingProvider } from "context/instanceLoading";
Expand Down Expand Up @@ -33,11 +32,14 @@ const Root: FC = () => {
<InstanceLoadingProvider>
<OperationsProvider>
<EventQueueProvider>
<div className="l-application" role="presentation">
<div
id="l-application"
className="l-application"
role="presentation"
>
<Navigation />
<ErrorBoundary fallback={ErrorPage}>
<App />
<Panels />
<Events />
<StatusBar />
</ErrorBoundary>
Expand Down
25 changes: 0 additions & 25 deletions src/components/Panels.tsx

This file was deleted.

22 changes: 14 additions & 8 deletions src/components/SidePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FC, PropsWithChildren, ReactNode } from "react";
import Loader from "components/Loader";
import classnames from "classnames";
import { Spinner } from "@canonical/react-components";
import { createPortal } from "react-dom";
import usePanelParams from "util/usePanelParams";
import useEventListener from "@use-it/event-listener";

interface CommonProps {
className?: string;
Expand Down Expand Up @@ -95,7 +97,14 @@ const SidePanelComponent: FC<SidePanelProps> = ({
width,
pinned,
}) => {
return (
const panelParams = usePanelParams();

useEventListener("keydown", (e: KeyboardEvent) => {
// Close panel if Escape key is pressed
if (e.code === "Escape") panelParams.clear();
});

return createPortal(
<aside
className={classnames("l-aside", className, {
"is-narrow": width === "narrow",
Expand All @@ -111,13 +120,10 @@ const SidePanelComponent: FC<SidePanelProps> = ({
<Spinner />
</div>
) : (
<>
{loading && <Loader />}
{!loading && hasError && <>Loading failed</>}
{!hasError && children}
</>
<>{hasError ? <>Loading failed</> : children}</>
)}
</aside>
</aside>,
document.getElementById("l-application") as Element,
);
};

Expand Down
Loading

0 comments on commit 6e07531

Please sign in to comment.