Skip to content

Commit

Permalink
feat: expose useNode plugin (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde authored Jul 31, 2022
1 parent c4b4654 commit 8e4a337
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,16 @@ paired with the [`useVirtualize()`](#usevirtualize) hook.

---

### useNodeProps()
### useNode()

A hook that creates and memoizes node-specific props from a set of input props.
A plugin that creates and memoizes node-specific props.

#### Arguments

| Name | Type | Required? | Description |
| ------ | -------------------------- | --------- | -------------------------------------------- |
| config | [`NodeProps`](#node-props) | Yes | Options to generate node-specific props from |
| Name | Type | Required? | Description |
| -------- | ----------------------------------------------------- | --------- | -------------------------------------------- |
| fileTree | `FileTree<Meta>` | Yes | A file tree |
| config | `Pick<NodeProps<Meta>, "node" \| "index" \| "style">` | Yes | Options to generate node-specific props from |

---

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type {
FileTreeFactory,
GetNodes,
} from "./file-tree";
export { Node } from "./node";
export type { NodeProps } from "./node";
export { Node, useNode } from "./node";
export type { NodeProps, UseNodeConfig } from "./node";
export { SubjectMap, SubjectSet } from "./observable-data";
export * as pathFx from "./path-fx";
export { subject } from "./tree/subject";
Expand Down
42 changes: 25 additions & 17 deletions src/node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,29 @@ import { retryWithBackoff } from "./utils";
* @param props - Node props
*/
export function Node<Meta>(props: NodeProps<Meta>) {
const nodeProps = useNodeProps(props);

const elementProps = useNodePlugins(props.node.id, [
...(props.plugins ?? empty),
{
didChange: noopSubject,
getProps() {
return nodeProps;
},
},
useNode(props.tree, props),
]);

return React.createElement(props.as ?? "div", elementProps, props.children);
}

/**
* A hook that creates and memoizes node-specific props from a set of input props.
* A plugin that creates and memoizes node-specific props.
*
* @param fileTree - A file tree
* @param config - Props to generate exploration node-specific props from
*/
export function useNodeProps<Meta>(config: Omit<NodeProps<Meta>, "as">) {
const { node, tree, index, style } = config;
export function useNode<Meta>(
fileTree: FileTree<Meta>,
config: UseNodeConfig<Meta>
) {
const { node, index, style } = config;
const type = isDir(node) ? "dir" : isFile(node) ? "file" : "prompt";
const expanded = isDir(node) ? node.expanded : undefined;
const { id, depth } = node;

return React.useMemo<React.HTMLAttributes<HTMLElement>>(() => {
const props = React.useMemo<React.HTMLAttributes<HTMLElement>>(() => {
return {
role: "button",
style,
Expand All @@ -63,18 +59,25 @@ export function useNodeProps<Meta>(config: Omit<NodeProps<Meta>, "as">) {

if (isDir(node)) {
if (expanded) {
tree.collapse(node);
fileTree.collapse(node);
} else {
retryWithBackoff(() => tree.expand(node), {
retryWithBackoff(() => fileTree.expand(node), {
shouldRetry() {
return node.expanded && !tree.isExpanded(node);
return node.expanded && !fileTree.isExpanded(node);
},
}).catch(() => {});
}
}
},
};
}, [index, depth, expanded, style, type, node, tree, id]);
}, [index, depth, expanded, style, type, node, fileTree, id]);

return {
didChange: noopSubject,
getProps() {
return props;
},
};
}

const empty: [] = [];
Expand Down Expand Up @@ -112,3 +115,8 @@ export interface NodeProps<Meta> {
*/
children: React.ReactNode;
}

export type UseNodeConfig<Meta> = Pick<
NodeProps<Meta>,
"node" | "index" | "style"
>;
6 changes: 3 additions & 3 deletions src/use-virtualize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ const createStyle = trieMemoize(
[Map, Map],
(height: number, top: number): React.CSSProperties => ({
position: "absolute",
width: "100%",
height,
contain: "strict",
userSelect: "none",
top,
width: "100%",
left: 0,
height,
top,
})
);

Expand Down

0 comments on commit 8e4a337

Please sign in to comment.