Skip to content

Commit 38d5b88

Browse files
authored
feat: add standalone navigation (#693)
* feat: add standalone navigation
1 parent c683873 commit 38d5b88

File tree

13 files changed

+82
-21
lines changed

13 files changed

+82
-21
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ const Page: React.FC<PageProps> = ({children}) => (
162162
);
163163
```
164164

165+
### Navigation
166+
167+
Page navigation can also be used separately from the constructor:
168+
169+
```jsx
170+
import {Navigation} from '@gravity-ui/page-constructor';
171+
172+
const Page: React.FC<PageProps> = ({data, logo}) => <Navigation data={data} logo={logo} />;
173+
```
174+
165175
### Blocks
166176

167177
Each block is an atomic top-level component. They're stored in the `src/units/constructor/blocks` directory.

src/components/RootCn/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, {PropsWithChildren} from 'react';
2+
3+
import {useTheme} from '../../context/theme';
4+
import {ClassNameProps} from '../../models';
5+
import {rootCn} from '../../utils';
6+
7+
const RootCn = ({className, children}: PropsWithChildren<ClassNameProps>) => {
8+
const theme = useTheme();
9+
10+
return <div className={rootCn({theme}, className)}>{children}</div>;
11+
};
12+
13+
export default RootCn;

src/containers/PageConstructor/PageConstructor.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, {useMemo} from 'react';
33
import '@doc-tools/transform/dist/js/yfm';
44

55
import BackgroundMedia from '../../components/BackgroundMedia/BackgroundMedia';
6-
import {UIKIT_ROOT_CLASS} from '../../components/constants';
6+
import RootCn from '../../components/RootCn';
77
import {blockMap, navItemMap, subBlockMap} from '../../constructor-items';
88
import {AnimateContext} from '../../context/animateContext';
99
import {InnerContext} from '../../context/innerContext';
@@ -23,7 +23,6 @@ import {
2323
} from '../../models';
2424
import Layout from '../../navigation/containers/Layout/Layout';
2525
import {
26-
cn as blockOrigin,
2726
block as cnBlock,
2827
getCustomItems,
2928
getCustomTypes,
@@ -39,7 +38,6 @@ import {ConstructorRow} from './components/ConstructorRow';
3938
import './PageConstructor.scss';
4039

4140
const b = cnBlock('page-constructor');
42-
const ycr = blockOrigin(UIKIT_ROOT_CLASS);
4341

4442
export type ItemMap = typeof blockMap & typeof subBlockMap & CustomItems;
4543

@@ -99,7 +97,7 @@ export const Constructor = (props: PageConstructorProps) => {
9997

10098
return (
10199
<InnerContext.Provider value={context}>
102-
<div className={b(null, ycr({theme}))}>
100+
<RootCn>
103101
<div className={b('wrapper')}>
104102
{themedBackground && (
105103
<BackgroundMedia {...themedBackground} className={b('background')} />
@@ -118,7 +116,7 @@ export const Constructor = (props: PageConstructorProps) => {
118116
</Grid>
119117
</Layout>
120118
</div>
121-
</div>
119+
</RootCn>
122120
</InnerContext.Provider>
123121
);
124122
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export * from './utils';
1212
export * from './schema';
1313
export * from './hooks';
1414
export * from './icons';
15+
export * from './navigation';
1516

1617
export {BREAKPOINTS} from './constants';

src/navigation/components/Navigation/Navigation.scss

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
$block: '.#{$ns}navigation';
55

66
#{$block} {
7-
$root: &;
8-
$navigationZIndex: 98;
9-
10-
position: sticky;
11-
z-index: $navigationZIndex;
12-
top: 0;
13-
147
display: flex;
158
justify-content: center;
169
align-items: center;

src/navigation/components/Navigation/Navigation.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import debounce from 'lodash/debounce';
44

55
import OutsideClick from '../../../components/OutsideClick/OutsideClick';
66
import {Col, Grid, Row} from '../../../grid';
7-
import {HeaderData, ThemedNavigationLogoData} from '../../../models';
7+
import {ClassNameProps, HeaderData, ThemedNavigationLogoData} from '../../../models';
88
import {block} from '../../../utils';
99
import {getNavigationItemWithIconSize} from '../../utils';
1010
import DesktopNavigation from '../DesktopNavigation/DesktopNavigation';
@@ -14,12 +14,12 @@ import './Navigation.scss';
1414

1515
const b = block('navigation');
1616

17-
export interface NavigationProps {
17+
export interface NavigationProps extends ClassNameProps {
1818
logo: ThemedNavigationLogoData;
1919
data: HeaderData;
2020
}
2121

22-
export const Navigation: React.FC<NavigationProps> = ({data, logo}) => {
22+
export const Navigation: React.FC<NavigationProps> = ({data, logo, className}) => {
2323
const {leftItems, rightItems, iconSize = 20, withBorder = false} = data;
2424
const [isSidebarOpened, setIsSidebarOpened] = useState(false);
2525
const [activeItemId, setActiveItemId] = useState<string | undefined>(undefined);
@@ -56,7 +56,7 @@ export const Navigation: React.FC<NavigationProps> = ({data, logo}) => {
5656
});
5757

5858
return (
59-
<Grid className={b({'with-border': showBorder})}>
59+
<Grid className={b({'with-border': showBorder}, className)}>
6060
<Row>
6161
<Col>
6262
<nav>

src/navigation/components/NavigationItem/NavigationItem.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import React, {useContext, useMemo} from 'react';
1+
import React, {useMemo} from 'react';
22

33
import omit from 'lodash/omit';
44

55
import {BlockIdContext} from '../../../context/blockIdContext';
6-
import {InnerContext} from '../../../context/innerContext';
76
import {CustomItem, NavigationItemType} from '../../../models';
87
import {block} from '../../../utils';
98
import {NavigationItemProps} from '../../models';
109

10+
import {useNavigationItemMap} from './hooks/useNavigationItemMap';
11+
1112
import './NavigationItem.scss';
1213

1314
const b = block('navigation-item');
@@ -21,7 +22,7 @@ const NavigationItem: React.FC<NavigationItemProps> = ({
2122
...props
2223
}: NavigationItemProps) => {
2324
const {type = NavigationItemType.Link} = data;
24-
const {navItemMap} = useContext(InnerContext);
25+
const navItemMap = useNavigationItemMap();
2526
const Component = navItemMap[type] as CustomItem;
2627
const componentProps = useMemo(() => {
2728
const componentProperties = {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {useContext} from 'react';
2+
3+
import isEmpty from 'lodash/isEmpty';
4+
5+
import {navItemMap as NavItemMapDefault} from '../../../../constructor-items';
6+
import {InnerContext} from '../../../../context/innerContext';
7+
8+
export const useNavigationItemMap = () => {
9+
const {navItemMap} = useContext(InnerContext);
10+
11+
return isEmpty(navItemMap) ? NavItemMapDefault : navItemMap;
12+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
import RootCn from '../../../components/RootCn';
4+
5+
import Navigation, {NavigationProps} from './../../components/Navigation/Navigation';
6+
7+
const Standalone = (props: NavigationProps) => (
8+
<RootCn>
9+
<Navigation {...props} />
10+
</RootCn>
11+
);
12+
13+
export default Standalone;

src/navigation/containers/Layout/Layout.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
.layout {
1+
@import '../../../../styles/variables';
2+
3+
$block: '.#{$ns}layout';
4+
5+
#{$block} {
26
display: flex;
37
flex-direction: column;
48

@@ -9,4 +13,10 @@
913
flex-grow: 1;
1014
flex-direction: column;
1115
}
16+
17+
&__navigation {
18+
position: sticky;
19+
z-index: 98;
20+
top: 0;
21+
}
1222
}

0 commit comments

Comments
 (0)