Skip to content

Commit 6411af9

Browse files
committed
Implement interactive swiss map
1 parent c3a1ce3 commit 6411af9

14 files changed

+4805
-87
lines changed

gatsby-config.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
icon: `src/images/DB.svg`, // This path is relative to the root of the site.
5151
},
5252
},
53+
`gatsby-plugin-styled-components`,
5354
// this (optional) plugin enables Progressive Web App + Offline functionality
5455
// To learn more, visit: https://gatsby.dev/offline
5556
// `gatsby-plugin-offline`,

gatsby-node.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const cantons = require("./src/assets/cantons")
2+
13
exports.createPages = async ({ actions, graphql, reporter }) => {
24
const { createPage } = actions
35

@@ -36,4 +38,16 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
3638
},
3739
})
3840
})
41+
42+
const cantonPageTemplate = require.resolve("./src/templates/cantonTemplate.js")
43+
44+
cantons.forEach(canton =>
45+
createPage({
46+
path: `tour-de-suisse/${canton.id.toLowerCase()}`,
47+
component: cantonPageTemplate,
48+
context: {
49+
canton,
50+
},
51+
})
52+
)
3953
}

package-lock.json

+3,869-84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@mdx-js/mdx": "^1.6.22",
99
"@mdx-js/react": "^1.6.22",
1010
"@react-icons/all-files": "^4.1.0",
11+
"babel-plugin-styled-components": "^1.13.2",
1112
"bootstrap": "^4.5.3",
1213
"dayjs": "^1.9.7",
1314
"dotenv": "^8.2.0",
@@ -20,6 +21,7 @@
2021
"gatsby-plugin-react-helmet": "^3.3.14",
2122
"gatsby-plugin-sass": "^2.4.2",
2223
"gatsby-plugin-sharp": "^2.7.1",
24+
"gatsby-plugin-styled-components": "^4.11.0",
2325
"gatsby-source-filesystem": "^3.1.0",
2426
"gatsby-source-github-api": "^0.2.1",
2527
"gatsby-transformer-remark": "^3.1.0",
@@ -30,7 +32,8 @@
3032
"react-dom": "^16.14.0",
3133
"react-helmet": "^6.1.0",
3234
"react-icons": "^4.1.0",
33-
"react-vertical-timeline-component": "^3.3.1"
35+
"react-vertical-timeline-component": "^3.3.1",
36+
"styled-components": "^5.3.0"
3437
},
3538
"devDependencies": {
3639
"prettier": "2.1.2"

src/assets/cantons.js

+267
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/cantons/canton.tsx

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Link } from "gatsby"
2+
import React, { useState } from "react"
3+
import { Canton as CantonType } from "../../types/canton"
4+
5+
interface CantonProps extends CantonType {
6+
children?: React.ReactNode
7+
onClick?: (id: string) => void
8+
isActive: boolean
9+
}
10+
11+
const Canton = ({
12+
name,
13+
capital,
14+
path,
15+
id,
16+
onClick,
17+
colour,
18+
circleTransform,
19+
textX,
20+
textY,
21+
isActive,
22+
}: CantonProps) => {
23+
const [isFocus, setFocus] = useState(false)
24+
25+
return (
26+
<Link
27+
to={`/tour-de-suisse/${id.toLowerCase()}`}
28+
css={`
29+
&:hover {
30+
text-decoration: none;
31+
}
32+
`}
33+
>
34+
<g transform="translate(154.89465,-548.33966)">
35+
<g transform="matrix(0.4753467,0,0,0.4753467,-71.265996,459.47553)">
36+
<g transform="translate(-190.45925,197.87884)">
37+
<path
38+
d={path}
39+
onMouseOver={() => setFocus(true)}
40+
onMouseLeave={() => setFocus(false)}
41+
onClick={() => onClick?.(id)}
42+
css={`
43+
fill: ${isActive
44+
? "var(--primary)"
45+
: colour ?? "var(--secondary)"};
46+
stroke: white;
47+
strok-width: 0.005;
48+
&:hover {
49+
fill: var(--primary);
50+
}
51+
`}
52+
/>
53+
</g>
54+
</g>
55+
</g>
56+
<g transform="translate(154.89465,-548.33966)">
57+
<g transform="matrix(0.4753467,0,0,0.4753467,-71.265996,459.47553)">
58+
<circle
59+
cx="340"
60+
cy="160"
61+
r="3"
62+
transform={circleTransform}
63+
css={`
64+
fill: ${isFocus ? "white" : "#000000"};
65+
stroke: ${isFocus ? "white" : "#000000"};
66+
display: ${isFocus ? "block" : "none"};
67+
stroke-width: 0pt;
68+
z-index: 50;
69+
`}
70+
/>
71+
<text
72+
x={textX}
73+
y={textY}
74+
css={`
75+
font-size: 10px;
76+
text-align: start;
77+
line-height: 125%;
78+
text-anchor: start;
79+
fill: ${isFocus ? "#d3ebfa" : "#000000"};
80+
display: ${isFocus ? "block" : "none"};
81+
font-family: Verdana;
82+
z-index: 50;
83+
`}
84+
>
85+
<tspan
86+
x={textX}
87+
y={textY}
88+
css={`
89+
z-index: 50;
90+
`}
91+
>
92+
{capital}
93+
</tspan>
94+
</text>
95+
</g>
96+
</g>
97+
</Link>
98+
)
99+
}
100+
101+
export default Canton

src/components/cantons/map.tsx

+184
Large diffs are not rendered by default.

src/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type {} from "styled-components/cssprop"

src/images/Schweizer_Karte_mit_Kantons-_und_Hauptstadtbezeichnungen_(deutsch).svg

+211
Loading

src/images/Switzerland_administrative_divisions_-_colored_unlabelled.svg

+112
Loading

src/pages/tour-de-suisse/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react"
2+
import Map from "../../components/cantons/map"
3+
4+
const TDS = () => {
5+
return <Map />
6+
}
7+
8+
export default TDS

src/style/blog.scss

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@
8585
strong {
8686
color: var(--primary);
8787
}
88-
th, td{
89-
padding:15px;
88+
th,
89+
td {
90+
padding: 15px;
9091
width: 20%;
9192
}
9293
}

src/templates/cantonTemplate.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react"
2+
import Map from "../components/cantons/map"
3+
4+
/**
5+
*
6+
* @param {Object} props
7+
* @param {Object} props.pageContext
8+
* @param {import("../types/canton").Canton} props.pageContext.canton
9+
* @returns
10+
*/
11+
const TDS = ({ pageContext: { canton } }) => {
12+
return (
13+
<>
14+
{canton.name}
15+
<Map activeCanton={canton.id} />
16+
</>
17+
)
18+
}
19+
20+
export default TDS

src/types/canton.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Canton {
2+
name: string
3+
capital: string
4+
id: string
5+
path: string
6+
colour?: string
7+
circleTransform: string
8+
textX: number
9+
textY: number
10+
}

0 commit comments

Comments
 (0)