-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Referansesider: Dynamic route-list generation for header and homepage
- Loading branch information
Showing
5 changed files
with
112 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useRouter } from "@tanstack/react-router"; | ||
|
||
const pathToName = (path: string) => { | ||
const parsed = path.replace(/\//g, " ").trim(); | ||
if (parsed === "") return "Hjem"; | ||
return parsed; | ||
}; | ||
|
||
export const RouteMapper = ({ | ||
children, | ||
skipRoot = false, | ||
}: { | ||
children: (path: string, name: string, root: boolean) => React.ReactNode; | ||
skipRoot?: boolean; | ||
}) => { | ||
const { routesByPath } = useRouter(); | ||
let paths = Object.keys(routesByPath); | ||
if (skipRoot) { | ||
paths = paths.filter((path) => path !== "/"); | ||
} | ||
|
||
return ( | ||
<> | ||
{paths.map((path) => { | ||
const name = pathToName(path); | ||
return children(path, name, path === "/"); | ||
})} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
import { Link, createFileRoute } from "@tanstack/react-router"; | ||
import styled from "styled-components"; | ||
import * as tokens from "@navikt/ds-tokens/dist/darkside/tokens"; | ||
import { Page } from "../components/Page"; | ||
import { RouteMapper } from "../components/RouteMapper"; | ||
|
||
const ScCard = styled(Link)` | ||
padding: 1.5rem; | ||
display: block; | ||
border-radius: 12px; | ||
text-transform: capitalize; | ||
color: ${tokens.TextAccent}; | ||
font-weight: 550; | ||
font-size: 24px; | ||
border: 1px solid ${tokens.BorderAccentSubtle}; | ||
background: ${tokens.BgAccent}; | ||
&:hover { | ||
background: ${tokens.BgAccentHover}; | ||
text-decoration: underline; | ||
} | ||
`; | ||
|
||
const Component = () => { | ||
return <h1>pick a referanseside</h1>; | ||
return ( | ||
<Page> | ||
<ul className="py-12 flex flex-col gap-4"> | ||
<RouteMapper skipRoot> | ||
{(path, name) => ( | ||
<li key={path}> | ||
<ScCard to={path}>{name}</ScCard> | ||
</li> | ||
)} | ||
</RouteMapper> | ||
</ul> | ||
</Page> | ||
); | ||
}; | ||
|
||
export const Route = createFileRoute("/")({ component: Component }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters