Skip to content
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

Prototype aria-live for interactive graphs #1960

Draft
wants to merge 1 commit into
base: LEMS-2622-update-point-copy
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type PolitenessLevel = "assertive" | "polite";

export function ariaLiveAnnounce(message: string, options?: {level?: PolitenessLevel}): void {
const region = recreateAriaLiveRegion(options?.level ?? "polite")
region.innerText = message
}

let currentAriaLiveRegion: HTMLDivElement | null = null
function recreateAriaLiveRegion(politenessLevel: PolitenessLevel): HTMLDivElement {
if (currentAriaLiveRegion) {
document.body.removeChild(currentAriaLiveRegion)
}

currentAriaLiveRegion = createAriaLiveRegion(politenessLevel)
return currentAriaLiveRegion
}

function createAriaLiveRegion(politenessLevel: PolitenessLevel): HTMLDivElement {
console.log("createAriaLiveRegion")
const newRegion = document.createElement("div")
newRegion.setAttribute("aria-live", politenessLevel)
newRegion.classList.add("perseus-aria-live-region")
return document.body.appendChild(newRegion)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Dispatch,
InteractiveGraphElementSuite,
} from "../types";
import {ariaLiveAnnounce} from "../ariaLiveAnnounce";

export function renderPointGraph(
state: PointGraphState,
Expand All @@ -42,7 +43,14 @@ function PointGraph(props: PointGraphProps) {
}

function LimitedPointGraph(props: PointGraphProps) {
const {dispatch} = props;
const {dispatch, graphState} = props;
const {announcement} = graphState;

React.useEffect(() => {
if (announcement) {
ariaLiveAnnounce(announcement.text, {level: "assertive"})
}
}, [announcement])

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import type {
PairOfPoints,
} from "../types";
import type {Interval} from "mafs";
import {ariaLiveAnnounce} from "../ariaLiveAnnounce";

const minDistanceBetweenAngleVertexAndSidePoint = 2;

Expand Down Expand Up @@ -507,6 +508,7 @@ function doMovePoint(
index: action.index,
newValue: boundAndSnapToGrid(action.destination, state),
}),
announcement: {text: "updated"}
};
}
case "sinusoid": {
Expand Down
1 change: 1 addition & 0 deletions packages/perseus/src/widgets/interactive-graphs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface InteractiveGraphStateCommon {
range: [xRange: Interval, yRange: Interval];
// snapStep = [xStep, yStep] in Cartesian units
snapStep: vec.Vector2;
announcement?: {text: string};
}

export interface SegmentGraphState extends InteractiveGraphStateCommon {
Expand Down
Loading