Skip to content

Commit 820ab58

Browse files
committed
WIP navigation
1 parent 34a4a16 commit 820ab58

File tree

7 files changed

+123
-59
lines changed

7 files changed

+123
-59
lines changed

src/components/Navigation/Vertical/ListItem.tsx

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4+
import { NavigationContext } from './NavigationContext';
45

56
interface NavigationListItemProps extends React.HTMLAttributes<HTMLDivElement> {
67
active?: boolean;
8+
content?: React.ReactNode;
79
icon?: React.ReactElement;
810
}
911

@@ -15,25 +17,31 @@ const NavigationListItem = React.forwardRef<HTMLDivElement, NavigationListItemPr
1517
icon
1618
},
1719
ref
18-
): React.ReactElement => (
19-
<div
20-
ref={ref}
21-
className={clsx(
22-
'vertical-navigation-list-item',
23-
active && 'active',
24-
className
25-
)}
26-
>
27-
{icon && (
28-
<div className="vertical-navigation-list-item-icon">
29-
{icon}
30-
</div>
31-
)}
32-
<span className="vertical-navigation-list-item-text">
33-
{children}
34-
</span>
35-
</div>
36-
));
20+
): React.ReactElement => {
21+
return (
22+
<NavigationContext.Consumer>
23+
{() => (
24+
<div
25+
ref={ref}
26+
className={clsx(
27+
'vertical-navigation-list-item',
28+
active && 'active',
29+
className
30+
)}
31+
>
32+
{icon && (
33+
<div className="vertical-navigation-list-item-icon">
34+
{icon}
35+
</div>
36+
)}
37+
<span className="vertical-navigation-list-item-text">
38+
{children}
39+
</span>
40+
</div>
41+
)}
42+
</NavigationContext.Consumer>
43+
)
44+
});
3745

3846
NavigationListItem.displayName = 'NavigationListItem';
3947
NavigationListItem.propTypes = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createContext } from 'react';
2+
export interface NavigationContextProps {
3+
tooltipsWhenCollapsed: boolean;
4+
collapsed: boolean;
5+
}
6+
7+
export const NavigationContext = createContext<NavigationContextProps>({
8+
tooltipsWhenCollapsed: true,
9+
collapsed: false
10+
});

src/components/Navigation/VerticalNavigation.tsx

+53-18
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import NavigationTop from '@/components/Navigation/Vertical/Top';
77
import NavigationList from '@/components/Navigation/Vertical/List';
88
import NavigationListItem from '@/components/Navigation/Vertical/ListItem';
99
import NavigationDivider from '@/components/Navigation/Vertical/Divider';
10-
import { useState } from 'react';
11-
import { VerticalNavigationScope } from './Vertical/VerticalNavigationScope';
10+
import { useEffect, useState } from 'react';
11+
import { VerticalNavigationScope } from '@/components/Navigation/Vertical/VerticalNavigationScope';
12+
import { NavigationContext } from './Vertical/NavigationContext';
1213

