Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 5,323 Bytes
a8fb296 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import { useMemo, useRef, useState, type ReactNode } from 'react'
import { SectionContext, useInViewOnce } from './hooks'
import TitleBlock from './TitleBlock'
import ScreenPager from './ScreenPager'
import { CHAPTERS, SECTION_EYEBROW } from '../../utils/matrixStory'
import { SECTIONS, type MatrixCtx, type SectionId, type TableSpec } from './types'
/** A real <table> of the rows and values a chart draws (same scope, same formatting). The first column is the row header. */
export function DataTable({ id, caption, spec }: { id: string; caption: string; spec: TableSpec }) {
return (
<div className="folio-datatable" id={id}>
<table className="folio-table folio-table-auto">
<caption className="folio-sr-only">{caption}</caption>
<thead>
<tr>
{spec.columns.map((c, i) => (
<th key={i} scope="col" className={c.numeric ? 'text-right' : 'text-left'}>
{c.label}
</th>
))}
</tr>
</thead>
<tbody>
{spec.rows.map((r, ri) => (
<tr key={ri}>
{r.map((cell, ci) =>
ci === 0 ? (
<th key={ci} scope="row" className="text-left folio-rowhead">
{cell}
</th>
) : (
<td key={ci} className={spec.columns[ci]?.numeric ? 'text-right' : 'text-left'}>
{cell}
</td>
),
)}
</tr>
))}
</tbody>
</table>
{spec.rows.length === 0 && <p className="folio-muted">No data available for this view.</p>}
</div>
)
}
interface Props {
ctx: MatrixCtx
id: SectionId
/** The section's story sentence (matrixStory.ts). It rises line by line (80 ms / 160 ms stagger) the first time it enters view. */
headline: string
/** One supporting sentence under the rule, or null to leave it out. */
lead?: string | null
/** Short text column: how to read the figure, with real numbers from the data in scope. */
text: ReactNode
/** Per-section controls (above the figure). */
controls?: ReactNode
/** The figure's caption, including the scope. */
caption: ReactNode
/** The rows the chart draws, for "Show data table". Evaluated only while the table is open. */
table?: () => TableSpec | null
/** Put the figure full width under the text (the Table and Heatmap sections). */
wide?: boolean
children: ReactNode
}
/**
* One screen of the report: a plain eyebrow, the headline and one supporting sentence on the left (the longer explanation sits
* under "More about this chart"), the figure with its caption on the right (about 45 / 55 with a hairline between), and the
* Continue / Back controls under the text. Stacked text-first below 1024 px. Each screen is sized to the viewport (folio.css).
*/
export default function SectionShell({ ctx, id, headline, lead, text, controls, caption, table, wide, children }: Props) {
const ref = useRef<HTMLElement>(null)
const seen = useInViewOnce(ref)
const [el, setEl] = useState<HTMLElement | null>(null)
const name = SECTIONS.find((s) => s.id === id)?.name ?? id
const opens = CHAPTERS.some((c) => c.sections[0] === id)
const open = ctx.sp.get('table') === id
const spec = useMemo(() => (open && table ? table() : null), [open, table])
return (
<section
id={id}
ref={(n) => {
const holder = ref as { current: HTMLElement | null }
holder.current = n
if (n !== el) setEl(n)
}}
className={`folio-section${seen ? ' is-in' : ''}${wide ? ' folio-section-wide' : ''}${opens ? ' has-opener' : ''}`}
aria-labelledby={`${id}-h`}
data-section={id}
>
<div className="folio-spread">
<div className="folio-spread-text">
<TitleBlock as="h3" id={`${id}-h`} eyebrow={SECTION_EYEBROW[id]} text={headline} variant="section" />
<hr className="folio-rule" />
{lead && <p className="folio-lede">{lead}</p>}
<details className="folio-more">
<summary>More about this chart</summary>
<div className="folio-body">{text}</div>
</details>
</div>
<div className="folio-spread-fig">
{controls && <div className="folio-controls">{controls}</div>}
<SectionContext.Provider value={{ seen, el }}>
<div className={`folio-figure${seen ? '' : ' folio-prime'}`}>{children}</div>
</SectionContext.Provider>
<p className="folio-fig">{caption}</p>
{table && (
<>
<button
type="button"
className="folio-btn"
aria-expanded={open}
aria-controls={`${id}-data`}
onClick={() => ctx.setQP('table', open ? null : id)}
>
{open ? 'Hide data table' : 'Show data table'} {open ? '↑' : '↓'}
</button>
{open && spec && <DataTable id={`${id}-data`} caption={`Data table: ${name}`} spec={spec} />}
{open && !spec && <p className="folio-muted" id={`${id}-data`}>No data available for this view.</p>}
</>
)}
</div>
<ScreenPager ctx={ctx} section={id} />
</div>
</section>
)
}
|