Skip to content
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

add Horizontaly view #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ Accepts the following props:
- **lineStyle:** _\(default solid\)_ The line style as a [css line-style](https://developer.mozilla.org/en-US/docs/Web/CSS/border-style#values)
- **lineBorderRadius:** _\(default 5px\)_ The border radius of the Path as a [css border-radius](https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius)
- **nodePadding:** _\(default 5px\)_ The left and right padding of every `<TreeNode>` as a [css length](https://developer.mozilla.org/en-US/docs/Web/CSS/length)
- **isHorizontal:** _\(default false\)_ View Mode: Vertical or Horizontal

### `TreeNode` - A node in the tree

- **label:** _**\(required\)**_ Any react `Node`
- **children:** _**\(required\)**_ Any number of `<TreeNode>`
- **isHorizontal:** _\(default false\)_ View Mode: Vertical or Horizontal

## Motivation

Expand Down
6 changes: 5 additions & 1 deletion src/components/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface TreeProps {
*/
nodePadding?: string;
children: TreeNodeProps['children'];
isHorizontal?: boolean;
}

/**
Expand All @@ -49,6 +50,7 @@ function Tree({
nodePadding = '5px',
lineStyle = 'solid',
lineBorderRadius = '5px',
isHorizontal = false,
}: TreeProps) {
return (
<ul
Expand All @@ -72,7 +74,9 @@ function Tree({
--tree-node-padding: var(--node-padding, 5px);
`}
>
<TreeNode label={label}>{children}</TreeNode>
<TreeNode label={label} isHorizontal={isHorizontal}>
{children}
</TreeNode>
</ul>
);
}
Expand Down
131 changes: 124 additions & 7 deletions src/components/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface TreeNodeProps {
label: React.ReactNode;
className?: string;
children?: ReactNode;
isHorizontal?: boolean;
}

const verticalLine = css`
Expand All @@ -19,7 +20,7 @@ const verticalLine = css`
box-sizing: border-box;
`;

const childrenContainer = css`
const childrenContainerVertical = css`
display: flex;
padding-inline-start: 0;
margin: 0;
Expand All @@ -35,7 +36,7 @@ const childrenContainer = css`
}
`;

const node = css`
const nodeVertical = css`
flex: auto;
text-align: center;
list-style-type: none;
Expand All @@ -44,7 +45,7 @@ const node = css`
var(--tree-node-padding);
`;

const nodeLines = css`
const nodeLinesVertical = css`
::before,
::after {
${verticalLine};
Expand Down Expand Up @@ -88,12 +89,128 @@ const nodeLines = css`
}
`;

function TreeNode({ children, label, className }: TreeNodeProps) {
const horizontalLine = css`
content: '';
position: absolute;
left: 0;
width: var(--tree-line-height);
box-sizing: border-box;
height: 100%;
`;

const childrenContainerHorizontal = css`
display: flex;
padding-inline-start: 0;
margin: 0;
padding-left: var(--tree-line-height);
position: relative;
flex-direction: column;

::before {
${horizontalLine};
top: calc(50% - var(--tree-line-width) / 2);
height: 0;
border-top: var(--tree-line-width) var(--tree-node-line-style)
var(--tree-line-color);
}
`;

const nodeHorizontal = css`
flex: auto;
text-align: center;
list-style-type: none;
display: flex;
position: relative;
padding: var(--tree-node-padding) 0 var(--tree-node-padding)
var(--tree-line-height);
`;

const nodeLinesHorizontal = css`
::before,
::after {
${horizontalLine};
right: 50%;
border-top: 0;
top: 0;
}
::after {
left: 0;
border-left: var(--tree-line-width) var(--tree-node-line-style)
var(--tree-line-color);
}

:before {
height: 50%;
border-top: var(--tree-line-width) var(--tree-node-line-style);
top: 50%;
}

:only-of-type {
padding: 0;
::after,
:before {
display: none;
}
}

:first-of-type {
::before {
border: 0 none;
height: 50%;
}
::after {
border-radius: var(--tree-line-border-radius) 0 0 0;
top: 50%;
height: 50%;
border-top: var(--tree-line-width) var(--tree-node-line-style)
var(--tree-line-color);
}
}

:last-of-type {
::before {
border-left: var(--tree-line-width) var(--tree-node-line-style)
var(--tree-line-color);
border-radius: 0 0 0 var(--tree-line-border-radius);
border-bottom: var(--tree-line-width) var(--tree-node-line-style)
var(--tree-line-color);
border-top: 0;
bottom: 50%;
height: 50%;
top: 0;
}
::after {
border: 0 none;
height: 50%;
}
}
`;

function TreeNode({
children,
label,
className,
isHorizontal = false,
}: TreeNodeProps) {
return (
<li className={cx(node, nodeLines, className)}>
{label}
<li
className={
isHorizontal
? cx(nodeHorizontal, nodeLinesHorizontal, className)
: cx(nodeVertical, nodeLinesVertical, className)
}
>
<span style={isHorizontal ? { alignSelf: 'center' } : {}}>{label}</span>
{React.Children.count(children) > 0 && (
<ul className={childrenContainer}>{children}</ul>
<ul
className={
isHorizontal
? childrenContainerHorizontal
: childrenContainerVertical
}
>
{children}
</ul>
)}
</li>
);
Expand Down
29 changes: 18 additions & 11 deletions src/stories/Tree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default {
} as Meta;

export const Basic: Story<TreeProps> = (args) => (
<Tree {...args} children={getNodes()} />
<Tree {...args} children={getNodes()} isHorizontal={true} />
);
Basic.args = {
label: 'Root',
};

export const Styled: Story<TreeProps> = (args) => (
<Tree {...args} children={getNodes()} />
<Tree {...args} children={getNodes()} isHorizontal={true} />
);

Styled.args = {
Expand All @@ -38,6 +38,7 @@ export const StyledNodes: Story<TreeProps> = ({ label, ...args }) => (
label={<StyledNode>{label}</StyledNode>}
{...args}
children={getNodes(StyledNode)}
isHorizontal={true}
/>
);

Expand All @@ -62,18 +63,24 @@ function StyledNode({ children }: React.PropsWithChildren<{}>) {

function getNodes(Label: React.ElementType = 'div') {
return [
<TreeNode label={<Label>Child 1</Label>}>
<TreeNode label={<Label>Grand Child</Label>} />
<TreeNode label={<Label>Child 1</Label>} isHorizontal={true}>
<TreeNode label={<Label>Grand Child</Label>} isHorizontal={true} />
</TreeNode>,
<TreeNode label={<Label>Child 2</Label>}>
<TreeNode label={<Label>Grand Child</Label>}>
<TreeNode label={<Label>Great Grand Child 1</Label>} />
<TreeNode label={<Label>Great Grand Child 2</Label>} />
<TreeNode label={<Label>Child 2</Label>} isHorizontal={true}>
<TreeNode label={<Label>Grand Child</Label>} isHorizontal={true}>
<TreeNode
label={<Label>Great Grand Child 1</Label>}
isHorizontal={true}
/>
<TreeNode
label={<Label>Great Grand Child 2</Label>}
isHorizontal={true}
/>
</TreeNode>
</TreeNode>,
<TreeNode label={<Label>Child 3</Label>}>
<TreeNode label={<Label>Grand Child 1</Label>} />
<TreeNode label={<Label>Grand Child 2</Label>} />
<TreeNode label={<Label>Child 3</Label>} isHorizontal={true}>
<TreeNode label={<Label>Grand Child 1</Label>} isHorizontal={true} />
<TreeNode label={<Label>Grand Child 2</Label>} isHorizontal={true} />
</TreeNode>,
];
}