Fix TailwindCSS classes not being recognized issue
Problem:
TailwindCSS classes not being recognized while using a switch function in Laravel 10 model
I'm using a switch function in my Listing model to control the TailwindCSS classes based on the field value. I've tried adding all the Tailwind color classes to the tailwind.config file but that didn't work. When I inspect the elements on the page, the classes are populated to the page, but not recognized.
Listing.php
... public function getStatusClass() { switch ($this->listing_status) { case 'Draft': return 'slate'; case 'Active': return 'green'; case 'Sold': return 'red'; case 'Under Contract': return 'orange'; case 'Pending': return 'purple'; default: return 'gray'; // Default color for unknown status } } ... |
components/partials/listing-desktop.blade.php
@foreach($listings as $listing) ... <td class="px-4 py-3 whitespace-nowrap"> <span class="bg-{{ $listing->getStatusClass() }}-100 text-{{ $listing->getStatusClass() }}-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-{{ $listing->getStatusClass() }}-900 dark:text-{{ $listing->getStatusClass() }}-300">{{ $listing->listing_status }}</span> </td> ... @endforeach |
Solution:
As per >the documentation:
The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files.
If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS:
Don’t construct class names dynamically
<div class="text-{{ error ? 'red' : 'green' }}-600"></div> |
In the example above, the strings text-red-600 and text-green-600 do not exist, so Tailwind will not generate those classes. Instead, make sure any class names you’re using exist in full:
Always use complete class names
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div> |
You could consider:
public function getStatusClass() { switch ($this->listing_status) { case 'Draft': return 'bg-slate-100 text-slate-800 dark:bg-slate-900 dark:text-slate-300'; case 'Active': return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300'; case 'Sold': return 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300'; case 'Under Contract': return 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-300'; case 'Pending': return 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300'; default: return 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-300'; // Default color for unknown status } } |
@foreach($listings as $listing) ... <td class="px-4 py-3 whitespace-nowrap"> <span class="{{ $listing->getStatusClass() }} text-xs font-medium mr-2 px-2.5 py-0.5 rounded">{{ $listing->listing_status }}</span> </td> ... @endforeach |
/** @type {import('tailwindcss').Config} */ module.exports = { safelist: [ { pattern: '^bg-(slate|green|red|orange|purple|gray)-100$' }, { pattern: '^text-(slate|green|red|orange|purple|gray)-800$' }, { pattern: '^bg-(slate|green|red|orange|purple|gray)-900$', variants: ['dark'] }, { pattern: '^text-(slate|green|red|orange|purple|gray)-300$', variants: ['dark'] }, ] // … } |
Suggested blogs:
>How can you read a Blob in Deno in TypeScript?
>How to do Yup validation in form with TypeScript?
>How can I merge two arrays of objects that can be undefined in TypeScript?
>Javascript Error Solved: Property 'id' does not exist on type 'T'
>How to make sticky div remain stuck in JavaScript?
>How to manipulate manipulating Array object in JavaScript?
>How to do light and dark mode in a website using HTML and JavaScript?
>How to fix mouseover event glitch in JavaScript?
>Activating Virtual environment in visual studio code windows 11
>Fix webapp stops working issue in Django- Python webapp
>Creating a form in Django to upload a picture from website
>Sending Audio file from Django to Vue
>How to keep all query parameters intact when changing page in Django?