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

feat: add support for priority #44

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions packages/live-region-element/src/live-region-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ type AnnounceOptions = {
* @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions
*/
politeness?: Politeness

/**
* The priority level for a message.
*/
priority?: 'immediate' | 'background'
}

type Message = {
contents: string
politeness: Politeness
scheduled: number
priority: AnnounceOptions['priority']
}

/**
Expand Down Expand Up @@ -94,10 +100,11 @@ class LiveRegionElement extends HTMLElement {
* level.
*/
public announce(message: string, options: AnnounceOptions = {}): Cancel {
const {delayMs, politeness = 'polite'} = options
const {delayMs, politeness = 'polite', priority = 'background'} = options
const now = Date.now()
const item: Message = {
politeness,
priority,
contents: message,
scheduled: delayMs !== undefined ? now + delayMs : now,
}
Expand Down Expand Up @@ -248,17 +255,31 @@ function getTemplate() {
}

function compareMessages(a: Message, b: Message): Order {
if (a.scheduled === b.scheduled) {
return Ordering.Equal
if (a.priority === b.priority) {
if (a.scheduled === b.scheduled) {
return Ordering.Equal
}

// Schedule a before b
if (a.scheduled < b.scheduled) {
return Ordering.Less
}

// Schedule a after b
return Ordering.Greater
}

// Schedule a before b
if (a.scheduled < b.scheduled) {
if (a.priority === 'immediate' && b.priority !== 'immediate') {
return Ordering.Less
}

// Schedule a after b
return Ordering.Greater
if (a.priority !== 'immediate' && b.priority === 'immediate') {
return Ordering.Greater
}

return Ordering.Equal
}

function noop() {}
Expand Down
Loading