mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-12 21:28:18 +00:00
122 lines
3.1 KiB
Plaintext
122 lines
3.1 KiB
Plaintext
{% macro dropdown(
|
|
id=None,
|
|
trigger=None,
|
|
main_attrs=None,
|
|
trigger_attrs=None,
|
|
menu_attrs=None
|
|
)
|
|
%}
|
|
{% if registerComponent('dropdown') %}
|
|
<script>
|
|
document.addEventListener('alpine:init', () => {
|
|
Alpine.data('dropdown', () => ({
|
|
open: false,
|
|
focusedIndex: -1,
|
|
menuItems: [],
|
|
|
|
init() {
|
|
this.$nextTick(() => {
|
|
const menuElement = this.$el.querySelector('[role="menu"]');
|
|
this.menuItems = Array.from(menuElement.querySelectorAll('[role=menuitem]'));
|
|
});
|
|
},
|
|
|
|
$trigger: {
|
|
'@click'() { this.handleClick() },
|
|
'@keydown.down.prevent'() { this.open && this.nextItem() },
|
|
':aria-expanded'() { return this.open },
|
|
':aria-pressed'() { return this.open }
|
|
},
|
|
|
|
$menu: {
|
|
'@click'(e) { this.handleMenuItemClick(e) },
|
|
'@keydown.escape.window'() { this.handleEscape() },
|
|
'@keydown.down.prevent'() { this.nextItem() },
|
|
'@keydown.up.prevent'() { this.prevItem() },
|
|
'@keydown.home.prevent'() { this.focusItem(0) },
|
|
'@keydown.end.prevent'() { this.focusItem(this.menuItems.length - 1) },
|
|
':aria-hidden'() { return !this.open },
|
|
'x-cloak': ''
|
|
},
|
|
|
|
focusItem(index) {
|
|
if (index >= 0 && index < this.menuItems.length) {
|
|
this.focusedIndex = index;
|
|
this.menuItems[index].focus();
|
|
}
|
|
},
|
|
|
|
nextItem() {
|
|
this.focusItem((this.focusedIndex + 1) % this.menuItems.length);
|
|
},
|
|
|
|
prevItem() {
|
|
this.focusItem(this.focusedIndex <= 0 ? this.menuItems.length - 1 : this.focusedIndex - 1);
|
|
},
|
|
|
|
handleClick() {
|
|
this.open = !this.open;
|
|
this.$nextTick(() => {
|
|
if(this.open && this.menuItems.length) this.focusItem(0);
|
|
});
|
|
},
|
|
|
|
handleEscape() {
|
|
if (this.open) this.open = false;
|
|
},
|
|
|
|
handleMenuItemClick(event) {
|
|
const menuItem = event.target.closest('[role=menuitem]');
|
|
if (menuItem && !menuItem.disabled) {
|
|
menuItem.blur();
|
|
this.open = false;
|
|
}
|
|
},
|
|
}));
|
|
});
|
|
</script>
|
|
{% endif %}
|
|
|
|
<div
|
|
class="dropdown {% if main_attrs %}{{ main_attrs.pop('class', '') }}{% endif %}"
|
|
x-data="dropdown"
|
|
@click.away="open = false"
|
|
{% if id %}id="{{ id }}"{% endif %}
|
|
{% for key, value in main_attrs %}
|
|
{{ key }}="{{ value }}"
|
|
{% endfor %}
|
|
>
|
|
{% if trigger %}
|
|
<button
|
|
type="button"
|
|
aria-haspopup="menu"
|
|
aria-expanded="false"
|
|
aria-pressed="false"
|
|
x-bind="$trigger"
|
|
{% if id %}
|
|
id="{{ id }}-trigger"
|
|
aria-controls="{{ id }}-menu"
|
|
{% endif %}
|
|
{% for key, value in trigger_attrs %}
|
|
{{ key }}="{{ value }}"
|
|
{% endfor %}
|
|
>
|
|
{{ trigger | safe }}
|
|
</button>
|
|
{% endif %}
|
|
<nav
|
|
role="menu"
|
|
aria-orientation="vertical"
|
|
aria-hidden="true"
|
|
x-bind="$menu"
|
|
{% if id %}id="{{ id }}-menu"{% endif %}
|
|
{% for key, value in menu_attrs %}
|
|
{{ key }}="{{ value }}"
|
|
{% endfor %}
|
|
>
|
|
{{ caller() }}
|
|
</nav>
|
|
</div>
|
|
{% endmacro %}
|
|
|