Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Make livewire modal scrollable #378

Merged
merged 9 commits into from
Oct 22, 2020
Merged

[1.x] Make livewire modal scrollable #378

merged 9 commits into from
Oct 22, 2020

Conversation

andreich1980
Copy link
Contributor

The problem

When using x-jet-dialog-modal on mobile and if the dialog has a lot of content you can't scroll to reach the content that doesn't fit in the screen.
Also the body of the document scrolls when modal is open.

The solution

This PR makes Modal scrollable when it is taller than the screen.

It is based on the #305 (it just disabled the body scrolling when modal is open) but also makes the modal itself scrollable.

It contains a hack for Firefox that can't work with padding bottom and overflow: auto as described on SO.

P.S. During the testing I have noticed that there's sm:items-top class that doesn't even exist. It should be sm:items-start.
Thinking about it - these classes just make the modal centered horizontally on larger screenssm:flex sm:items-start sm:justify-center.
We can achieve the same with just sm:mx-auto on the child element. And it will also help with the bottom space mentioned above. When we use flex the hack doesn't work.

How to test the PR

I tested this PR locally on Windows in fresh Chrome and Firefox.

You might want to recompile your assets to make sure that the classes I use here were not purged.
And you might want to hard refresh browser page Ctrl+F5 to prevent it using cached version of the app.css.

To test this PR I created a fresh Laravel project with Livewire stack.

laravel new jetstream-test --jet --stack=livewire

Set up your local database

DB_CONNECTION=sqlite
touch database/database.sqlite
php artisan migrate

Create a Livewire component

php artisan livewire:make TestModal

Edit the component and the view

class TestModal extends Component
{
    public $isOpen = false; // Add this public property

    public function render()
    {
        return view('livewire.test-modal');
    }
}
<div class="p-6">
    <x-jet-button wire:click="$set('isOpen', true)">Open Modal</x-jet-button>

    <x-jet-dialog-modal wire:model="isOpen">
        <x-slot name="title">Title</x-slot>

        <x-slot name="content">
            <div class="space-y-32">
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
                <div>Content</div>
            </div>
        </x-slot>

        <x-slot name="footer">
            <x-jet-secondary-button wire:click="$toggle('isOpen')" wire:loading.attr="disabled">Close</x-jet-secondary-button>
        </x-slot>
    </x-jet-dialog-modal>
</div>

Prepare the welcome page to use the modal

{{-- resources\views\welcome.blade.php --}}
<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Dashboard') }}
        </h2>
    </x-slot>

    <div class="py-6 pb-64">
        @livewire('test-modal')
    </div>
</x-app-layout>

Don't forget to add a repository to the composer.json as described here

Run the website php artisan server and open http://127.0.0.1:8000/ in your browser.

Make the browser window smaller and this is what you should see
изображение

Click the button and try to scroll the modal.

изображение

изображение

Notice that the body of the document doesn't scroll when modal is open.

Hope you'll like it.

claudiodekker and others added 5 commits October 21, 2020 12:02
* inset-0, overflow-y-auto - scroll the modal
* py-6 - add bottom padding for the modal parent
Replace parent `padding-bottom` to child `margin-bottom` as described here https://stackoverflow.com/a/38177668/3071884
@driesvints driesvints changed the title Make livewire modal scrollable [1.x] Make livewire modal scrollable Oct 22, 2020
@taylorotwell
Copy link
Member

Nothing needs to be changed on the Inertia version?

@andreich1980
Copy link
Contributor Author

andreich1980 commented Oct 22, 2020

@taylorotwell I have commited the same changes for Inertia.

I tested it locally copying Modal.vue that I have commited
to the resources/js/Jetstream folder.

UPD. Inertia already had overflow-hidden on body when modal is open.

@taylorotwell taylorotwell changed the base branch from 1.x to master October 22, 2020 13:10
@taylorotwell taylorotwell merged commit 24d3893 into laravel:master Oct 22, 2020
@andreich1980 andreich1980 deleted the modal-scrollable branch October 22, 2020 13:24
@kreierson
Copy link

kreierson commented Nov 24, 2020

The following commit eb0ff84 has a little annoyance. When the modal shows, the scrollbar disappears in the browser causing a slight shift. Might be distracting to some.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants