Skip to content

Commit 1b45a77

Browse files
committed
Improve component props docs
1 parent f84477e commit 1b45a77

31 files changed

+360
-2002
lines changed

lib/components/grid/types.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,29 @@ export type GridProps<
9696
*
9797
* ⚠️ The `useGridRef` and `useGridCallbackRef` hooks are exported for convenience use in TypeScript projects.
9898
*/
99-
gridRef?: Ref<GridImperativeAPI>;
99+
gridRef?: Ref<{
100+
get element(): HTMLDivElement | null;
101+
102+
scrollToCell(config: {
103+
behavior?: "auto" | "instant" | "smooth";
104+
columnAlign?: "auto" | "center" | "end" | "smart" | "start";
105+
columnIndex: number;
106+
rowAlign?: "auto" | "center" | "end" | "smart" | "start";
107+
rowIndex: number;
108+
}): void;
109+
110+
scrollToColumn(config: {
111+
align?: "auto" | "center" | "end" | "smart" | "start";
112+
behavior?: "auto" | "instant" | "smooth";
113+
index: number;
114+
}): void;
115+
116+
scrollToRow(config: {
117+
align?: "auto" | "center" | "end" | "smart" | "start";
118+
behavior?: "auto" | "instant" | "smooth";
119+
index: number;
120+
}): void;
121+
}>;
100122

101123
/**
102124
* Callback notified when the range of rendered cells changes.

lib/components/list/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ export type ListProps<
4747
*
4848
* ⚠️ The `useListRef` and `useListCallbackRef` hooks are exported for convenience use in TypeScript projects.
4949
*/
50-
listRef?: Ref<ListImperativeAPI>;
50+
listRef?: Ref<{
51+
get element(): HTMLDivElement | null;
52+
53+
scrollToRow(config: {
54+
align?: "auto" | "center" | "end" | "smart" | "start";
55+
behavior?: "auto" | "instant" | "smooth";
56+
index: number;
57+
}): void;
58+
}>;
5159

