Dropdown gets closed while using mouse event args

Jagan 0 Reputation points
2024-05-13T06:24:56.8566667+00:00

Hi Everyone,

This works fine when i hover over the button it shows and closes when it comes out of the dropdown however the dropdown still closes while mouse pointer is still in the dropdown.

Could some share your ideas!!

My need is to display the dropdown while in the button and should be open while the mouse in the dropdown and only to close if the pointer is outside the button or dropdown.

PS: Thanks in advance

<button class="btn btn-secondary dropdown-toggle" type="button" id="transformDropdown" data-toggle="dropdown" aria-expanded="@dropdownVisible.ToString().ToLower()" style="color: #212529; background-color: #f8f9fa;" @onmouseover="ToggleDropdown" >
    Applied Actions
</button>

<ul class="dropdown-menu" @ref="dropdownRef" id="action-dropdown-list" aria-labelledby="transformDropdown" style=" background-color: #ffffff;display: @(dropdownVisible ? "block" : "none")" @onmouseover="ToggleDropdown" @onmouseout="CloseDropdown">

private void ToggleDropdown()
{
    dropdownVisible = true;
    isInButton = true;
}
private void ToggleDropdown1()
{
    dropdownVisible = true;
}
private void CloseDropdown()
{
    if (!isInButton)
    {
        dropdownVisible = false;
    }
}
Developer technologies .NET Blazor
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-05-13T13:39:22.31+00:00

    Hi, I have tested with following code and works well.

    @page "/"
    <button class="btn btn-secondary dropdown-toggle" type="button" id="transformDropdown" data-toggle="dropdown" style="color: #212529; background-color: #f8f9fa;"
            aria-expanded="@dropdownVisible.ToString().ToLower()" @onmouseover="ToggleDropdown" @onmouseout="CloseDropdown">
        Applied Actions
    </button>
    <ul class="dropdown-menu"  id="action-dropdown-list" aria-labelledby="transformDropdown" 
    style=" background-color: #ffffff;display: @(dropdownVisible ? "block" : "none")" @onmouseover="ToggleDropdown" @onmouseout="CloseDropdown">
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
    </ul>
    @code{
        private bool dropdownVisible=false;
        private void ToggleDropdown()
        {
            dropdownVisible = true;
        }
        private void ToggleDropdown1()
        {
            dropdownVisible = true;
        }
        private void CloseDropdown()
        {
            dropdownVisible = false;
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.