Mudblazor - Tap or click within the designated area to trigger the drag and drop functionality

I have incorporated the Mudblazor CSS framework into my Blazor WebAssembly project.

Within the drag and drop zone, I have included a button that is supposed to remove each uploaded image. However, when I click on the remove button, it only opens up the file manager.

The issue seems to be related to the fact that the actual drag and drop zone is set to position: absolute none.

Is there a solution to this problem?

This is how the scenario appears - it's impossible to click on the remove button as the file manager pops up instead.

https://i.sstatic.net/HjGZF.png

CSS:

 .drag-drop-zone {
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .4s;
    /*        min-height: 400px;
*/ border: 3px dotted;
    min-height: 100px;
    border: 2px dashed rgb(0, 135, 247);
}

.drag-drop-input {
    position: absolute;
    width: 100%;
    height: 90%;
    opacity: 0;
    cursor: pointer;
    z-index: 2;
}

.drag-enter {
    box-shadow: var(--mud-elevation-10);
}

.list {
    padding: 2em;
    min-width: 100%;
}

Razor

<MudList Style="padding:2em;width:100%;" Dense="true">
@foreach (var file in fileNames)
{
    <MudListItem @key="@file">
        <MudChip Color="Color.Dark"
                 Style="width:60px; overflow:hidden;"
                 Text="@(file.Split(".").Last())" />
        @file <MudButton Color="Color.Error" OnClick="() => Remove(file)" Style="position:unset;">Remove</MudButton>
    </MudListItem>}

Remove method:

    void Remove(string file)
{
    var ret = fileNames.FirstOrDefault(x => x.Contains(file));
    if (ret != null)
    {
        fileNames.Remove(ret);
    }
}

Answer №1

I encountered a similar issue while working with the code provided on MudBlazor's homepage at this link.

Memetican pointed out that it appeared as though the .drag-drop-input was covering up other content. Without fully understanding the technical aspects, I found the position: absolute; part intriguing. Researching more about it here, I decided to change it from absolute to static. Along with some minor adjustments to the sample code, the modifications below seem to have resolved the issue:

@page "/tools/csvimport"
@inject ISnackbar Snackbar
<style>
    .drag-drop-zone {
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all .4s;
        height: 100%;
    }

    .drag-drop-input {
        position: static;
        width: 100%;
        height: 100%;
        opacity: 0;
        cursor: pointer;
        z-index: 2;
    }

    .drag-enter {
        box-shadow: var(--mud-elevation-10);
    }

    .list {
        padding: 2em;
        min-width: 100%;
    }
</style>

<MudGrid Justify="Justify.Center" Spacing="4">

    <MudItem xs="12" sm="12" md="6">
        <MudPaper @ondragenter="@(()=>_dragEnterStyle="drag-enter")"
                  @ondragleave="@(()=>_dragEnterStyle=null)"
                  @ondragend="@(()=>_dragEnterStyle=null)"
                  Class=@("drag-drop-zone "+ _dragEnterStyle)
                  MinHeight="200px">

            <InputFile OnChange="OnInputFileChanged" class="drag-drop-input" />

            @if (file is null)
            {
                <MudText Typo="Typo.h6">Drag file here, or click to browse your files</MudText>
            }
            else
            {
                <MudChip Color="Color.Info">@file.Name</MudChip>
            }


        </MudPaper>
        <MudGrid Justify="Justify.Center" Spacing="4" Class="mt-4">
            <MudItem>
                <MudButton OnClick="Upload" Disabled="@(file is null)" Color="Color.Primary" Variant="Variant.Filled">Upload</MudButton>
            </MudItem>
            <MudItem>
                <MudButton OnClick="@(() => file = null)" Disabled="@(file is null)" Color="Color.Error" Variant="Variant.Filled">Clear</MudButton>
            </MudItem>
        </MudGrid>
    </MudItem>

</MudGrid>
@code {
    string _dragEnterStyle;
    IBrowserFile file;

    void OnInputFileChanged(InputFileChangeEventArgs e)
    {
        file = e.File;
    }
    void Upload()
    {
        //Upload the files here
        Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
        Snackbar.Add("TODO: Upload " + file.Name, Severity.Normal);
    }
}

Answer №2

It seems like there may be some missing code for your drop target, as not all of your CSS styles are being utilized.

From what I can gather, it looks like your drop target is placed on top of your files list. Take note of the z-index in your CSS...

.drag-drop-input {
    position: absolute;
    width: 100%;
    height: 90%;
    opacity: 0;
    cursor: pointer;
    z-index: 2; /* <== this line */
}

This setup creates a sort of glass-pane effect. While you can still see your files list and remove buttons due to opacity: 0;, interaction with them is hindered because the drop target is layered above them in terms of visibility.

Think of an HTML page like a stack of overlapping rectangles. Mouse interactions will register on the topmost layer only.

Solution 1: (viable) Remove the z-index or adjust the z-index of your files list and buttons so they sit higher than the drop target. This workaround should function, but might cause issues in certain browsers if users attempt to drop a file directly onto the files list or remove buttons.

Solution 2: (recommended) Opt to relocate your file list and remove buttons outside of the drop target. Create your drop target solely for handling file drops and mouse clicks triggering file uploads.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

execute the middleware function within the router

