react-cheminfo 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/README.md +16 -0
  2. package/lib/citation/core/index.d.ts +1 -0
  3. package/lib/citation/core/index.d.ts.map +1 -1
  4. package/lib/citation/core/index.js +1 -0
  5. package/lib/citation/core/index.js.map +1 -1
  6. package/lib/citation/core/platformPaper.d.ts +14 -0
  7. package/lib/citation/core/platformPaper.d.ts.map +1 -0
  8. package/lib/citation/core/platformPaper.js +28 -0
  9. package/lib/citation/core/platformPaper.js.map +1 -0
  10. package/lib/color/core/index.d.ts +1 -1
  11. package/lib/color/core/index.d.ts.map +1 -1
  12. package/lib/color/core/index.js +1 -1
  13. package/lib/color/core/index.js.map +1 -1
  14. package/lib/core.d.ts +1 -0
  15. package/lib/core.d.ts.map +1 -1
  16. package/lib/core.js +1 -0
  17. package/lib/core.js.map +1 -1
  18. package/lib/ecosystem/core/sites.js +5 -5
  19. package/lib/orbital/core/index.d.ts +1 -1
  20. package/lib/orbital/core/index.d.ts.map +1 -1
  21. package/lib/orbital/core/index.js +1 -1
  22. package/lib/orbital/core/index.js.map +1 -1
  23. package/lib/periodic/core/categories.d.ts +25 -0
  24. package/lib/periodic/core/categories.d.ts.map +1 -0
  25. package/lib/periodic/core/categories.js +70 -0
  26. package/lib/periodic/core/categories.js.map +1 -0
  27. package/lib/periodic/core/elements.d.ts +49 -0
  28. package/lib/periodic/core/elements.d.ts.map +1 -0
  29. package/lib/periodic/core/elements.js +175 -0
  30. package/lib/periodic/core/elements.js.map +1 -0
  31. package/lib/periodic/core/index.d.ts +6 -0
  32. package/lib/periodic/core/index.d.ts.map +1 -0
  33. package/lib/periodic/core/index.js +4 -0
  34. package/lib/periodic/core/index.js.map +1 -0
  35. package/lib/periodic/core/layout.d.ts +73 -0
  36. package/lib/periodic/core/layout.d.ts.map +1 -0
  37. package/lib/periodic/core/layout.js +132 -0
  38. package/lib/periodic/core/layout.js.map +1 -0
  39. package/lib/periodic/ui/CategoryLegend.d.ts +26 -0
  40. package/lib/periodic/ui/CategoryLegend.d.ts.map +1 -0
  41. package/lib/periodic/ui/CategoryLegend.js +53 -0
  42. package/lib/periodic/ui/CategoryLegend.js.map +1 -0
  43. package/lib/periodic/ui/ElementCell.d.ts +53 -0
  44. package/lib/periodic/ui/ElementCell.d.ts.map +1 -0
  45. package/lib/periodic/ui/ElementCell.js +56 -0
  46. package/lib/periodic/ui/ElementCell.js.map +1 -0
  47. package/lib/periodic/ui/PeriodicTable.d.ts +90 -0
  48. package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -0
  49. package/lib/periodic/ui/PeriodicTable.js +77 -0
  50. package/lib/periodic/ui/PeriodicTable.js.map +1 -0
  51. package/lib/periodic/ui/PeriodicTableChrome.d.ts +36 -0
  52. package/lib/periodic/ui/PeriodicTableChrome.d.ts.map +1 -0
  53. package/lib/periodic/ui/PeriodicTableChrome.js +72 -0
  54. package/lib/periodic/ui/PeriodicTableChrome.js.map +1 -0
  55. package/lib/periodic/ui/index.d.ts +7 -0
  56. package/lib/periodic/ui/index.d.ts.map +1 -0
  57. package/lib/periodic/ui/index.js +4 -0
  58. package/lib/periodic/ui/index.js.map +1 -0
  59. package/lib/ui.d.ts +1 -0
  60. package/lib/ui.d.ts.map +1 -1
  61. package/lib/ui.js +1 -0
  62. package/lib/ui.js.map +1 -1
  63. package/package.json +2 -1
  64. package/src/citation/core/index.ts +1 -0
  65. package/src/citation/core/platformPaper.ts +32 -0
  66. package/src/color/core/index.ts +6 -1
  67. package/src/core.ts +1 -0
  68. package/src/ecosystem/core/sites.ts +5 -5
  69. package/src/orbital/core/index.ts +1 -1
  70. package/src/periodic/core/categories.ts +83 -0
  71. package/src/periodic/core/elements.ts +217 -0
  72. package/src/periodic/core/index.ts +27 -0
  73. package/src/periodic/core/layout.ts +183 -0
  74. package/src/periodic/ui/CategoryLegend.tsx +105 -0
  75. package/src/periodic/ui/ElementCell.tsx +137 -0
  76. package/src/periodic/ui/PeriodicTable.tsx +226 -0
  77. package/src/periodic/ui/PeriodicTableChrome.tsx +170 -0
  78. package/src/periodic/ui/index.ts +6 -0
  79. package/src/ui.ts +1 -0
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Where every element is drawn.
3
+ *
4
+ * The group and the period place the main block, but not the lanthanoids and
5
+ * the actinoids: those have no group, and are drawn on the two rows underneath.
6
+ * The grid is therefore 18 columns by 10 rows — seven periods, a blank spacer,
7
+ * and the two inner-transition rows.
8
+ *
9
+ * The cells here are the *logical* ones, so column 1 is group 1 and row 1 is
10
+ * period 1. A table drawn with the group and period header strips offsets them
11
+ * by one; nothing in this module needs to know that.
12
+ */
13
+
14
+ import type { ElementCategory, PeriodicElement } from './elements.ts';
15
+ import { PERIODIC_ELEMENTS, elementBySymbol } from './elements.ts';
16
+
17
+ /** A whole run of the table: one group, or one period. */
18
+ export interface ElementRange {
19
+ kind: 'group' | 'period';
20
+ value: number;
21
+ }
22
+
23
+ /** A cell of the grid, one-based as CSS grid lines are. */
24
+ export interface Cell {
25
+ column: number;
26
+ row: number;
27
+ }
28
+
29
+ /** Columns of the table. */
30
+ export const COLUMN_COUNT = 18;
31
+
32
+ /** Rows of the table, the blank spacer between the blocks included. */
33
+ export const ROW_COUNT = 10;
34
+
35
+ /** Row the lanthanoids are drawn on, and the actinoids on the one below. */
36
+ const INNER_TRANSITION_ROW = 9;
37
+
38
+ /** Atomic number the lanthanoids and the actinoids start at. */
39
+ const INNER_TRANSITION_STARTS = [57, 89] as const;
40
+
41
+ /** How many elements each inner-transition series holds. */
42
+ const INNER_TRANSITION_LENGTH = 15;
43
+
44
+ /** Column the inner-transition rows start at, under the d block. */
45
+ const INNER_TRANSITION_COLUMN = 3;
46
+
47
+ /**
48
+ * The cell an element occupies.
49
+ * @param element - Element to place.
50
+ * @returns Its column and row, both one-based.
51
+ */
52
+ export function cellOf(element: PeriodicElement): Cell {
53
+ const inner = innerTransitionCell(element);
54
+ if (inner !== null) return inner;
55
+ return { column: element.group ?? 1, row: element.period };
56
+ }
57
+
58
+ /**
59
+ * Every element with the cell it occupies, in reading order.
60
+ * @returns One entry per element, ordered by row then column, so a keyboard
61
+ * walk over the list moves the way the eye does.
62
+ */
63
+ export function placedElements(): ReadonlyArray<{
64
+ element: PeriodicElement;
65
+ cell: Cell;
66
+ }> {
67
+ PLACED ??= PERIODIC_ELEMENTS.map((element) => ({
68
+ element,
69
+ cell: cellOf(element),
70
+ })).toSorted((first, second) =>
71
+ first.cell.row === second.cell.row
72
+ ? first.cell.column - second.cell.column
73
+ : first.cell.row - second.cell.row,
74
+ );
75
+ return PLACED;
76
+ }
77
+
78
+ let PLACED: ReadonlyArray<{ element: PeriodicElement; cell: Cell }> | null =
79
+ null;
80
+
81
+ /**
82
+ * The two cells the lanthanoids and the actinoids were lifted out of.
83
+ *
84
+ * Without them the main block has a hole in it and the two rows underneath
85
+ * belong nowhere; with them the reader can see where each series was taken
86
+ * from.
87
+ */
88
+ export const INNER_TRANSITION_MARKERS: ReadonlyArray<{
89
+ cell: Cell;
90
+ label: string;
91
+ category: ElementCategory;
92
+ }> = [
93
+ { cell: { column: 3, row: 6 }, label: '57–71', category: 'lanthanoid' },
94
+ { cell: { column: 3, row: 7 }, label: '89–103', category: 'actinoid' },
95
+ ];
96
+
97
+ /** The period each inner-transition row belongs to, for its row label. */
98
+ export const INNER_TRANSITION_ROWS: ReadonlyArray<{
99
+ row: number;
100
+ period: number;
101
+ }> = [
102
+ { row: INNER_TRANSITION_ROW, period: 6 },
103
+ { row: INNER_TRANSITION_ROW + 1, period: 7 },
104
+ ];
105
+
106
+ /**
107
+ * The element an arrow key walks to.
108
+ *
109
+ * The walk follows the grid, not the atomic number: down from carbon is
110
+ * silicon, the cell underneath it, and a step of 18 protons would instead be
111
+ * chromium because period 2 holds only eight elements. Left and right follow
112
+ * reading order, so the end of a period continues at the start of the next one
113
+ * and the two inner-transition rows come after the main block.
114
+ * @param key - The pressed key, as `KeyboardEvent.key` reports it.
115
+ * @param from - Symbol the walk starts at; any arrow lands on hydrogen when it names no element.
116
+ * @returns The element to move to, or `null` when the key is not an arrow or the table has no cell that way.
117
+ */
118
+ export function elementByArrowKey(
119
+ key: string,
120
+ from: string | undefined,
121
+ ): PeriodicElement | null {
122
+ const direction = ARROW_DIRECTIONS[key];
123
+ if (direction === undefined) return null;
124
+
125
+ const placed = placedElements();
126
+ const start = from === undefined ? undefined : elementBySymbol(from);
127
+ if (start === undefined) return placed[0]?.element ?? null;
128
+
129
+ if (direction.column !== 0) {
130
+ const index = readingOrderIndex().get(start.symbol);
131
+ if (index === undefined) return null;
132
+ return placed[index + direction.column]?.element ?? null;
133
+ }
134
+
135
+ const cell = cellOf(start);
136
+ return byCell().get(cellKey(cell.column, cell.row + direction.row)) ?? null;
137
+ }
138
+
139
+ /** Which way each arrow key moves, in grid cells. */
140
+ const ARROW_DIRECTIONS: Record<string, { column: number; row: number }> = {
141
+ ArrowRight: { column: 1, row: 0 },
142
+ ArrowLeft: { column: -1, row: 0 },
143
+ ArrowDown: { column: 0, row: 1 },
144
+ ArrowUp: { column: 0, row: -1 },
145
+ };
146
+
147
+ function readingOrderIndex(): Map<string, number> {
148
+ READING_ORDER ??= new Map(
149
+ placedElements().map(({ element }, index) => [element.symbol, index]),
150
+ );
151
+ return READING_ORDER;
152
+ }
153
+
154
+ let READING_ORDER: Map<string, number> | null = null;
155
+
156
+ function byCell(): Map<string, PeriodicElement> {
157
+ BY_CELL ??= new Map(
158
+ placedElements().map(({ element, cell }) => [
159
+ cellKey(cell.column, cell.row),
160
+ element,
161
+ ]),
162
+ );
163
+ return BY_CELL;
164
+ }
165
+
166
+ let BY_CELL: Map<string, PeriodicElement> | null = null;
167
+
168
+ function cellKey(column: number, row: number): string {
169
+ return `${String(column)}:${String(row)}`;
170
+ }
171
+
172
+ function innerTransitionCell(element: PeriodicElement): Cell | null {
173
+ for (const [index, start] of INNER_TRANSITION_STARTS.entries()) {
174
+ const offset = element.atomicNumber - start;
175
+ if (offset >= 0 && offset < INNER_TRANSITION_LENGTH) {
176
+ return {
177
+ column: INNER_TRANSITION_COLUMN + offset,
178
+ row: INNER_TRANSITION_ROW + index,
179
+ };
180
+ }
181
+ }
182
+ return null;
183
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * The key to the family colours, under the table.
3
+ */
4
+
5
+ import type { CSSProperties, ReactElement } from 'react';
6
+
7
+ import {
8
+ CATEGORY_LABELS,
9
+ CATEGORY_ORDER,
10
+ categorySwatch,
11
+ } from '../core/categories.ts';
12
+ import type { ElementCategory } from '../core/elements.ts';
13
+
14
+ /** What {@link CategoryLegend} needs. */
15
+ export interface CategoryLegendProps {
16
+ /**
17
+ * Called with the family whose swatch was clicked. Without it the legend is
18
+ * a key rather than a control, and nothing in it is focusable.
19
+ * @default undefined
20
+ */
21
+ onSelect?: (category: ElementCategory) => void;
22
+ /**
23
+ * The family drawn as the active one.
24
+ * @default undefined — none is
25
+ */
26
+ selected?: ElementCategory;
27
+ }
28
+
29
+ /**
30
+ * The ten families, each with its colour.
31
+ * @param props - See {@link CategoryLegendProps}.
32
+ * @returns The legend.
33
+ */
34
+ export function CategoryLegend(props: CategoryLegendProps): ReactElement {
35
+ const { onSelect, selected } = props;
36
+
37
+ return (
38
+ <div style={legendStyle}>
39
+ {CATEGORY_ORDER.map((category) => {
40
+ const swatch = categorySwatch(category);
41
+ const label = CATEGORY_LABELS[category];
42
+ const mark = (
43
+ <span style={{ ...swatchStyle, background: swatch.background }} />
44
+ );
45
+ if (onSelect === undefined) {
46
+ return (
47
+ <span key={category} style={itemStyle}>
48
+ {mark}
49
+ {label}
50
+ </span>
51
+ );
52
+ }
53
+ return (
54
+ <button
55
+ key={category}
56
+ type="button"
57
+ aria-pressed={category === selected}
58
+ onClick={() => {
59
+ onSelect(category);
60
+ }}
61
+ style={{
62
+ ...itemStyle,
63
+ ...buttonStyle,
64
+ fontWeight: category === selected ? 700 : 400,
65
+ }}
66
+ >
67
+ {mark}
68
+ {label}
69
+ </button>
70
+ );
71
+ })}
72
+ </div>
73
+ );
74
+ }
75
+
76
+ const legendStyle = {
77
+ color: 'rgb(95 107 124)',
78
+ display: 'flex',
79
+ flexWrap: 'wrap',
80
+ fontSize: 11,
81
+ gap: '2px 10px',
82
+ } as const satisfies CSSProperties;
83
+
84
+ const itemStyle = {
85
+ alignItems: 'center',
86
+ display: 'inline-flex',
87
+ gap: 4,
88
+ } as const satisfies CSSProperties;
89
+
90
+ const buttonStyle = {
91
+ background: 'none',
92
+ border: 'none',
93
+ color: 'inherit',
94
+ cursor: 'pointer',
95
+ font: 'inherit',
96
+ padding: 0,
97
+ } as const satisfies CSSProperties;
98
+
99
+ const swatchStyle = {
100
+ border: '1px solid rgb(255 255 255 / 0.55)',
101
+ borderRadius: 2,
102
+ display: 'inline-block',
103
+ height: 10,
104
+ width: 10,
105
+ } as const satisfies CSSProperties;
@@ -0,0 +1,137 @@
1
+ /**
2
+ * One cell of the periodic table: the atomic number, the symbol, and whatever
3
+ * the tool asked to be written under it.
4
+ *
5
+ * A cell is a real `<button>`, so Tab reaches it, Enter and Space activate it,
6
+ * and a screen reader reads the element's name rather than its symbol.
7
+ */
8
+
9
+ import type { CSSProperties, ReactElement } from 'react';
10
+
11
+ import type { Swatch } from '../../color/core/scale.ts';
12
+
13
+ /** What {@link ElementCell} needs to draw one element. */
14
+ export interface ElementCellProps {
15
+ /** Atomic number, written small in the corner. */
16
+ atomicNumber: number;
17
+ /** Chemical symbol, the largest thing in the cell. */
18
+ symbol: string;
19
+ /** Full name, which is what the cell is announced as. */
20
+ name: string;
21
+ /** Background, and the ink that stays readable on it. */
22
+ swatch: Swatch;
23
+ /** Column of the grid, one-based. */
24
+ column: number;
25
+ /** Row of the grid, one-based. */
26
+ row: number;
27
+ onSelect: (symbol: string) => void;
28
+ /**
29
+ * Third line, under the symbol: the value the tool is showing.
30
+ * @default '' — nothing is written
31
+ */
32
+ detail?: string;
33
+ /**
34
+ * Whether the cell is the one the tools are pointed at.
35
+ * @default false
36
+ */
37
+ isSelected?: boolean;
38
+ /**
39
+ * Whether the cell is inside the set the tool is showing. Everything outside
40
+ * it is dimmed rather than removed, so the table keeps its shape.
41
+ * @default true
42
+ */
43
+ isIncluded?: boolean;
44
+ /**
45
+ * Called on pointer enter with the symbol, and on leave with `null`.
46
+ * @default undefined
47
+ */
48
+ onHover?: (symbol: string | null) => void;
49
+ }
50
+
51
+ /**
52
+ * A single element of the table.
53
+ * @param props - See {@link ElementCellProps}.
54
+ * @returns The cell.
55
+ */
56
+ export function ElementCell(props: ElementCellProps): ReactElement {
57
+ const {
58
+ atomicNumber,
59
+ symbol,
60
+ name,
61
+ swatch,
62
+ column,
63
+ row,
64
+ onSelect,
65
+ detail = '',
66
+ isSelected = false,
67
+ isIncluded = true,
68
+ onHover,
69
+ } = props;
70
+
71
+ return (
72
+ <button
73
+ type="button"
74
+ data-testid={`element-${symbol}`}
75
+ data-symbol={symbol}
76
+ aria-label={`${name} (${symbol}, Z = ${String(atomicNumber)})`}
77
+ aria-pressed={isSelected}
78
+ onClick={() => {
79
+ onSelect(symbol);
80
+ }}
81
+ onPointerEnter={() => onHover?.(symbol)}
82
+ onPointerLeave={() => onHover?.(null)}
83
+ style={{
84
+ ...cellStyle,
85
+ gridColumn: column,
86
+ gridRow: row,
87
+ background: swatch.background,
88
+ color: swatch.foreground,
89
+ opacity: isIncluded ? 1 : 0.28,
90
+ // An outline rather than a fill: a table coloured by a property must
91
+ // keep saying what the value is while a cell is selected.
92
+ outline: isSelected ? `2px solid ${swatch.foreground}` : 'none',
93
+ outlineOffset: -3,
94
+ }}
95
+ >
96
+ <span style={numberStyle}>{atomicNumber}</span>
97
+ <span style={symbolStyle}>{symbol}</span>
98
+ {detail === '' ? null : <span style={detailStyle}>{detail}</span>}
99
+ </button>
100
+ );
101
+ }
102
+
103
+ const cellStyle = {
104
+ alignItems: 'center',
105
+ border: '1px solid rgb(255 255 255 / 0.55)',
106
+ borderRadius: 3,
107
+ cursor: 'pointer',
108
+ display: 'flex',
109
+ flexDirection: 'column',
110
+ font: 'inherit',
111
+ justifyContent: 'center',
112
+ lineHeight: 1.05,
113
+ minWidth: 0,
114
+ overflow: 'hidden',
115
+ padding: 1,
116
+ transition: 'opacity 120ms ease',
117
+ } as const satisfies CSSProperties;
118
+
119
+ const numberStyle = {
120
+ alignSelf: 'flex-start',
121
+ fontSize: 'clamp(0.34rem, 1.6cqw, 0.5rem)',
122
+ opacity: 0.8,
123
+ paddingLeft: 2,
124
+ } as const satisfies CSSProperties;
125
+
126
+ const symbolStyle = {
127
+ fontSize: 'clamp(0.55rem, 3.3cqw, 1.05rem)',
128
+ fontWeight: 700,
129
+ } as const satisfies CSSProperties;
130
+
131
+ const detailStyle = {
132
+ fontSize: 'clamp(0.34rem, 1.6cqw, 0.5rem)',
133
+ maxWidth: '100%',
134
+ overflow: 'hidden',
135
+ textOverflow: 'ellipsis',
136
+ whiteSpace: 'nowrap',
137
+ } as const satisfies CSSProperties;
@@ -0,0 +1,226 @@
1
+ /**
2
+ * The periodic table.
3
+ *
4
+ * It knows nothing about what it is showing: the caller says what colour each
5
+ * cell takes and what is written in it, and gets back which element was
6
+ * clicked. That is what lets the same grid be an element picker, a property
7
+ * map, and a chart's selection control.
8
+ *
9
+ * Everything site-specific arrives through a callback keyed on the element, so
10
+ * a site's own richer element record never has to cross into this component.
11
+ */
12
+
13
+ import type { CSSProperties, KeyboardEvent, ReactElement } from 'react';
14
+ import { useEffect, useRef } from 'react';
15
+
16
+ import type { Swatch } from '../../color/core/scale.ts';
17
+ import { categorySwatch } from '../core/categories.ts';
18
+ import type { PeriodicElement } from '../core/elements.ts';
19
+ import type { ElementRange } from '../core/layout.ts';
20
+ import {
21
+ COLUMN_COUNT,
22
+ elementByArrowKey,
23
+ placedElements,
24
+ } from '../core/layout.ts';
25
+
26
+ import { CategoryLegend } from './CategoryLegend.tsx';
27
+ import { ElementCell } from './ElementCell.tsx';
28
+ import {
29
+ HeaderStrips,
30
+ InnerTransitionMarkers,
31
+ } from './PeriodicTableChrome.tsx';
32
+
33
+ /** What {@link PeriodicTable} needs. */
34
+ export interface PeriodicTableProps {
35
+ /**
36
+ * Symbol of the element the tools are pointed at.
37
+ * @default undefined — none is
38
+ */
39
+ selected?: string;
40
+ /**
41
+ * Called with the symbol of the element that was clicked, and by the arrow
42
+ * keys.
43
+ * @default undefined — the table is a figure rather than a control
44
+ */
45
+ onSelect?: (symbol: string) => void;
46
+ /**
47
+ * The colour each cell takes.
48
+ * @default the family colour
49
+ */
50
+ swatchOf?: (element: PeriodicElement) => Swatch;
51
+ /**
52
+ * What is written under the symbol; an empty string writes nothing.
53
+ * @default nothing is written
54
+ */
55
+ detailOf?: (element: PeriodicElement) => string;
56
+ /**
57
+ * How an element is named, for the label a screen reader reads. The hook a
58
+ * site translating the table writes its own names through.
59
+ * @default the English name
60
+ */
61
+ nameOf?: (element: PeriodicElement) => string;
62
+ /**
63
+ * Whether an element is inside the set the tool is showing. Everything
64
+ * outside it is dimmed rather than removed, so the table keeps its shape.
65
+ * @default every element is
66
+ */
67
+ isIncluded?: (element: PeriodicElement) => boolean;
68
+ /**
69
+ * Whether to draw the group and period strips.
70
+ * @default false
71
+ */
72
+ headers?: boolean;
73
+ /**
74
+ * Called when a whole group or period header is clicked. Without it the
75
+ * strips are labels rather than buttons.
76
+ * @default undefined
77
+ */
78
+ onSelectRange?: (range: ElementRange) => void;
79
+ /**
80
+ * Whether to draw the family legend under the grid.
81
+ * @default false
82
+ */
83
+ legend?: boolean;
84
+ /**
85
+ * Whether the dashed markers stand where the two inner-transition series were
86
+ * lifted out of the main block.
87
+ * @default true
88
+ */
89
+ markers?: boolean;
90
+ /**
91
+ * Whether the arrow keys walk the table by atomic number.
92
+ * @default true
93
+ */
94
+ keyboard?: boolean;
95
+ /**
96
+ * Called on pointer enter with the symbol, and on leave with `null`.
97
+ * @default undefined
98
+ */
99
+ onHover?: (symbol: string | null) => void;
100
+ }
101
+
102
+ /**
103
+ * The 118 elements, laid out as the table.
104
+ * @param props - See {@link PeriodicTableProps}.
105
+ * @returns The grid, its optional chrome, and its optional legend.
106
+ */
107
+ export function PeriodicTable(props: PeriodicTableProps): ReactElement {
108
+ const {
109
+ selected,
110
+ onSelect,
111
+ swatchOf = defaultSwatchOf,
112
+ detailOf,
113
+ nameOf = defaultNameOf,
114
+ isIncluded,
115
+ headers = false,
116
+ onSelectRange,
117
+ legend = false,
118
+ markers = true,
119
+ keyboard = true,
120
+ onHover,
121
+ } = props;
122
+
123
+ const gridRef = useRef<HTMLDivElement>(null);
124
+ const cameFromKeyRef = useRef(false);
125
+ const offset = headers ? 1 : 0;
126
+
127
+ useEffect(() => {
128
+ if (!cameFromKeyRef.current) return;
129
+ cameFromKeyRef.current = false;
130
+ const cell = gridRef.current?.querySelector<HTMLButtonElement>(
131
+ `[data-symbol="${CSS.escape(selected ?? '')}"]`,
132
+ );
133
+ cell?.focus();
134
+ cell?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
135
+ }, [selected]);
136
+
137
+ function handleKeyDown(event: KeyboardEvent<HTMLDivElement>): void {
138
+ if (!keyboard || onSelect === undefined) return;
139
+ const next = elementByArrowKey(event.key, selected);
140
+ if (next === null) return;
141
+ event.preventDefault();
142
+ cameFromKeyRef.current = true;
143
+ onSelect(next.symbol);
144
+ }
145
+
146
+ return (
147
+ <div style={rootStyle}>
148
+ <div
149
+ ref={gridRef}
150
+ role="grid"
151
+ aria-label="Periodic table"
152
+ data-testid="periodic-table"
153
+ style={headers ? gridWithHeadersStyle : gridStyle}
154
+ onKeyDown={handleKeyDown}
155
+ >
156
+ {headers ? <HeaderStrips onSelectRange={onSelectRange} /> : null}
157
+ {markers ? <InnerTransitionMarkers offset={offset} /> : null}
158
+ {placedElements().map(({ element, cell }) => (
159
+ <ElementCell
160
+ key={element.symbol}
161
+ atomicNumber={element.atomicNumber}
162
+ symbol={element.symbol}
163
+ name={nameOf(element)}
164
+ detail={detailOf?.(element)}
165
+ swatch={swatchOf(element)}
166
+ isSelected={element.symbol === selected}
167
+ isIncluded={isIncluded?.(element)}
168
+ column={cell.column + offset}
169
+ row={cell.row + offset}
170
+ onSelect={onSelect ?? noop}
171
+ onHover={onHover}
172
+ />
173
+ ))}
174
+ </div>
175
+ {legend ? <CategoryLegend /> : null}
176
+ </div>
177
+ );
178
+ }
179
+
180
+ function defaultSwatchOf(element: PeriodicElement): Swatch {
181
+ return categorySwatch(element.category);
182
+ }
183
+
184
+ function defaultNameOf(element: PeriodicElement): string {
185
+ return element.name;
186
+ }
187
+
188
+ function noop(): void {
189
+ // A table with no `onSelect` is a figure; its cells stay buttons so the
190
+ // keyboard and a screen reader still reach every element.
191
+ }
192
+
193
+ const rootStyle = {
194
+ display: 'flex',
195
+ flexDirection: 'column',
196
+ gap: 8,
197
+ } as const satisfies CSSProperties;
198
+
199
+ const baseGridStyle = {
200
+ display: 'grid',
201
+ gap: 2,
202
+ width: '100%',
203
+ // The cells size their type against this box rather than against the page,
204
+ // so the same table reads at 320px beside a chart and at 900px on its own.
205
+ containerType: 'inline-size',
206
+ } as const satisfies CSSProperties;
207
+
208
+ const gridStyle = {
209
+ ...baseGridStyle,
210
+ // The eighth row is the gap the inner-transition series are lifted out into.
211
+ gridTemplateColumns: `repeat(${String(COLUMN_COUNT)}, minmax(0, 1fr))`,
212
+ gridTemplateRows:
213
+ 'repeat(7, minmax(0, 1fr)) 0.5rem repeat(2, minmax(0, 1fr))',
214
+ aspectRatio: `${String(COLUMN_COUNT)} / 9.7`,
215
+ minWidth: 280,
216
+ } as const satisfies CSSProperties;
217
+
218
+ const gridWithHeadersStyle = {
219
+ ...baseGridStyle,
220
+ // A leading column and a leading row hold the period and group numbers.
221
+ gridTemplateColumns: `1.4rem repeat(${String(COLUMN_COUNT)}, minmax(0, 1fr))`,
222
+ gridTemplateRows:
223
+ '1rem repeat(7, minmax(0, 1fr)) 0.5rem repeat(2, minmax(0, 1fr))',
224
+ aspectRatio: `${String(COLUMN_COUNT + 1.2)} / 10.6`,
225
+ minWidth: 280,
226
+ } as const satisfies CSSProperties;