[openspending, maps][m]: fix build for leaflet map

This commit is contained in:
João Demenech 2023-07-06 17:13:37 -03:00
parent 5e72711629
commit 299477a717
11 changed files with 51 additions and 44 deletions

View File

@ -1,19 +1,10 @@
<!--
This is necessary for maps to work.
If we decide to distribute this component,
perhaps we could use a provider for this,
e.g.
.../_app.tsx
<PortalJSProvider>
<Component />
</PortalJSProvider>
This is necessary for Leaflet maps to work.
-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<!-- <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script> -->
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""
/>

View File

@ -1,6 +1,7 @@
import 'tailwindcss/tailwind.css'
import '../src/index.css'
import type { Preview } from '@storybook/react';
const preview: Preview = {

View File

@ -12,12 +12,14 @@
],
"scripts": {
"dev": "npm run storybook",
"build": "tsc && vite build && npm run build-tailwind",
"example": "vite",
"build": "tsc && vite build && npm run build-tailwind && npm run fix-leaflet",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"build-tailwind": "NODE_ENV=production npx tailwindcss -o ./dist/styles.css --minify",
"prepare": "npm run build"
"build-tailwind": "NODE_ENV=production npx tailwindcss --postcss -c tailwind.config.js -i src/index.css -o ./dist/styles.css --minify",
"prepare": "npm run build",
"fix-leaflet": "node ./scripts/fix-leaflet.cjs"
},
"peerDependencies": {
"react": "^18.2.0",
@ -66,6 +68,8 @@
"eslint-plugin-storybook": "^0.6.11",
"json": "^11.0.0",
"postcss": "^8.4.23",
"postcss-import": "^15.1.0",
"postcss-import-url": "^7.2.0",
"prop-types": "^15.8.1",
"storybook": "^7.0.7",
"tailwindcss": "^3.3.2",

View File

@ -1,6 +1,9 @@
console.log('PostCSS')
export default {
plugins: {
'postcss-import': {},
tailwindcss: {},
autoprefixer: {},
},
}
};

View File

@ -0,0 +1,6 @@
const fs = require('fs');
const path = require('path');
const leafletPath = path.join(require.resolve('leaflet'), '../')
fs.cpSync(`${leafletPath}images`,'./dist/images', { recursive: true });

View File

@ -6,7 +6,6 @@ import {
MapContainer,
TileLayer,
GeoJSON as GeoJSONLayer,
useMap,
} from 'react-leaflet';
import * as L from 'leaflet';
@ -37,7 +36,6 @@ export function Map({
}: MapProps) {
const [isLoading, setIsLoading] = useState<boolean>(false);
title;
// By default, assumes data is an Array...
const [geoJsonData, setGeoJsonData] = useState<any>(null);
@ -69,8 +67,6 @@ export function Map({
}, []);
const onEachFeature = (feature, layer) => {
const map = useMap();
const geometryType = feature.type;
if (tooltip.prop)
@ -105,29 +101,32 @@ export function Map({
center={[center.latitude, center.longitude]}
zoom={zoom}
scrollWheelZoom={false}
className="h-[500px]"
whenReady={(map) => {
className="h-80 w-full"
// @ts-ignore
whenReady={(map: any) => {
map.target.scrollWheelZoom.enable();
var info = L.control();
var info = new L.Control() as any;
info.onAdd = function (map) {
info.onAdd = function () {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
info.update = function () {
this._div.innerHTML = `<h4 style="font-weight: 600; background: #f9f9f9; padding: 5px; border-radius: 5px; color: #464646;">${title}</h4>`;
};
if (title) info.addTo(map.target);
setTimeout(() => map.target.invalidateSize(), 5000);
}}
>
<GeoJSONLayer
data={geoJsonData}
style={(geoJsonFeature) => {
return { color: geoJsonFeature.color };
style={(geoJsonFeature: any) => {
return { color: geoJsonFeature?.color };
}}
onEachFeature={onEachFeature}
/>

View File

@ -0,0 +1,6 @@
/* Temporary fix for a size issue with FlatUiTable loading indicator on Firefox */
@layer base {
svg[tw^='animate-pulse w-12'] {
max-width: 100px;
}
}

View File

@ -1,10 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
/* Temporary fix for a size issue with FlatUiTable loading indicator on Firefox */
@layer base {
svg[tw^='animate-pulse w-12'] {
max-width: 100px;
}
}
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "leaflet";
@import "include";
@import "tailwindcss/utilities";

View File

@ -4,3 +4,4 @@ export * from "./components/LineChart";
export * from "./components/Vega";
export * from "./components/VegaLite";
export * from "./components/FlatUiTable";
export * from "./components/Map";

View File

@ -1,8 +1,6 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
},
theme: {},
plugins: [],
};

View File

@ -41,12 +41,13 @@ const app = async (): Promise<UserConfigExport> => {
fileName: (format) => `components.${format}.js`,
},
rollupOptions: {
external: ['react', 'react-dom', 'tailwindcss', 'vega-lite', 'vega', 'react-vega'],
external: ['react', 'react-dom', 'tailwindcss', 'vega-lite', 'vega', 'react-vega', 'leaflet'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
tailwindcss: 'tailwindcss',
leaflet: 'leaflet'
},
},
},