Skip to content

Commit

Permalink
Add ability to show/hide options when panel is on
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Oct 23, 2023
1 parent bc770ec commit a69a067
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 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: 28 additions & 5 deletions src/components/controls/panelHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { HeaderContainer } from "./styles";
import PanelToggle from "./panelToggle";
import { AnnotatedTitle } from "./annotatedTitle";
import { PanelChevron } from "./panelChevron";

type Props = {
/** Panel identifier used internally. */
Expand All @@ -16,20 +17,42 @@ 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>>

/** Indicates mobile display. */
mobile: boolean
}

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

let titleContainerProps = {}

if (hasOptions) {
titleContainerProps = {
role: "button",
onClick: () => setOptionsAreVisible(!optionsAreVisible),
}
}

return (
<HeaderContainer>
<AnnotatedTitle
title={title}
tooltip={tooltip}
mobile={mobile} />
<span {...titleContainerProps}>
{hasOptions && <PanelChevron show={optionsAreVisible} />}
<AnnotatedTitle
title={title}
tooltip={tooltip}
mobile={mobile} />
</span>
<PanelToggle
panel={panel}
on={panelIsVisible} />
Expand Down
13 changes: 12 additions & 1 deletion src/components/controls/panelSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export const PanelSection = ({ panel, title, tooltip, options=undefined, mobile

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
Expand All @@ -38,8 +46,11 @@ export const PanelSection = ({ panel, title, tooltip, options=undefined, mobile
tooltip={tooltip}
panelIsVisible={panelIsVisible}
mobile={mobile}
hasOptions={options!==undefined}
optionsAreVisible={optionsAreVisible}
setOptionsAreVisible={setOptionsAreVisible}
/>
{panelIsVisible && options}
{optionsAreVisible && options}
</PanelSectionContainer>
);
};

0 comments on commit a69a067

Please sign in to comment.