mstdmstd
mstdmstd

Reputation: 3103

How in alpineJS to shadow content beyond bootstrap modal dialog?

In Bootstrap 4.5/jquery 3.3 / alpinejs 2.2 app I show image with size restrictions and clicking on the image I open modal dialog with aplineJS method

x-on:click="show_hostel_image_modal = true"

and it is opened ok, but I dislike that all content behind the dialog box is visible. If there is a way to shadow it, without calling bootstrap js methods, as I move from jquery methods?

I have :

@if(!empty($currentHostelImage))
    <div
        x-data="{ show_hostel_image_modal: false, hostelImages: <?php print prepareEncodeArray($hostelImages) ?>,  current_image_id:
{{$currentHostelImage->id }}, currentImage : {{$currentHostelImage }} } "
        class="hostel_images_block_wrapper">
        
        
        <div
            class="modal_editor_container p-2"
            x-show="show_hostel_image_modal"
            @click.away="show_hostel_image_modal = false"
        >
            <div
                class="modal_editor_title"
            >
                <h4 class="modal-title p-2">
                    {!! $viewFuncs->showAppIcon('image') !!}Image view : <span x-html="currentImage.filenameData.image"></span>
                    <button class="close" type="button" x-on:click="show_hostel_image_modal= false">
                        <i class="test-device p-2 pl-4 pr-4 m-0"></i>
                        <span aria-hidden="true">&times;</span>
                    </button>
                </h4>
            </div> <!-- modal_editor_title -->
            
            <div class="modal_editor_fields" :style="'max-height: ' + ( modalHeight() - 20 ) +'px; '">
                <div x-show="typeof currentImage.filenameData != 'undefined'">
                    <img :src="currentImage.filenameData.image_url" class="hostel_dialog_image m-1 p-1"
                    
                         :class="{ 'hostel_image' : true }"
                    >
                    
                    <div class="alert alert-info" role="alert">
                        {!! $viewFuncs->showAppIcon('info') !!}
                        <span x-html="currentImage.filenameData.file_info"></span>
                    </div>
                
                </div> <!-- <div> -->
            </div>  <!-- modal_editor_fields-->
            
            
        
        </div> <!-- <div class="modal_editor_container"> -->
        
        
        <template x-for="nextHostelImage in hostelImages">
            <div class="hostel_view_current_image_wrapper" x-show="current_image_id === nextHostelImage.id ">
                <img :src="nextHostelImage.filenameData.image_url" class="hostel_view_current_image"
                     :alt="nextHostelImage.filenameData.file_info" x-on:click="show_hostel_image_modal = true" data-toggle="tooltip"
                     data-placement="top" title="Click to view in full size">
            </div>
        </template>
    
    
    </div>
@endif

and in styles :

.modal_editor_container {
  width: 90%;// !important;
  top: 20px;
  left: 50px;
  position: absolute;
  z-index: 900;
  flex: 1;
  flex-direction: column;
  justify-items: flex-start;
  padding: 0;
  margin: 0;// !important;
}

MODIFIED : I tried to follow your steps in fiddle with my Navigation menu.

Could you please take a look at https://jsfiddle.net/z83m7fnc/

I just need to note that hostel_images_block_wrapper - that is wrapper class for all images block and modal_editor_container class of modal dialog.

Thanks!

Upvotes: 1

Views: 2169

Answers (1)

William Rodriguez
William Rodriguez

Reputation: 36

You can apply the transparent background to your div.hostel_images_block_wrapper

.hostel_images_block_wrapper {
  background-color: rgba(0,0,0,0.5);
  position: absolute; (could also be fixed)
  top: 0;
  left: 0;
  height: 100vh;
  width: 100%;
}

Make sure to check the z-index of this element and it's containing element in context of your page.

With javascript, you can stop the page from scrolling behind the modal by adding overflow-y: hidden to the body.

On your div.hostel_images_block_wrapper add the following listener

@body-scroll="document.body.style.overflowY = open ? 'hidden' : ''"

Then from any element that has a click even that updates your show_hostel_image_modal property, you can dispatch the following custom event:

@click="$dispatch('body-scroll', {})"

Just as a side note, because AlpineJS is so well contained, you don't need to be so specific with your variable names. For example, show_hostel_image_modal could just be show or open. You can clean up a lot of redundant naming this way and keep your markup much cleaner.

Upvotes: 2

Related Questions