livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
331 stars 19 forks source link

Volt "Single File Component" with <style> + <script> blocks #111

Closed camya closed 1 month ago

camya commented 1 month ago

I work a lot with Vue3 Single File Components, so I really miss being able to add CSS styles and Javascript script blocks directly into the Volt Single File Component.

I was thinking of something like this. (Full example below)

I really hope, this is possible. :)

There is a Laravel Mix extension, which does something similary, but I never tried it. (Link added below)

<?php

use Livewire\Volt\Component;

new class extends Component {
    // Name used in Blade template
    public $name = 'Nuno';

    // Color used in inline CSS
    public $inlineCssColor = '#AABBCC';
};

?>

<volt-template>
<div class="InfoComponent">
    <div class="card">
        The name is <span>{ $name }}</span>
    </div>
</div>
</volt-template>

<volt-style lang="scss">
div.InfoComponent {
    div.card {
        border: solid 1px gray;
        color: #333333;
    }
}
</volt-style>

<volt-style lang="scss" inline>
// Forced css to be added inline to allow inline variables from PHP.
div.InfoComponent div.card span {
    color: {{ $inlineCssColor }};
}
</volt-style>

<volt-script>
console.log("log instantly each time component is used");
</volt-script>

<volt-script runOnce runOnDomReady>
console.log("Only run once, if dom is ready");
</volt-script>

Existing extension for Laravel Mix

https://laravel-mix.com/extensions/single-file-blade-components

driesvints commented 1 month ago

Thanks. this is quite the undertaking. Right now we don't have plans to work on this sorry.