Skip to content

Commit

Permalink
🚧 Add expand/collapse transition
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed May 24, 2024
1 parent f5cf6b0 commit bcc4ac9
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions static-site/src/components/ListResources/Showcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Card, FilterOption, Group } from './types';

const cardWidthHeight = 160; // pixels
const expandPreviewHeight = 50 //pixels
// FIXME: remove transition on page load
const transitionStyle = "0.3s ease"

type ShowcaseProps = {
cards: Card[]
Expand All @@ -20,6 +22,7 @@ type ShowcaseProps = {
export const Showcase = ({cards, setSelectedFilterOptions}: ShowcaseProps) => {

const [numRows, setNumRows] = useState<number>(0);
const [showcaseHeight, setShowcaseHeight] = useState<number>(0);
const [isExpanded, setIsExpanded] = useState<boolean>(false);

const toggleExpand = () => {
Expand All @@ -35,6 +38,9 @@ export const Showcase = ({cards, setSelectedFilterOptions}: ShowcaseProps) => {
if (!container) return;

const cards = container.children;
if(showcaseHeight != container.clientHeight) {
setShowcaseHeight(container.clientHeight)
}

if (cards && cards.length > 0) {
const leftBorder = (cards[0] as HTMLElement).offsetLeft;
Expand All @@ -58,13 +64,13 @@ export const Showcase = ({cards, setSelectedFilterOptions}: ShowcaseProps) => {
<Byline>
Showcase resources: click to filter the resources to a pathogen
</Byline>
<ShowcaseContainer $isExpanded={isExpanded} $isExpandable={isExpandable}>
<ShowcaseContainer className={!isExpandable ? "singleRow" : isExpanded ? "expanded" : "collapsed"} $fullHeight={showcaseHeight}>
<CardsContainer ref={containerRef}>
{cards.map((el) => (
<ShowcaseTile card={el} key={el.name} setSelectedFilterOptions={setSelectedFilterOptions}/>
))}
</CardsContainer>
{isExpandable && !isExpanded && <PreviewOverlay onClick={toggleExpand} />}
<PreviewOverlay onClick={toggleExpand} className={!isExpandable || isExpanded ? "hidden" : "visible"} />
</ShowcaseContainer>
{isExpandable && <>
<ArrowButton onClick={toggleExpand}>
Expand Down Expand Up @@ -112,15 +118,22 @@ const ShowcaseTile = ({card, setSelectedFilterOptions}: ShowcaseTileProps) => {
element we can leverage the intelligent wrapping of the flexbox to decide how
many tiles to show in a single row. The downside is that showcase tiles are
still in the DOM, and the images are still fetched etc */
const ShowcaseContainer = styled.div<{$isExpanded: boolean, $isExpandable: boolean}>`
const ShowcaseContainer = styled.div<{$fullHeight: number}>`
position: relative;
height: ${(props) =>
props.$isExpandable ?
props.$isExpanded ?
"" : `${cardWidthHeight + expandPreviewHeight}px`
: `${cardWidthHeight}px`
};
&.singleRow {
max-height: ${cardWidthHeight}px
}
&.collapsed {
max-height: ${cardWidthHeight + expandPreviewHeight}px
}
&.expanded {
max-height: ${(props) => `${props.$fullHeight}px`}
}
transition: max-height ${transitionStyle};
overflow-y: hidden;
`

Expand All @@ -143,6 +156,16 @@ const PreviewOverlay = styled.div`
width: 100%;
height: ${expandPreviewHeight}px;
cursor: pointer;
&.visible {
opacity: 1;
}
&.hidden {
opacity: 0;
}
transition: opacity ${transitionStyle};
`;

const CardsContainer = styled.div`
Expand Down

0 comments on commit bcc4ac9

Please sign in to comment.