Skip to content

Visualize Your Ideas With Code

docs: add Node typedocs (#151)

Jacob153b1424c150
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.ts
index 3b45a935..ff2c10e0 100644
--- a/packages/2d/src/components/Node.ts
+++ b/packages/2d/src/components/Node.ts
@@ -387,10 +387,64 @@ export class Node implements Promisable<Node> {
return this.parent()?.view() ?? null;
}
+ /**
+ * Add the given node(s) as the children of this node.
+ *
+ * @remarks
+ * The nodes will be appended at the end of the children list.
+ *
+ * @example
+ * ```tsx
+ * const node = <Layout />;
+ * node.add(<Rect />);
+ * node.add(<Circle />);
+ * ```
+ * Result:
+ * ```mermaid
+ * graph TD;
+ * layout([Layout])
+ * circle([Circle])
+ * rect([Rect])
+ * layout-->rect;
+ * layout-->circle;
+ * ```
+ *
+ * @param node - A node or an array of nodes to append.
+ */
public add(node: ComponentChildren): this {
return this.insert(node, Infinity);
}
+ /**
+ * Insert the given node(s) at the specified index in the children list.
+ *
+ * @example
+ * ```tsx
+ * const node = (
+ * <Layout>
+ * <Rect />
+ * <Circle />
+ * </Layout>
+ * );
+ *
+ * node.insert(<Text />, 1);
+ * ```
+ *
+ * Result:
+ * ```mermaid
+ * graph TD;
+ * layout([Layout])
+ * circle([Circle])
+ * text([Text])
+ * rect([Rect])
+ * layout-->rect;
+ * layout-->text;
+ * layout-->circle;
+ * ```
+ *
+ * @param node - A node or an array of nodes to insert.
+ * @param index - An index at which to insert the node(s).
+ */
public insert(node: ComponentChildren, index = 0): this {
const array: ComponentChild[] = Array.isArray(node) ? node : [node];
if (array.length === 0) {
@@ -413,6 +467,9 @@ export class Node implements Promisable<Node> {
return this;
}
+ /**
+ * Remove this node from the tree.
+ */
public remove(): this {
const current = this.parent();
if (current === null) {
@@ -424,6 +481,18 @@ export class Node implements Promisable<Node> {
return this;
}
+ /**
+ * Rearrange this node in relation to its siblings.
+ *
+ * @remarks
+ * Children are rendered starting from the beginning of the children list.
+ * We can change the rendering order by rearranging said list.
+ *
+ * A positive `by` arguments move the node up (it will be rendered on top of
+ * the elements it has passed). Negative values move it down.
+ *
+ * @param by - Number of places by which the node should be moved.
+ */
public move(by = 1): this {
const parent = this.parent();
if (by === 0 || !parent) {
@@ -462,18 +531,46 @@ export class Node implements Promisable<Node> {
return this;
}
+ /**
+ * Move the node up in relation to its siblings.
+ *
+ * @remarks
+ * The node will exchange places with the sibling right above it (if any) and
+ * from then on will be rendered on top of it.
+ */
public moveUp(): this {
return this.move(1);
}
+ /**
+ * Move the node down in relation to its siblings.
+ *
+ * @remarks
+ * The node will exchange places with the sibling right below it (if any) and
+ * from then on will be rendered under it.
+ */
public moveDown(): this {
return this.move(-1);
}
+ /**
+ * Move the node to the top in relation to its siblings.
+ *
+ * @remarks
+ * The node will be placed at the end of the children list and from then on
+ * will be rendered on top of all of its siblings.
+ */
public moveToTop(): this {
return this.move(Infinity);
}
+ /**
+ * Move the node to the bottom in relation to its siblings.
+ *
+ * @remarks
+ * The node will be placed at the beginning of the children list and from then
+ * on will be rendered below all of its siblings.
+ */
public moveToBottom(): this {
return this.move(-Infinity);
}
@@ -490,12 +587,25 @@ export class Node implements Promisable<Node> {
return this;
}
+ /**
+ * Remove all children of this node.
+ */
public removeChildren() {
for (const node of this.children()) {
node.remove();
}
}
+ /**
+ * Prepare this node to be disposed of.
+ *
+ * @remarks
+ * This method is called automatically when a scene is refreshed. It will
+ * be called even if the node is not currently attached to the tree.
+ *
+ * The goal of this method is to clean any external references to allow the
+ * node to be garbage collected.
+ */
public dispose() {
// do nothing
}
diff --git a/packages/docs/docs/getting-started/hierarchy.mdx b/packages/docs/docs/getting-started/hierarchy.mdx
index 3de6cfbb..c74de615 100644
--- a/packages/docs/docs/getting-started/hierarchy.mdx
+++ b/packages/docs/docs/getting-started/hierarchy.mdx
@@ -18,8 +18,9 @@ representation:
<div className="row margin-bottom--md">
<div className="col col--6">
- <CodeBlock language={'tsx'}>
- {`view.add(
+
+```tsx
+view.add(
<>
<Circle />
<Layout>
@@ -27,12 +28,14 @@ representation:
<Text>Hi</Text>
</Layout>
</>,
-);`}
- </CodeBlock>
+);
+```
+
</div>
<div className="col col--6">
- <Mermaid
- value={`graph TD;
+
+```mermaid
+graph TD;
view[Scene View]
circle([Circle])
layout([Layout])
@@ -41,8 +44,9 @@ representation:
view-->circle;
view-->layout;
layout-->rect;
- layout-->text;`}
- />
+ layout-->text;
+```
+
</div>
</div>
@@ -56,8 +60,9 @@ code snippets are equivalent:
<div className="row">
<div className="col col--6">
- <CodeBlock language={'tsx'}>
- {`// JSX
+
+```tsx
+// JSX
view.add(
<>
<Circle />
@@ -66,21 +71,25 @@ view.add(
<Text>Hi</Text>
</Layout>
</>,
-);`}
- </CodeBlock>
+);
+```
+
</div>
<div className="col col--6">
- <CodeBlock language={'tsx'}>
- {`// No JSX
+
+```tsx
+// No JSX
view.add([
new Circle({}),
new Layout({
children: [
+ // highlight-start
new Rect({}),
- new Text({text: 'Hi'})
- ]
- })
-]);`} </CodeBlock>
+ new Text({text: 'Hi'}),
+ ],
+ }),
+]);
+```
</div>
</div>
diff --git a/packages/docs/src/components/Api/Comment/index.tsx b/packages/docs/src/components/Api/Comment/index.tsx
index 90033b84..9b1b1bb2 100644
--- a/packages/docs/src/components/Api/Comment/index.tsx
+++ b/packages/docs/src/components/Api/Comment/index.tsx
@@ -1,6 +1,9 @@
-import React, {useMemo} from 'react';
+import React, {useMemo, useState} from 'react';
import type {JSONOutput} from 'typedoc';
import Summary from '@site/src/components/Api/Comment/Summary';
+import {Collapsible} from '@docusaurus/theme-common';
+import styles from './styles.module.css';
+import clsx from 'clsx';
export default function Comment({
comment,
@@ -23,6 +26,7 @@ export default function Comment({
}
function FullComment({comment}: {comment: JSONOutput.Comment}) {
+ const [collapsed, setCollapsed] = useState(true);
const examples = useMemo(
() => comment?.blockTags?.filter(({tag}) => tag === '@example') ?? [],
[comment],
@@ -40,10 +44,27 @@ function FullComment({comment}: {comment: JSONOutput.Comment}) {
<>
{examples.length > 0 && (
<>
- <h4>Examples</h4>
- {examples.map(example => (
- <Summary key={example.contentId} id={example.contentId} />
- ))}
+ <h4>
+ <a
+ className={clsx(styles.toggle, collapsed && styles.collapsed)}
+ onClick={e => {
+ e.preventDefault();
+ setCollapsed(!collapsed);
+ }}
+ href="#"
+ >
+ Examples
+ </a>
+ </h4>
+ <Collapsible lazy as={'div'} collapsed={collapsed}>
+ <div className={styles.collapse}>
+ {examples.map(example => (
+ <Summary key={example.contentId} id={example.contentId} />
+ ))}
+ </div>
+ <div className={styles.clearFix}></div>
+ </Collapsible>
+ <div className={clsx(styles.clearFix, styles.inverse)}></div>
</>
)}
{deprecated && (
diff --git a/packages/docs/src/components/Api/Comment/styles.module.css b/packages/docs/src/components/Api/Comment/styles.module.css
new file mode 100644
index 00000000..6c823309
--- /dev/null
+++ b/packages/docs/src/components/Api/Comment/styles.module.css
@@ -0,0 +1,28 @@
+.toggle::after {
+ display: inline-block;
+ content: '';
+ background: var(--ifm-menu-link-sublist-icon) 50% / 2rem 2rem;
+ filter: var(--ifm-menu-link-sublist-icon-filter);
+ height: 1.25rem;
+ width: 1.25rem;
+ transform: rotate(180deg);
+ transition: transform var(--ifm-transition-fast) linear;
+ vertical-align: text-bottom;
+ margin-left: 0.3rem;
+}
+
+.toggle.collapsed::after {
+ transform: rotateZ(90deg);
+}
+
+.collapse {
+ margin-bottom: var(--ifm-leading);
+}
+
+.inverse {
+ margin-bottom: calc(-1 * var(--ifm-leading));
+}
+
+.clearFix {
+ height: 1px;
+}