-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from andrewgryan/feature/divicon
DivIcon
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
+++ | ||
title = "Div icons" | ||
+++ | ||
|
||
A customisable icon using a single `<div/>` and CSS. | ||
For inspiration to style a single div, visit [https://a.singlediv.com/](https://a.singlediv.com/) | ||
|
||
<style> | ||
.icon { | ||
border: none; | ||
border-radius: 1000px; | ||
} | ||
|
||
.pink { | ||
background-color: hotpink; | ||
} | ||
|
||
.blue { | ||
background-color: cadetblue; | ||
} | ||
</style> | ||
|
||
<l-map zoom="5" center="[45, 0]"> | ||
<l-tile-layer | ||
url-template="https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png" | ||
></l-tile-layer> | ||
<l-marker lat-lng="[45, 1]"> | ||
<l-div-icon class-name="icon pink"></l-div-icon> | ||
</l-marker> | ||
<l-marker lat-lng="[45, -1]"> | ||
<l-div-icon class-name="icon blue"></l-div-icon> | ||
</l-marker> | ||
</l-map> | ||
## Styling | ||
|
||
If custom styles are to be applied, | ||
it is best to use the JS attribute `className` by using the `class-name` equivalent HTML attribute. | ||
|
||
```css | ||
.icon { | ||
border: none; | ||
border-radius: 1000px; | ||
} | ||
|
||
.pink { | ||
background-color: hotpink; | ||
} | ||
|
||
.blue { | ||
background-color: cadetblue; | ||
} | ||
``` | ||
|
||
## Mark-up | ||
|
||
The usual rules for converting from JS to HTML apply. | ||
|
||
```html,hl_lines=6 9,linenos | ||
<l-map zoom="5" center="[45, 0]"> | ||
<l-tile-layer | ||
url-template="https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png" | ||
></l-tile-layer> | ||
<l-marker lat-lng="[45, 1]"> | ||
<l-div-icon class-name="icon pink"></l-div-icon> | ||
</l-marker> | ||
<l-marker lat-lng="[45, -1]"> | ||
<l-div-icon class-name="icon blue"></l-div-icon> | ||
</l-marker> | ||
</l-map> | ||
``` | ||
|
||
Line 6 and 9 above show how to use a div icon to create a pink and blue circle icon. | ||
|
||
# Conclusion | ||
|
||
Div icons enable a front-end developer to craft amazing visualisations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { divIcon } from "leaflet"; | ||
import { iconConnected } from "./events"; | ||
|
||
export default class CustomElement extends HTMLElement { | ||
constructor() { | ||
super(); | ||
this.icon = null; | ||
} | ||
|
||
connectedCallback() { | ||
// Leaflet JS DivIcon options | ||
const options = { | ||
html: this.innerHTML, | ||
}; | ||
const className = this.getAttribute("class-name"); | ||
if (className !== null) { | ||
options["className"] = className; | ||
} | ||
|
||
this.icon = divIcon(options); | ||
this.dispatchEvent( | ||
new CustomEvent(iconConnected, { | ||
bubbles: true, | ||
cancelable: true, | ||
detail: { | ||
icon: this.icon, | ||
}, | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @vitest-environment happy-dom | ||
import { divIcon, DivIcon } from "leaflet"; | ||
import { it, expect } from "vitest"; | ||
import "./index.js"; | ||
|
||
it("should render a div icon", () => { | ||
const el = document.createElement("l-div-icon"); | ||
el.innerHTML = "Hello, World!"; | ||
document.body.appendChild(el); | ||
expect(el.icon).toBeInstanceOf(DivIcon); | ||
expect(el.icon).toEqual(divIcon({ html: "Hello, World!" })); | ||
}); | ||
|
||
it("should attach div icon to marker", () => { | ||
const icon = document.createElement("l-div-icon"); | ||
const marker = document.createElement("l-marker"); | ||
marker.setAttribute("lat-lng", "[0,0]"); | ||
marker.appendChild(icon); | ||
document.body.appendChild(marker); | ||
|
||
const actual = marker.layer.getIcon(); | ||
expect(icon.icon).toEqual(marker.layer.getIcon()); | ||
}); |