Skip to content

Commit 76523ce

Browse files
authored
docs(NavigationLayout): fix collapse/expand behavior (#6970)
1 parent 0e6dca1 commit 76523ce

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

packages/main/src/webComponents/NavigationLayout/NavigationLayout.mdx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,34 @@ import { SideNavigationSubItem } from '../SideNavigationSubItem/index';
2323
```tsx
2424
function NavigationLayoutComponent(props) {
2525
const [selectedContent, setSelectedContent] = useState('Home');
26-
const [collapsed, setCollapsed] = useState(false);
26+
const [mode, setMode] = useState(props.mode);
27+
const navigationLayoutRef = useRef<NavigationLayoutDomRef>(null);
28+
2729
const handleSelectionChange: SideNavigationPropTypes['onSelectionChange'] = (e) => {
2830
setSelectedContent(e.detail.item.text);
2931
};
32+
3033
useEffect(() => {
31-
setCollapsed(props.sideCollapsed);
32-
}, [props.sideCollapsed]);
34+
setMode(props.mode);
35+
}, [props.mode]);
36+
3337
return (
3438
<div style={{ position: 'relative', height: '800px' }}>
3539
<NavigationLayout
3640
{...props}
37-
sideCollapsed={collapsed}
41+
ref={navigationLayoutRef}
42+
mode={mode}
3843
header={
3944
<ShellBar
4045
startButton={
4146
<Button
4247
icon={menuIcon}
43-
tooltip={`${collapsed ? 'Expand' : 'Collapse'} Side-Bar`}
4448
onClick={() => {
45-
setCollapsed((prev) => !prev);
49+
if (navigationLayoutRef.current?.isSideCollapsed()) {
50+
setMode(NavigationLayoutMode.Expanded);
51+
} else {
52+
setMode(NavigationLayoutMode.Collapsed);
53+
}
4654
}}
4755
/>
4856
}
@@ -72,6 +80,7 @@ function NavigationLayoutComponent(props) {
7280
<SideNavigationItem text="Item 5" icon="source-code" />
7381
<SideNavigationItem text="Item 6" icon="background" />
7482
</SideNavigationGroup>
83+
7584
<SideNavigationItem
7685
slot="fixedItems"
7786
text="Legal"

packages/main/src/webComponents/NavigationLayout/NavigationLayout.stories.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Meta, StoryObj } from '@storybook/react';
2+
import NavigationLayoutMode from '@ui5/webcomponents-fiori/dist/types/NavigationLayoutMode.js';
23
import menuIcon from '@ui5/webcomponents-icons/dist/menu.js';
34
import '@ui5/webcomponents-icons/dist/home.js';
45
import '@ui5/webcomponents-icons/dist/group.js';
@@ -13,7 +14,7 @@ import '@ui5/webcomponents-icons/dist/chain-link.js';
1314
import '@ui5/webcomponents-icons/dist/document-text.js';
1415
import '@ui5/webcomponents-icons/dist/compare.js';
1516
import '@ui5/webcomponents-icons/dist/locked.js';
16-
import { useEffect, useState } from 'react';
17+
import { useEffect, useRef, useState } from 'react';
1718
import { Button } from '../Button/index.js';
1819
import { ShellBar } from '../ShellBar/index.js';
1920
import type { SideNavigationPropTypes } from '../SideNavigation/index.js';
@@ -23,6 +24,7 @@ import { SideNavigationItem } from '../SideNavigationItem/index.js';
2324
import { SideNavigationSubItem } from '../SideNavigationSubItem/index.js';
2425
import { Text } from '../Text/index.js';
2526
import { Title } from '../Title/index.js';
27+
import type { NavigationLayoutDomRef } from './index.js';
2628
import { NavigationLayout } from './index.js';
2729

2830
const meta = {
@@ -33,7 +35,9 @@ const meta = {
3335
sideContent: { control: { disable: true } },
3436
children: { control: { disable: true } }
3537
},
36-
args: {},
38+
args: {
39+
mode: NavigationLayoutMode.Auto
40+
},
3741
tags: ['package:@ui5/webcomponents-fiori']
3842
} satisfies Meta<typeof NavigationLayout>;
3943

@@ -43,28 +47,33 @@ type Story = StoryObj<typeof meta>;
4347
export const Default: Story = {
4448
render: (args) => {
4549
const [selectedContent, setSelectedContent] = useState('Home');
46-
const [collapsed, setCollapsed] = useState(false);
50+
const [mode, setMode] = useState(args.mode);
51+
const navigationLayoutRef = useRef<NavigationLayoutDomRef>(null);
4752
const handleSelectionChange: SideNavigationPropTypes['onSelectionChange'] = (e) => {
4853
setSelectedContent(e.detail.item.text);
4954
};
5055

5156
useEffect(() => {
52-
setCollapsed(args.sideCollapsed);
53-
}, [args.sideCollapsed]);
57+
setMode(args.mode);
58+
}, [args.mode]);
5459

5560
return (
5661
<div style={{ position: 'relative', height: '800px' }}>
5762
<NavigationLayout
5863
{...args}
59-
sideCollapsed={collapsed}
64+
ref={navigationLayoutRef}
65+
mode={mode}
6066
header={
6167
<ShellBar
6268
startButton={
6369
<Button
6470
icon={menuIcon}
65-
tooltip={`${collapsed ? 'Expand' : 'Collapse'} Side-Bar`}
6671
onClick={() => {
67-
setCollapsed((prev) => !prev);
72+
if (navigationLayoutRef.current?.isSideCollapsed()) {
73+
setMode(NavigationLayoutMode.Expanded);
74+
} else {
75+
setMode(NavigationLayoutMode.Collapsed);
76+
}
6877
}}
6978
/>
7079
}

0 commit comments

Comments
 (0)