2

I created a Flash livewire component, it works pretty fine bu I'm having a weird issue with a specific div when trying to change dynamically its class value it appears that the properties {$type and $colors} are empty (but only in that div, outside it's not empty), I'm not sure what is preventing those properties from being recognized! Please this is the blade file:

<div x-data="{ open: false }" @flash-message.window="open = true; setTimeout(() => open = false ,7000);">
    <div x-show="open" x-cloak class="border px-2 py-2 mb-2 rounded {{ $type ? $colors[$type] : '' }}" >
        {{ $type ? $colors[$type] : '' }}{{ $message }}
    </div>
</div>

the Flash class component :

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Flash extends Component
{
    public $message;
    public $type;
    public $colors = [
        'error' => 'border-red-700 text-red-700 bg-red-200',
        'success' => 'border-green-700 text-green-700 bg-green-200',
        'warning' => 'border-orange-700 text-orange-700 bg-orange-200',
        'info' => 'border-blue-700 text-blue-700 bg-blue-200'
    ];
    protected $listeners = ['flash' => 'setFlashMessage'];

    /**
     * Set the message & type to be displayed on the event 
     *
     * @param string $message
     * @param string $type
     * @return void
     */
    public function setFlashMessage( $message,  $type)
    {
        $this->message = $message;
        $this->type = $type;

        $this->dispatchBrowserEvent('flash-message');
    }

    public function render()
    {   
        return view('livewire.flash');
    }
}

And it should be displaying the div styled as following: How it should the div be styled but in contrary it's displaying the model as following: Bad styled div

Can anyone check that code and find what's missing or what's wrong with it?

Thank you!

4
  • You have quotes around your variable '$colors[$type]' so it's treated as a string and not interpreted as a variable Commented Sep 4, 2022 at 14:04
  • Thank you for your feedback but even without them it keeps displaying an empty string! Commented Sep 4, 2022 at 17:16
  • Are you using Tailwind 3? Commented Sep 5, 2022 at 7:46
  • @Yinci Yes I'm using it! Commented Sep 5, 2022 at 8:12

1 Answer 1

0

After several temptation I finally was able to handle/bypass this issue, by bringing out the condition outside of that specific div as following:

<div x-data="{ open: false }" @flash-message.window="open = true; setTimeout(() => open = false ,7000);">
    @if ($type)
        <div x-show="open" x-cloak class="border px-2 py-2 mb-2 rounded {{ $colors[$type] }}" >
            {{ $message }}
        </div>
    @endif
</div>

This is how I've been able to display properly that: enter image description here If someone in the future may know the reason why the way in the question wasn't working properly and update this solution that would be great!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.