Skip to content

Commit 3cf1f91

Browse files
authored
fix(Template - nextjs-app): fix assets import & ThemeProvider position (#5273)
This PR also adds a theme-switch popover to the shellbar.
1 parent 8f0243d commit 3cf1f91

File tree

5 files changed

+91
-30
lines changed

5 files changed

+91
-30
lines changed
Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
'use client';
22

3-
import navBackIcon from '@ui5/webcomponents-icons/dist/nav-back.js';
4-
import { Button, ShellBar } from '@ui5/webcomponents-react';
53
import '@ui5/webcomponents-react/dist/Assets.js';
6-
import { usePathname, useRouter } from 'next/navigation';
4+
import { AppShellBar } from '@/app/components/AppShellBar';
5+
import { ThemeProvider } from '@ui5/webcomponents-react';
6+
import { ReactNode } from 'react';
77

8-
export function AppShell() {
9-
const router = useRouter();
10-
const pathname = usePathname();
8+
interface AppShellProps {
9+
children?: ReactNode | ReactNode[];
10+
}
1111

12+
export function AppShell({ children }: AppShellProps) {
1213
return (
13-
<>
14-
<ShellBar
15-
primaryTitle={'UI5 Web Components for React Examples'}
16-
secondaryTitle={'NextJS - App Router'}
17-
startButton={
18-
pathname !== '/' && (
19-
<Button
20-
icon={navBackIcon}
21-
onClick={() => {
22-
router.back();
23-
}}
24-
/>
25-
)
26-
}
27-
/>
28-
</>
14+
<ThemeProvider>
15+
<AppShellBar />
16+
<div className="appScrollContainer">{children}</div>
17+
</ThemeProvider>
2918
);
3019
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.popover {
2+
}
3+
.popover::part(content) {
4+
padding: 0;
5+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use client';
2+
3+
import navBackIcon from '@ui5/webcomponents-icons/dist/nav-back.js';
4+
import paletteIcon from '@ui5/webcomponents-icons/dist/palette.js';
5+
import {
6+
Button,
7+
List,
8+
ListMode,
9+
ListPropTypes,
10+
ResponsivePopover,
11+
ResponsivePopoverDomRef,
12+
ShellBar,
13+
ShellBarItem,
14+
ShellBarItemPropTypes,
15+
StandardListItem
16+
} from '@ui5/webcomponents-react';
17+
import { usePathname, useRouter } from 'next/navigation';
18+
import { useRef, useState } from 'react';
19+
import classes from './AppShellBar.module.css';
20+
import { getTheme, setTheme } from '@ui5/webcomponents-base/dist/config/Theme.js';
21+
22+
const THEMES = [
23+
{ key: 'sap_horizon', value: 'Morning Horizon (Light)' },
24+
{ key: 'sap_horizon_dark', value: 'Evening Horizon (Dark)' },
25+
{ key: 'sap_horizon_hcb', value: 'Horizon High Contrast Black' },
26+
{ key: 'sap_horizon_hcw', value: 'Horizon High Contrast White' }
27+
];
28+
29+
export function AppShellBar() {
30+
const router = useRouter();
31+
const pathname = usePathname();
32+
const popoverRef = useRef<ResponsivePopoverDomRef | null>(null);
33+
const [currentTheme, setCurrentTheme] = useState(getTheme);
34+
35+
const handleThemeSwitchItemClick: ShellBarItemPropTypes['onClick'] = (e) => {
36+
popoverRef.current?.showAt(e.detail.targetRef);
37+
};
38+
const handleThemeSwitch: ListPropTypes['onSelectionChange'] = (e) => {
39+
const { targetItem } = e.detail;
40+
setTheme(targetItem.dataset.key!);
41+
setCurrentTheme(targetItem.dataset.key!);
42+
};
43+
44+
return (
45+
<>
46+
<ShellBar
47+
primaryTitle={'UI5 Web Components for React Examples'}
48+
secondaryTitle={'NextJS - App Router'}
49+
startButton={
50+
pathname !== '/' && (
51+
<Button
52+
icon={navBackIcon}
53+
onClick={() => {
54+
router.back();
55+
}}
56+
/>
57+
)
58+
}
59+
>
60+
<ShellBarItem icon={paletteIcon} text="Change Theme" onClick={handleThemeSwitchItemClick} />
61+
</ShellBar>
62+
<ResponsivePopover ref={popoverRef} className={classes.popover}>
63+
<List onSelectionChange={handleThemeSwitch} headerText="Change Theme" mode={ListMode.SingleSelect}>
64+
{THEMES.map((theme) => (
65+
<StandardListItem key={theme.key} selected={currentTheme === theme.key} data-key={theme.key}>
66+
{theme.value}
67+
</StandardListItem>
68+
))}
69+
</List>
70+
</ResponsivePopover>
71+
</>
72+
);
73+
}

examples/nextjs-app/app/layout.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import './globals.css';
12
import { AppShell } from '@/app/components/AppShell';
23
import { CssRegistry } from '@/app/CssRegistry';
3-
import { ThemeProvider } from '@ui5/webcomponents-react';
4-
import './globals.css';
54

65
export default function RootLayout({ children }: { children: React.ReactNode }) {
76
return (
@@ -20,10 +19,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
2019
<body>
2120
<div className="appShell">
2221
<CssRegistry>
23-
<ThemeProvider>
24-
<AppShell />
25-
<div className="appScrollContainer">{children}</div>
26-
</ThemeProvider>
22+
<AppShell>{children}</AppShell>
2723
</CssRegistry>
2824
</div>
2925
</body>

examples/nextjs-app/app/todos/[id]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use client';
2-
31
import { Todo, todos } from '@/app/mockData/todos';
42
import {
53
DatePicker,

0 commit comments

Comments
 (0)