Skip to content

Commit

Permalink
Add ability to show/hide panel options
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Nov 13, 2023
1 parent 970fe1c commit 8a69ca2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
25 changes: 25 additions & 0 deletions src/components/controls/panelChevron.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import styled from 'styled-components';
import { FaChevronRight, FaChevronDown } from "react-icons/fa";

const Container = styled.span`
padding-right: 6px;
color: ${(props) => props.theme.color};
`

type Props = {
show: boolean
}

/**
* An interactive chevron to show/hide a panel's options.
*/
export const PanelChevron = ({ show }: Props) => {
const icon = show ? <FaChevronDown /> : <FaChevronRight />

return (
<Container>
{icon}
</Container>
)
}
51 changes: 39 additions & 12 deletions src/components/controls/panelHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { togglePanelDisplay } from "../../actions/panelDisplay";
import { HeaderContainer } from "./styles";
import Toggle from "./toggle";
import { AnnotatedTitle, Title, Tooltip } from "./annotatedTitle";
import { PanelChevron } from "./panelChevron";

/** Panel identifier used internally. */
export type PanelId = string;
Expand All @@ -15,30 +16,56 @@ type Props = {

/** Indicates panel visibility. */
panelIsVisible: boolean

/** Indicates whether there are options for the panel. */
hasOptions: boolean

/** Indicates options visibility. */
optionsAreVisible: boolean

/** Update options visibility. */
setOptionsAreVisible: React.Dispatch<React.SetStateAction<boolean>>
}

/**
* A header used by all panel controls, containing an interactive title.
*/
export const PanelHeader = ({ panel, title, tooltip, panelIsVisible }: Props) => {
export const PanelHeader = ({ panel, title, tooltip, panelIsVisible, hasOptions, optionsAreVisible, setOptionsAreVisible }: Props) => {
const dispatch = useAppDispatch();

function togglePanelVisibility() {
dispatch(togglePanelDisplay(panel))
}

function toggleOptionsVisibility() {
setOptionsAreVisible(!optionsAreVisible);
}

return (
<HeaderContainer>
<AnnotatedTitle
title={title}
tooltip={tooltip} />
<Toggle
display={true}
on={panelIsVisible}
callback={togglePanelVisibility}
label=""
style={{ height: "21px" }} // height from ToggleBackground. FIXME: this should be hardcoded in Toggle
/>
<HeaderContainer {...(hasOptions ? {
onClick: toggleOptionsVisibility,
style: {cursor: "pointer"}
} :
{} )}>
<span>
{hasOptions &&
<PanelChevron
show={optionsAreVisible} />}
<AnnotatedTitle
title={title}
tooltip={tooltip} />
</span>
<span
// Don't allow the parent click handler to do anything here.
onClick={(event) => event.stopPropagation()}>
<Toggle
display={true}
on={panelIsVisible}
callback={togglePanelVisibility}
label=""
style={{ height: "21px" }} // height from ToggleBackground. FIXME: this should be hardcoded in Toggle
/>
</span>
</HeaderContainer>
);
};
13 changes: 12 additions & 1 deletion src/components/controls/panelSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ export const PanelSection = ({ panel, title, tooltip, options=undefined }: Props

const panelIsVisible = panelsToDisplay.includes(panel)

// Initially, panel visibility determines options visibility.
const [optionsAreVisible, setOptionsAreVisible] = React.useState(panelIsVisible);

// Subsequent panel visibility updates also determines options visibility.
React.useEffect(() => {
setOptionsAreVisible(panelIsVisible)
}, [panelIsVisible])

return (
<PanelSectionContainer>
<PanelHeader
panel={panel}
title={title}
tooltip={tooltip}
panelIsVisible={panelIsVisible}
hasOptions={options!==undefined}
optionsAreVisible={optionsAreVisible}
setOptionsAreVisible={setOptionsAreVisible}
/>
{panelIsVisible && options}
{optionsAreVisible && options}
</PanelSectionContainer>
);
};
2 changes: 1 addition & 1 deletion src/components/controls/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const PanelSectionContainer = styled.div`
`;

export const TitleAndIconContainer = styled.span`
display: flex;
display: inline-flex;
align-items: center;
`;

Expand Down

0 comments on commit 8a69ca2

Please sign in to comment.