Skip to content

dehboris/livewired

Repository files navigation

Livewired

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

This package was created by, and is maintained by Brian Faust, and provides reusable Laravel Livewire components.

Architectural Decisions

All components in this package have been extracted from real-world production applications and have been designed to be as flexible as possible to achieve maximum reusability. Each component is broken down into multiple methods that can be overwritten each on their own to overwrite specific parts without having to overwrite or reimplement everything.

That being said there will still be times where you will be better off to reimplement a component on your own as all of these components are extracted from real-world production applications which means major changes to their behavior are unlikely to be merged into this package.

before*

This method is called before the main functionality of a component is invoked. This should be the place where you perform anything that you want to be executed before the actual implementation is executed. This will generally be before{ComponentName}.

{componentName}

Each component has a method that is named the same as the component. This method is the bread and butter of each component. It contains the actual logic that is being executed when all authorization checks pass. You generally shouldn't face scenarios where you need to overwrite it but if you do you should make sure that you don't deviate too much from the original implementation as breaking changes to the component in other places could have unwanted side effects on your application.

after*

This method is called before the main functionality of a component is invoked. This should be the place where you perform anything that you want to be executed after the actual implementation is executed. This will generally be after{ComponentName}.

Exceptions

Note that there might be components that diverge from this structure due to limitations with how Livewire components work. An example of this would be a case where we want to split behaviors without having to use events for communication which means we could use nested components but due to them not being reactive we would end up with inconsistent and wrong results.

Views

This package comes with empty views as a default. If you need to quickly get some markup for the components your best bet is to grab a license of Tailwind UI and use one of their many components to save you time.

Installation

composer require kodekeep/livewired

Usage

There are two ways in which the components of this package can be consumed. The first is through component registration and the second is by creating your own component and extending ours. Both approaches have their benefits but as always it's up to you as a developer to choose the best one for your situation.

Registration

If you don't need to overwrite any methods of the component but only a before or after hook you can register the component directly with Livewire instead of relying on discovery. Place the following code in the boot method of any service provider to register the component and an after hook.

Livewire::component('update-password', UpdatePassword::class);

UpdatePassword::macro('afterUpdatePassword', fn () => flash()->success('Your password has been updated!'));

This approach is recommended if you don't plan on overwriting behaviors and have small before and after hooks.

Extension

If you want to alter or extend the behavior of a component you will need to create your own and extend the one you wish to work with. Place the following code in your desired component and call the updatePassword password method to see afterUpdatePassword take effect.

namespace App\Http\Livewire;

use KodeKeep\Livewired\Components\UpdatePassword as Component;

class UpdatePassword extends Component
{
    public function afterUpdatePassword(): void
    {
        $this->reset();

        flash()->success('Your password has been updated!');
    }
}

This approach is recommended if you need to perform larger changes or have complex before and after hooks. In these scenarios you should go with this approach to make it easier to maintain and test your component.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover a security vulnerability within this package, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

Support Us

We invest a lot of resources into creating and maintaining our packages. You can support us and the development through GitHub Sponsors.

License

Livewired is an open-sourced software licensed under the MPL-2.0.

About

Reusable components for @livewire applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published