Skip to content

docs: add NextJS app router example #4714

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

Merged
merged 13 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
strategy:
matrix:
path:
- nextjs-app
- nextjs-pages
- vite-ts
fail-fast: false
Expand Down
4 changes: 4 additions & 0 deletions examples/nextjs-app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions examples/nextjs-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
47 changes: 47 additions & 0 deletions examples/nextjs-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## UI5 Web Components React - Next.js App Router Example

This example shows how to use the [Next.js](https://nextjs.org/) App Router with UI5 Web Components for React.

## How to use this template

```bash
npx degit SAP/ui5-webcomponents-react/examples/nextjs-app#main my-project
cd my-project
```

## Getting Started

First, install the node_modules:

```bash
npm install
# or
yarn install
# or
pnpm install
```

Then, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31 changes: 31 additions & 0 deletions examples/nextjs-app/app/CssRegistry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import { useServerInsertedHTML } from 'next/navigation';
import { useEffect, useState } from 'react';
import { createGenerateId, JssProvider, SheetsRegistry } from 'react-jss';

export function CssRegistry({ children }: { children: React.ReactNode }) {
const [registry] = useState(() => new SheetsRegistry());
const generateId = createGenerateId();

useServerInsertedHTML(() => {
return (
<>
<style id="server-side-styles">{registry.toString()}</style>
</>
);
});

useEffect(() => {
const style = document.getElementById('server-side-styles');
if (style) {
style.parentNode?.removeChild(style);
}
}, []);

return (
<JssProvider registry={registry} generateId={generateId}>
{children}
</JssProvider>
);
}
29 changes: 29 additions & 0 deletions examples/nextjs-app/app/components/AppShell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';
import navBackIcon from '@ui5/webcomponents-icons/dist/nav-back.js';
import { Button, ShellBar } from '@ui5/webcomponents-react';
import '@ui5/webcomponents-react/dist/Assets.js';
import { usePathname, useRouter } from 'next/navigation';

export function AppShell() {
const router = useRouter();
const pathname = usePathname();

return (
<>
<ShellBar
primaryTitle={'UI5 Web Components for React Examples'}
secondaryTitle={'NextJS - App Router'}
startButton={
pathname !== '/' && (
<Button
icon={navBackIcon}
onClick={() => {
router.back();
}}
/>
)
}
/>
</>
);
}
34 changes: 34 additions & 0 deletions examples/nextjs-app/app/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client';

import { Todo } from '@/app/mockData/todos';
import { List, ListItemType, ListPropTypes, StandardListItem, ValueState } from '@ui5/webcomponents-react';
import { useRouter } from 'next/navigation';

interface TodoListProps {
items: Todo[];
}

export function TodoList({ items }: TodoListProps) {
const router = useRouter();
const handleTodoClick: ListPropTypes['onItemClick'] = (event) => {
router.push(`/todos/${event.detail.item.dataset.id}`);
};

return (
<List onItemClick={handleTodoClick}>
{items.map((todo) => {
return (
<StandardListItem
key={todo.id}
data-id={todo.id}
type={ListItemType.Navigation}
additionalText={`${!todo.completed ? 'Not ' : ''}Completed`}
additionalTextState={todo.completed ? ValueState.Success : ValueState.None}
>
{todo.title}
</StandardListItem>
);
})}
</List>
);
}
Binary file added examples/nextjs-app/app/favicon.ico
Binary file not shown.
24 changes: 24 additions & 0 deletions examples/nextjs-app/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
:not(:defined) {
display: none;
}

html,
body {
max-width: 100vw;
overflow-x: hidden;
padding: 0;
margin: 0;
}

.appShell {
height: 100vh;
width: 100vw;
overflow: hidden;
}

.appScrollContainer {
height: calc(100vh - 3.25rem);
width: 100vw;
overflow-y: auto;
position: relative;
}
32 changes: 32 additions & 0 deletions examples/nextjs-app/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AppShell } from '@/app/components/AppShell';
import { CssRegistry } from '@/app/CssRegistry';
import { ThemeProvider } from '@ui5/webcomponents-react';
import './globals.css';

export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script
data-ui5-config
type="application/json"
dangerouslySetInnerHTML={{
__html: JSON.stringify({
theme: 'sap_horizon'
})
}}
/>
</head>
<body>
<div className="appShell">
<CssRegistry>
<ThemeProvider>
<AppShell />
<div className="appScrollContainer">{children}</div>
</ThemeProvider>
</CssRegistry>
</div>
</body>
</html>
);
}
7 changes: 7 additions & 0 deletions examples/nextjs-app/app/loading.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.container {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
10 changes: 10 additions & 0 deletions examples/nextjs-app/app/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BusyIndicator, BusyIndicatorSize } from '@ui5/webcomponents-react';
import classes from './loading.module.css';

export default function HomeLoading() {
return (
<div className={classes.container}>
<BusyIndicator active size={BusyIndicatorSize.Large} delay={0} />
</div>
);
}
Loading