I've integrated sessions in my express.js application using Mozilla's client-sessions and I'm encountering an issue. My post and get requests are in router.js, while the middleware is in app.js. When I try to run it, I receive the following ...

Executing a function within a ng-repeat iteration

Is there a way to call a method within an ng-repeat loop, even if the element does not exist at that moment? I'm facing this issue and I'm not sure how to resolve it... The ID seems to be correct. Strangely, when I run it in the console after ev ...

jQuery's event delegation feature fails to work in conjunction with AJAX requests

I'm currently facing issues with jQuery's on event delegation: Below is the code snippet I am working with: HTML portion: <body> <!-- Page Content Wrapper--> <div id="sb-site"> <div id="overlay">& ...

What sets usePreloadedQuery apart from useQueryLoader?

I've been exploring graphQL and the react-relay library. These are the key points I've covered: Rendering Queries: where usePreloadedQuery is introduced. Fetching Queries for Render: where useQueryLoader is introduced. To keep it simple, I&apo ...

Disappearing a menu list while zooming in on a page: The art of vanishing navigation

[QUESTION] Is there a way to make the Menu List disappear when zooming in on a webpage? For example: <-- Normal Page [No Zoom] --> [Logo] Profile Gallery Guestbook <-- Zoom In 120% --> [Logo] Profile Guestbook <-- Zoom In 150% --> ...

Can you provide a brief explanation for this bubble sort JavaScript code?

Can someone please explain to me what the line j<len-i is doing in this bubble sort code? I believe removing -i from that line will still make the program work properly, var arr=[3,5,4,7,8,9,30,0,-1]; function bubble_Sort(arr){ var len = arr.length, ...

Tips for keeping notification icons in the upper right corner of the box?

I am facing an issue with absolute positioning where the notification icons I've positioned to the top-right corner of a box are moving away when the screen size changes. Desired appearance of the image Actual appearance when screen size is adjusted ...

Creating an Image slideShow with only one Image - making it slide on itself

Apologies for this confusing and vague question. Imagine I only have a single image, and I want to create a slideshow using technologies like Angular, jQuery, JavaScript, or just CSS. What I am aiming for is when the user clicks on my slide button, the i ...

Issue with cutting of rotating pseudo element

Trying to rotate a pseudo element with a background-image is posing a challenge. The background, positioned at the center of the main element for visual effect, ends up being cut off when rotated. An illustrative example has been created using random imag ...

What is the best way to access query string values using JavaScript?

Is it possible to retrieve query string values without using a plugin in jQuery? If the answer is yes, how can this be accomplished? If not, are there any plugins available that can help with this task? ...

initiating an event within a modal controller

I have a $scope.$on callback function in my mainController. It works perfectly whenever I call it from each controller, but when I call it from a modalController after opening the modal, it doesn't work: define(['app', 'jquery'], ...

Can a single file in NextJS 13 contain both client and server components?

I have a component in one of my page.tsx files in my NextJS 13 app that can be almost fully rendered on the server. The only client interactivity required is a button that calls useRouter.pop() when clicked. It seems like I have to create a new file with ...

Tips for maintaining a website within a 960px width while stretching the background on the x-axis

I have enclosed the entire website within a div with the ID container. The CSS styling for the container is as follows: #container { width: 960px; background: #FFFFFF; margin: 0 auto; border: 1px solid #000000; text-align: left; } How ...

Issue encountered while trying to exhibit jpg image content from server side on html

I am attempting to display an image from server-side data using an Ajax call. The data stored on the server looks something like this: "ÿØÿàJFIFÿÛC4­¶¸UOHlʵcBò)3¹_È'I4~&éüÿ§Ú<ã©ã4>'Øù¿ÇÄmËCÈgÚsZ¥ë" Upo ...

Modify the css with JQUERY when there are no rows inside the tbody section

Is it possible to change the css using jquery if there are no rows in the table body? I have attempted this but my current approach is not working. I tried adding an alert within the if statement, but the alert did not appear. My goal is to hide the table ...

Utilizing ng-class for dynamic routing and controlling

I am currently in the process of developing a dynamic framework using AngularJS. My plan involves allowing users to add new templateUrl and controller from a JSON file, like the one shown in templates.json: { "pages" : [ { "name" : "home" ...

Tips for incorporating JavaScript modules into non-module files

Learning how to use js modules as a beginner has been quite the challenge for me. I'm currently developing a basic web application that utilizes typescript and angular 2, both of which heavily rely on modules. The majority of my app's ts files ...

It appears that the home page of next.js is not appearing properly in the Storybook

Currently, I am in the process of setting up my next home page in storybooks for the first time. Following a tutorial, I successfully created my next-app and initialized storybooks. Now, I am stuck at importing my homepage into storybooks. To achieve this, ...

Utilize my package to make necessary changes to the dashboard configuration

I have successfully created my own application or section using the sectionservice called from within my package. Now, I am looking to incorporate an HTML/Angular dashboard view into this section. However, all the tutorials I come across discuss editing th ...

Unable to execute a basic opacity transition on a div element

Why isn't the opacity transitioning in Chrome? I only want it to fade in with the 'transitions' class without fading out first. Check out this code sample: http://jsfiddle.net/chovy/t37k3/9/ <div></div> <a href="#" class="s ...