├── .php_cs ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── SECURITY.md ├── composer.json ├── config └── livewire-portals.php ├── database └── migrations │ └── create_skeleton_table.php.stub ├── psalm.xml ├── resources └── views │ ├── .gitkeep │ ├── portal-layout.blade.php │ ├── portal-scripts.blade.php │ ├── portal-view.blade.php │ └── portal.blade.php └── src ├── Commands └── LivewirePortalCommand.php ├── LivewirePortal.php ├── LivewirePortalFacade.php └── LivewirePortalServiceProvider.php /.php_cs: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('resources/view/mail/*') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return PhpCsFixer\Config::create() 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sortAlgorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline_array' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'method_argument_space' => [ 33 | 'on_multiline' => 'ensure_fully_multiline', 34 | 'keep_multiple_spaces_after_comma' => true, 35 | ] 36 | ]) 37 | ->setFinder($finder); 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `LivewirePortal` will be documented in this file 4 | 5 | ## 1.0.0 - 202X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Jeffochoa bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Livewire Portals 2 | 3 | Render a [Livewire](https://github.com/livewire/livewire) component on a specific target in the DOM. 4 | 5 | ## Install 6 | 7 | THIS PACKAGE IS STILL IN DEVELOPMENT, TO USE, PLEASE ADD THE FOLLOWING REPOSITORY - NOT READY FOR PRODUCTION YET 8 | 9 | ```json 10 | // composer.json 11 | { 12 | "repositories": { 13 | "jeffochoa/livewire-portals": { 14 | "type": "vcs", 15 | "url": "git@github.com:jeffochoa/livewire-portals.git" 16 | } 17 | } 18 | } 19 | ``` 20 | 21 | This package require some of the livewire directives to be registered first, so you'll need to disable the package auto-discovery and add manually the service-providers in your `config/app.php` file: 22 | 23 | ```php 24 | // composer.json 25 | { 26 | "extra": { 27 | "laravel": { 28 | "dont-discover": [ 29 | "jeffochoa/livewire-portals", 30 | "livewire/livewire" 31 | ] 32 | } 33 | }, 34 | } 35 | ``` 36 | 37 | In the `config/app.php` file: 38 | 39 | ```php 40 | return [ 41 | 'providers' => [ 42 | // ... 43 | Livewire\LivewireServiceProvider::class, 44 | Jeffochoa\LivewirePortal\LivewirePortalServiceProvider::class 45 | ] 46 | ]; 47 | ``` 48 | 49 | ## Use case 50 | 51 | Let's say you have a `message-window` livewire component to display a message in the view after running an action in the application, like a mail-list subscription. 52 | 53 | Your mail-list subscription component is also, a livewire component. 54 | 55 | ```html 56 | @livewire('newsletter') 57 | ``` 58 | 59 | In the component class, you'll probably have a method to handle the form submission: 60 | 61 | ```php 62 | class Newsletter extends Component { 63 | public function create() 64 | { 65 | // handle subscription 66 | 67 | // push notification 68 | } 69 | } 70 | ``` 71 | 72 | So, instead of pushing notifications through events, and so on, we just want to append the `message-window` component in the view, that's it. 73 | 74 | How could we do that? 🤔 75 | 76 | ## Using Livewire portals 77 | 78 | This package allows you to `open` a portal to append any livewire component in the DOM in runtime. 79 | 80 | This is how it looks: 81 | 82 | Open a new portal and push the `message-window` after registering a new subscription in your `newsletter` component. 83 | 84 | ```php 85 | class Newsletter extends Component { 86 | public function create() 87 | { 88 | // handle subscription 89 | 90 | // push notification 91 | $this->openPortal( 92 | $target = 'messages-container', 93 | $component = 'message-window', 94 | $componentAttributes = ['text' => 'You are subscribed'] 95 | ); 96 | } 97 | } 98 | ``` 99 | 100 | > The $componentAttributes variable will be passed down to the `message-window`'s `mount()` method. 101 | 102 | Then, somewhere in the DOM you just need to include the new portal (container); 103 | 104 | ```html 105 |
109 | ``` 110 | 111 | > The `wire:portal.replace` is optional, if not present, then instead of replacing the container's `innerHtml`, the component will be included as a new node within the container. 112 | 113 | Have fun 🎉 114 | 115 | ## FAQ 116 | 117 | **Why a new package and not a pull-request to the livewire core?** 118 | 119 | I'm using this approach right now on a project, so far it's just experimental, but if it gets enough attention, then, ideally I'd like to pull-request this changes to the livewire repository, but for now, having it as a package will give anyone the opportunity to install and test this solution. 120 | 121 | **Why i can't install this package from packagist?** 122 | 123 | This package is still under development, so I won't bother in to publishing it to packagist until having a more stable version. -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker. 6 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jeffochoa/livewire-portals", 3 | "description": "", 4 | "keywords": [ 5 | "LivewirePortal" 6 | ], 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Jeff Ochoa", 11 | "homepage": "https://jeffochoa.me", 12 | "role": "Developer" 13 | } 14 | ], 15 | "require": { 16 | "php": "^7.4", 17 | "livewire/livewire": "^1.3" 18 | }, 19 | "require-dev": { 20 | "friendsofphp/php-cs-fixer": "^2.16", 21 | "orchestra/testbench": "^5.0", 22 | "phpunit/phpunit": "^9.0", 23 | "psalm/plugin-laravel": "^1.2", 24 | "vimeo/psalm": "^3.11" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Jeffochoa\\LivewirePortal\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "Jeffochoa\\LivewirePortal\\Tests\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "psalm": "vendor/bin/psalm", 38 | "test": "vendor/bin/phpunit", 39 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage", 40 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Jeffochoa\\LivewirePortal\\LivewirePortalServiceProvider" 49 | ], 50 | "aliases": { 51 | "LivewirePortal": "Jeffochoa\\LivewirePortal\\LivewirePortalFacade" 52 | } 53 | } 54 | }, 55 | "minimum-stability": "dev", 56 | "prefer-stable": true 57 | } 58 | -------------------------------------------------------------------------------- /config/livewire-portals.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 13 | 14 | // add fields 15 | 16 | $table->timestamps(); 17 | }); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /psalm.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffochoa/livewire-portals/f4ec546353c80a5f6415035e1c73fa94a2f0bdd9/resources/views/.gitkeep -------------------------------------------------------------------------------- /resources/views/portal-layout.blade.php: -------------------------------------------------------------------------------- 1 | @yield('content') -------------------------------------------------------------------------------- /resources/views/portal-scripts.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/portal-view.blade.php: -------------------------------------------------------------------------------- 1 | @extends($layout) 2 | 3 | @section($section) 4 | @livewire($component, $componentParameters) 5 | @endsection -------------------------------------------------------------------------------- /resources/views/portal.blade.php: -------------------------------------------------------------------------------- 1 | {{--
--}} 2 |
3 | {{--
--}} 8 | 9 | -------------------------------------------------------------------------------- /src/Commands/LivewirePortalCommand.php: -------------------------------------------------------------------------------- 1 | comment('All done'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/LivewirePortal.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 15 | $this->publishes([ 16 | __DIR__.'/../config/livewire-portals.php' => config_path('livewire-portals.php'), 17 | ], 'config'); 18 | 19 | $this->publishes([ 20 | __DIR__.'/../resources/views' => base_path('resources/views/vendor/livewire-portals'), 21 | ], 'views'); 22 | 23 | $this->commands([ 24 | LivewirePortalCommand::class, 25 | ]); 26 | } 27 | 28 | $this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-portals'); 29 | Blade::include('livewire-portals::portal-scripts', 'livewirePortalScripts'); 30 | } 31 | 32 | public function register() 33 | { 34 | $this->mergeConfigFrom(__DIR__.'/../config/livewire-portals.php', 'livewire-portals'); 35 | 36 | /** 37 | * Stolen from: https://github.com/livewire/livewire/blob/5c7bf4eae4bb0b0a811fa79199a906a7c9d7917f/src/Macros/RouterMacros.php#L25 38 | */ 39 | Component::macro('openPortal', function (string $portal, string $component, array $data = []) { 40 | $content = view('livewire-portals::portal-view', [ 41 | 'layout' => 'livewire-portals::portal-layout', 42 | 'section' => 'content', 43 | 'component' => $component, 44 | 'componentParameters' => $data, 45 | ])->render(); 46 | 47 | $this->dispatchBrowserEvent('portal-open', compact('content', 'portal')); 48 | }); 49 | } 50 | } 51 | --------------------------------------------------------------------------------