Skip to content

A programming framework for agentic AI

[Accessibility] fix screen reader is not announcing 'Copied' information (#6059)

cheng-tanff847cccadc4
diff --git a/python/packages/autogen-core/docs/src/_static/custom.js b/python/packages/autogen-core/docs/src/_static/custom.js
index 9d2bf9e70..8c77eea71 100644
--- a/python/packages/autogen-core/docs/src/_static/custom.js
+++ b/python/packages/autogen-core/docs/src/_static/custom.js
@@ -1,16 +1,16 @@
-document.addEventListener('DOMContentLoaded', function() {
+document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.copybtn').forEach(button => {
- // Return focus to copy button after activation
- button.addEventListener('click', async function(event) {
- // Save the current focus
- const focusedElement = document.activeElement;
+ // Return focus to copy button after activation
+ button.addEventListener('click', async function (event) {
+ // Save the current focus
+ const focusedElement = document.activeElement;
- // Perform the copy action
- await copyToClipboard(this);
+ // Perform the copy action
+ await copyToClipboard(this);
- // Restore the focus
- focusedElement.focus();
- });
+ // Restore the focus
+ focusedElement.focus();
+ });
});
document.querySelectorAll('.search-button-field').forEach(button => {
@@ -69,8 +69,30 @@ async function copyToClipboard(button) {
const targetSelector = button.getAttribute('data-clipboard-target');
const codeBlock = document.querySelector(targetSelector);
try {
- await navigator.clipboard.writeText(codeBlock.textContent);
+ await navigator.clipboard.writeText(codeBlock.textContent);
+
+ // Add a visually hidden element for screen readers to announce
+ const srAnnouncement = document.createElement('div');
+ srAnnouncement.textContent = 'Copied to clipboard';
+ srAnnouncement.setAttribute('role', 'status');
+ srAnnouncement.setAttribute('aria-live', 'polite');
+ srAnnouncement.style.position = 'absolute';
+ srAnnouncement.style.width = '1px';
+ srAnnouncement.style.height = '1px';
+ srAnnouncement.style.padding = '0';
+ srAnnouncement.style.margin = '-1px';
+ srAnnouncement.style.overflow = 'hidden';
+ srAnnouncement.style.clipPath = 'inset(50%)';
+ srAnnouncement.style.whiteSpace = 'nowrap';
+ srAnnouncement.style.border = '0';
+
+ document.body.appendChild(srAnnouncement);
+
+ // Remove the announcement element after it's been read
+ setTimeout(() => {
+ document.body.removeChild(srAnnouncement);
+ }, 3000);
} catch (err) {
- console.error('Failed to copy text: ', err);
+ console.error('Failed to copy text: ', err);
}
-}
\ No newline at end of file
+}