Skip to content

Commit

Permalink
fix: Accessibility in <my-map> Web component (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed Jan 15, 2022
1 parent 87d9a35 commit 8af4875
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion demo/data/landmarks.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
}, {
"type": "Feature",
"properties": {
"popupContent": "This is Big Ben!"
"name": "Big Ben",
"popupContent": "This is Big Ben in London!",
"url": "https://www.latlong.net/place/big-ben-london-uk-8963.html"
},
"geometry": {
"type": "Point",
Expand Down
46 changes: 40 additions & 6 deletions src/components/MyMapElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class MyMapElement extends MyElement {
this.shadowRoot.appendChild(SC);
}, 100); */

setTimeout(() => {
setTimeout(async () => {
const L = window.L;

const map = L.map(mapElem).setView([attr.lat, attr.long], attr.zoom);
Expand All @@ -61,11 +61,13 @@ export class MyMapElement extends MyElement {
};

if (attr.geojson) {
this.loadGeoJson(attr.geojson).then(res => res.addTo(map));
await this.loadGeoJson(attr.geojson).then(res => res.addTo(map));
}

// this._accessibilityFixes();

console.debug('my-map:', L.version, this.$$, this);
}, 800); // Was: 250;
}, 1800); // Was: 250;
}

async loadGeoJson (geojson) {
Expand All @@ -78,16 +80,27 @@ export class MyMapElement extends MyElement {
// const template = document.getElementById('geojson');
// const geoJsonFeatures = JSON.parse(template.content.textContent);

let count = 0;

const res = L.geoJSON(geoJsonFeatures,
{
pointToLayer: (feature, latlng) => {
// console.debug('pointToLayer:', feature, latlng, markerIcon);
return L.marker(latlng, { icon: this.markerIcon() });
// console.debug('pointToLayer:', feature, latlng);
const NAME = feature.properties.name;
count++;

return L.marker(latlng, {
alt: `Marker ${count}: ${NAME}`, // Accessibility !!
// title: `Marker ${count}: ${NAME}`,
icon: this._markerIcon()
});
},
onEachFeature: (feature, layer) => {
// does this feature have a property named popupContent?
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);

layer.on('popupopen', ev => this._accessibilityFixPopup(ev));
}
}
});
Expand All @@ -96,7 +109,7 @@ export class MyMapElement extends MyElement {
return res;
}

markerIcon () {
_markerIcon () {
const L = window.L;

return L.icon({
Expand All @@ -107,6 +120,27 @@ export class MyMapElement extends MyElement {
shadowUrl: 'https://unpkg.com/[email protected]/dist/images/marker-shadow.png'
});
}

_accessibilityFixes () {
const MARKERS = this.shadowRoot.querySelectorAll('.leaflet-marker-pane [ tabindex ]');

MARKERS.forEach(marker => { marker.title = marker.alt; });
}

_accessibilityFixPopup (ev) {
const CLOSE_BTN = ev.popup._closeButton; // ev.target._popup._closeButton;
// const CLOSE_BTN = this.shadowRoot.querySelector('.leaflet-popup-close-button');
const POPUP = ev.popup._wrapper.parentElement;

CLOSE_BTN.setAttribute('role', 'button');
CLOSE_BTN.setAttribute('aria-label', 'Close popup');
CLOSE_BTN.title = 'Close popup';
CLOSE_BTN.focus();

POPUP.setAttribute('role', 'dialog');

console.debug('Event:', ev);
}
}

MyMapElement.define();
Expand Down
8 changes: 7 additions & 1 deletion src/components/my-map.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@
stroke: var(--my-map-path-color, #3388ff);
stroke-width: var(--my-map-path-width, 3px);
}

/* Accessibility fixes */
:focus {
outline: 3px solid var(--my-map-focus-color, navy) !important;
outline-offset: 1px;
}
</style>

<div id="desc" class="map-desc"><slot> Description of the map. </slot></div>

<div id="my-map" role="region" aria-label="Map" aria-describedby="desc">
<div id="my-map" role="region" aria-label="Embedded Map" aria-describedby="desc">
<p class="my-loading">Loading map…</p>
</div>

Expand Down

0 comments on commit 8af4875

Please sign in to comment.