Skip to content

Commit

Permalink
[web] Allow opening the Sidebar from outside
Browse files Browse the repository at this point in the history
By using the Sidebar.OpenButton "subcomponent". It's using a ref by now, but
it's needed to research if there is a better way to do it (exploring the
useImperativeHanlde[1] hook or any other technique).

[1] https://react.dev/reference/react/useImperativeHandle
  • Loading branch information
dgdavid authored and ancorgs committed Mar 24, 2023
1 parent 0a5e325 commit f1a56eb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions web/src/components/core/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import React, { useEffect, useRef, useState } from "react";
import { Button, Text } from "@patternfly/react-core";
import { Icon, PageActions } from "~/components/layout";

// FIXME: look for a better way to allow opening the Sidebar from outside
let openButtonRef = {};

/**
* D-Installer sidebar navigation
*/
export default function Sidebar({ children }) {
const Sidebar = ({ children }) => {
const [isOpen, setIsOpen] = useState(false);
openButtonRef = useRef(null);
const closeButtonRef = useRef(null);

const open = () => setIsOpen(true);
Expand Down Expand Up @@ -83,6 +87,7 @@ export default function Sidebar({ children }) {
<>
<PageActions>
<button
ref={openButtonRef}
onClick={open}
className="plain-control"
aria-label="Show navigation and other options"
Expand Down Expand Up @@ -125,4 +130,10 @@ export default function Sidebar({ children }) {
</nav>
</>
);
}
};

Sidebar.OpenButton = ({ children }) => (
<Button variant="link" isInline onClick={() => openButtonRef.current.click()}>{children}</Button>
);

export default Sidebar;

0 comments on commit f1a56eb

Please sign in to comment.