An accessible Astro dropdown menu is a <button> with aria-expanded and aria-controls that shows and hides a list of links. Write it as an .astro component with a small <script> that toggles the state, closes on Escape and outside clicks, and returns focus to the button. No framework or client:* directive is needed.
Key Takeaways
- For site navigation, use the disclosure pattern (a button that reveals links), not an ARIA
menu.aria-expandedon the button tells screen readers whether the list is open.- Close on Escape, on outside click and when focus leaves the dropdown.
- Don’t open on hover only; touch and keyboard users need the button.
The component
---
// src/components/Dropdown.astro
interface Props {
label: string;
items: { href: string; text: string }[];
}
const { label, items } = Astro.props;
const id = `dd-${label.toLowerCase().replace(/\W+/g, '-')}`;
---
<div class="dropdown" data-dropdown>
<button type="button" aria-expanded="false" aria-controls={id}>
{label}
<svg aria-hidden="true" width="12" height="12" viewBox="0 0 12 12"><path d="M2 4l4 4 4-4" fill="none" stroke="currentColor" stroke-width="1.5" /></svg>
</button>
<ul id={id} hidden>
{items.map((item) => <li><a href={item.href}>{item.text}</a></li>)}
</ul>
</div>
<script>
function setup(root: HTMLElement) {
const button = root.querySelector('button')!;
const list = root.querySelector('ul')!;
const setOpen = (open: boolean) => {
button.setAttribute('aria-expanded', String(open));
list.hidden = !open;
};
button.addEventListener('click', () => setOpen(list.hidden));
root.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !list.hidden) {
setOpen(false);
button.focus();
}
});
root.addEventListener('focusout', (event) => {
if (!root.contains(event.relatedTarget as Node)) setOpen(false);
});
document.addEventListener('click', (event) => {
if (!root.contains(event.target as Node)) setOpen(false);
});
}
document.querySelectorAll<HTMLElement>('[data-dropdown]').forEach(setup);
</script>
<style>
.dropdown { position: relative; }
button { display: inline-flex; align-items: center; gap: 0.35rem; }
button[aria-expanded='true'] svg { transform: rotate(180deg); }
ul {
position: absolute;
top: calc(100% + 0.25rem);
left: 0;
min-width: 12rem;
margin: 0;
padding: 0.5rem;
list-style: none;
background: var(--color-bg, #fff);
border: 1px solid var(--color-border, #e5e7eb);
border-radius: 0.5rem;
box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);
z-index: 50;
}
a { display: block; padding: 0.5rem 0.75rem; border-radius: 0.375rem; }
a:hover, a:focus-visible { background: var(--color-surface-hover, #f3f4f6); }
</style>
Use it in a header:
<Dropdown
label="Resources"
items={[
{ href: '/blog/', text: 'Blog' },
{ href: '/docs/', text: 'Documentation' },
{ href: '/tips-and-tricks/', text: 'Tips & Tricks' },
]}
/>
Astro bundles the script once per page, and querySelectorAll sets up every dropdown on it. The script tag guide explains that behavior.
Why a disclosure and not role=“menu”
ARIA’s menu role is meant for application menus, like a toolbar’s File menu, and it requires arrow-key navigation and roving focus. Screen reader users expect website navigation to be a list of links they Tab through. The disclosure pattern used here matches that expectation and needs far less code. The ARIA Authoring Practices disclosure navigation example is the reference.
The no-JavaScript fallback
With hidden in the markup, links are invisible if the script fails. Two options:
- Remove
hiddenfrom the HTML and add it in the script (list.hidden = trueat setup). Without JavaScript the links simply show as a list. - Use
<details>and<summary>, which toggle natively. It works without any script; add the Escape and outside-click script as an enhancement. The trade-off is thatsummaryisn’t announced as a button in every screen reader the same way.
For a popover-style menu, the native popover attribute with popovertarget on the button also gives you light dismiss and Escape handling for free in current browsers.
With view transitions
If you use <ClientRouter />, page scripts don’t re-run after navigation. Wrap the setup call in document.addEventListener('astro:page-load', () => { ... }) so new pages get working dropdowns. See our view transitions guide. On small screens, most sites swap dropdowns for a mobile nav component. The component drops into the header of any Astro theme that uses plain .astro components.
FAQ
How do I make a dropdown menu in Astro without React?
Write an .astro component with a button and a list, and a <script> that toggles hidden and aria-expanded. Astro bundles the script automatically, so you don’t need a framework or a client:* directive.
Should a navigation dropdown open on hover?
Hover can be an extra, but the button click must work on its own, because touch screens and keyboard users can’t hover. Hover-only menus also tend to close when the pointer crosses a gap.
What ARIA attributes does a dropdown menu need?
For navigation, aria-expanded on the toggle button and aria-controls pointing at the list’s id. Keep the items as normal links in a list, and avoid role="menu" unless you implement full arrow-key menu behavior.