Skip to content

Commit bcc4ac9

Browse files
committed
🚧 Add expand/collapse transition
1 parent f5cf6b0 commit bcc4ac9

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

‎static-site/src/components/ListResources/Showcase.tsx

+32-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { Card, FilterOption, Group } from './types';
1111

1212
const cardWidthHeight = 160; // pixels
1313
const expandPreviewHeight = 50 //pixels
14+
// FIXME: remove transition on page load
15+
const transitionStyle = "0.3s ease"
1416

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

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

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

3740
const cards = container.children;
41+
if(showcaseHeight != container.clientHeight) {
42+
setShowcaseHeight(container.clientHeight)
43+
}
3844

3945
if (cards && cards.length > 0) {
4046
const leftBorder = (cards[0] as HTMLElement).offsetLeft;
@@ -58,13 +64,13 @@ export const Showcase = ({cards, setSelectedFilterOptions}: ShowcaseProps) => {
5864
<Byline>
5965
Showcase resources: click to filter the resources to a pathogen
6066
</Byline>
61-
<ShowcaseContainer $isExpanded={isExpanded} $isExpandable={isExpandable}>
67+
<ShowcaseContainer className={!isExpandable ? "singleRow" : isExpanded ? "expanded" : "collapsed"} $fullHeight={showcaseHeight}>
6268
<CardsContainer ref={containerRef}>
6369
{cards.map((el) => (
6470
<ShowcaseTile card={el} key={el.name} setSelectedFilterOptions={setSelectedFilterOptions}/>
6571
))}
6672
</CardsContainer>
67-
{isExpandable && !isExpanded && <PreviewOverlay onClick={toggleExpand} />}
73+
<PreviewOverlay onClick={toggleExpand} className={!isExpandable || isExpanded ? "hidden" : "visible"} />
6874
</ShowcaseContainer>
6975
{isExpandable && <>
7076
<ArrowButton onClick={toggleExpand}>
@@ -112,15 +118,22 @@ const ShowcaseTile = ({card, setSelectedFilterOptions}: ShowcaseTileProps) => {
112118
element we can leverage the intelligent wrapping of the flexbox to decide how
113119
many tiles to show in a single row. The downside is that showcase tiles are
114120
still in the DOM, and the images are still fetched etc */
115-
const ShowcaseContainer = styled.div<{$isExpanded: boolean, $isExpandable: boolean}>`
121+
const ShowcaseContainer = styled.div<{$fullHeight: number}>`
116122
position: relative;
117-
height: ${(props) =>
118-
props.$isExpandable ?
119-
props.$isExpanded ?
120-
"" : `${cardWidthHeight + expandPreviewHeight}px`
121-
: `${cardWidthHeight}px`
122123
123-
};
124+
&.singleRow {
125+
max-height: ${cardWidthHeight}px
126+
}
127+
128+
&.collapsed {
129+
max-height: ${cardWidthHeight + expandPreviewHeight}px
130+
}
131+
132+
&.expanded {
133+
max-height: ${(props) => `${props.$fullHeight}px`}
134+
}
135+
136+
transition: max-height ${transitionStyle};
124137
overflow-y: hidden;
125138
`
126139

@@ -143,6 +156,16 @@ const PreviewOverlay = styled.div`
143156
width: 100%;
144157
height: ${expandPreviewHeight}px;
145158
cursor: pointer;
159+
160+
&.visible {
161+
opacity: 1;
162+
}
163+
164+
&.hidden {
165+
opacity: 0;
166+
}
167+
168+
transition: opacity ${transitionStyle};
146169
`;
147170

148171
const CardsContainer = styled.div`

0 commit comments

Comments
 (0)