1314
export interface VerticalNavigationStatics {
1415
Top: typeof NavigationTop;
@@ -17,26 +18,51 @@ export interface VerticalNavigationStatics {
1718
Divider: typeof NavigationDivider;
1819
}
1920

20-
export interface SideNavigationProps extends React.HTMLAttributes<HTMLDivElement> {
21+
export interface VerticalNavigationProps extends React.HTMLAttributes<HTMLDivElement> {
22+
/**
23+
* Variant of the menu. With this prop, the color of the menu can be set.
24+
*/
2125
variant?: Variant | string;
26+
/**
27+
* Indicates whether the menu collapse is controllable.
28+
*/
2229
collapsable?: boolean;
30+
/**
31+
* Indicating whether the menu is collapsed. Can be used to make the navigation a controllable component
32+
*/
33+
collapsed?: boolean;
34+
/**
35+
* Indicates whether a tooltip with the menu text must be shown when menu is collapsed. Default: true
36+
*/
37+
tooltipsWhenCollapsed?: boolean;
38+
/**
39+
* Children of the menu. This can be either a React ChildNode or callback function to access various actions
40+
*/
2341
children: React.ReactNode | ((scope: VerticalNavigationScope) => React.ReactNode);
2442
}
2543

2644
// @ts-ignore
27-
const VerticalNavigation: ForwardComponentWithStatics<HTMLDivElement, SideNavigationProps, VerticalNavigationStatics> =
45+
const VerticalNavigation: ForwardComponentWithStatics<HTMLDivElement, VerticalNavigationProps, VerticalNavigationStatics> =
2846
React.forwardRef((
2947
{
3048
children,
3149
className,
32-
variant
50+
variant,
51+
collapsed,
52+
tooltipsWhenCollapsed = true
3353
},
3454
ref
3555
): React.ReactElement => {
36-
const [collapsed, setCollapsed] = useState<boolean>(false);
56+
const [isCollapsed, setCollapsed] = useState<boolean>(false);
57+
58+
useEffect(() => {
59+
if (collapsed) {
60+
setCollapsed(collapsed);
61+
}
62+
}, [collapsed])
3763

3864
const makeScope = (): VerticalNavigationScope => ({
39-
collapsed,
65+
collapsed: isCollapsed,
4066
collapse: () => setCollapsed(!collapsed)
4167
});
4268

@@ -45,25 +71,34 @@ const VerticalNavigation: ForwardComponentWithStatics<HTMLDivElement, SideNaviga
4571
: children;
4672

4773
return (
48-
<div
49-
ref={ref}
50-
className={clsx(
51-
'vertical-navigation-container',
52-
collapsed && 'is-collapsed',
53-
variant && `navigation-${variant}`,
54-
className
55-
)}
74+
<NavigationContext.Provider
75+
value={{
76+
tooltipsWhenCollapsed: tooltipsWhenCollapsed,
77+
collapsed: isCollapsed
78+
}}
5679
>
57-
{children}
58-
</div>
80+
<div
81+
ref={ref}
82+
className={clsx(
83+
'vertical-navigation-container',
84+
isCollapsed && 'is-collapsed',
85+
variant && `navigation-${variant}`,
86+
className
87+
)}
88+
>
89+
{children}
90+
</div>
91+
</NavigationContext.Provider>
5992
)
6093
});
6194

6295
VerticalNavigation.displayName = 'VerticalNavigation';
6396
VerticalNavigation.propTypes = {
6497
children: PropTypes.node,
6598
className: PropTypes.string,
66-
variant: PropTypes.string
99+
variant: PropTypes.string,
100+
collapsed: PropTypes.bool,
101+
tooltipsWhenCollapsed: PropTypes.bool
67102
}
68103

69104
VerticalNavigation.Top = NavigationTop;

src/components/Overlay/OverlayTrigger.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Overlay from '@/components/Overlay/index';
55
import { Placement, PositioningStrategy } from '@popperjs/core';
66
import { Trigger, triggerPropTypes } from '@/components/Overlay/Trigger';
77
import { AnimatePresence } from 'framer-motion';
8+
import { Modifier } from '@popperjs/core/lib/types';
89

910
interface OverlayTriggerProps {
1011
arrow?: boolean;
@@ -15,6 +16,7 @@ interface OverlayTriggerProps {
1516
className?: string;
1617
trigger?: Trigger | string;
1718
motion?: string;
19+
config?: Array<Partial<Modifier<any, any>>>;
1820
}
1921

2022
const OverlayTrigger = ({
@@ -25,7 +27,8 @@ const OverlayTrigger = ({
2527
placement,
2628
positionStrategy,
2729
trigger = 'hover',
28-
motion
30+
motion,
31+
config
2932
}: OverlayTriggerProps): React.ReactElement => {
3033
const [shown, setShown] = useState<boolean>(false);
3134
const triggerRef = useRef<HTMLElement>();
@@ -71,6 +74,7 @@ const OverlayTrigger = ({
7174
placement={placement}
7275
positionStrategy={positionStrategy}
7376
className={className}
77+
config={config}
7478
>
7579
{overlay}
7680
</Overlay>

src/components/Overlay/index.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface OverlayProps {
2020
arrow?: boolean;
2121
positionStrategy?: PositioningStrategy;
2222
motion?: string;
23+
config?: Array<Partial<Modifier<any, any>>>;
2324
}
2425

2526
const Overlay = ({
@@ -29,7 +30,8 @@ const Overlay = ({
2930
placement = 'top',
3031
arrow = true,
3132
positionStrategy = 'absolute',
32-
motion: triggerMotion
33+
motion: triggerMotion,
34+
config
3335
}: React.PropsWithChildren<OverlayProps>): React.ReactElement => {
3436
const ref = useRef<HTMLDivElement | null>(null);
3537
const popper = useRef<PopperInstance>();
@@ -40,7 +42,8 @@ const Overlay = ({
4042
options: {
4143
element: '.arrow'
4244
}
43-
}] : [])
45+
}] : []),
46+
...(config || [])
4447
]);
4548

4649
const createPopperOptions = (): PopperOptions => ({

src/style/components/_navigation.scss

+5-8
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@
7575
.vertical-navigation-list-item-text {
7676
display: none;
7777
}
78-
79-
&:hover {
80-
white-space: nowrap;
81-
82-
.vertical-navigation-list-item-text {
83-
display: block;
84-
}
85-
}
8678
}
8779
}
8880
}
@@ -98,3 +90,8 @@
9890
background: var(--primary-color-light-background);
9991
}
10092
}
93+
94+
.vertical-navigation-tooltip {
95+
margin-left: 0.5rem;
96+
background: #000;
97+
}

www/src/index.tsx

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { useState } from 'react';
33
import * as ReactDom from 'react-dom';
44
import '../../src/style/index.scss';
55
import { Button, Card, Col, Grid, Icon, Page, Row, SelectField, Tag, TextField, Variant } from '../../src';
6-
import logo from '@/images/coderan/logo.svg';
6+
import logoFull from '@/images/coderan/logo.svg';
7+
import logoLetter from '@/images/coderan/logo-letter.svg';
78
import Container from '../../src/components/Container/Container';
89
import FormGroup from '../../src/components/Form/Group';
910
import FormLabel from '../../src/components/Form/Label';
@@ -36,21 +37,27 @@ const TestControllable = () => {
3637
ReactDom.render(
3738
<Page>
3839
<Page.Left fixed>
39-
<VerticalNavigation collapsable>
40-
{({ collapse }) => (
40+
<VerticalNavigation collapsed>
41+
{({ collapse, collapsed }) => (
4142
<>
4243
<VerticalNavigation.Top className="justify-content-center mb-4">
43-
<button onClick={collapse}></button>
44-
<img src={logo} width={160} className="mb-8" />
44+
{collapsed ? (
45+
<img src={logoLetter} height={36} />
46+
) : (
47+
<img src={logoFull} height={36} />
48+
)}
4549
</VerticalNavigation.Top>
4650
<VerticalNavigation.Divider />
4751
<VerticalNavigation.List>
48-
<VerticalNavigation.Item active icon={<Icon icon="home" />}>
49-
Home
50-
</VerticalNavigation.Item>
51-
<VerticalNavigation.Item icon={<Icon icon="user" />}>
52-
User
53-
</VerticalNavigation.Item>
52+
<VerticalNavigation.Item
53+
active
54+
icon={<Icon icon="home" />}
55+
content="Home"
56+
/>
57+
<VerticalNavigation.Item
58+
icon={<Icon icon="user" />}
59+
content="Tooltips"
60+
/>
5461
</VerticalNavigation.List>
5562
</>
5663
)}

0 commit comments

Comments
 (0)