5260
/**
5361
* Callback notified when the List's outermost HTMLElement resizes.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"build:docs": "TARGET=docs vite build",
2525
"build:lib": "TARGET=lib vite build",
2626
"compile": "pnpm run compile:code-snippets && pnpm run compile:docs",
27-
"compile:code-snippets": "node --loader ts-node/esm ./scripts/code-snippets/run.ts",
28-
"compile:docs": "node --loader ts-node/esm ./scripts/docs/run.ts",
27+
"compile:code-snippets": "node --loader ts-node/esm ./scripts/compile-code-snippets.ts",
28+
"compile:docs": "node --loader ts-node/esm ./scripts/compile-docs.ts",
2929
"lint": "eslint .",
3030
"prerelease": "rm -rf dist && pnpm run build:lib",
3131
"prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",

public/generated/js-docs/Grid.json

Lines changed: 41 additions & 959 deletions
Large diffs are not rendered by default.

public/generated/js-docs/List.json

Lines changed: 32 additions & 876 deletions
Large diffs are not rendered by default.

scripts/code-snippets/ts-to-js.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
import { mkdir, readFile, writeFile } from "node:fs/promises";
1+
import { readFile, writeFile } from "node:fs/promises";
22
import { basename, join } from "node:path";
3-
import { cwd } from "node:process";
4-
import { getFilesWithExtensions, rmFilesWithExtensions } from "../utils.ts";
5-
import { syntaxHighlight } from "./syntax-highlight.ts";
3+
import { initialize } from "./utils/initialize.ts";
4+
import { syntaxHighlight } from "./utils/syntax-highlight.ts";
5+
import { trimExcludedText } from "./utils/trimExcludedText.ts";
66

77
async function run() {
8-
const inputDir = join(cwd(), "src", "routes");
9-
const outputDir = join(cwd(), "public", "generated", "code-snippets");
8+
const { files, outputDir } = await initialize({
9+
fileExtensions: [".html", ".ts", ".tsx"],
10+
inputPath: ["src", "routes"],
11+
outputDirName: "code-snippets"
12+
});
1013

11-
await mkdir(outputDir, { recursive: true });
12-
13-
await rmFilesWithExtensions(outputDir, [".json"]);
14-
15-
const tsFiles = await getFilesWithExtensions(inputDir, [
16-
".html",
17-
".ts",
18-
".tsx"
19-
]);
20-
const exampleFiles = tsFiles.filter((file) => file.includes(".example."));
14+
const exampleFiles = files.filter((file) => file.includes(".example."));
2115

2216
for (const file of exampleFiles) {
2317
console.debug("Extracting", file);
@@ -28,14 +22,7 @@ async function run() {
2822

2923
{
3024
// Remove special comments and directives before syntax highlighting
31-
{
32-
const pieces = rawText.split("// <begin>");
33-
rawText = pieces[pieces.length - 1].trim();
34-
}
35-
{
36-
const pieces = rawText.split("// <end>");
37-
rawText = pieces[0].trim();
38-
}
25+
rawText = trimExcludedText(rawText);
3926

4027
rawText = rawText
4128
.split("\n")

scripts/compile-docs.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { writeFile } from "node:fs/promises";
2+
import { join, relative } from "node:path";
3+
import { cwd } from "node:process";
4+
import { withCustomConfig } from "react-docgen-typescript";
5+
import type { ComponentMetadata } from "../src/types.ts";
6+
import { formatDescriptionText } from "./utils/formatDescriptionText.ts";
7+
import { initialize } from "./utils/initialize.ts";
8+
import { syntaxHighlight } from "./utils/syntax-highlight.ts";
9+
10+
const parser = withCustomConfig("./tsconfig.json", {
11+
savePropValueAsString: true,
12+
shouldExtractLiteralValuesFromEnum: true,
13+
shouldExtractValuesFromUnion: true
14+
});
15+
16+
const TOKEN_TO_REPLACE = "TOKEN_TO_REPLACE";
17+
18+
async function run() {
19+
const { files, outputDir } = await initialize({
20+
fileExtensions: [".ts", ".tsx"],
21+
fileFilter: (file) =>
22+
file.endsWith("/List.tsx") || file.endsWith("/Grid.tsx"),
23+
inputPath: ["lib", "components"],
24+
outputDirName: "js-docs"
25+
});
26+
27+
for (const file of files) {
28+
console.debug("Parsing", file);
29+
30+
const parsed = parser.parse(file);
31+
32+
for (const component of parsed) {
33+
// Convert to local paths
34+
component.filePath = relative(cwd(), file);
35+
36+
const componentMetadata: ComponentMetadata = {
37+
filePath: component.filePath,
38+
name: component.displayName,
39+
props: {}
40+
};
41+
42+
// Filter inherited HTML attributes
43+
for (const key in component.props) {
44+
const prop = component.props[key];
45+
if (
46+
prop.declarations?.filter(
47+
(declaration) => !declaration.fileName.includes("node_modules")
48+
).length === 0
49+
) {
50+
delete component.props[key];
51+
}
52+
}
53+
54+
// Syntax highlight prop types
55+
for (const name in component.props) {
56+
const prop = component.props[name];
57+
58+
let textToFormat = prop.type.raw;
59+
if (!textToFormat && prop.type.name.includes(":")) {
60+
// Edge case where some prop types aren't registered as containing raw TS
61+
textToFormat = prop.type.name;
62+
}
63+
64+
if (!textToFormat) {
65+
textToFormat = `${prop.type.name}`;
66+
}
67+
68+
if (prop.defaultValue?.value) {
69+
textToFormat = `${textToFormat} = ${prop.defaultValue.value}`;
70+
}
71+
72+
// Format with a placeholder token so we can replace it with a formatted string
73+
textToFormat = `${TOKEN_TO_REPLACE}${prop.required ? "" : "?"}: ${textToFormat}`;
74+
75+
try {
76+
let html = await syntaxHighlight(textToFormat, "TS");
77+
html = html.replace(
78+
TOKEN_TO_REPLACE,
79+
`<span class="tok-propertyName">${name}</span>`
80+
);
81+
82+
const [description, warning = ""] = prop.description.split("⚠️");
83+
84+
componentMetadata.props[name] = {
85+
description: formatDescriptionText(description.trim()),
86+
html,
87+
name,
88+
required: prop.required,
89+
warning: warning ? formatDescriptionText(warning.trim()) : undefined
90+
};
91+
} catch (error) {
92+
console.error(error);
93+
}
94+
}
95+
96+
const outputFile = join(outputDir, `${component.displayName}.json`);
97+
98+
console.debug("Writing to", outputFile);
99+
100+
await writeFile(outputFile, JSON.stringify(componentMetadata, null, 2));
101+
}
102+
}
103+
}
104+
105+
run();

scripts/docs/run.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function formatDescriptionText(text: string) {
2+
return text
3+
.replaceAll("\n- ", "<br/>• ")
4+
.replaceAll("\n\n", "<br/><br/>")
5+
.replaceAll(/`([^`]+)`/g, "<code>$1</code>");
6+
}

0 commit comments

Comments
 (0)