Repository navigation
a11y improvements for map #4285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0f30dc
a11y improvements
adamhaeger 72f38bf
minor cleanup
adamhaeger 6f149e3
Merge branch 'main' into fix/map-a11y-improvements
adamhaeger 3cee46b
changed from region to group, fixing wcag test
adamhaeger 692fa02
added ref to issue not solved
adamhaeger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { useEffect } from 'react'; | ||
| import { useMap } from 'react-leaflet'; | ||
|
|
||
| import { useLanguage } from 'src/features/language/useLanguage'; | ||
| import { useItemWhenType } from 'src/utils/layout/useNodeItem'; | ||
|
|
||
| interface MapRegionA11yProps { | ||
| baseComponentId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Sets accessibility attributes directly on the Leaflet container element. | ||
| * | ||
| * Must be rendered as a child of <MapContainer> so that `useMap()` resolves the Leaflet instance. | ||
| * We set the attributes on the real container (via `getContainer()`) rather than forwarding props, | ||
| * because MapContainer does not forward arbitrary DOM attributes. | ||
| * | ||
| * - `role="region"` keeps the screen reader in browse mode so map content (e.g. geometry tooltip | ||
| * labels) stays explorable, while still grouping the map under a single named landmark. | ||
| * - `aria-label` gives the region a localized accessible name. The component's own label (rendered | ||
| * separately by ComponentStructureWrapper) is prepended so it is announced first when the map | ||
| * gets focus, followed by the interaction instructions. This way the map is announced once | ||
| * instead of as a run-on concatenation of its inner controls. | ||
| */ | ||
| export function MapRegionA11y({ baseComponentId }: MapRegionA11yProps) { | ||
| const map = useMap(); | ||
| const { langAsString } = useLanguage(); | ||
| const { textResourceBindings } = useItemWhenType(baseComponentId, 'Map'); | ||
| const title = textResourceBindings?.title; | ||
| const label = title ? langAsString(title) : undefined; | ||
|
|
||
| const instructions = langAsString('map_component.ariaLabel'); | ||
| const ariaLabel = label ? `${label}. ${instructions}` : instructions; | ||
|
|
||
| useEffect(() => { | ||
| const container = map.getContainer(); | ||
| container.setAttribute('role', 'region'); | ||
| container.setAttribute('aria-label', ariaLabel); | ||
| }, [map, ariaLabel]); | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or 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,44 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { parseAndCleanText } from 'src/language/sharedLanguage'; | ||
| import classes from 'src/layout/Map/MapComponent.module.css'; | ||
| import { DefaultMapLayers } from 'src/layout/Map/utils'; | ||
| import { useExternalItem } from 'src/utils/layout/hooks'; | ||
|
|
||
| interface MapAttributionProps { | ||
| baseComponentId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Renders the map layer attribution as plain text/links OUTSIDE the Leaflet container. | ||
| * | ||
| * Leaflet's built-in AttributionControl renders the attribution inside the map container, which | ||
| * causes screen readers to read it as part of the map region and puts the attribution links in the | ||
| * tab order of the map. By rendering the attribution ourselves as a sibling of the map, the map | ||
| * region stays clean for assistive technology while the attribution remains visually present (as | ||
| * required by the OpenStreetMap/Kartverket licenses) with working links. | ||
| */ | ||
| export function MapAttribution({ baseComponentId }: MapAttributionProps) { | ||
|
Check warning on line 21 in src/layout/Map/features/attribution/MapAttribution.tsx
|
||
| const customLayers = useExternalItem(baseComponentId, 'Map').layers; | ||
| const layers = customLayers ?? DefaultMapLayers; | ||
|
|
||
| // Collect unique, non-empty attributions in layer order (mirrors how Leaflet dedupes them) | ||
| const attributions = Array.from( | ||
| new Set(layers.map((layer) => layer.attribution?.trim()).filter((a): a is string => !!a)), | ||
| ); | ||
|
|
||
| if (attributions.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <p className={classes.attribution}> | ||
| {attributions.map((attribution, i) => ( | ||
| <React.Fragment key={i}> | ||
|
Check warning on line 37 in src/layout/Map/features/attribution/MapAttribution.tsx
|
||
| {i > 0 && ' | '} | ||
| {parseAndCleanText(attribution)} | ||
| </React.Fragment> | ||
| ))} | ||
| </p> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.