@enikakis99/family-tree 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 enikakis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,575 @@
1
- # Temporary Holding Version
1
+ # @enikakis99/family-tree
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ One person's family as a tree you scroll, not pan: a React component that fits the width and works
4
+ on phones and with screen readers.
5
+
6
+ [![CI](https://img.shields.io/github/actions/workflow/status/enikakis/family-tree/ci.yml?branch=main&label=CI)](https://github.com/enikakis/family-tree/actions/workflows/ci.yml)
7
+ [![npm](https://img.shields.io/npm/v/@enikakis99/family-tree)](https://www.npmjs.com/package/@enikakis99/family-tree)
8
+ [![license: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
9
+
10
+ **[Live demo](https://enikakis.github.io/family-tree/)**
11
+
12
+ <a href="https://enikakis.github.io/family-tree/">
13
+ <picture>
14
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/enikakis/family-tree/main/docs/images/demo-dark.png">
15
+ <img src="https://raw.githubusercontent.com/enikakis/family-tree/main/docs/images/demo-light.png" alt="The live demo, in English, with the names in Greek letters: Dimos Karydas in the middle, his parents, grandparents and great-grandparents above, his sister and half-brother beside him, and his children and grandchildren below. The card says how he is related to Leon Karydas: grandfather, related by blood in the direct line, 2nd degree." width="825">
16
+ </picture>
17
+ </a>
18
+
19
+ It is built on [`@enikakis99/kinship`](https://github.com/enikakis/kinship), which does the family
20
+ lookups (parents, children, half-siblings) and reads GEDCOM files into the data the tree takes.
21
+
22
+ Many family tree views draw the whole family as one picture, in an SVG or on a canvas, that you
23
+ drag and zoom. On a phone that is awkward, and screen readers cannot read it. This one shows one
24
+ person at a time: their ancestors above, their brothers and sisters beside them, the person in the
25
+ middle, and their descendants below, grouped by marriage. Click anyone and they move to the middle.
26
+
27
+ - **You scroll, you don't pan or zoom.** The tree is as wide as its container and as long as the
28
+ family, and the page scrolls as any page does.
29
+ - **It fits the width.** As many generations of ancestors as fit, cards in three densities (full,
30
+ smaller, and given names only on the top row), and descendants that branch out as a tree on wide
31
+ screens and become an indented list on narrow ones.
32
+ - **It is real HTML.** Nested lists and real links, so screen readers, the keyboard, "open in new
33
+ tab" and the browser's find all work. The lines between the cards are a decorative SVG layer.
34
+
35
+ ## Install
36
+
37
+ ```sh
38
+ npm install @enikakis99/family-tree
39
+ ```
40
+
41
+ React and React DOM 18 or 19 are peer dependencies, which your app already has;
42
+ `@enikakis99/kinship` comes with the package. Your tooling needs Node 22.18 or later, and the tree
43
+ runs in any modern browser. Minified and gzipped (esbuild, React and React DOM left out, kinship
44
+ included, measured 2026-11-27): `FamilyTree` with the Greek and English texts, about 10.0 kB; the
45
+ `./layout` entry alone, about 2.4 kB. `style.css` is about 5.7 kB gzipped.
46
+
47
+ ## Usage
48
+
49
+ ```tsx
50
+ import { FamilyTree } from "@enikakis99/family-tree";
51
+ import { en } from "@enikakis99/family-tree/locales/en";
52
+ import "@enikakis99/family-tree/style.css";
53
+ import { useState } from "react";
54
+
55
+ // Outside the component, so the tree gets the same data and functions on every render.
56
+ const family = {
57
+ people: [
58
+ { id: "petros", sex: "male", given: "Petros", surname: "Papadopoulos", born: 1890, died: 1962 },
59
+ { id: "sofia", sex: "female", given: "Sofia", surname: "Papadopoulou", born: 1895, died: 1971 },
60
+ { id: "nikos", sex: "male", given: "Nikos", surname: "Papadopoulos", born: 1920, died: 1998 },
61
+ { id: "maria", sex: "female", given: "Maria", surname: "Papadopoulou", born: 1925, died: 2010 },
62
+ { id: "eleni", sex: "female", given: "Eleni", surname: "Papadopoulou", born: 1950 },
63
+ { id: "pavlos", sex: "male", given: "Pavlos", surname: "Papadopoulos", born: 1953 },
64
+ { id: "katerina", sex: "female", given: "Katerina", surname: "Papadopoulou", born: 1956 },
65
+ ],
66
+ parents: [
67
+ ["petros", "nikos"], // [parent, child]
68
+ ["sofia", "nikos"],
69
+ ["nikos", "eleni"],
70
+ ["maria", "eleni"],
71
+ ["nikos", "pavlos"],
72
+ ["maria", "pavlos"],
73
+ ],
74
+ unions: [
75
+ { a: "petros", b: "sofia" },
76
+ { a: "nikos", b: "maria", married: 1948, place: "Patras" },
77
+ { a: "pavlos", b: "katerina", married: 1979 },
78
+ ],
79
+ } as const;
80
+
81
+ const href = (id: string) => `#${id}`;
82
+
83
+ export function App() {
84
+ const [focus, setFocus] = useState("pavlos");
85
+ return <FamilyTree data={family} focus={focus} href={href} onOpen={setFocus} locale={en} />;
86
+ }
87
+ ```
88
+
89
+ This example keeps the person in state only, so a link opened in a new tab shows Pavlos again. To
90
+ make the links work, read the address too ([Moving around](#moving-around)).
91
+
92
+ Import `@enikakis99/family-tree/style.css` once, anywhere in your app. The tree's layout, colours
93
+ and themes are all in that one stylesheet: without it, the tree is unstyled HTML.
94
+
95
+ Keep `data`, `href`, `locale`, `person`, `union` and `renderFocus` the same from one render to the
96
+ next: define them at module level, as here, or make them with `useMemo` and `useCallback`. New ones
97
+ on every render make the tree redraw its cards and lines each time, and new `data`, `person` or
98
+ `union` make it index the family again.
99
+
100
+ The tree takes the width of its container, never the width of its content: it sets
101
+ `container-type: inline-size` on its root. So in a flex row or an `auto` grid column it shrinks to
102
+ nothing. Give it a width there: `flex: 1` on the tree (through `className`) or on its wrapper, a
103
+ `minmax(0, 1fr)` grid column, or a `width`.
104
+
105
+ Keep the same tree mounted while `focus` changes. When `focus` changes, the tree scrolls to the
106
+ person, with their parents in view, and moves the keyboard focus to their name. It never does this
107
+ on its first drawing, so a tree that is mounted again for each person (a `key` that changes, a new
108
+ page) does neither.
109
+
110
+ With the Next.js app router, render the tree from a client component of your own: its props include
111
+ functions, which a server component cannot pass ([Server rendering and
112
+ Next.js](#server-rendering-and-nextjs)).
113
+
114
+ ## Your data
115
+
116
+ The data is kinship's input shape, with your own fields on people and marriages:
117
+
118
+ - `people`: your person objects. Each needs an `id` and a `sex` (`"female"`, `"male"` or
119
+ `"unknown"`); every other field is yours.
120
+ - `parents`: `[parentId, childId]` pairs.
121
+ - `unions`: marriages, as `{ a, b }` with the two spouses' ids, and any fields of yours.
122
+
123
+ `parents` and `unions` may be left out. Links and marriages that name an unknown id are skipped.
124
+
125
+ By default the tree reads these fields of a person, when they are there:
126
+
127
+ | Field | What it shows |
128
+ |---|---|
129
+ | `name` | The full name. Without it, `given` and `surname` joined; without those, the `id`. |
130
+ | `given` | The given name, on the cards of ancestors and in captions such as "Children with Maria". Without it, the full name. |
131
+ | `surname` | The surname, with the given name on the cards of ancestors, and in the initials. |
132
+ | `nickname` | A nickname, shown in quotes: “Takis”, in Greek «Τάκης». |
133
+ | `birthSurname` | The surname at birth, as "née Andreou" under a spouse's name in the person's card, when it differs from `surname`. |
134
+ | `born`, `died` | The years, as numbers: "1890–1962", "1925–", "d. 1962". `born` also orders brothers and sisters, and children, oldest first. |
135
+ | `living` | `true` for someone alive: their years read "b. 1950", not "1950–". |
136
+ | `photo` | An image URL for the avatar. Without it, the avatar shows the initials. |
137
+
138
+ And these of a union:
139
+
140
+ | Field | What it shows |
141
+ |---|---|
142
+ | `married` | The wedding year, as a number: "married 1948". It also orders a person's marriages. |
143
+ | `place` | Where they married, after the year: "married 1948, Patras". |
144
+
145
+ Only finite numbers count as years, and text of only spaces counts as missing.
146
+
147
+ When your fields have other names, pass readers. `person` takes a `PersonFields` object and `union`
148
+ a `UnionFields` object: a function for each field you read your own way, named as above. The other
149
+ fields keep their defaults. Two readers have no field behind them: `years` replaces the whole
150
+ lifespan ("1890–1962"), and `wedding` the whole wedding line ("1948, Patras").
151
+
152
+ ```tsx
153
+ import { type FamilyInput, FamilyTree, type PersonFields, type Sex } from "@enikakis99/family-tree";
154
+
155
+ // Your people, as your database or API gives them.
156
+ type Relative = {
157
+ id: string;
158
+ sex: Sex;
159
+ firstName: string;
160
+ lastName: string;
161
+ birthYear: number;
162
+ deathYear?: number;
163
+ image?: string;
164
+ };
165
+
166
+ // Outside the component, like the data: new readers on every render would index the family again.
167
+ export const readers: PersonFields<Relative> = {
168
+ given: (person) => person.firstName,
169
+ surname: (person) => person.lastName,
170
+ born: (person) => person.birthYear,
171
+ died: (person) => person.deathYear,
172
+ photo: (person) => person.image,
173
+ };
174
+
175
+ const href = (id: string) => `/people/${encodeURIComponent(id)}`;
176
+
177
+ type Props = { data: FamilyInput<Relative>; focus: string; onOpen: (id: string) => void };
178
+
179
+ export function RelativesTree({ data, focus, onOpen }: Props) {
180
+ return <FamilyTree data={data} focus={focus} href={href} onOpen={onOpen} person={readers} />;
181
+ }
182
+ ```
183
+
184
+ Without a `name` reader, the full name is `given` and `surname` joined, so here it comes from
185
+ `firstName` and `lastName`.
186
+
187
+ A GEDCOM file needs no readers. kinship's `fromGedcom` gives this shape, with `name`, `given`,
188
+ `surname`, `born` and `died` on people and `married` and `place` on unions. The ids are the file's
189
+ record ids, such as `I1`. Add `@enikakis99/kinship` to your own dependencies to import it:
190
+
191
+ ```ts
192
+ import { fromGedcom } from "@enikakis99/kinship/gedcom";
193
+
194
+ const data = fromGedcom(text); // the file's contents, as a string
195
+ ```
196
+
197
+ What it reads and what it skips is in
198
+ [kinship's README](https://github.com/enikakis/kinship#fromgedcomtext-read-a-gedcom-file).
199
+
200
+ ## Props
201
+
202
+ | Prop | Type | Default | What it does |
203
+ |---|---|---|---|
204
+ | `data` | `FamilyInput<P, U>` | required | Your people, parent links and marriages ([Your data](#your-data)). |
205
+ | `focus` | `string` | required | The id of the person in the middle. An id that is not in `data` draws an empty tree. When it changes, the tree moves: it scrolls to the person, with their parents in view, and their name takes the focus. |
206
+ | `href` | `(id: string) => string` | required | The address of a person's link. Every person is a real link, so new tabs and the back button work. |
207
+ | `onOpen` | `(id: string) => void` | none: links just navigate | Called with a person's id on a plain click; modified clicks (a new tab or window) follow the link. Set `focus` in it, in your own state, not through anything that settles later (a router's transition). See [Moving around](#moving-around). |
208
+ | `locale` | `FamilyTreeLocale` | English | The tree's texts: `el` from `@enikakis99/family-tree/locales/el`, `en` from `@enikakis99/family-tree/locales/en`, or your own ([Languages](#languages)). |
209
+ | `person` | `PersonFields<P>` | the fields in [Your data](#your-data) | Readers for your person shape: name, years, photo… |
210
+ | `union` | `UnionFields<U>` | the fields in [Your data](#your-data) | Readers for your union shape: the wedding. |
211
+ | `renderFocus` | `(person: P) => ReactNode` | none | Your own content in the person's card: dates and places, a life story, buttons. The classes `ft-facts` (for a `dl`) and `ft-bio` are styled for it ([The person's card](#the-persons-card)). |
212
+ | `nameLang` | `string` | none | `lang` on names, when they are in another language than the texts. |
213
+ | `theme` | `"light" \| "dark"` | follows the system | Light or dark ([Theming](#theming)). |
214
+ | `className` | `string` | none | Classes added to the root element. |
215
+
216
+ `P` and `U` are your person and union types, inferred from `data`. Keep `data`, `href`, `locale`,
217
+ `person`, `union` and `renderFocus` the same between renders ([Usage](#usage)). The root entry
218
+ exports the types: `FamilyTreeProps`, `FamilyInput`, `PersonFields`, `UnionFields`,
219
+ `FamilyTreeLocale`, `UnionRecord`, `Person` and `Sex`.
220
+
221
+ ## Moving around
222
+
223
+ Every person in the tree is a real link to `href(id)`, so new tabs, copied links and the back button
224
+ work. A plain click on a person calls `onOpen(id)` instead of following the link, and so does Enter
225
+ on a focused one. Clicks with a modifier key (Ctrl, Cmd, Shift, Alt) and middle clicks follow the
226
+ link, as every click does when there is no `onOpen`.
227
+
228
+ In `onOpen`, set `focus` to the id. To keep the address in step, change it there too, and set
229
+ `focus` from it on back and forward, as the [Next.js example](#server-rendering-and-nextjs) does.
230
+ With a router, navigate in `onOpen` and derive `focus` from the address, as long as the router
231
+ changes it synchronously:
232
+
233
+ - Where the browser has view transitions and the reader has not asked for less motion, `onOpen` runs
234
+ inside one (`document.startViewTransition`, with React's `flushSync`), and the avatars glide from
235
+ their old places to their new ones. For that, `focus` must change while `onOpen` runs, in the same
236
+ frame.
237
+ - A router that changes the address in a React transition, or after loading data, settles too late:
238
+ the tree still moves, without the glide.
239
+ - Whatever `onOpen` returns is ignored, so the view transition never waits for a navigation's
240
+ promise.
241
+
242
+ After a move, once the new person is drawn, the page scrolls so their card sits about a third of the
243
+ window's height below the top (15rem at most), with their parents in view above it, and the keyboard
244
+ focus moves to their name. This never happens on the first drawing, nor when only the data changes,
245
+ so keep the same tree mounted ([Usage](#usage)).
246
+
247
+ - **Every scrolling ancestor scrolls.** The tree calls `scrollIntoView` on the card, so the window
248
+ and any scrolling box of yours around the tree all scroll. The scroll is instant.
249
+ - **Fixed headers.** A fixed header covers the top of the window. Set `scroll-padding-top` to its
250
+ height on the scrolling element (`html` for the window), and the card lands below it.
251
+ - **Near the top.** When the card is close to the top of the page, the page scrolls only as far as
252
+ it can.
253
+
254
+ When the reader asks for less motion (`prefers-reduced-motion: reduce`), `onOpen` runs without a
255
+ view transition, and the stylesheet turns off the avatars' animation in any view transition of
256
+ yours.
257
+
258
+ The package times the avatars' glide: half a second, easing out. Each avatar has
259
+ `view-transition-class: ft-person`, and `style.css` times `::view-transition-group(*.ft-person)`, so
260
+ the avatars glide this way in any view transition on the page, yours too. The rest of the page
261
+ crossfades in the root transition, which is yours to time. The browser's default is a quarter of a
262
+ second. To match the avatars:
263
+
264
+ ```css
265
+ ::view-transition-group(root) {
266
+ animation-duration: 0.5s;
267
+ }
268
+ ```
269
+
270
+ ## The person's card
271
+
272
+ The card in the middle shows the person's avatar, name, nickname and years, then their spouses with
273
+ the weddings. `renderFocus` puts your own content between the years and the spouses: facts, a life
274
+ story, links or buttons. It is called with the person in the middle: one of your own person objects.
275
+
276
+ Two classes are styled for it:
277
+
278
+ - `ft-facts`, on a `dl`, with each row a `div` holding a `dt` and a `dd`;
279
+ - `ft-bio`, on a paragraph. It keeps the line breaks of your text.
280
+
281
+ ```tsx
282
+ import type { Sex } from "@enikakis99/family-tree";
283
+
284
+ // Your people: fields the tree reads by default, the places as kinship's `fromGedcom` gives them,
285
+ // and one of your own, `bio`.
286
+ type Relative = {
287
+ id: string;
288
+ sex: Sex;
289
+ given: string;
290
+ surname: string;
291
+ born?: number;
292
+ died?: number;
293
+ birthPlace?: string;
294
+ deathPlace?: string;
295
+ bio?: string;
296
+ };
297
+
298
+ /** The year and the place, as far as they are known: "1920, Patras". */
299
+ const when = (year?: number, place?: string) => [year, place].filter((part) => part !== undefined).join(", ");
300
+
301
+ // At module level, like the data: a new function on every render would redraw the tree.
302
+ export function renderFocus(person: Relative) {
303
+ const facts = [
304
+ { term: "Born", text: when(person.born, person.birthPlace) },
305
+ { term: "Died", text: when(person.died, person.deathPlace) },
306
+ ].filter((fact) => fact.text !== "");
307
+ // Nothing to show: null, not an empty fragment, so the spouses keep the card's whole width.
308
+ if (facts.length === 0 && !person.bio) return null;
309
+ return (
310
+ <>
311
+ {facts.length > 0 && (
312
+ <dl className="ft-facts">
313
+ {facts.map(({ term, text }) => (
314
+ <div key={term}>
315
+ <dt>{term}</dt>
316
+ <dd>{text}</dd>
317
+ </div>
318
+ ))}
319
+ </dl>
320
+ )}
321
+ {person.bio && <p className="ft-bio">{person.bio}</p>}
322
+ </>
323
+ );
324
+ }
325
+ ```
326
+
327
+ Pass it as `renderFocus={renderFocus}`. When there is nothing to show, return `null`, not an
328
+ empty fragment: the card counts a fragment as content, even an empty one, and gives it the first
329
+ column on wide screens.
330
+
331
+ The card is at most 40rem wide. In a tree 60rem wide or more it widens to 48rem, and your content
332
+ and the spouses sit side by side in two columns; either one alone takes the whole width. Content
333
+ wider than its column runs out of the card instead of widening it: an image without `max-width`, or
334
+ a wide table. Give images `max-width: 100%`, and put a table in a box with `overflow-x: auto`.
335
+
336
+ ## Theming
337
+
338
+ The tree has a light and a dark theme, and follows the system's setting
339
+ (`prefers-color-scheme`). `theme="light"` or `theme="dark"` fixes one. It sets `data-ft-theme` on
340
+ the tree's root element, the only place the stylesheet reads it: a theme attribute or class on your
341
+ page's `html` does not reach the tree. A site with its own light and dark switch passes the reader's
342
+ choice through `theme`, and leaves it out to follow the system, as the demo does.
343
+
344
+ The colours, the corner radius and the display font are custom properties, set on the tree's root:
345
+
346
+ | Property | What it sets | Light | Dark |
347
+ |---|---|---|---|
348
+ | `--ft-bg` | The background of the tree and of its cards | `hsl(40 20% 99%)` | `hsl(220 18% 9%)` |
349
+ | `--ft-fg` | The text | `hsl(220 15% 14%)` | `hsl(40 15% 92%)` |
350
+ | `--ft-muted` | The background of the avatars, and of a hovered sibling, spouse or descendant | `hsl(40 15% 96%)` | `hsl(220 14% 15%)` |
351
+ | `--ft-muted-fg` | Secondary text: years, nicknames, captions, the ancestors' generation labels, the initials; and the border of a living person's avatar | `hsl(220 8% 40%)` | `hsl(220 8% 65%)` |
352
+ | `--ft-border` | The borders of the cards and avatars, and the rules between and inside them | `hsl(220 12% 90%)` | `hsl(220 12% 20%)` |
353
+ | `--ft-accent` | The person's card and avatar, the line down to them from their parents, the marriage rings, the half-sibling tag, the mark for more ancestors, the "Show more" button and "see" links, the rule beside `ft-bio`, a hovered avatar | `hsl(15 55% 42%)` | `hsl(15 55% 60%)` |
354
+ | `--ft-accent-fg` | Text on the accent: the mark for more ancestors, hovered | `hsl(40 20% 99%)` | `hsl(220 18% 9%)` |
355
+ | `--ft-line` | The lines between the ancestors, and to the brothers and sisters | `hsl(220 9% 70%)` | `hsl(220 8% 36%)` |
356
+ | `--ft-gen-1` | The children: their avatars' rings, the lines down to them, their generation label | `hsl(165 45% 32%)` | `hsl(165 40% 52%)` |
357
+ | `--ft-gen-2` | The grandchildren, the same way | `hsl(212 55% 45%)` | `hsl(212 60% 66%)` |
358
+ | `--ft-gen-3` | The great-grandchildren | `hsl(280 32% 48%)` | `hsl(280 38% 70%)` |
359
+ | `--ft-gen-4` | The great-great-grandchildren | `hsl(32 70% 38%)` | `hsl(36 70% 58%)` |
360
+ | `--ft-radius` | The cards' corner radius (twice that on the person's card) | `0.375rem` | the same |
361
+ | `--ft-font-display` | The font of the person's name and of the initials | `inherit` | the same |
362
+
363
+ The theme sets them with no specificity (in `:where(…)`), so any rule of yours on the tree's root
364
+ wins, in both themes, without `!important`: a class passed through `className`, or a rule for
365
+ `.ft-root`, as long as it is not in a cascade layer (below).
366
+
367
+ A value of yours holds in both themes. To give the dark theme its own, write the dark rules the way
368
+ the stylesheet does: dark with the system unless `theme="light"`, and dark whenever `theme="dark"`.
369
+ Here `my-tree` is the class passed through `className`:
370
+
371
+ ```css
372
+ .my-tree {
373
+ --ft-accent: hsl(210 60% 40%);
374
+ --ft-font-display: Georgia, serif;
375
+ }
376
+ @media (prefers-color-scheme: dark) {
377
+ .my-tree:not([data-ft-theme="light"]) {
378
+ --ft-accent: hsl(210 60% 65%);
379
+ }
380
+ }
381
+ .my-tree[data-ft-theme="dark"] {
382
+ --ft-accent: hsl(210 60% 65%);
383
+ }
384
+ ```
385
+
386
+ The accent is also a text colour (the person's initials, the half-sibling tag), so give it at least
387
+ 4.5:1 contrast with `--ft-bg` and `--ft-muted` in each theme, as these two have. `--ft-accent-fg` is
388
+ the text on the accent; its default, the theme's background, suits an accent like these. A site
389
+ with its own theme variables can set `--ft-accent: var(--brand)` once, on the tree's root, and let
390
+ the page switch `--brand` between its themes, as the demo does. The demo sets the background, the
391
+ accent and the display font this way, so the screenshot at the top is not the tree's default look.
392
+
393
+ Set them on the root itself. The theme sets every one there, so a value on an element above it
394
+ (`:root`, `body`) does not reach the tree. The exception is `--ft-font-display`: its default,
395
+ `inherit`, takes the value of an element above that sets it; without one, the name and the initials
396
+ keep the font around the tree.
397
+
398
+ Every class the tree uses starts with `ft-`, and so does every custom property, so they do not
399
+ clash with yours.
400
+
401
+ `style.css` is not in a cascade layer, and CSS outside any layer beats CSS in a layer, whatever the
402
+ specificity. So the tree's defaults beat your layered rules, such as Tailwind v4's utilities: an
403
+ arbitrary property like `[--ft-accent:…]` in `className` loses. You can import the stylesheet into a
404
+ layer, from your CSS instead of from JavaScript, after Tailwind's own import:
405
+
406
+ ```css
407
+ @import "tailwindcss";
408
+ @import "@enikakis99/family-tree/style.css" layer(components);
409
+ ```
410
+
411
+ Layers are ordered by where they first appear, so the tree's import must come after Tailwind's:
412
+ before it, `components` would come ahead of Tailwind's `base`, and Tailwind's reset would beat the
413
+ tree. Once the stylesheet is in a layer, every rule of yours outside one beats the tree's, so a
414
+ site-wide `a { … }` would restyle the people's links too.
415
+
416
+ Or leave the stylesheet unlayered, and keep your overrides of the tree outside any layer.
417
+
418
+ ## Languages
419
+
420
+ The tree's texts come in Greek and English: `el` from `@enikakis99/family-tree/locales/el` and `en`
421
+ from `@enikakis99/family-tree/locales/en`. English is the default. The tree's root takes the texts'
422
+ language as its `lang`.
423
+
424
+ The Greek texts put the article and the accusative after «με», from the given name and the sex:
425
+ «Παιδιά με τον Παύλο», «Με τη Μαρία». A name of unknown sex stays as it is, without an article.
426
+
427
+ For another language, write an object of type `FamilyTreeLocale` (from the root entry). Every member
428
+ is documented in [`src/locale.ts`](src/locale.ts), with where it appears: on the page, or only for
429
+ screen readers. Some are functions, for counts and names: `childCount(3)` is "3 children",
430
+ `childrenWith({ given: "Maria", sex: "female" })` is "Children with Maria". Start from
431
+ `{ ...en, lang: "fr", … }` to keep English for what you have not translated yet. Define it at module
432
+ level, like the data: a new object on every render redraws the tree.
433
+
434
+ Names stay as given: the texts are translated, the names are not. When the names are in another
435
+ language than the texts, such as Greek names under English texts, set `nameLang` to theirs. It
436
+ becomes the names' `lang`, so screen readers read them in that language and browsers hyphenate them
437
+ by its rules.
438
+
439
+ ## Accessibility
440
+
441
+ - **Real HTML.** The tree is a region named after the person ("Family tree: Pavlos Papadopoulos").
442
+ Each of its parts (ancestors, siblings, the person, descendants) is a labelled section, the people
443
+ are in nested lists, and the person's name is a heading. Each couple of ancestors is introduced to
444
+ screen readers as whose parents they are ("Pavlos Papadopoulos, parents:").
445
+ - **Headings.** The person's name is an `h2`, and the heading over their spouses ("Spouse" or
446
+ "Spouses") an `h3`. The levels are fixed, so place the tree where an `h2` fits your page's
447
+ outline, such as under the page's `h1`.
448
+ - **Decoration stays out of the way.** The lines (an SVG layer), the generation labels in the side
449
+ gutter and the avatars are hidden from screen readers (`aria-hidden`).
450
+ - **The focus follows a move.** After a move, the new person's name takes the keyboard focus, so
451
+ screen readers announce it.
452
+ - **The keyboard.** Every person is a link: Tab reaches them all in page order, and Enter opens one.
453
+ "Show more" is a button, and after it the focus goes to the first child it revealed.
454
+ - **Checked with axe.** The browser tests run axe-core on the tree at 360, 768 and 1280px wide, in
455
+ the light and the dark theme, around three people of the test family, with the lines and without
456
+ them (axe cannot see through an SVG). They also check that a hovered person's initials keep their
457
+ contrast, in both themes.
458
+ - **Reduced motion.** With `prefers-reduced-motion: reduce`, there is no glide
459
+ ([Moving around](#moving-around)).
460
+
461
+ ## Server rendering and Next.js
462
+
463
+ The root entry, `@enikakis99/family-tree`, is marked `"use client"`: with React Server Components,
464
+ as in the Next.js app router, `FamilyTree` is a client component. The `./layout` and `./locales/*`
465
+ entries have no React and no such mark.
466
+
467
+ On the server, the tree renders its region, empty but named after the person, and draws the family
468
+ in the browser: what fits depends on its width, which only the browser can measure. Its first render
469
+ in the browser is the same empty region, so hydration matches.
470
+
471
+ Its props include functions: `href`, `onOpen`, `renderFocus`, the readers, and the texts, which are
472
+ objects with functions. A server component cannot pass functions to a client component, so render
473
+ the tree from a client component of your own. It takes what the server can pass, such as the family
474
+ and the id from the route, and holds the functions itself:
475
+
476
+ ```tsx
477
+ "use client";
478
+
479
+ import { type FamilyInput, FamilyTree, type Person } from "@enikakis99/family-tree";
480
+ import { useEffect, useState } from "react";
481
+
482
+ // A person's page is /people/<id>.
483
+ const href = (id: string) => `/people/${encodeURIComponent(id)}`;
484
+
485
+ /** The id in a person's address, or null on any other page. */
486
+ function idIn(path: string): string | null {
487
+ const id = /^\/people\/([^/]+)$/.exec(path)?.[1];
488
+ return id === undefined ? null : decodeURIComponent(id);
489
+ }
490
+
491
+ // What a server component can pass: data, no functions.
492
+ type Props = { data: FamilyInput<Person>; id: string };
493
+
494
+ export function FamilyView({ data, id }: Props) {
495
+ const [focus, setFocus] = useState(id);
496
+
497
+ // The address says who is in the middle: when the view mounts, and on back and forward.
498
+ useEffect(() => {
499
+ const read = () => {
500
+ const shown = idIn(location.pathname);
501
+ if (shown !== null) setFocus(shown);
502
+ };
503
+ read();
504
+ window.addEventListener("popstate", read);
505
+ return () => window.removeEventListener("popstate", read);
506
+ }, []);
507
+
508
+ // A new address, and the person in the middle at once, so the avatars glide.
509
+ const onOpen = (next: string) => {
510
+ history.pushState(null, "", href(next));
511
+ setFocus(next);
512
+ };
513
+
514
+ return <FamilyTree data={data} focus={focus} href={href} onOpen={onOpen} />;
515
+ }
516
+ ```
517
+
518
+ Your page, a server component such as `app/people/[id]/page.tsx`, loads the family and renders
519
+ `<FamilyView data={data} id={id} />`. A locale, readers or `renderFocus` go in `FamilyView` too,
520
+ since the page cannot pass them. `onOpen` changes the address with the browser's
521
+ `history.pushState`, which the Next.js router supports from version 14.1, and moves the tree in the
522
+ same frame, so the avatars glide. When `FamilyView` mounts, and on back and forward, it sets the
523
+ person from the address.
524
+
525
+ `history.pushState` changes only the address: your page does not run again. Whatever else it
526
+ renders from the id, such as a heading or its metadata (the title), stays on the first person until
527
+ a reload. So keep everything that depends on the person inside `FamilyView`, driven by `focus`, or
528
+ in `renderFocus`. Or navigate with `router.push`, which renders the page for the new person, but
529
+ changes the route in a React transition, which settles too late for the glide
530
+ ([Moving around](#moving-around)).
531
+
532
+ ## The layout without React
533
+
534
+ The `./layout` entry is the tree's shape as plain data, with no React: which ancestors fit a width,
535
+ the siblings, the children grouped by marriage, and whether the descendants branch as a tree or form
536
+ a list. The component draws what it returns, and another framework could draw the same tree from it.
537
+ React is an optional peer, so this entry needs none installed. Widths are in rem.
538
+
539
+ | Export | What it gives |
540
+ |---|---|
541
+ | `createFamily(data, options?)` | The lookups the others take, over kinship's tree: parents, children, siblings, half-siblings, and each person's marriages in marriage order. `options` reads the name, birth year and wedding year from your own shapes. |
542
+ | `pickAncestors(family, id, width)` | The ancestors to draw in `width` rem: as many generations as fit, and how each one is drawn (full cards, smaller ones, or given names only on the top row). `null` without known parents. |
543
+ | `parentsInOrder(family, id)` | A person's parents, father first, then mother. |
544
+ | `leafWeight(couple)` | The width a couple's branch of ancestors needs, counted in couples of its top row. |
545
+ | `EMPTY_WEIGHT` | The share of a row a parent with no known parents keeps: `0.5`. |
546
+ | `siblingsOf(family, id)` | Brothers and sisters: older, younger, and of unknown order, each marked half or not. |
547
+ | `unionGroups(family, id)` | A person's children by other parent: the marriages in marriage order (childless ones kept), then co-parents with no recorded marriage, then children of no known other parent. |
548
+ | `descendantsFitAsTree(family, groups, width)` | Whether the descendants fit as a branching tree in `width` rem; otherwise they are an indented list. |
549
+ | `descendantCount(family, id)` | How many people descend from a person. |
550
+ | `leafCount(family, id, depth)` | How many leaves a branch has, down to the depth drawn: the width it needs as a tree. |
551
+ | `DESCENDANT_DEPTH, CHILDREN_SHOWN` | How deep the descendants are drawn (`4`), and how many children a list shows before "Show more" (`8`). |
552
+
553
+ It also exports the types: `Family`, `FamilyInput`, `FamilyOptions`, `Spouse`, `UnionRecord`,
554
+ `AncestorCouple`, `Pedigree`, `RowMode`, `Sibling`, `UnionGroup`, `Person` and `Sex`.
555
+
556
+ ## Development
557
+
558
+ ```sh
559
+ npm install
560
+ npm test # Vitest: unit tests in Node, browser tests in Chromium
561
+ npm run test:unit # only the unit tests, no browser needed
562
+ npm run typecheck # tsc, strict
563
+ npm run lint # Biome
564
+ npm run build # tsup: ESM, CommonJS and types for every entry, and dist/style.css
565
+ npm run check:package # publint and are-the-types-wrong, after a build
566
+ npm run test:dist # smoke test of the built package
567
+ npm run demo # the live demo, served from the source by Vite
568
+ npm run demo:build # the demo as a static site in demo/dist
569
+ ```
570
+
571
+ The browser tests need Chromium: run `npx playwright install chromium` once.
572
+
573
+ ## License
574
+
575
+ [MIT](LICENSE)