Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor draggable/resizable modal bootstrap dialog

I've got a nice implementation of, curtesy of Kyle, for a Blazor component for a bootstrap modal dialog here: How to use Bootstrap modal in Blazor client app? But I would like to extend this solution to make the modal draggable & resizable.

The best approach (to avoid re-inventing the wheel) seems to be to use the JQuery UI draggable()/resizeable() functions. I've got this link to a demonstration of how to do this in pure Javascript: DRAG AND RESIZE BOOTSTRAP MODAL that essentially calls the resizeable and draggable functions on the modal divs

<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
    $('.modal-content').resizable({
        //alsoResize: ".modal-dialog",
        minHeight: 300,
        minWidth: 300
    });
    $('.modal-dialog').draggable();
</script>

I've tried adding this script to my _Host.cshtml page but it has no effect. Any advice on how to merge the blazor component solution with the jquery UI solution would be much appreciated

David

like image 598
David Regan Avatar asked Mar 12 '26 21:03

David Regan


1 Answers

The answer is to explicitly call a javascript function in the OnAfterRenderAsync override to apply the JQuery UI functions to the modal divs.

E.g.

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await jsRuntime.InvokeVoidAsync("setModalDraggableAndResizable");
        await base.OnAfterRenderAsync(firstRender);
    }

where setModalDraggableAndResizable is a javascript function in the _Hosts.cshtml:

    <script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
    <script type="text/javascript">
        function setModalDraggableAndResizable() {
            $('.modal-content').resizable({
                //alsoResize: ".modal-dialog",
                minHeight: 300,
                minWidth: 300
            });
            $('.modal-dialog').draggable();
        }
    </script>

And the modal is now draggable and resizable...

Modal example image

like image 136
David Regan Avatar answered Mar 14 '26 20:03

David Regan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!