{"id":10376,"date":"2025-10-16T11:32:34","date_gmt":"2025-10-16T11:32:33","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10376"},"modified":"2025-10-16T11:32:34","modified_gmt":"2025-10-16T11:32:33","slug":"building-accessible-components-with-vanilla-js","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/building-accessible-components-with-vanilla-js\/","title":{"rendered":"Building Accessible Components with Vanilla JS"},"content":{"rendered":"<h1>Building Accessible Components with Vanilla JavaScript<\/h1>\n<p>In today&#8217;s digital landscape, web accessibility is not just a necessity; it&#8217;s a fundamental principle that guides design and development. Building components that everyone can use, regardless of their abilities, includes using Semantic HTML and ensuring that your site can be navigated effectively with assistive technologies. This article will walk you through how to create accessible UI components using vanilla JavaScript, focusing on enhancing user experience for all.<\/p>\n<h2>Understanding Accessibility<\/h2>\n<p>Web accessibility refers to the practice of making websites usable for people of all abilities and disabilities. According to the World Health Organization, over a billion people live with some form of disability. If your web application is not accessible, you are alienating a significant portion of your audience.<\/p>\n<p>Accessibility standards such as the Web Content Accessibility Guidelines (WCAG) outline how to create user-friendly components. These guidelines categorize accessible practices into four principles: Perceivable, Operable, Understandable, and Robust (POUR).<\/p>\n<h2>Setting the Stage: Semantic HTML<\/h2>\n<p>Before diving into JavaScript, it&#8217;s essential to understand the role of semantic HTML. Using the right HTML elements improves accessibility significantly. For example, instead of using <strong>div<\/strong> elements for buttons or links, utilize <strong>button<\/strong>, <strong>anchor<\/strong>, and other semantic elements. This approach helps screen readers interpret your content correctly.<\/p>\n<h3>Example of Semantic HTML<\/h3>\n<pre><code>&lt;button type=\"button\" aria-label=\"Close\" onclick=\"closeModal()\"&gt;\n  Close\n&lt;\/button&gt;\n\n&lt;a href=\"https:\/\/example.com\" target=\"_blank\" rel=\"noopener noreferrer\"&gt;\n  Visit Example\n&lt;\/a&gt;<\/code><\/pre>\n<h2>Creating Accessible Components with Vanilla JavaScript<\/h2>\n<p>Now, let&#8217;s build a few essential components using vanilla JavaScript. We will cover a modal dialog, a dropdown menu, and a simple tooltip, ensuring each is accessible out-of-the-box.<\/p>\n<h3>1. Accessible Modal Dialog<\/h3>\n<p>A modal dialog is a component that appears on top of an application\u2019s main content, often used to ask for user confirmation or display important information. Here&#8217;s how you can create an accessible modal:<\/p>\n<pre><code>&lt;button id=\"openModal\"&gt;Open Modal&lt;\/button&gt;\n\n&lt;div id=\"modal\" role=\"dialog\" aria-labelledby=\"modalTitle\" aria-modal=\"true\" style=\"display: none;\"&gt;\n  &lt;h2 id=\"modalTitle\"&gt;Modal Title&lt;\/h2&gt;\n  &lt;p&gt;This is a modal dialog that requires user interaction.&lt;\/p&gt;\n  &lt;button id=\"closeModal\"&gt;Close&lt;\/button&gt;\n&lt;\/div&gt;\n\n\n  const modal = document.getElementById('modal');\n  const openModalButton = document.getElementById('openModal');\n  const closeModalButton = document.getElementById('closeModal');\n\n  openModalButton.onclick = function() {\n    modal.style.display = 'block';\n    modal.focus();\n    document.addEventListener('keydown', handleKeydown);\n  }\n\n  closeModalButton.onclick = function() {\n    closeModal();\n  }\n\n  function closeModal() {\n    modal.style.display = 'none';\n    document.removeEventListener('keydown', handleKeydown);\n  }\n\n  function handleKeydown(event) {\n    if (event.key === 'Escape') {\n      closeModal();\n    }\n  }\n<\/code><\/pre>\n<p>In this modal example:<\/p>\n<ul>\n<li>We use <strong>role=&#8221;dialog&#8221;<\/strong> to define it as a dialog.<\/li>\n<li>The <strong>aria-labelledby<\/strong> attribute maps the title of the modal for screen readers.<\/li>\n<li>We listen for the &#8220;Escape&#8221; key to close the modal for enhanced usability.<\/li>\n<\/ul>\n<h3>2. Accessible Dropdown Menu<\/h3>\n<p>Dropdown menus can pose significant challenges for accessibility. Here\u2019s one way to implement an accessible dropdown menu:<\/p>\n<pre><code>&lt;div class=\"dropdown\"&gt;\n  &lt;button id=\"dropdownButton\" aria-haspopup=\"true\" aria-expanded=\"false\"&gt;Select an option&lt;\/button&gt;\n  &lt;ul id=\"dropdownMenu\" role=\"menu\" style=\"display: none;\"&gt;\n    &lt;li role=\"menuitem\"&gt;&lt;a href=\"#item1\"&gt;Item 1&lt;\/a&gt;&lt;\/li&gt;\n    &lt;li role=\"menuitem\"&gt;&lt;a href=\"#item2\"&gt;Item 2&lt;\/a&gt;&lt;\/li&gt;\n    &lt;li role=\"menuitem\"&gt;&lt;a href=\"#item3\"&gt;Item 3&lt;\/a&gt;&lt;\/li&gt;\n  &lt;\/ul&gt;\n&lt;\/div&gt;\n\n\n  const dropdownButton = document.getElementById('dropdownButton');\n  const dropdownMenu = document.getElementById('dropdownMenu');\n\n  dropdownButton.onclick = function() {\n    const expanded = dropdownButton.getAttribute('aria-expanded') === 'true' || false;\n    dropdownButton.setAttribute('aria-expanded', !expanded);\n    dropdownMenu.style.display = expanded ? 'none' : 'block';\n  }\n<\/code><\/pre>\n<p>This implementation features:<\/p>\n<ul>\n<li>Usage of <strong>aria-haspopup<\/strong> to signal that the button controls a dropdown.<\/li>\n<li><strong>aria-expanded<\/strong> gives information about the current state of the dropdown.<\/li>\n<li>A clear role for items within the menu defined using <strong>role=&#8221;menuitem&#8221;<\/strong>.<\/li>\n<\/ul>\n<h3>3. Accessible Tooltips<\/h3>\n<p>Tooltips can enhance user experience by providing additional context. Here&#8217;s a basic implementation:<\/p>\n<pre><code>&lt;button id=\"tooltipButton\" aria-describedby=\"tooltip\"&gt;Hover me&lt;\/button&gt;\n&lt;div id=\"tooltip\" role=\"tooltip\" style=\"display: none;\"&gt;This is a tooltip message.&lt;\/div&gt;\n\n\n  const tooltipButton = document.getElementById('tooltipButton');\n  const tooltip = document.getElementById('tooltip');\n\n  tooltipButton.onmouseenter = function() {\n    tooltip.style.display = 'block';\n  }\n\n  tooltipButton.onmouseleave = function() {\n    tooltip.style.display = 'none';\n  }\n<\/code><\/pre>\n<p>In this tooltip demonstration:<\/p>\n<ul>\n<li><strong>aria-describedby<\/strong> associates the button with its tooltip for screen readers.<\/li>\n<li>It\u2019s essential to manage display styles to ensure tooltips are visible during hover interactions.<\/li>\n<\/ul>\n<h2>Testing for Accessibility<\/h2>\n<p>Creating accessible components is essential, but testing is equally important. Here are some effective methods you can use:<\/p>\n<ul>\n<li><strong>Use Screen Readers:<\/strong> Tools like NVDA, JAWS, or VoiceOver will help you understand the experience of users with visual impairments.<\/li>\n<li><strong>Color Contrast Checkers:<\/strong> Online tools can help ensure your text is readable against its background.<\/li>\n<li><strong>Automated Testing Tools:<\/strong> Tools like Axe and Lighthouse can identify accessibility issues in your application.<\/li>\n<li><strong>User Testing:<\/strong> Including users with disabilities in your testing phase will provide invaluable feedback on your components.<\/li>\n<\/ul>\n<h2>Final Thoughts<\/h2>\n<p>Building accessible components with vanilla JavaScript requires careful attention to semantic HTML, proper ARIA practices, and a user-first mindset. As web developers, it&#8217;s our responsibility to ensure that our applications can be used by everyone, regardless of their abilities.<\/p>\n<p>By implementing the accessible components and testing practices detailed in this article, you not only create a better experience for all users but also expand your reach and adherence to legal requirements.<\/p>\n<p>Remember, accessibility is an ongoing process; keep learning and adapting your components as best practices evolve and improve. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building Accessible Components with Vanilla JavaScript In today&#8217;s digital landscape, web accessibility is not just a necessity; it&#8217;s a fundamental principle that guides design and development. Building components that everyone can use, regardless of their abilities, includes using Semantic HTML and ensuring that your site can be navigated effectively with assistive technologies. This article will<\/p>\n","protected":false},"author":178,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[265],"tags":[330],"class_list":{"0":"post-10376","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-front-end-development","7":"tag-javascript"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/178"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10376"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10376\/revisions"}],"predecessor-version":[{"id":10377,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10376\/revisions\/10377"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}