mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-15 14:38:27 +00:00
88 lines
2.0 KiB
Plaintext
88 lines
2.0 KiB
Plaintext
{% macro popover(
|
|
id=None,
|
|
trigger=None,
|
|
main_attrs=None,
|
|
trigger_attrs=None,
|
|
content_attrs=None,
|
|
include_html=true,
|
|
include_script=true,
|
|
register_on=["alpine:init"]
|
|
) %}
|
|
{% if include_script %}
|
|
<script>
|
|
if (!window._popoverComponentLoaded) {
|
|
window._popoverComponentLoaded = true;
|
|
|
|
const registerPopoverComponent = () => {
|
|
Alpine.data('popover', () => ({
|
|
open: false,
|
|
|
|
$trigger: {
|
|
'@click'() { this.open = !this.open },
|
|
'@keydown.escape.prevent'() {
|
|
this.open = false;
|
|
this.$refs.trigger.focus();
|
|
},
|
|
':aria-expanded'() { return this.open },
|
|
'x-ref': 'trigger'
|
|
},
|
|
$content: {
|
|
'@keydown.escape.prevent'() {
|
|
this.open = false;
|
|
this.$refs.trigger.focus();
|
|
},
|
|
':aria-hidden'() { return !this.open },
|
|
'x-cloak': ''
|
|
},
|
|
}));
|
|
}
|
|
|
|
{% for event in register_on %}
|
|
document.addEventListener('{{ event }}', registerPopoverComponent);
|
|
{% endfor %}
|
|
}
|
|
</script>
|
|
{% endif %}
|
|
|
|
{% if include_html %}
|
|
<div
|
|
class="popover {{ main_attrs.class }}"
|
|
x-data="popover"
|
|
@click.away="open = false"
|
|
{% if id %}id="{{ id }}"{% endif %}
|
|
{% for key, value in main_attrs %}
|
|
{% if key != 'class' %}{{ key }}="{{ value }}"{% endif %}
|
|
{% endfor %}
|
|
>
|
|
{% if trigger %}
|
|
<button
|
|
type="button"
|
|
aria-haspopup="menu"
|
|
aria-expanded="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 %}
|
|
<div
|
|
data-popover
|
|
aria-hidden="true"
|
|
x-bind="$content"
|
|
{% if id %}id="{{ id }}-menu"{% endif %}
|
|
{% for key, value in content_attrs %}
|
|
{{ key }}="{{ value }}"
|
|
{% endfor %}
|
|
>
|
|
{% if caller %}{{ caller() }}{% endif %}
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
{% endmacro %}
|