Skip to content

Commit 834d33f

Browse files
committed
docs: auto generate prop documentation
1 parent fbd98d6 commit 834d33f

File tree

4 files changed

+817
-1
lines changed

4 files changed

+817
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ Which would look like this:
4747
In contrast to how it would look without react-fitter:
4848

4949
![Example without react-fitter](./docs/without-fitter.png)
50+
51+
## `Fitter` component props
52+
53+
<!-- PROPS_TABLE_START -->
54+
Prop name | Type | Default value | Description
55+
--- | --- | --- | ---
56+
`children` | `ReactNode` | (required) | The content to fit.
57+
`minSize` | `number` | `0.25` | The minimum scale that the text will shrink to. E.g. 0.25 means the text will shrink to no less than 25% of its original size.
58+
`maxLines` | `number` | `1` | The maximum number of lines that the text will shrink to fit.
59+
`settlePrecision` | `number` | `0.01` | The precision at which the text will settle. The component finds the best size by halving the difference between the current size and the min/max size. If the difference is less than the settle precision, the component will stop and settle on that size. A value of 0.01 means the component will settle when the difference is less than 1%.
60+
`updateOnSizeChange` | `boolean` | `true` | Whether to update the text size when the size of the component changes.
61+
`resizeDebounceMs` | `number` | `50` | The time in milliseconds to wait before updating the text size when the size of the component changes. This is useful when the component is being resized frequently and you want to avoid updating the text size on every resize event.
62+
<!-- PROPS_TABLE_END -->

generate-docs.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { parse } from "react-docgen";
2+
import { readFile, writeFile } from "node:fs/promises";
3+
4+
const componentFile = "lib/Fitter.tsx";
5+
const outputFile = "README.md";
6+
7+
const componentCode = await readFile(componentFile, "utf-8");
8+
const [documentation] = parse(componentCode);
9+
10+
const tableRows = [];
11+
12+
for (const [name, prop] of Object.entries(documentation.props ?? {})) {
13+
const typeName = prop.tsType?.name ?? prop.flowType?.name;
14+
const defaultValue = prop.defaultValue?.value
15+
? `\`${prop.defaultValue?.value}\``
16+
: "";
17+
const defaultColumn = prop.required ? "(required)" : defaultValue;
18+
const description = (prop.description ?? "")
19+
.replace(/\n/g, " ")
20+
.replace(/\s+/g, " ");
21+
22+
const columns = [
23+
`\`${name}\``,
24+
`\`${typeName}\``,
25+
defaultColumn,
26+
description,
27+
];
28+
29+
tableRows.push(columns.join(" | "));
30+
}
31+
32+
const tableHeader = ["Prop name", "Type", "Default value", "Description"].join(
33+
" | ",
34+
);
35+
const tableDivider = ["---", "---", "---", "---"].join(" | ");
36+
const table = [tableHeader, tableDivider, ...tableRows].join("\n");
37+
38+
const outputContent = await readFile(outputFile, "utf-8");
39+
const newContent = outputContent.replace(
40+
/<!-- PROPS_TABLE_START -->.*<!-- PROPS_TABLE_END -->/s,
41+
`<!-- PROPS_TABLE_START -->\n${table}\n<!-- PROPS_TABLE_END -->`,
42+
);
43+
await writeFile(outputFile, newContent);

0 commit comments

Comments
 (0)