Skip to content

Commit

Permalink
refactor: live-region code
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Apr 24, 2022
1 parent 6705af8 commit ef1c1f6
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions packages/utilities/dom/src/live-region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@ import { setVisuallyHidden } from "./visually-hidden"

export type LiveRegionOptions = {
level: "polite" | "assertive"
doc?: Document
document?: Document
root?: HTMLElement | null
delay?: number
}

export type LiveRegion = ReturnType<typeof createLiveRegion>

export function createLiveRegion(opts: Partial<LiveRegionOptions> = {}) {
const { level = "polite", doc: ownerDocument, root, delay: rootDelay = 0 } = opts
const { level = "polite", document: doc = document, root, delay: _delay = 0 } = opts

const doc = ownerDocument ?? document
const win = doc.defaultView ?? window

const parent = root ?? doc.body

function announce(msg: string, delay?: number) {
function announce(message: string, delay?: number) {
const oldRegion = doc.getElementById("__live-region__")

// remove old region
if (!!oldRegion) {
parent.removeChild(oldRegion)
}
oldRegion?.remove()

// Did an override level get set?
delay = delay ?? rootDelay
delay = delay ?? _delay

// create fresh region
const region = doc.createElement("span")
region.id = "__live-region__"
region.dataset.liveAnnouncer = "true"

// Determine redundant role
var role = level !== "assertive" ? "status" : "alert"
const role = level !== "assertive" ? "status" : "alert"

// add role and attributes
region.setAttribute("aria-live", level)
Expand All @@ -46,15 +43,13 @@ export function createLiveRegion(opts: Partial<LiveRegionOptions> = {}) {

// populate region to trigger it
win.setTimeout(() => {
region!.textContent = msg
region.textContent = message
}, delay)
}

function destroy() {
const oldRegion = doc.getElementById("__live-region__")
if (oldRegion) {
parent.removeChild(oldRegion)
}
oldRegion?.remove()
}

return { announce, destroy }
Expand Down

0 comments on commit ef1c1f6

Please sign in to comment.