Blazor and AlpineJS -- click away from child Component

Viewed 620

I am trying to make a dropdown section appear when a user clicks on a card. It's a Blazor WebAssembly project using .net 5.

Alpine has cool animations, so I am trying to use that, but it gives an error when trying to put the @click.away attribute on the div containing my component.

The below gives an "unhandled error has occurred" for me:

<div x-data="{ open: false }">
     <button @click="open = !open"  class="relative">
        <p>hello</p>
     </button

     <div x-show="open" @click.away="open = false" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="transform opacity-0 scale-95" x-transition:enter-end="transform opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="transform opacity-100 scale-100" x-transition:leave-end="transform opacity-0 scale-95" class="w-full mt-2 origin-top-right rounded-md shadow-lg">
    <Card/>
     </div>
</div>

Moving the @click.away to the button works fine for me, though:

<div x-data="{ open: false }">
     <button @click.away="open = false" @click="open = !open"  class="relative">
        <p>hello</p>
     </button

     <div x-show="open" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="transform opacity-0 scale-95" x-transition:enter-end="transform opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="transform opacity-100 scale-100" x-transition:leave-end="transform opacity-0 scale-95" class="w-full mt-2 origin-top-right rounded-md shadow-lg">
    <Card />
     </div>
</div>

The only problem with this is, I want the dropdown section to hide (with the animation) when I click outside the Card component, not the button.

Here is the simplified Card.razor:

<button>
I'm larry
</button>

I'm using basically the default project otherwise. Here is the alpinejs script:

<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer></script>

I've spent a good while narrowing it this far down. I'm thinking it's something with the load-order of the Javascript? Is there a way to fix that?

Does anyone know a way around this? If there is some other way to give an animation when clicking/clicking away from a component.

Thank you so much!

1 Answers

Try replacing '@' with 'x-on'.

I think using the Alpine '@' shortcut is confusing Blazor with its @onclick events. I'm doing something similar and the x-on:click.away is working.

Also, it seems like you can use this to wire both Alpine and Blazor up to the event. I'm using Blazor Server Side in .Net 6.

<button x-on:click="open = !open"  @onclick="CloseDialog" type="button" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:col-start-2 sm:text-sm">
                            Deactivate
</button>

Related