Skip to content

Commit

Permalink
Added CSS for arcgis and maplibre mapping clients, and other general …
Browse files Browse the repository at this point in the history
…updates (#12)
  • Loading branch information
zachsa committed Aug 5, 2022
1 parent fa6e75b commit 8805505
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"Algoa",
"appleboy",
"arange",
"arcgis",
"BASEMAP",
"bottomlat",
"browserslist",
"bson",
Expand Down
5 changes: 5 additions & 0 deletions web/client/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ html, body, #root {
.debug {
box-sizing: border-box;
border: 2px solid red !important;
}

/* TODO - file this as an issue on maplibre github repository - should not be required */
.mapboxgl-ctrl-bottom-right, .maplibregl-ctrl-bottom-right {
min-height: 44px;
}
2 changes: 0 additions & 2 deletions web/client/html/esri-atlas.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
</script>

<link rel="stylesheet" href="/index.css" />

<link rel="stylesheet" href="https://js.arcgis.com/4.24/esri/themes/light/main.css" />
<script type="module" src="index.esri-atlas.js"></script>

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
Expand Down
15 changes: 15 additions & 0 deletions web/client/modules/config/env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ export const API_HTTP = `${API}/http`
export const API_GQL = `${API}/graphql`
export const TECHNICAL_CONTACT =
process.env.TECHNICAL_CONTACT || 'Missing configuration value [TECHNICAL_CONTACT]'

let importMap = undefined
let packageJson = undefined

try {
packageJson = JSON.parse(process.env.PACKAGE_JSON)
importMap = JSON.parse(
[...document.getElementsByTagName('script')].find(({ type }) => type === 'importmap').innerHTML
)
} catch (error) {
console.warn('Might be better to avoid this error rather than catch it', error)
}

export const IMPORT_MAP = importMap
export const PACKAGE_JSON = packageJson
42 changes: 30 additions & 12 deletions web/client/pages/esri-atlas/ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
/**
* This page doesn't support SSR
*/
import { useState, useEffect, lazy, Suspense } from 'react'
import { useState, useEffect, lazy, Suspense, useContext, useMemo } from 'react'
import { ctx as configContext } from '../../modules/config'
import { createPortal } from 'react-dom'
import Box from '@mui/material/Box'
import { Linear as Loading } from '../../components/loading'

const Map = lazy(() => import('./map'))

export default () => {
const [isClient, setIsClient] = useState(false)
const { IMPORT_MAP } = useContext(configContext)

useEffect(() => {
setIsClient(true)
}, [])

const arcGisVersion = useMemo(
() => IMPORT_MAP?.scopes['../']['@arcgis/core/Map'].match(/\@arcgis\/core@(.*)\/Map.js/)[1],
[]
)

if (!isClient) {
return null
}

return (
<Suspense fallback={<Loading />}>
<Box
sx={{
flexGrow: 1,
background: theme => theme.palette.grey[100],
transition: theme => theme.transitions.create(['background-color']),
}}
>
<Map />
</Box>
</Suspense>
<>
{/* Render the stylesheet into the head */}
{createPortal(
<link
rel="stylesheet"
href={`https://js.arcgis.com/${arcGisVersion}/esri/themes/light/main.css`}
/>,
document.getElementsByTagName('head')[0]
)}
<Suspense fallback={<Loading />}>
<Box
sx={{
flexGrow: 1,
background: theme => theme.palette.grey[100],
transition: theme => theme.transitions.create(['background-color']),
}}
>
<Map />
</Box>
</Suspense>
</>
)
}
48 changes: 3 additions & 45 deletions web/client/pages/maplibre-atlas/map/_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,11 @@ import { createContext, useEffect, useRef, useContext } from 'react'
import { ctx as configContext } from '../../../modules/config'
import useTheme from '@mui/material/styles/useTheme'
import Div from '../../../components/div'
import Span from '../../../components/span'
import maplibre from 'maplibre-gl'
import Typography from '@mui/material/Typography'
import { alpha } from '@mui/system/colorManipulator'

const Attribution = () => {
return (
<Typography
variant="caption"
sx={{
position: 'absolute',
bottom: 0,
right: 0,
backgroundColor: theme => alpha(theme.palette.common.white, 0.5),
m: theme => theme.spacing(0),
p: theme => theme.spacing(0.5),
zIndex: 1,
fontSize: 12,
}}
>
<Span
sx={{
display: 'flex',
alignItems: 'center',
}}
>
Powered by Esri
</Span>
</Typography>
)
}

export const ctx = createContext(null)

const ESRI_BASEMAP = 'ArcGIS:Terrain'
const ESRI_BASEMAP = 'ArcGIS:ChartedTerritory'

export default ({ children }) => {
const { TILESERV_BASE_URL, ESRI_API_KEY } = useContext(configContext)
Expand All @@ -50,7 +20,7 @@ export default ({ children }) => {
style: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${ESRI_BASEMAP}?type=style&token=${ESRI_API_KEY}`,
center: [25, -30],
zoom: 5,
attributionControl: false,
attributionControl: true,
})

window.maplibre = {
Expand Down Expand Up @@ -115,19 +85,7 @@ export default ({ children }) => {

return (
<ctx.Provider value={{ map: mapRef.current }}>
<Div
ref={ref}
sx={{
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
}}
>
{' '}
<Attribution />
</Div>
<Div ref={ref} sx={{ display: 'flex', flexGrow: 1 }} />
{children}
</ctx.Provider>
)
Expand Down
44 changes: 32 additions & 12 deletions web/client/pages/maplibre-atlas/ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
/**
* This page doesn't support SSR
*/
import { useState, useEffect, lazy, Suspense } from 'react'
import { useState, useEffect, lazy, Suspense, useContext, useMemo } from 'react'
import { ctx as configContext } from '../../modules/config'
import { createPortal } from 'react-dom'
import Box from '@mui/material/Box'
import { Linear as Loading } from '../../components/loading'

const Map = lazy(() => import('./map'))

export default () => {
const [isClient, setIsClient] = useState(false)
const { IMPORT_MAP } = useContext(configContext)

useEffect(() => {
setIsClient(true)
}, [])

const maplibreVersion = useMemo(
() => IMPORT_MAP?.scopes['../']['maplibre-gl'].match(/maplibre-gl@(.*)\/dist/)[1],
[]
)

if (!isClient) {
return null
}

return (
<Suspense fallback={<Loading />}>
<Box
sx={{
flexGrow: 1,
background: theme => theme.palette.grey[100],
transition: theme => theme.transitions.create(['background-color']),
}}
>
<Map />
</Box>
</Suspense>
<>
{/* Render the stylesheet into the head */}
{createPortal(
<link
rel="stylesheet"
href={`https://unpkg.com/maplibre-gl@${maplibreVersion}/dist/maplibre-gl.css`}
/>,
document.getElementsByTagName('head')[0]
)}
<Suspense fallback={<Loading />}>
<Box
sx={{
display: 'flex',
flexGrow: 1,
background: theme => theme.palette.grey[100],
transition: theme => theme.transitions.create(['background-color']),
}}
>
<Map />
</Box>
</Suspense>
</>
)
}
4 changes: 0 additions & 4 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"type": "module",
"description": "SOMISANA web stack",
"author": "[email protected]",
"scripts": {
"rollup:client": "rollup --config rollup/client.config.js",
"rollup:ssr": "rollup --config rollup/server.config.js"
},
"dependencies": {
"@apollo/client": "^3.6.9",
"@arcgis/core": "^4.24.7",
Expand Down
16 changes: 8 additions & 8 deletions web/rollup/client.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import swc from 'rollup-plugin-swc'
import replace from '@rollup/plugin-replace'
import { dirname } from 'path'
import { dirname, normalize } from 'path'
import { fileURLToPath } from 'url'
import { join } from 'path'
import fs from 'fs'
import { readdirSync, lstatSync, readFileSync } from 'fs'
import rimraf from 'rimraf'
import extensions from './plugins/extensions.js'
import css from 'rollup-plugin-import-css'
Expand All @@ -13,6 +13,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url))

