Skip to content

Commit 1170c27

Browse files
committed
react mapbox typescript tailwindcss basic setup
0 parents  commit 1170c27

20 files changed

+2639
-0
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_MAPBOX_ACCESS_TOKEN=pk.eyJ1IjoiZGlsZGFyLWtoYW44NyIsImEiOiJjbHM5MHF6eWowMjFtMmpycWl4cDl2Y3ZpIn0.13bOgPipqiJEyl1KLQKNdA

.eslintrc.cjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:react-hooks/recommended',
8+
],
9+
ignorePatterns: ['dist', '.eslintrc.cjs'],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['react-refresh'],
12+
rules: {
13+
'react-refresh/only-export-components': [
14+
'warn',
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
}

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# React + TypeScript + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13+
14+
- Configure the top-level `parserOptions` property like this:
15+
16+
```js
17+
export default {
18+
// other rules...
19+
parserOptions: {
20+
ecmaVersion: 'latest',
21+
sourceType: 'module',
22+
project: ['./tsconfig.json', './tsconfig.node.json'],
23+
tsconfigRootDir: __dirname,
24+
},
25+
}
26+
```
27+
28+
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
29+
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>React Mapbox Typescript Integration</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "react-typescript-mapbox-tailwindcss",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"mapbox-gl": "^3.1.2",
14+
"react": "^18.2.0",
15+
"react-dom": "^18.2.0"
16+
},
17+
"devDependencies": {
18+
"@types/mapbox-gl": "^2.7.20",
19+
"@types/react": "^18.2.43",
20+
"@types/react-dom": "^18.2.17",
21+
"@typescript-eslint/eslint-plugin": "^6.14.0",
22+
"@typescript-eslint/parser": "^6.14.0",
23+
"@vitejs/plugin-react": "^4.2.1",
24+
"autoprefixer": "^10.4.17",
25+
"eslint": "^8.55.0",
26+
"eslint-plugin-react-hooks": "^4.6.0",
27+
"eslint-plugin-react-refresh": "^0.4.5",
28+
"postcss": "^8.4.33",
29+
"tailwindcss": "^3.4.1",
30+
"typescript": "^5.2.2",
31+
"vite": "^5.0.8"
32+
}
33+
}

postcss.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
},
6+
}

public/vite.svg

+1
Loading

src/App.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Map from "./components/Map";
2+
3+
const App = () => {
4+
return (
5+
<div>
6+
<Map />
7+
</div>
8+
);
9+
};
10+
11+
export default App;

src/components/Map.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useRef } from "react";
2+
import { useMap } from "../hooks/useMap";
3+
4+
const Map = () => {
5+
const mapRef = useRef<HTMLDivElement>(null);
6+
useMap(mapRef);
7+
8+
return <div ref={mapRef} className="w-screen h-screen" />;
9+
};
10+
11+
export default Map;

