larawire-garage/larawire-modals
Composer 安装命令:
composer require larawire-garage/larawire-modals
包简介
Modal window with Livewire
README 文档
README
👉 For Backend Coding Enjoyers 😉
Create modal dialog easily with laravel project with livewire 3 + Alpinejs & manage modal dialog as livewire component.
Still on beta version
Installation
composer require larawire-garage/larawire-modals
Setup
publish configurations
php artisan vendor:publish --tag=larawire-modals-configs
Themes
Currently supports 2 themes.
- Bootstrap
- Tailwind
Bootstrap Installation
You can use CDN Link to apply bootstrap.
If you are using package managers like npm use Bootstrap Documentation to install.
When Importing Bootstrap add below code to app.js file.
// Import all of Bootstrap's JS import * as bootstrap from 'bootstrap' window.bootstrap = bootstrap; // 👈 required
Tailwind Installation
You can use CDN Link to apply Tailwindcss.
If you are using package managers like npm use Tailwindcss Documentation to install. Then Add below code to tailwind.config.js file.
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./resources/**/*.blade.php", "./resources/**/*.js", "./resources/**/*.vue", "./vendor/larawire-garage/larawire-modals/**/*.blade.php", // 👈 Add this line ], //... }
If not working correctly? Check Here
Change Theme
You can add theme your proejct used in larawire-modals config file.
// configs/larawire-modals.php return [ 'theme' => 'bootstrap', // OR 'theme' => 'tailwind', // .... ];
Also you can change theme in your modal variable inside the modal component.
// app/Livewire/MODAL_PATH/MYMODAL.php class MYMODAL extends ModalComponent{ public array $modal = [ 'theme' => 'bootstrap', // OR 'theme' => 'tailwind', //... ]; }
Usage
create modal component using below command.
php artisan make:modal MyModal
Now you can use modal component like regular livewire component. By default this command create component:
Class: app/Livewire/ModalsView: resources/views/livewire/modals
directories. You can change it in the larawire-modals config file.
// configs/larawire-modals.php return [ 'class_namespace' => 'App\\Livewire\\Modals', 'view_path' => resource_path('views/livewire/modals'), // .... ];
ModalComponent is a regular Livewire component. You can do everything normal Livewire component can do.
- Fire or listen events with parameters
- Parse parameters to component
- Call functions etc...
By Default ModalComponent listen 2 events
- show [parameters parse to beforeShow function]
- hook : beforeShow(...$args)
- close [parameters not supported yet]
- hook : beforeClose()
To show the modal dialog
dispatch show event to "your-modal-component".
$this->dispatch('show',id:$post->id)->to(MyModal::class);
<button wire:click="$dispatchTo('your-modal-component', 'show', { id: {{ $post->id }} })"> Show Modal </button>
To close the modal dialog
- From other component:
$this->dispatch('close')->to(MyModal::class);
<button wire:click="$dispatchTo('your-modal-component', 'close')"> Close Modal </button>
- From modal component:
$this->dispatch('close')->self(); //or $this->closeModal();
Options
Modal component has $modal public variable containing modal options.
public array $modal = [ 'id' => 'my-modal', 'title' => 'My Modal', //... ];
Available Options
id- Modal IDtitle- Modal title text on the headertheme- Theme using in the project (bootstrap or tailwind). Can be defind in the larawire-modals config fileresetBeforeShow- if this is true, public variables in the modal component automatically reset before show the modal. Can be defind in the larawire-modals config fileresetValidationBeforeShow- if this is true, Reset the validation error bag of modal component automatically before show the modal. Can be defind in the larawire-modals config file
Methods
showModal()- Show the modal dialogcloseModal()- Close the modal dialog
You can define:
beforeShow()- Runs before show the modal dialogbeforeClose()- Runs before close the modal dialog
Add Form
If your modal contains form, add formSubmit attribute to modal component.
When formSubmit attribute defined, modal body and footer slots automatically covered by form tag with wire:submit="[form-submit-value]" attribute.
<x-larawire::modal :modal="$modal" formSubmit="createNewUser"> <!-- 👆 Add form submit parameter here --> <x-slot name="body"> <!-- Modal body content --> <!-- Add form content here --> <input type="text" class="" name="name" id="name-input" wire:model="name"> </x-slot> <x-slot name="footer" :defaultClose="true"> <!-- Modal footer content --> <!-- 👇 Add button for submit the form --> <button type="submit" wire:loading.attr="disabled" wire:target='createNewUser'> <span wire:loading.class="d-none hidden">Create</span> <span wire:loading wire:target="createNewUser">Creating...</span> </button> </x-slot> </x-larawire::modal>
Customizaion
In the larawire-modals config file you can change classes of the containers of the modal. Also can change in the modal variable of the modal component.
// configs/larawire-modals.php return [ // ... 'theme-classes' => [ 'bootstrap' => [ 'backdropClasses' => '', 'containerClasses' => '', 'windowClasses' => '', 'headerClasses' => '', 'headerCloseBtnClasses' => '', 'bodyClasses' => '', 'footerClasses' => '', 'footerCloseBtnClasses' => '', ], 'tailwind' => [ 'backdropClasses' => '', 'containerClasses' => '', 'windowClasses' => '', 'headerClasses' => '', 'headerCloseBtnClasses' => '', 'bodyClasses' => '', 'footerClasses' => '', 'footerCloseBtnClasses' => '', ], ], ];
// app/Livewire/MODAL_PATH/MYMODAL.php class MYMODAL extends ModalComponent{ public array $modal = [ /** available if needs to customize */ 'backdropClasses' => '', 'containerClasses' => '', 'windowClasses' => '', 'headerClasses' => '', 'headerCloseBtnClasses' => '', 'bodyClasses' => '', 'footerClasses' => '', 'footerCloseBtnClasses' => '', //... ]; }
Changing classes still not working for you ?
To customize appearance of the modal, you can publish the views and edit it.
php artisan vendor:publish --tag=larawire-modals-views
Animation
Also you can change animation of the modal.
Bootstrap Theme :
In bootstrap, follow bootstrap documentation to change the modal animation.
Tailwind Theme :
In tailwind theme under animation key in them larawire-modals config file.
Also you can change it in the modal variable in the modal component.
// app/Livewire/MODAL_PATH/MYMODAL.php class MYMODAL extends ModalComponent{ public array $modal = [ /** Only for tailwind */ 'animation' => 'slide-down', // <== slide-down, scale-up //... ]; }
For deep customize animation classes check under animation-classes key in larawire-modals config file.
In Tailwind theme, modal use alpinejs & tailwindcss animation classes to animate modals.
Not Working Correctly
Tailwind
In case modal not working correctly, Eg:
- Modal not rendering correctly
- Animation not working
Add below array of classes to safelist array
If you change the classes in config file or in class modal variable, that classes also need to include here
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./resources/**/*.blade.php", "./resources/**/*.js", "./resources/**/*.vue", "./vendor/larawire-garage/larawire-modals/**/*.blade.php", // 👈 Add this line ], /** * ⚠️⚠️⚠️ If in-case (content path) 👆 not working.⚠️⚠️⚠️ * 👇 Add this block */ safelist: [ 'backdrop-blur-sm', 'bg-white', 'bg-gray-400', 'bg-gray-800/30', 'bg-red-300', 'border', 'border-b', 'border-t', 'dark:bg-gray-900', 'dark:text-gray-100', 'duration-200', 'duration-300', 'ease-in', 'ease-out', 'fixed', 'inset-0', 'flex', 'flex-none', 'grow', 'items-center', 'justify-between', 'self-end', 'font-bold', 'h-fit', 'h-screen', 'hover:bg-gray-800', 'hover:text-gray-100', 'hover:text-red-600', 'opacity-0', 'opacity-100', 'overflow-y-auto', 'overflow-y-hidden', 'p-5', 'px-2', 'px-5', 'py-1', 'py-2', 'py-3', 'md:p-14', 'rounded-b-lg', 'rounded-lg', 'rounded-t-lg', 'sm:scale-100', 'sm:scale-95', 'text-3xl', 'text-black', 'text-end', 'text-gray-900', 'transform', 'transition', 'transition-all', 'translate-y-0', 'translate-y-4', 'sm:translate-y-0', '-translate-y-8', 'w-full', 'w-3/5', 'z-50', ] //... }
!!! 🎉🎉🎉 Enjoy 🎉🎉🎉 !!!
larawire-garage/larawire-modals 适用场景与选型建议
larawire-garage/larawire-modals 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 larawire-garage/larawire-modals 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 larawire-garage/larawire-modals 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-30