rimraf.sync(join(__dirname, '../.cache/*.js'))

const PACKAGE_JSON = readFileSync(normalize(join(__dirname, '../package.json'))).toString('utf8')
const NODE_ENV = process.env.NODE_ENV || 'development'
const API = process.env.API || 'http://localhost:3000'
const TILESERV_BASE_URL = process.env.TILESERV_BASE_URL || 'http://localhost:7800'
Expand All @@ -21,15 +22,13 @@ const TECHNICAL_CONTACT =
process.env.TECHNICAL_CONTACT || 'Missing configuration (TECHNICAL_CONTACT)'

export default {
input: fs
.readdirSync(join(__dirname, '../client/pages'))
.filter(name => fs.lstatSync(join(__dirname, `../client/pages/${name}`)).isDirectory())
input: readdirSync(join(__dirname, '../client/pages'))
.filter(name => lstatSync(join(__dirname, `../client/pages/${name}`)).isDirectory())
.map(name =>
fs
.readdirSync(join(__dirname, `../client/pages/${name}`))
readdirSync(join(__dirname, `../client/pages/${name}`))
.filter(n => {
const p = join(__dirname, `../client/pages/${name}/${n}`)
return fs.lstatSync(p).isFile() && !p.endsWith('.html')
return lstatSync(p).isFile() && !p.endsWith('.html')
})
.map(f => join(__dirname, `../client/pages/${name}/${f}`))
)
Expand Down Expand Up @@ -63,6 +62,7 @@ export default {
'process.env.TECHNICAL_CONTACT': JSON.stringify(TECHNICAL_CONTACT),
'process.env.TILESERV_BASE_URL': JSON.stringify(TILESERV_BASE_URL),
'process.env.ESRI_API_KEY': JSON.stringify(ESRI_API_KEY),
'process.env.PACKAGE_JSON': JSON.stringify(PACKAGE_JSON),
}),
css({
output: 'index.css',
Expand Down
5 changes: 5 additions & 0 deletions web/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"jsx": "react-jsx"
}
}

0 comments on commit 8805505

Please sign in to comment.