Custom Row Classes - How to assign a different background color to each row (Category)

Hello. I have a resource where the user assigns a special color of his choice (with ColorPicker::make()) and I would like in the list (table) to show every row with that color in the background (opacity 50%). I have tried, but it is not working because it seems that only tailwind predefined classes can be used. I have used these examples in: https://filamentphp.com/docs/3.x/tables/advanced#custom-row-classes and changed to:
->recordClasses(fn (Category $record) => match ($record->color) {
default => 'background-color: ' . $record->color,
})
->recordClasses(fn (Category $record) => match ($record->color) {
default => 'background-color: ' . $record->color,
})
Any ideas whether this can be possible? Thank you.
16 Replies
Mark Chaney
Mark Chaney16mo ago
@Albert Lens have you dumped your $record->color value to see what that value is? The way you are using it, it would have to be a hex value, or rgb(....) nvm, cant be used that way, you are doing a style instead of a class name so obviously class="background-color: #fffff" isnt going be valid
Albert Lens
Albert LensOP16mo ago
I see. And how could I use that #fffff hex color and use it as the background of the row? Is there a ->recordCss() function or ->recordHtml() ? to apply those html or css attributes to the rows? Tks.
Mark Chaney
Mark Chaney16mo ago
personally i dont think color pickers are a good idea as you dont just want any color to be chosen. Remember that you typically want it to work with both dark mode and light mode, then you need your different shades, etc. I think its better to only have X amount of preselected ones. Plus any color can also make your app look awful
Mark Chaney
Mark Chaney16mo ago
Im personally doing something more like this, but you are right, its still a pain unless you have all the color classes already available. Ive been trying to figure out how to use the new Color class, but every component is setup to mainly uses class names, so i dont see how its going to solve those types
awcodes
awcodes16mo ago
You can use Color::hex() and it’ll generate the shades for you.
Mark Chaney
Mark Chaney16mo ago
yes, but thats still just set of rgb values. doesnt help you with all the components that are setup to only accept hex or classes (mostly classes)
awcodes
awcodes16mo ago
Those worthless rgb values work with the css ‘—c-‘ custom properties. In the theme. Would still need to be able to apply styles though instead of classes for this particular use case.
Mark Chaney
Mark Chaney16mo ago
and every other component from what im seeing. Heck, the ColorColumn requires a hex or rgb value, but you even ahve to go as far as providing rgb(.....), not just the 3 comma separated integers that you get back from Color
awcodes
awcodes16mo ago
have a look at some of the components to see how it works:
$buttonStyles = \Illuminate\Support\Arr::toCssStyles([
\Filament\Support\get_color_css_variables($color, shades: [400, 500, 600]) => $color !== 'gray',
]);
$buttonStyles = \Illuminate\Support\Arr::toCssStyles([
\Filament\Support\get_color_css_variables($color, shades: [400, 500, 600]) => $color !== 'gray',
]);
then the classes would be something along these lines:
text-custom-600 ring-custom-600 hover:bg-custom-400/10 dark:text-custom-400 dark:ring-custom-500
text-custom-600 ring-custom-600 hover:bg-custom-400/10 dark:text-custom-400 dark:ring-custom-500
Mark Chaney
Mark Chaney16mo ago
so how does that help @Albert Lens provide custom colors to a method that requires a class name or any other class that requires that? i get that its useful to plugin developers
Dennis Koch
Dennis Koch16mo ago
The output is class names?!
awcodes
awcodes16mo ago
I was just explaining how the colors work. in the case of a table row though, since there is no way to apply styles to them then you'd have to try to do something at the CSS level and not the PHP level.
Mark Chaney
Mark Chaney16mo ago
@Albert Lens work a try, but have you tried someting like bg-[#21B8A6] as the returned class name for the row? see if that works statically and if so, then maybe you can dynamically set the hex value
Albert Lens
Albert LensOP16mo ago
I'll try and feedback. Tks It does not work. Sorry. Don't worry. I think I will have to change the approach and convince the client to do differently.
Mark Chaney
Mark Chaney16mo ago
@Albert Lens yep, i just realized those still have to be accessed when running builing out the app.css, so since its not in a blade, it wont be seen since its dynamic
Albert Lens
Albert LensOP16mo ago
I'll make a different approach letting them choose from a list of preconfigured set of colors
Want results from more Discord servers?
Add your server