Learn how to create a web application with Map Components and Calcite Components using Vite developer build tools.
In this tutorial, you will:
- Load and display a pre-configured web map stored in ArcGIS Online with a LayerList widget.
- Create an application layout and user interface with Calcite Components
- Add a map and a layer list leveraging Map Components
- Learn how to use the ArcGIS Maps SDK for JavaScript to interact with the View, Map, and layers.
Prerequisites
- The most recent LTS Node.js runtime environment.
- A text editor to edit files.
- A terminal to enter commands.
Steps
Create a new project using Vite
- Download the inital Vite vanilla Javascript project to your local machine.
- Unzip the downloaded file.
- Open the unzipped folder's files in your text editor.
- Navigate to the unzipped folder in your terminal.
Install dependencies
-
Install Map Components in the terminal as a dependency.
Use dark colors for code blocks Copy npm install @arcgis/map-components
-
Later in this tutorial we will need to know which version of
@esri/calcite-components
was installed as a dependency of@arcgis/map-components
, run this command in the terminal window to find the correct version.Use dark colors for code blocks Copy npm list @esri/calcite-components
-
Start the Vite development server.
Use dark colors for code blocks Copy npm run dev
-
Open a web browser and navigate to http://localhost:5173, this webpage will be empty, however, it will update as we proceed through the remaining steps.
-
Import functions to define the custom HTML elements from the Map Components and Calcite Components libraries in
main.js
. You will need to use an alias, because the imported function from each library has the same name,define
.Custom Elements main.jsUse dark colors for code blocks import "./style.css"; import { defineCustomElements as defineMapElements } from "@arcgis/map-components/dist/loader"; import { defineCustomElements as defineCalciteElements } from "@esri/calcite-components/dist/loader";
-
Define the custom elements on the window using the Calcite Components distribution build. You will use CDN-hosted assets. When using the CDN-hosted assets, you need to keep the version number in the path the same as the version of
@esri/calcite-components
found in step two.main.jsUse dark colors for code blocks import "./style.css"; import { defineCustomElements as defineMapElements } from "@arcgis/map-components/dist/loader"; import { defineCustomElements as defineCalciteElements } from "@esri/calcite-components/dist/loader"; defineCalciteElements(window, { resourcesUrl: "https://js.arcgis.com/calcite-components/2.11.1/assets" });
-
Next, use the Map Components distribution build to define and lazy load the custom map elements.
main.jsUse dark colors for code blocks import "./style.css"; import { defineCustomElements as defineMapElements } from "@arcgis/map-components/dist/loader"; import { defineCustomElements as defineCalciteElements } from "@esri/calcite-components/dist/loader"; defineCalciteElements(window, { resourcesUrl: "https://js.arcgis.com/calcite-components/2.11.1/assets" }); defineMapElements(window, { resourcesUrl: "https://js.arcgis.com/map-components/4.30/assets" });
Add styles
-
In
style.css
, import the@arcgis/core
light theme.style.cssUse dark colors for code blocks @import "https://js.arcgis.com/4.30/@arcgis/core/assets/esri/themes/light/main.css";
-
Add CSS styles to make the
html
,body
andarcgis-map
elements the full width and height of their parent containers.style.cssUse dark colors for code blocks @import "https://js.arcgis.com/4.30/@arcgis/core/assets/esri/themes/light/main.css"; html, body, arcgis-map { height: 100%; width: 100%; margin: 0; padding: 0; }
Add components
-
To create an application layout and user interface for your app add
calcite-shell
,calcite-navigation
,calcite-navigation-logo
components toindex.html
.index.htmlUse dark colors for code blocks <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" href="data:;base64,=" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>ArcGIS Maps SDK for JavaScript Tutorials: Create a web app using components</title> </head> <body> <calcite-shell> <calcite-navigation slot="header"> <calcite-navigation-logo slot="logo"></calcite-navigation-logo> </calcite-navigation> </calcite-shell> <script type="module" src="/main.js"></script> </body> </html>
-
Next, add the
arcgis-map
andarcgis-layer-list
components.index.htmlUse dark colors for code blocks <calcite-shell> <calcite-navigation slot="header"> <calcite-navigation-logo slot="logo"></calcite-navigation-logo> </calcite-navigation> <arcgis-map item-id="e691172598f04ea8881cd2a4adaa45ba" zoom="4"> <arcgis-layer-list position="top-right"></arcgis-layer-list> </arcgis-map> </calcite-shell>
Add a Legend to the LayerList
-
In
main.js
usedocument.query
to reference theSelector() arcgis-layer-list
component and then add an event listener for the component'sarcgis
event.Layer List Ready main.jsUse dark colors for code blocks document.querySelector("arcgis-layer-list").addEventListener("arcgisLayerListReady", (event) => { });
-
Get a reference to the
arcgis-layer-list
component from theevent.target
object.main.jsUse dark colors for code blocks document.querySelector("arcgis-layer-list").addEventListener("arcgisLayerListReady", (event) => { const arcgisLayerList = event.target; });
-
Add a
list
to the LayerList. This function will add a Legend in the ListItemPanel for all layers except group layers.Item Created Function main.jsUse dark colors for code blocks document.querySelector("arcgis-layer-list").addEventListener("arcgisLayerListReady", (event) => { const arcgisLayerList = event.target; arcgisLayerList.listItemCreatedFunction = (event) => { const { item } = event; if (item.layer.type !== "group") { item.panel = { content: "legend" }; } }; });
Interacting with the the View, Map, and layers
-
Use
document.query
to reference theSelector() arcgis-map
component and then add an event listener for thearcgis-map
component'sarcgis
event.View Ready Change main.jsUse dark colors for code blocks document.querySelector("arcgis-map").addEventListener("arcgisViewReadyChange", (event) => { });
-
Create a variable for the map's PortalItem instance.
main.jsUse dark colors for code blocks document.querySelector("arcgis-map").addEventListener("arcgisViewReadyChange", (event) => { const { portalItem } = event.target.map; });
-
Next, in the
calcite-navigation
you will dynamically add a heading, description, and thumbnail to your app's UI using the PortalItem. To do so, setcalcite-navigation-logo
'sheading
,description
, andthumbnail
properties to the PortalItem'stitle
,snippet
andthumbnail
properties.Url main.jsUse dark colors for code blocks document.querySelector("arcgis-map").addEventListener("arcgisViewReadyChange", (event) => { const { portalItem } = event.target.map; const navigationLogo = document.querySelector("calcite-navigation-logo"); navigationLogo.heading = portalItem.title; navigationLogo.description = portalItem.snippet; navigationLogo.thumbnail = portalItem.thumbnailUrl; });
-
Find the accidental deaths layer in the
view.map.layers
collection.main.jsUse dark colors for code blocks document.querySelector("arcgis-map").addEventListener("arcgisViewReadyChange", (event) => { const { portalItem } = event.target.map; const navigationLogo = document.querySelector("calcite-navigation-logo"); navigationLogo.heading = portalItem.title; navigationLogo.description = portalItem.snippet; navigationLogo.thumbnail = portalItem.thumbnailUrl; const layer = event.target.map.layers.find((layer) => layer.id === "Accidental_Deaths_8938"); });
-
Modify the layer's PopupTemplate title.
main.jsUse dark colors for code blocks document.querySelector("arcgis-map").addEventListener("arcgisViewReadyChange", (event) => { const { portalItem } = event.target.map; const navigationLogo = document.querySelector("calcite-navigation-logo"); navigationLogo.heading = portalItem.title; navigationLogo.description = portalItem.snippet; navigationLogo.thumbnail = portalItem.thumbnailUrl; const layer = event.target.map.layers.find((layer) => layer.id === "Accidental_Deaths_8938"); layer.popupTemplate.title = "Accidental Deaths"; });
Run the application
- Start the application from your project directory by running npm run dev in a terminal window. It should launch and be visible in your browser at http://localhost:5173.