Question:
Fix TailwindCSS classes not being recognized issue: Laravel

Solution

When you go through>> >the documentation of the TailwindCSS you will notice that it only finds classes that are completely unbroken strings in your source files. 


In case you are using both string interpolation or concatenate partial class names, Tailwind will be unable to find class names. Plus, it will not generate the corresponding CSS:


Make sure that you do not construct class names dynamically


<div class="text-{{ error ? 'red' : 'green' }}-600"></div>


Now, as you can see in the above example that the strings text-red-600 and text-green-600 are missing. Therefore, Tailwind will not generate those classes. Make sure that you use the full class name. Look at the following example:


<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>


You can consider using the full class names like the following::


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


>Safelisting the classes you will use:


/** @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?


Nisha Patel

Nisha Patel

Submit
0 Answers