Skip to content

Commit 8c4c972

Browse files
authored
Merge pull request #58 from atellmer/feature/router-link
Link & NavLink
2 parents 4708acf + 5e54d32 commit 8c4c972

File tree

20 files changed

+260
-117
lines changed

20 files changed

+260
-117
lines changed

examples/router/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { component, lazy, Suspense, type DarkElement } from '@dark-engine/core';
22
import { createRoot } from '@dark-engine/platform-browser';
3-
import { type Routes, Router, RouterLink, useLocation } from '@dark-engine/web-router';
3+
import { type Routes, Router, NavLink, useLocation } from '@dark-engine/web-router';
44
import { createGlobalStyle, keyframes } from '@dark-engine/styled';
55

66
const Home = lazy(() => import('./home'));
@@ -35,9 +35,9 @@ const Shell = component<ShellProps>(({ slot }) => {
3535
return (
3636
<>
3737
<header>
38-
<RouterLink to='/home'>Home</RouterLink>
39-
<RouterLink to='/about'>About</RouterLink>
40-
<RouterLink to='/contacts'>Contacts</RouterLink>
38+
<NavLink to='/home'>Home</NavLink>
39+
<NavLink to='/about'>About</NavLink>
40+
<NavLink to='/contacts'>Contacts</NavLink>
4141
</header>
4242
<Suspense fallback={<Spinner />}>
4343
<main key={key} class='fade'>
@@ -162,12 +162,12 @@ const GlobalStyle = createGlobalStyle`
162162
grid-template-rows: 48px 1fr;
163163
}
164164
165-
.router-link-active {
165+
.active-link {
166166
color: #ffeb3b;
167167
text-decoration: underline;
168168
}
169169
170-
.router-link-active:hover {
170+
.active-link:hover {
171171
color: #ffeb3b;
172172
}
173173

examples/server-side-rendering/frontend/components/app.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { component, Suspense, lazy, useMemo, useEffect } from '@dark-engine/core';
2-
import { type Routes, Router, RouterLink } from '@dark-engine/web-router';
2+
import { type Routes, Router, NavLink } from '@dark-engine/web-router';
33
import { DataClient, DataClientProvider, InMemoryCache } from '@dark-engine/data';
44

55
import { type Api, Key } from '../../contract';
@@ -102,9 +102,9 @@ const App = component<AppProps>(({ url, api }) => {
102102
<Root>
103103
<Header>
104104
<Menu>
105-
<RouterLink to='/products'>Products</RouterLink>
106-
<RouterLink to='/operations'>Operations</RouterLink>
107-
<RouterLink to='/invoices'>Invoices</RouterLink>
105+
<NavLink to='/products'>Products</NavLink>
106+
<NavLink to='/operations'>Operations</NavLink>
107+
<NavLink to='/invoices'>Invoices</NavLink>
108108
</Menu>
109109
</Header>
110110
<Content>{slot}</Content>

examples/server-side-rendering/frontend/components/product-card.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type DarkElement, component } from '@dark-engine/core';
2-
import { RouterLink, useMatch, useParams } from '@dark-engine/web-router';
2+
import { Link, useMatch, useParams } from '@dark-engine/web-router';
33

44
import { useProduct } from '../hooks';
55
import { Spinner, Error, Card, Button } from './ui';
@@ -29,10 +29,10 @@ const ProductCard = component<{ slot: DarkElement }>(({ slot }) => {
2929
<Card>
3030
<h3>{data.name}</h3>
3131
<p>{data.description}</p>
32-
<Button as={RouterLink} {...{ to: urlToEdit }}>
32+
<Button as={Link} {...{ to: urlToEdit }}>
3333
Edit
3434
</Button>
35-
<Button as={RouterLink} {...{ to: urlToRemove }}>
35+
<Button as={Link} {...{ to: urlToRemove }}>
3636
Remove
3737
</Button>
3838
</Card>

examples/server-side-rendering/frontend/components/product-list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type DarkElement, component } from '@dark-engine/core';
2-
import { RouterLink, useMatch, useLocation } from '@dark-engine/web-router';
2+
import { Link, useMatch, useLocation } from '@dark-engine/web-router';
33
import { styled } from '@dark-engine/styled';
44

55
import { useProducts } from '../hooks';
@@ -21,7 +21,7 @@ const ProductList = component<{ slot: DarkElement }>(({ slot }) => {
2121
{[...data].reverse().map(x => {
2222
return (
2323
<ListItem key={x.id}>
24-
<RouterLink to={`${url}/${x.id}`}>{x.name}</RouterLink>
24+
<Link to={`${url}/${x.id}`}>{x.name}</Link>
2525
</ListItem>
2626
);
2727
})}
@@ -36,7 +36,7 @@ const ProductList = component<{ slot: DarkElement }>(({ slot }) => {
3636
<AnimationFade>
3737
<Header>
3838
{isList ? (
39-
<Button as={RouterLink} {...{ to: urlToAdd }}>
39+
<Button as={Link} {...{ to: urlToAdd }}>
4040
Add product
4141
</Button>
4242
) : (

examples/server-side-rendering/frontend/components/products.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type DarkElement, component } from '@dark-engine/core';
2-
import { RouterLink, useMatch } from '@dark-engine/web-router';
2+
import { NavLink, useMatch } from '@dark-engine/web-router';
33

44
import { AnimationFade, Menu, Sticky } from './ui';
55

@@ -15,9 +15,9 @@ const Products = component<ProductsProps>(({ slot }) => {
1515
<Sticky>
1616
<h1>Products 📈</h1>
1717
<Menu $isSecondary>
18-
<RouterLink to={`${url}/list`}>List</RouterLink>
19-
<RouterLink to={`${url}/analytics`}>Analytics</RouterLink>
20-
<RouterLink to={`${url}/balance`}>Balance</RouterLink>
18+
<NavLink to={`${url}/list`}>List</NavLink>
19+
<NavLink to={`${url}/analytics`}>Analytics</NavLink>
20+
<NavLink to={`${url}/balance`}>Balance</NavLink>
2121
</Menu>
2222
</Sticky>
2323
{slot}

examples/server-side-rendering/frontend/components/ui.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const GlobalStyle = createGlobalStyle`
6565
text-decoration: underline;
6666
}
6767
68-
.router-link-active, .router-link-active:hover {
68+
.active-link, .active-link:hover {
6969
text-decoration: underline;
7070
}
7171
`;

packages/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ Allows you to avoid reloading the entire interface when changing code in develop
936936

937937
```tsx
938938
// index.tsx
939-
import { h, hot } from '@dark-engine/core';
939+
import { hot } from '@dark-engine/core';
940940
import { createRoot } from '@dark-engine/platform-browser';
941941

942942
import { App } from './app';

packages/web-router/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
type Routes,
4040
type RouterRef,
4141
Router,
42-
RouterLink,
42+
Link,
43+
NavLink,
4344
useLocation,
4445
useHistory,
4546
useParams,
@@ -63,8 +64,8 @@ const App = component(() => {
6364
return (
6465
<>
6566
<header>
66-
<RouterLink to='/first-component'>first-component</RouterLink>
67-
<RouterLink to='/second-component'>second-component</RouterLink>
67+
<NavLink to='/first-component'>first-component</NavLink>
68+
<NavLink to='/second-component'>second-component</NavLink>
6869
</header>
6970
<main>{slot}</main> {/*<-- a route content will be placed here*/}
7071
</>
@@ -222,12 +223,18 @@ const routes: Routes = [
222223

223224
## Navigation
224225

225-
### via `RouterLink`
226+
### via `Link` or `NavLink`
226227

227228
```tsx
228-
<RouterLink to='/home'>Home</RouterLink>
229+
<Link to='/user/50'>Go to profile</Link>
230+
<NavLink to='/home'>Home</NavLink>
229231
```
230232

233+
`NavLink` internally uses `Link`, but at the same time provides a CSS class `.active-link` if the current URL is equal to or contains the `to` parameter of `NavLink`.
234+
`NavLink` can be used for headers and menus, which will continue to be on the page when it is clicked and the content is changed.
235+
`Link` means that it will disappear from the screen after you click it and go to another page. Of course you can create your own logic based on `Link`, using it as a base component.
236+
237+
231238
### via `history`
232239

233240
```tsx
@@ -317,7 +324,7 @@ const App = component<AppProps>(({ url, routes }) => {
317324

318325
## Server-Side Rendering (SSR)
319326

320-
If you are rendering the application on the server, then you must pass the request url to the router to emulate routing when rendering to a string.
327+
If you are rendering the application on the server, then you must pass the request URL to the router to emulate routing when rendering to a string.
321328

322329
```tsx
323330
server.get('*', async (req, res) => {

packages/web-router/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export const PROTOCOL_MARK = '://';
66
export const SEARCH_MARK = '?';
77
export const HASH_MARK = '#';
88
export const ROOT_MARK = '__ROOT__';
9-
export const ACTIVE_LINK_CLASSNAME = 'router-link-active';
9+
export const ACTIVE_LINK_CLASSNAME = 'active-link';

packages/web-router/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export { type Routes } from './create-routes';
22
export { type RouterRef, Router } from './router';
33
export { useLocation } from './use-location';
4-
export { RouterLink } from './router-link';
54
export { useHistory } from './use-history';
65
export { useParams } from './use-params';
76
export { useMatch } from './use-match';
87
export { VERSION } from './constants';
8+
export { NavLink } from './nav-link';
9+
export { Link } from './link';

0 commit comments

Comments
 (0)