-
-
Notifications
You must be signed in to change notification settings - Fork 375
fix: migrate live region to useAnnouncer #2268
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,6 +283,7 @@ | |
| } | ||
| onBeforeUnmount(() => { | ||
| updateUrlPage.cancel() | ||
| cancelPendingAnnouncements() | ||
| }) | ||
|
|
||
| // Update URL when page changes from scrolling | ||
|
|
@@ -584,94 +585,92 @@ | |
| }) | ||
|
|
||
| // ----------------------------------- | ||
| // Live region debouncing logic | ||
| // Live region announcements | ||
| // ----------------------------------- | ||
| const isMobile = useIsMobile() | ||
| const { polite } = useAnnouncer() | ||
|
|
||
| // Evaluate the text that should be announced to screen readers | ||
| const rawLiveRegionMessage = computed(() => { | ||
| if (isRateLimited.value) { | ||
| return $t('search.rate_limited') | ||
| } | ||
|
|
||
| // If status is pending, no update phrase needed yet | ||
| if (status.value === 'pending') { | ||
| return '' | ||
| } | ||
|
|
||
| if (visibleResults.value && displayResults.value.length > 0) { | ||
| if (viewMode.value === 'table' || paginationMode.value === 'paginated') { | ||
| const pSize = Math.min(preferredPageSize.value, effectiveTotal.value) | ||
|
|
||
| return $t( | ||
| 'filters.count.showing_paginated', | ||
| { | ||
| pageSize: pSize.toString(), | ||
| count: $n(effectiveTotal.value), | ||
| }, | ||
| effectiveTotal.value, | ||
| ) | ||
| } | ||
|
|
||
| if (isRelevanceSort.value) { | ||
| return $t( | ||
| 'search.found_packages', | ||
| { count: $n(visibleResults.value.total) }, | ||
| visibleResults.value.total, | ||
| ) | ||
| } | ||
| const announcePoliteDesktop = debounce((message: string) => { | ||
| polite(message) | ||
| }, 250) | ||
|
|
||
| return $t( | ||
| 'search.found_packages_sorted', | ||
| { count: $n(effectiveTotal.value) }, | ||
| effectiveTotal.value, | ||
| ) | ||
| } | ||
| const announcePoliteMobile = debounce((message: string) => { | ||
| polite(message) | ||
| }, 700) | ||
|
|
||
| if (status.value === 'success' || status.value === 'error') { | ||
| if (displayResults.value.length === 0 && query.value) { | ||
| return $t('search.no_results', { query: query.value }) | ||
| } | ||
| function announcePolite(message: string) { | ||
| if (isMobile.value) { | ||
| announcePoliteDesktop.cancel() | ||
| announcePoliteMobile(message) | ||
| return | ||
| } | ||
|
|
||
| return '' | ||
| }) | ||
|
|
||
| const debouncedLiveRegionMessage = ref('') | ||
|
|
||
| const updateLiveRegionMobile = debounce((val: string) => { | ||
| debouncedLiveRegionMessage.value = val | ||
| }, 700) | ||
| announcePoliteMobile.cancel() | ||
| announcePoliteDesktop(message) | ||
| } | ||
|
|
||
| const updateLiveRegionDesktop = debounce((val: string) => { | ||
| debouncedLiveRegionMessage.value = val | ||
| }, 250) | ||
| function cancelPendingAnnouncements() { | ||
| announcePoliteDesktop.cancel() | ||
| announcePoliteMobile.cancel() | ||
| } | ||
|
|
||
| // Announce search results changes to screen readers | ||
| watch( | ||
| rawLiveRegionMessage, | ||
| newVal => { | ||
| if (!newVal) { | ||
| updateLiveRegionMobile.cancel() | ||
| updateLiveRegionDesktop.cancel() | ||
| debouncedLiveRegionMessage.value = '' | ||
| () => ({ | ||
| rateLimited: isRateLimited.value, | ||
| searchStatus: status.value, | ||
| count: displayResults.value.length, | ||
| searchQuery: query.value, | ||
| mode: viewMode.value, | ||
| pagMode: paginationMode.value, | ||
| total: effectiveTotal.value, | ||
| }), | ||
| ({ rateLimited, searchStatus, count, searchQuery, mode, pagMode, total }) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename The static analysis tool correctly flags that 🛠️ Proposed fix- ({ rateLimited, searchStatus, count, searchQuery, mode, pagMode, total }) => {
+ ({ rateLimited, searchStatus, count, searchQuery: currentQuery, mode, pagMode, total }) => {
if (rateLimited) {
announcePolite($t('search.rate_limited'))
return
}
// ... rest of callback
} else if (searchStatus === 'success' || searchStatus === 'error') {
- if (searchQuery) {
- announcePolite($t('search.no_results', { query: searchQuery }))
+ if (currentQuery) {
+ announcePolite($t('search.no_results', { query: currentQuery }))
} else {
cancelPendingAnnouncements()
}
}🧰 Tools🪛 GitHub Check: 🤖 Autofix code[warning] 628-628: eslint(no-shadow) |
||
| if (rateLimited) { | ||
| announcePolite($t('search.rate_limited')) | ||
| return | ||
| } | ||
|
|
||
| if (isMobile.value) { | ||
| updateLiveRegionDesktop.cancel() | ||
| updateLiveRegionMobile(newVal) | ||
| } else { | ||
| updateLiveRegionMobile.cancel() | ||
| updateLiveRegionDesktop(newVal) | ||
| // Don't announce while searching | ||
| if (searchStatus === 'pending') { | ||
| cancelPendingAnnouncements() | ||
| return | ||
| } | ||
|
|
||
| if (count > 0) { | ||
| if (mode === 'table' || pagMode === 'paginated') { | ||
| const pSize = Math.min(preferredPageSize.value, total) | ||
|
|
||
| announcePolite( | ||
| $t( | ||
| 'filters.count.showing_paginated', | ||
| { | ||
| pageSize: pSize.toString(), | ||
| count: $n(total), | ||
| }, | ||
| total, | ||
| ), | ||
| ) | ||
| } else if (isRelevanceSort.value) { | ||
| announcePolite( | ||
| $t( | ||
| 'search.found_packages', | ||
| { count: $n(visibleResults.value?.total ?? 0) }, | ||
| visibleResults.value?.total ?? 0, | ||
| ), | ||
| ) | ||
| } else { | ||
| announcePolite($t('search.found_packages_sorted', { count: $n(total) }, total)) | ||
| } | ||
| } else if (searchStatus === 'success' || searchStatus === 'error') { | ||
| if (searchQuery) { | ||
| announcePolite($t('search.no_results', { query: searchQuery })) | ||
| } else { | ||
| cancelPendingAnnouncements() | ||
| } | ||
| } | ||
| }, | ||
| { immediate: true }, | ||
| ) | ||
|
|
||
| onBeforeUnmount(() => { | ||
| updateLiveRegionMobile.cancel() | ||
| updateLiveRegionDesktop.cancel() | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
|
|
@@ -910,10 +909,6 @@ | |
| :package-scope="packageScope" | ||
| :can-publish-to-scope="canPublishToScope" | ||
| /> | ||
|
|
||
| <div role="status" class="sr-only"> | ||
| {{ debouncedLiveRegionMessage }} | ||
| </div> | ||
| </main> | ||
| </template> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 125
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 1069
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 770
🏁 Script executed:
# Check the entire file structure to understand the component head -100 app/pages/search.vueRepository: npmx-dev/npmx.dev
Length of output: 3001
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 645
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 5801
Ensure screen reader announcements are triggered for repeated identical messages.
When
cancelPendingAnnouncements()cancels the debounces, subsequent calls toannouncePolite()with identical message text will not produce a fresh announcer update. Since multiple searches can generate the same announcement text (e.g., two searches each finding 100 results), the announcer state needs to be reset to trigger the screen reader update. Either reset the announcer or vary the message before reusing it.