Skip to content

Commit

Permalink
update prototype to move announcement out of reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
anakaren-rojas committed Dec 10, 2024
1 parent c4bbbcb commit c9c5c6c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type PolitenessLevel = "assertive" | "polite";
import { AriaLivePolitenessLevel } from "./types"

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

let currentAriaLiveRegion: HTMLDivElement | null = null
function recreateAriaLiveRegion(politenessLevel: PolitenessLevel): HTMLDivElement {
function recreateAriaLiveRegion(politenessLevel: AriaLivePolitenessLevel): HTMLDivElement {
if (currentAriaLiveRegion) {
document.body.removeChild(currentAriaLiveRegion)
}
Expand All @@ -15,8 +15,7 @@ function recreateAriaLiveRegion(politenessLevel: PolitenessLevel): HTMLDivElemen
return currentAriaLiveRegion
}

function createAriaLiveRegion(politenessLevel: PolitenessLevel): HTMLDivElement {
console.log("createAriaLiveRegion")
function createAriaLiveRegion(politenessLevel: AriaLivePolitenessLevel): HTMLDivElement {
const newRegion = document.createElement("div")
newRegion.setAttribute("aria-live", politenessLevel)
newRegion.classList.add("perseus-aria-live-region")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import * as React from "react";
import {useControlPoint} from "./use-control-point";

import type {CSSCursor} from "./css-cursor";
import type {AriaLive} from "../../types";
import type {AriaLivePolitenessLevel} from "../../types";
import type {KeyboardMovementConstraint} from "../use-draggable";
import type {vec} from "mafs";

type Props = {
point: vec.Vector2;
ariaDescribedBy?: string;
ariaLabel?: string;
ariaLive?: AriaLive;
ariaLive?: AriaLivePolitenessLevel;
color?: string;
constrain?: KeyboardMovementConstraint;
cursor?: CSSCursor | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {useDraggable} from "../use-draggable";
import {MovablePointView} from "./movable-point-view";

import type {CSSCursor} from "./css-cursor";
import type {AriaLive} from "../../types";
import type {AriaLivePolitenessLevel} from "../../types";
import type {KeyboardMovementConstraint} from "../use-draggable";
import type {vec} from "mafs";

type Params = {
point: vec.Vector2;
ariaDescribedBy?: string;
ariaLabel?: string;
ariaLive?: AriaLive;
ariaLive?: AriaLivePolitenessLevel;
color?: string | undefined;
constrain?: KeyboardMovementConstraint;
cursor?: CSSCursor | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ function LimitedPointGraph(props: PointGraphProps) {
const {dispatch, graphState} = props;
const {announcement} = graphState;

// Announcement would get updated here

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

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

export interface SegmentGraphState extends InteractiveGraphStateCommon {
Expand Down Expand Up @@ -137,4 +137,4 @@ export type GraphDimensions = {
height: number; // pixels
};

export type AriaLive = "off" | "assertive" | "polite" | undefined;
export type AriaLivePolitenessLevel = "off" | "assertive" | "polite";

0 comments on commit c9c5c6c

Please sign in to comment.