ethanbarlo / livewiremesh
This is my package livewiremesh
Fund package maintenance!
Ethan Barlow
Installs: 605
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Language:TypeScript
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- livewire/livewire: ^3.5
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0|^10.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
LivewireMesh is a powerful package that enables seamless integration of React components within Laravel Livewire applications. Inspired by MingleJS, LivewireMesh takes the concept further by building directly into Livewire's core functionality rather than relying on AlpineJS as an intermediary.
Key Features
- Native Livewire Integration: Built directly on top of Livewire's hooks system for better performance and reliability
- Reactive Props: React components automatically update when Livewire properties change
- Two-way Data Binding: Use the
useEntangle
hook to create bidirectional bindings between React state and Livewire properties - Live/Deferred Updates: Choose between live or deferred updates when using entangled properties
- Direct Wire Access: Access Livewire methods and properties directly through the
useWire
hook
Installation
You can install the package via composer:
composer require ethanbarlo/livewiremesh
Documentation
A full documentation and demo site is in the works.
Example Usage: React-Controlled Inputs
LivewireMesh allows you to create React components that can be used as Livewire form inputs using wire:model
. Here's how to create a custom React Select component that integrates seamlessly with Livewire:
- Generic Livewire Controlled Page
Controller
use Livewire\Component; class UserProfile extends Component { public ?string $country = null; public function save() { $this->validate([ 'country' => 'required|string' ]); } public function countries(): array { return [ ['value' => 'us', 'label' => 'United States'], ['value' => 'uk', 'label' => 'United Kingdom'], ['value' => 'ca', 'label' => 'Canada'], ]; } public function render() { return view('livewire.user-profile'); } }
View
<div> <form wire:submit.prevent="save"> <div> <label>Select your country:</label> <livewire:react-select wire:model="country" :options="$this->countries()" /> @error('country') <div class="text-red-500 text-sm mt-1">{{ $message }}</div> @enderror </div> <button type="submit">Save</button> </form> </div>
- One thing to note, is that you can use either 'wire:model' or 'wire:model.live'. Without needing to make any changes to the LivewireMesh component.
- The LivewireMesh / React component
Controller
use EthanBarlo\LivewireMesh\MeshComponent; use Livewire\Attributes\Modelable; class ReactSelect extends MeshComponent { #[Modelable] public string $value = ''; public function __construct( public array $options ) {} public function component(): string { return 'resources/js/components/ReactSelect/index.ts'; } public function props(): array { return [ 'options' => $this->options, ]; } }
View
import { useEntangle } from '@livewiremesh/react/contexts/LivewireContext'; import Select, { type SelectOption } from 'custom-select-component'; // Example interface IReactSelect{ options: SelectOption[]; } const ReactSelect: React.FC<IReactSelect> = ({ options }) => { const [value, setValue] = useEntangle<string>('value'); return ( <Select value={value} onChange={setValue} options={options} /> ); }
This example demonstrates how LivewireMesh enables you to:
- Use React components as form inputs with
wire:model
- Handle two-way data binding between React and Livewire
- Create reusable React components that work seamlessly within Livewire forms
- Maintain a reactive connection between your React state and Livewire properties
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Testing
composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.