This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,38 @@
export default function Dropdown( $dropdown ) {
const $dropdownOpen = $dropdown.querySelector(
'[data-vrts-dropdown-open]'
);
const toggleContent = ( e ) => {
const $el = e.currentTarget;
const controls = $el.getAttribute( 'aria-controls' );
const $controls = document.getElementById( controls );
const isExpanded = $el.getAttribute( 'aria-expanded' ) === 'true';
$el.setAttribute( 'aria-expanded', ! isExpanded );
$controls.setAttribute( 'aria-hidden', isExpanded );
};
const closeDropdown = ( e ) => {
if (
$dropdown &&
$dropdown !== e.target &&
! $dropdown.contains( e.target )
) {
$dropdownOpen.setAttribute( 'aria-expanded', false );
document
.getElementById( $dropdownOpen.getAttribute( 'aria-controls' ) )
.setAttribute( 'aria-hidden', true );
}
};
$dropdownOpen?.addEventListener( 'click', toggleContent );
// Close dropdown when clicking outside of it.
document.addEventListener( 'click', closeDropdown );
return () => {
$dropdownOpen?.removeEventListener( 'click', toggleContent );
document.removeEventListener( 'click', closeDropdown );
};
}