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

Usage with volt? #11

Open
deanmcpherson opened this issue May 19, 2024 · 5 comments
Open

Usage with volt? #11

deanmcpherson opened this issue May 19, 2024 · 5 comments

Comments

@deanmcpherson
Copy link

Just spitballing ideas here - but if there were a way to;

  1. Use this with volt functional style Livewire
  2. A vite plugin to extract ts/js from a blade file itself

Then you could conceivably make a single file Livewire react/vue component that defines and exposes it's own API layer/data requirements

With folio this would be really similar to next/remix style page components

It'd be really cool to be able to write volt functions in single files regardless of whether they're Livewire/alpine/react/vue/whatever

Can you think of any reason this wouldn't work?

@ijpatricio
Copy link
Owner

Hey Dean!

Only reason I can think of is my limited knowledge 😄

But I do love the boldness and thinking out of the box ❤️

Is this something you would like to put your hands on and contribute?

@deanmcpherson
Copy link
Author

deanmcpherson commented May 20, 2024 via email

@deanmcpherson
Copy link
Author

OK, I got this working;

Source: https://github.com/deanmcpherson/react-in-livewire-example
Demo: https://scenic-star-slvqcmdwqyh7.vapor-farm-f1.com

It essentially allows you to call Mingle::volt() in a volt blade file, and it will automatically compile out the first script tag.

E.g.

<?php

use function Livewire\Volt\{state};

Mingle::volt();

state([
    'hello' => 'world',
    'count' => 1,
]);

$makeItGoBoom = fn() => dd('This is dd');
$doubleIt = function() {
    $this->count *= 2;
};
?>
<script>
import {useState} from 'react';
import Button from '@/Button';

function render(props) {
    const doubleDown = () => {
        props.wire.doubleIt()
    }
    return <div className=" border p-4 rounded-lg mt-2">

       Hello {props.wire.hello} from react!
            <div>
                <button className="bg-red-200 m-2 p-2" onClick={() => {
                    props.wire.makeItGoBoom();
                }}>Go boom</button>
            </div>
            <div>
                <button onClick={doubleDown}>Double it! {props.wire.count}</button>
            </div>
            <Button />
        </div>
}
</script>

@ijpatricio
Copy link
Owner

Hey @deanmcpherson

This is AWESOME!! ❤️

I'm actually not a Volt user, but it does make all sense to also support it.

I'm just a little swamped with tasks, but I'll take that repo for a spin, and hopefully I can understand enough to also support it - I've never done a Vite plugin.

Thank you, I'll ping back soon

@deanmcpherson
Copy link
Author

Thanks!

I think there's a few pieces in here, I'm not sure if mingle is even the right place the auto-extracting-inline-component voodoo I've got running

On the simpler side, you can also just call Mingle::volt("resources/js/blah.jsx"); and have it render like a standard mingle component without any plugin shenanigans

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

No branches or pull requests

2 participants