src/hooks/useMap.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { useEffect, useRef, RefObject } from "react";
2+
import { Map } from "mapbox-gl";
3+
import { initMap } from "../utils";
4+
5+
export const useMap = (container: RefObject<HTMLDivElement>) => {
6+
const mapInitRef = useRef<Map | null>(null);
7+
8+
useEffect(() => {
9+
if (container.current) {
10+
mapInitRef.current = initMap(
11+
container.current,
12+
[-100.31019063199852, 25.66901932031443]
13+
);
14+
}
15+
}, []);
16+
17+
// useEffect(() => {
18+
// if (!container.current) return;
19+
20+
// mapInitRef.current.on("style.load", () => {
21+
// mapInitRef.current.setFog({}); // Set the default atmosphere style
22+
// });
23+
24+
// // At low zooms, complete a revolution every two minutes.
25+
// const secondsPerRevolution = 120;
26+
// // Above zoom level 5, do not rotate.
27+
// const maxSpinZoom = 5;
28+
// // Rotate at intermediate speeds between zoom levels 3 and 5.
29+
// const slowSpinZoom = 3;
30+
31+
// function spinGlobe() {
32+
// const zoom = mapInitRef.current && mapInitRef.current.getZoom();
33+
// if (zoom && zoom < maxSpinZoom) {
34+
// let distancePerSecond = 360 / secondsPerRevolution;
35+
// if (zoom > slowSpinZoom) {
36+
// // Slow spinning at higher zooms
37+
// const zoomDif = (maxSpinZoom - zoom) / (maxSpinZoom - slowSpinZoom);
38+
// distancePerSecond *= zoomDif;
39+
// }
40+
// const center = mapInitRef.current && mapInitRef.current.getCenter();
41+
// if (center) {
42+
// center.lng -= distancePerSecond;
43+
// // Smoothly animate the map over one second.
44+
// // When this animation is complete, it calls a 'moveend' event.
45+
// mapInitRef.current &&
46+
// mapInitRef.current.easeTo({
47+
// center,
48+
// duration: 1000,
49+
// easing: (n) => n,
50+
// });
51+
// }
52+
// }
53+
// }
54+
55+
// // When animation is complete, start spinning if there is no ongoing interaction
56+
// mapInitRef.current.on("moveend", () => {
57+
// spinGlobe();
58+
// });
59+
60+
// spinGlobe();
61+
// }, []);
62+
63+
// useEffect(() => {
64+
// mapInitRef.current && mapInitRef.current.on('dblclick', ({ lngLat }) => generateNewMarker({ map: mapInitRef.current!, ...lngLat }))
65+
66+
// return () => { mapInitRef.current?.off('dblclick', generateNewMarker) }
67+
// }, [])
68+
69+
// useEffect(() => {
70+
// mapInitRef.current && mapInitRef.current.on('load', () => generateNewMarker({ map: mapInitRef.current!, ...mapInitRef.current!.getCenter() }))
71+
72+
// return () => { mapInitRef.current?.off('load', generateNewMarker) }
73+
// }, [])
74+
};

src/index.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
@tailwind base;
7+
@tailwind components;
8+
@tailwind utilities;

src/main.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import App from "./App.tsx";
4+
import "./index.css";
5+
import "mapbox-gl/dist/mapbox-gl.css";
6+
7+
ReactDOM.createRoot(document.getElementById("root")!).render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);

src/utils/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Map, LngLatLike } from "mapbox-gl";
2+
3+
const { VITE_MAPBOX_ACCESS_TOKEN } = import.meta.env;
4+
5+
export const initMap = (container: HTMLDivElement, coords: LngLatLike) =>
6+
new Map({
7+
container,
8+
// style: "mapbox://styles/mapbox/satellite-v9",
9+
// pitchWithRotate: false,
10+
center: coords,
11+
zoom: 2,
12+
accessToken: VITE_MAPBOX_ACCESS_TOKEN,
13+
doubleClickZoom: false,
14+
projection: { name: "globe" },
15+
});

src/vite-env.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly VITE_MAPBOX_ACCESS_TOKEN: string;
5+
}
6+
7+
interface ImportMeta {
8+
readonly env: ImportMetaEnv;
9+
}

tailwind.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
4+
theme: {
5+
extend: {},
6+
},
7+
plugins: [],
8+
};

tsconfig.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"noEmit": true,
15+
"jsx": "react-jsx",
16+
17+
/* Linting */
18+
"strict": true,
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true,
21+
"noFallthroughCasesInSwitch": true
22+
},
23+
"include": ["src"],
24+
"references": [{ "path": "./tsconfig.node.json" }]
25+
}

tsconfig.node.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"skipLibCheck": true,
5+
"module": "ESNext",
6+
"moduleResolution": "bundler",
7+
"allowSyntheticDefaultImports": true
8+
},
9+
"include": ["vite.config.ts"]
10+
}

vite.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [react()],
7+
})

0 commit comments

Comments
 (0)