-
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 dev
You 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 git@github.com:gavinr/esri-svelte-example.git (REPLACE URL WITH YOUR REPO NAME!)
git push --set-upstream origin master
Your 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/calcite-components@1.0.0-beta.67/dist/calcite/assets');
defineCustomElements();
- Update
counter.svelte
to 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 dev
task. - Rename
App.svelte
toroutes/HomePage.svelte
- Update imports in
HomePage.svelte
to 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.svelte
toApp.svelte
- Move the
:root { ....
block in the<style>
tag fromHomePage.svelte
toApp.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 theh1
andp
from 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.yml
with these contents. - Update
vite.config.js
:
export default defineConfig({
plugins: [svelte()],
// add: -----------------------------------------
base: ""
})
- Add a blank file named
.nojekyll
in thepublic
folder. - Commit and push.
- See the
Actions
tab in your GitHub repository to see the build status. - Go to the
Settings > Pages
tab in your GitHub repository and enable GitHub Pages (Source: Branch: gh-pages).
If you see an issue with these instructions, please open an issue.