-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6785c18
commit aec23fc
Showing
4 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
.chart-book { | ||
$padding: 24px; | ||
$icon-width: 24px; | ||
|
||
margin: 0 (-$padding); | ||
|
||
.chart-book__header { | ||
padding: $padding; | ||
} | ||
|
||
.indicator-row { | ||
background-color: whitesmoke; | ||
} | ||
|
||
.indicator-row + .indicator-row { | ||
margin-top: 4px; | ||
} | ||
|
||
.indicator-row__header { | ||
padding: $padding; | ||
} | ||
|
||
.indicator-row__content { | ||
padding: 0 $padding; | ||
} | ||
|
||
.indicator-row__content > .spacer { | ||
width: 100%; | ||
height: $padding; | ||
} | ||
|
||
.indicator-row__header { | ||
position: relative; | ||
} | ||
|
||
.indicator-row__header > * { | ||
text-align: left; | ||
} | ||
|
||
.indicator-row__header > *:nth-last-child(2) { | ||
margin-right: $icon-width; | ||
} | ||
|
||
.indicator-row__header > *:last-child { | ||
position: absolute; | ||
width: $icon-width; | ||
top: $padding; | ||
right: $padding; | ||
text-align: right; | ||
} | ||
|
||
.indicator-row__content { | ||
overflow: hidden; | ||
transition: max-height 1s ease-in 1s; // TODO | ||
height: auto; | ||
max-height: 1.5 * $grapher-height; // TODO | ||
} | ||
|
||
.indicator-row__content.collapsed { | ||
transition: max-height 1s ease-out; | ||
max-height: 0; | ||
} | ||
|
||
.indicator-row__content h3 { | ||
margin-top: 0; | ||
} | ||
|
||
button { | ||
border: 0; | ||
padding: 0; | ||
cursor: pointer; | ||
background-color: transparent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useState } from "react" | ||
import cx from "classnames" | ||
import { | ||
EnrichedBlockChartBook, | ||
EnrichedBlockKeyIndicator, | ||
EnrichedBlockText, | ||
} from "@ourworldindata/types" | ||
import { urlToSlug } from "@ourworldindata/utils" | ||
import Chart from "./Chart.js" | ||
import Paragraph from "./Paragraph.js" | ||
|
||
export default function ChartBook({ | ||
d, | ||
className, | ||
}: { | ||
d: EnrichedBlockChartBook | ||
className?: string | ||
}) { | ||
const slugs = d.blocks.map((b: EnrichedBlockKeyIndicator) => | ||
urlToSlug(b.datapageUrl) | ||
) | ||
|
||
const [isOpen, setOpen] = useState<Record<string, boolean>>({ | ||
...makeDefault(slugs), | ||
[slugs[0]]: true, // the first block is open by default | ||
}) | ||
|
||
return ( | ||
<div className={cx("chart-book", className)}> | ||
<div className="chart-book__header grid grid-cols-12"> | ||
<div className="col-start-1 span-cols-3">Indicator name</div> | ||
<div className="col-start-4 span-cols-3">Source</div> | ||
<div className="col-start-7 span-cols-2">Date range</div> | ||
<div className="col-start-9 span-cols-2">Unit</div> | ||
<div className="col-start-11 span-cols-2">Last updated</div> | ||
</div> | ||
<div className="chart-book__rows"> | ||
{d.blocks.map((block: EnrichedBlockKeyIndicator) => { | ||
const slug = urlToSlug(block.datapageUrl) | ||
return ( | ||
<div className="indicator-row" key={slug}> | ||
<button | ||
className="indicator-row__header grid grid-cols-12" | ||
onClick={() => { | ||
if (isOpen[slug]) { | ||
// close block, leave others as they are | ||
setOpen({ | ||
...isOpen, | ||
[slug]: false, | ||
}) | ||
} else { | ||
// open block, close all others | ||
setOpen({ | ||
...makeDefault(slugs), | ||
[slug]: true, | ||
}) | ||
} | ||
}} | ||
> | ||
<div className="col-start-1 span-cols-3"> | ||
Share of population living in extreme | ||
poverty | ||
</div> | ||
<div className="col-start-4 span-cols-3"> | ||
World Bank and Inequality Platform | ||
</div> | ||
<div className="col-start-7 span-cols-2"> | ||
1967-2021 | ||
</div> | ||
<div className="col-start-9 span-cols-2">%</div> | ||
<div className="col-start-11 span-cols-2"> | ||
May 5, 2021 | ||
</div> | ||
<div className="icon">-</div> | ||
</button> | ||
<div | ||
className={cx( | ||
"indicator-row__content grid grid-cols-12", | ||
{ | ||
collapsed: !isOpen[slug], | ||
} | ||
)} | ||
> | ||
<div className="col-start-1 span-cols-5"> | ||
{/* which heading level is appropriate here? */} | ||
<h3>Some title</h3> | ||
{block.blurb.map( | ||
( | ||
textBlock: EnrichedBlockText, | ||
i: number | ||
) => ( | ||
<Paragraph d={textBlock} key={i} /> | ||
) | ||
)} | ||
</div> | ||
<Chart | ||
className="col-start-6 span-cols-7 margin-0" | ||
d={{ | ||
url: block.datapageUrl, | ||
type: "chart", | ||
parseErrors: [], | ||
}} | ||
/> | ||
<div className="spacer"></div> | ||
</div> | ||
</div> | ||
) | ||
})} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const makeDefault = (slugs: string[], defaultValue = false) => | ||
Object.fromEntries(slugs.map((s) => [s, defaultValue])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters