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 f310202 commit 0f31bcb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 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>
)
}
33 changes: 26 additions & 7 deletions src/components/controls/panelHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import { useAppDispatch } from "../../hooks";
import { togglePanelDisplay } from "../../actions/panelDisplay";
import { HeaderContainer } from "./styles";
import { PanelSectionHeaderContainer } 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,28 +16,46 @@ 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} />
<PanelSectionHeaderContainer onClick={toggleOptionsVisibility}>
<span>
{hasOptions &&
<PanelChevron
show={optionsAreVisible} />}
<AnnotatedTitle
title={title}
tooltip={tooltip} />
</span>
<Toggle
display={true}
on={panelIsVisible}
callback={togglePanelVisibility}
label="" />
</HeaderContainer>
</PanelSectionHeaderContainer>
);
};
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>
);
};
6 changes: 5 additions & 1 deletion src/components/controls/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export const HeaderContainer = styled.div`
margin-bottom: 5px;
`;

export const PanelSectionHeaderContainer = styled(HeaderContainer)`
cursor: pointer;
`

export const PanelSectionContainer = styled.div`
// Less padding is necessary on the top because there is already some space
// from HeaderContainer's top margin.
Expand All @@ -48,7 +52,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 0f31bcb

Please sign in to comment.