Skip to content

Commit 90b80a5

Browse files
committed
feat: dynamic aria label for the geomap
1 parent 1107a4e commit 90b80a5

7 files changed

Lines changed: 105 additions & 28 deletions

File tree

vis/js/components/Heading.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
StandardTitle,
1212
} from "../templates/headingtitles";
1313
import { queryConcatenator } from "../utils/data";
14+
import { getHeadingLabel } from "../utils/getHeadingLabel";
1415

1516
const Heading = ({
1617
localization,
@@ -179,26 +180,6 @@ const sliceText = (text, maxLength) => {
179180
return `${text.slice(0, maxLength - 3)}...`;
180181
};
181182

182-
/**
183-
* Refactored helper function for getting the heading label.
184-
*/
185-
export const getHeadingLabel = (labelType, localization) => {
186-
switch (labelType) {
187-
case "authorview-streamgraph":
188-
return localization.streamgraph_authors_label;
189-
case "authorview-knowledgemap":
190-
return localization.overview_authors_label;
191-
case "keywordview-streamgraph":
192-
return localization.streamgraph_label;
193-
case "keywordview-knowledgemap":
194-
return localization.overview_label;
195-
case "geomap":
196-
return localization.geomap_label;
197-
default:
198-
throw new Error(`Label of type '${labelType}' not supported.`);
199-
}
200-
};
201-
202183
const unescapeHTML = (string) => {
203184
const entityMap = {
204185
"&": "&",

vis/js/templates/Geomap/HeightContainer/config.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { queryConcatenator } from "@/js/utils/data";
2+
import { getHeadingLabel } from "@/js/utils/getHeadingLabel";
3+
import { unescapeHTML } from "@/js/utils/unescapeHTMLentities";
4+
5+
const MAX_LENGTH_CUSTOM = 100;
6+
7+
const sliceText = (text: string, maxLength: number) => {
8+
if (text.length <= maxLength) {
9+
return text;
10+
}
11+
12+
return `${text.slice(0, maxLength - 3)}...`;
13+
};
14+
15+
type Params = {
16+
titleLabelType: Parameters<typeof getHeadingLabel>[0];
17+
localization: Parameters<typeof getHeadingLabel>[1];
18+
query: string;
19+
advancedQuery: string | null;
20+
customTitle: unknown;
21+
titleStyle: string | null;
22+
};
23+
24+
export const getAriaLabel = ({
25+
titleLabelType,
26+
localization,
27+
query,
28+
advancedQuery,
29+
customTitle,
30+
titleStyle,
31+
}: Params) => {
32+
const headingLabel = getHeadingLabel(titleLabelType, localization);
33+
const queryString = queryConcatenator([query, advancedQuery]);
34+
35+
const effectiveTitle =
36+
titleStyle === "custom" &&
37+
typeof customTitle !== "undefined" &&
38+
customTitle !== null
39+
? unescapeHTML(sliceText(String(customTitle), MAX_LENGTH_CUSTOM))
40+
: queryString;
41+
42+
return effectiveTitle ? `${headingLabel} ${effectiveTitle}` : headingLabel;
43+
};

vis/js/templates/Geomap/HeightContainer/index.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
import { FC, PropsWithChildren } from "react";
22
import { useSelector } from "react-redux";
33

4-
import { getMapHeight } from "./selectors";
5-
import { ACCESSABILITY_ATTRIBUTES } from "./config";
4+
import { getAriaLabel } from "./getAriaLabel";
5+
import {
6+
getCustomTitle,
7+
getLocalization,
8+
getMapHeight,
9+
getQuery,
10+
getQueryAdvanced,
11+
getTitleLabelType,
12+
getTitleStyle,
13+
} from "./selectors";
614

715
export const HeightContainer: FC<PropsWithChildren> = ({ children }) => {
816
const height = useSelector(getMapHeight);
17+
const localization = useSelector(getLocalization);
18+
const titleLabelType = useSelector(getTitleLabelType);
19+
const query = useSelector(getQuery);
20+
const advancedQuery = useSelector(getQueryAdvanced);
21+
const customTitle = useSelector(getCustomTitle);
22+
const titleStyle = useSelector(getTitleStyle);
23+
24+
const ariaLabel = getAriaLabel({
25+
titleLabelType,
26+
localization,
27+
query,
28+
advancedQuery,
29+
customTitle,
30+
titleStyle,
31+
});
932

1033
return (
11-
<div style={{ height }} {...ACCESSABILITY_ATTRIBUTES}>
34+
<div role="region" aria-label={ariaLabel} style={{ height }}>
1235
{children}
1336
</div>
1437
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import { State } from "@/js/types";
22

33
export const getMapHeight = (state: State) => state.chart.height;
4+
export const getLocalization = (state: State) => state.localization;
5+
export const getTitleLabelType = (state: State) => state.heading.titleLabelType;
6+
export const getQuery = (state: State) => state.query.text;
7+
export const getQueryAdvanced = (state: State) => state.q_advanced.text;
8+
export const getCustomTitle = (state: State) => state.heading.customTitle;
9+
export const getTitleStyle = (state: State) => state.heading.titleStyle;

vis/js/templates/Geomap/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { MapContainer } from "react-leaflet";
55

66
import { CONFIG } from "./config";
77
import { HeightContainer } from "./HeightContainer";
8+
import { KeyboardZoomController } from "./KeyboardZoomController";
89
import { Layer } from "./Layer";
910
import { Pins } from "./Pins";
1011
import { SelectionResetHandler } from "./SelectionResetHandler";
1112
import { ZoomControls } from "./ZoomControls";
12-
import { KeyboardZoomController } from "./KeyboardZoomController";
1313

1414
export const Geomap: FC = () => (
1515
<HeightContainer>

vis/js/utils/getHeadingLabel.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Localization } from "../i18n/localization";
2+
3+
type TitleLabelType =
4+
| "authorview-streamgraph"
5+
| "authorview-knowledgemap"
6+
| "keywordview-streamgraph"
7+
| "keywordview-knowledgemap"
8+
| "geomap";
9+
10+
export const getHeadingLabel = (
11+
labelType: TitleLabelType,
12+
localization: Localization,
13+
) => {
14+
switch (labelType) {
15+
case "authorview-streamgraph":
16+
return localization.streamgraph_authors_label;
17+
case "authorview-knowledgemap":
18+
return localization.overview_authors_label;
19+
case "keywordview-streamgraph":
20+
return localization.streamgraph_label;
21+
case "keywordview-knowledgemap":
22+
return localization.overview_label;
23+
case "geomap":
24+
return localization.geomap_label;
25+
default:
26+
throw new Error(`Label of type '${labelType}' not supported.`);
27+
}
28+
};

0 commit comments

Comments
 (0)