-
Notifications
You must be signed in to change notification settings - Fork 5
Advanced Setup: Svelte with ArcGIS API for JavaScript, Calcite Components, and Svelte Router
An example Svelte application that shows how use the ArcGIS API for JavaScript to load a map, and also includes Calcite Components and Svelte SPA Router. For a more basic setup, see the repo README.
- Install Node.js (LTS version recommended)
- If using VS Code, install Svelte for VS Code extension.
In the terminal, go to parent folder of where you want to start, then run:
npm init vite@latest
(Name the folder the name of your repo, then choose "Svelte")
cd uiux-pathways-app
npm install
npm run devYou now have a working Svelte app!
This git stuff is optional.
- In browser, create new repo.
- Then run in terminal:
git init
git add .
git commit -m "init"
git remote add origin [email protected]:gavinr/esri-svelte-example.git (REPLACE URL WITH YOUR REPO NAME!)
git push --set-upstream origin masterYour code is now stored in GitHub!
npm install --save-dev @esri/calcite-components- Add to top of
App.svelte:
import '@esri/calcite-components/dist/calcite/calcite.css';
import { defineCustomElements, setAssetPath } from '@esri/calcite-components/dist/custom-elements';
setAssetPath('https://unpkg.com/@esri/[email protected]/dist/calcite/assets');
defineCustomElements();- Update
counter.svelteto be<calcite-button... - Replace paragraph text in
App.svelte:
<calcite-icon icon="banana" />You now have a Svelte app with Calcite Components!
To have the illusion of multiple "website pages" we will use a router. Note that SvelteKit has this built in, but using SvelteKit is beyond the scope of this simplified demo.
npm install --save-dev svelte-spa-router- Stop the
npm run devtask. - Rename
App.sveltetoroutes/HomePage.svelte- Update imports in
HomePage.svelteto be the correct relative paths.
- Update imports in
- Create a new file,
routes/MapPage.svelte:<div>MAP <calcite-icon icon="banana" /><a href="#/">Home</a></div> - Create a new file,
App.svelte:
<script>
import Router from 'svelte-spa-router';
import HomePage from './routes/HomePage.svelte';
import MapPage from './routes/MapPage.svelte';
const routes = {
'/': HomePage,
'/map': MapPage,
}
</script>
<Router {routes}/>- Add a link to
HomePage.svelte:<a href="#/map">Map</a> - Move the calcite block from
HomePage.sveltetoApp.svelte - Move the
:root { ....block in the<style>tag fromHomePage.sveltetoApp.svelte.
You now have a Svelte app with multiple pages!
Adding a map using the ArcGIS API for JavaScript to a second page.
npm install @arcgis/core --save-dev- Create a new file,
lib/Map.svelte. Make this the contents (but remove theh1andpfrom the file). - Add the map to
routes/MapPage.svelte:
<script>
import Map from '../lib/Map.svelte';
</script>and:
<Map centerText="Loading ..." />You now have a map in the page!
You can use github actions to automatically publish a public version of the application on GitHub Pages.
- Create file:
.github/workflows/main.ymlwith these contents. - Update
vite.config.js:
export default defineConfig({
plugins: [svelte()],
// add: -----------------------------------------
base: ""
})- Add a blank file named
.nojekyllin thepublicfolder. - Commit and push.
- See the
Actionstab in your GitHub repository to see the build status. - Go to the
Settings > Pagestab in your GitHub repository and enable GitHub Pages (Source: Branch: gh-pages).
If you see an issue with these instructions, please open an issue.