This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb82f54
commit 288e0b3
Showing
10 changed files
with
218 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@resembli/ui": patch | ||
--- | ||
|
||
Add initial tree component and move framer-motion to be a peer dependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as React from "react" | ||
|
||
import { css } from "../../css/css.js" | ||
import { Collapse } from "../Collapse.js" | ||
import { useTreeContext } from "./TreeRoot.js" | ||
|
||
const NodeCss = css({ | ||
margin: 0, | ||
padding: 0, | ||
cursor: "pointer", | ||
userSelect: "none", | ||
}) | ||
|
||
const NodeItemCss = css({ | ||
"&:hover": { | ||
background: "AliceBlue", | ||
}, | ||
}) | ||
|
||
const NodeContainerCss = css({ | ||
margin: 0, | ||
padding: 0, | ||
listStyle: "none", | ||
}) | ||
|
||
export interface TreeNodeProps { | ||
item: React.ReactNode | ||
} | ||
|
||
interface InternalTreeProps extends TreeNodeProps { | ||
__depth: number | ||
} | ||
|
||
export function TreeNode({ children, ...props }: React.PropsWithChildren<TreeNodeProps>) { | ||
const [open, setOpen] = React.useState(false) | ||
|
||
const { item, __depth = 0 } = props as InternalTreeProps | ||
|
||
const { indentSize } = useTreeContext() | ||
|
||
// Add the internal depth property to the elements. We either expect an array or a single | ||
// item. Note the user should be using TreeNode component, if they've provided other types | ||
// of React components, we don't do anything, but will render them as normal. | ||
let childElements | ||
if (Array.isArray(children)) { | ||
childElements = children.map((child) => { | ||
if ((child as JSX.Element)?.type?.name === "TreeNode") { | ||
return React.cloneElement(child as JSX.Element, { __depth: __depth + 1 }) | ||
} | ||
return child | ||
}) | ||
} else { | ||
if ((children as JSX.Element)?.type?.name === "TreeNode") { | ||
childElements = React.cloneElement(children as JSX.Element, { __depth: __depth + 1 }) | ||
} | ||
} | ||
|
||
const indentValue = | ||
typeof indentSize === "number" ? indentSize * __depth : `calc(${indentSize} * ${__depth})` | ||
|
||
return ( | ||
<li className={NodeCss()}> | ||
<div | ||
onClick={() => setOpen((prev) => !prev)} | ||
className={NodeItemCss({ css: { paddingLeft: __depth && indentValue } })} | ||
> | ||
{item} | ||
</div> | ||
{childElements && ( | ||
<Collapse as="ul" open={open} className={NodeContainerCss()}> | ||
{childElements} | ||
</Collapse> | ||
)} | ||
</li> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from "react" | ||
|
||
import { css } from "../../css/css.js" | ||
|
||
const TreeRootCss = css({ | ||
margin: 0, | ||
padding: 0, | ||
listStyle: "none", | ||
}) | ||
|
||
const DEFAULT_INDENT_SIZE = "1rem" | ||
|
||
interface TreeContextValue { | ||
indentSize: React.CSSProperties["marginLeft"] | ||
} | ||
|
||
const TreeContext = React.createContext<TreeContextValue>({ indentSize: DEFAULT_INDENT_SIZE }) | ||
export function useTreeContext() { | ||
return React.useContext(TreeContext) | ||
} | ||
|
||
export interface TreeRootProps { | ||
indent?: TreeContextValue["indentSize"] | ||
} | ||
|
||
export function TreeRoot({ children, indent }: React.PropsWithChildren<TreeRootProps>) { | ||
return ( | ||
<TreeContext.Provider value={{ indentSize: indent ?? DEFAULT_INDENT_SIZE }}> | ||
<ul className={TreeRootCss()}>{children}</ul> | ||
</TreeContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createStitches } from "@stitches/core" | ||
|
||
export const { css, keyframes, globalCss } = createStitches({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
export { Collapse } from "./components/Collapse" | ||
export { Collapse } from "./components/Collapse.js" | ||
export type { CollapseProps } from "./components/Collapse.js" | ||
|
||
export { TreeRoot } from "./components/Tree/TreeRoot.js" | ||
export type { TreeRootProps } from "./components/Tree/TreeRoot.js" | ||
|
||
export { TreeNode } from "./components/Tree/TreeNode.js" | ||
export type { TreeNodeProps } from "./components/Tree/TreeNode.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.