8000 feat: expose useNode plugin by jaredLunde · Pull Request #29 · jaredLunde/exploration · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: expose useNode plugin #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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