Installation:
composer require awcodes/preset-color-picker
npm run build # After updating tailwind.config.js
First Use Case:
Add the field to a Filament form (e.g., CreatePost or EditPost):
use Awcodes\PresetColorPicker\PresetColorPicker;
PresetColorPicker::make('color')
->colors([
Color::Red,
Color::Blue,
Color::Green,
])
->default(Color::Blue),
Where to Look First:
resources/views in the package for template overrides.Dynamic Color Palettes: Fetch colors from a database or API and pass them as an array:
$colors = Color::query()->pluck('hex')->toArray();
PresetColorPicker::make('theme_color')->colors($colors),
Theming Across Forms: Reuse a predefined palette in multiple forms via a helper:
function getBrandColors(): array {
return [Color::Blue500, Color::Green500, Color::Purple500];
}
Conditional Palettes: Use closures to dynamically adjust colors based on context (e.g., user role):
PresetColorPicker::make('accent_color')
->colors(fn () => auth()->user()->isAdmin()
? [Color::Red, Color::Black]
: [Color::Gray, Color::White]),
Integration with Filament Tables: Use the field in table actions or bulk operations:
use Filament\Tables\Actions\Action;
Action::make('updateColor')
->form([
PresetColorPicker::make('new_color')
->colors([Color::Yellow, Color::Orange]),
]),
Localization: Translate color labels (e.g., for multilingual apps):
PresetColorPicker::make('primary_color')
->colors([
Color::Red->label(__('Primary.Red')),
Color::Blue->label(__('Primary.Blue')),
]),
Custom CSS: Override styles via tailwind.config.js or a custom CSS file:
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
custom: {
'brand-1': '#1a365d',
'brand-2': '#2d3748',
},
},
},
},
};
Then use:
PresetColorPicker::make('brand_color')
->colors([
Color::Custom('brand-1'),
Color::Custom('brand-2'),
]),
Validation: Combine with Filament’s validation rules:
PresetColorPicker::make('background_color')
->required()
->colors([Color::White, Color::Black])
->rules(['required', 'in:'.implode(',', [Color::White->value, Color::Black->value])]),
Livewire/Alpine.js: Trigger events on color selection:
PresetColorPicker::make('notification_color')
->colors([Color::Green, Color::Red])
->extraAttributes(['x-data' => '{ selectedColor: null }'])
->extraAttributes(['x-on:selected-color.window' => 'selectedColor = $event.detail']),
Theme Compatibility:
tailwind.config.js includes the package’s views and run npm run build after updates.Unrecognized color: #hex).Color Object Mismatch:
Color objects (e.g., '#ff0000' vs Color::Red).Color::* constants or Color::make() for consistency.Color objects:
PresetColorPicker::make('color')
->colors(array_map(fn ($hex) => Color::make($hex), ['#ff0000', '#00ff00'])),
Performance with Large Palettes:
Default Value Conflicts:
default() may not work if the color isn’t in the palette.Tailwind Conflicts:
Color::Custom() with unique names (e.g., Color::Custom('app-brand-blue')).Inspect Rendered HTML: Use browser dev tools to verify the color palette structure:
<!-- Expected structure -->
<div class="preset-color-picker">
<button data-color="#ff0000" style="background-color: #ff0000;"></button>
<!-- ... -->
</div>
Log Color Data: Debug the palette array before passing it to the field:
$colors = [Color::Red, Color::Blue];
\Log::debug('Color palette:', $colors);
Clear Cache: After updating the package or theme:
php artisan filament:cache-reset
php artisan view:clear
npm run build
Custom Templates: Override the default Blade template:
cp vendor/awcodes/preset-color-picker/resources/views/fields/preset-color-picker.blade.php resources/views/vendor/filament/fields/preset-color-picker.blade.php
Modify the template to add features like:
Dynamic Color Generation: Extend the package to generate palettes programmatically:
use Awcodes\PresetColorPicker\Color;
class PaletteGenerator {
public static function generateMonochrome(int $shades = 5): array {
$base = Color::Gray;
return collect(range(100, 900, (900 - 100) / ($shades - 1)))
->map(fn ($shade) => Color::Gray->shade($shade))
->toArray();
}
}
Color History:
Track selected colors across sessions using middleware or Filament’s getSession():
PresetColorPicker::make('color')
->default(session('last_color_selection', Color::Blue)),
Accessibility: Ensure color contrast meets WCAG standards by validating palettes:
use Awcodes\PresetColorPicker\Color;
function isAccessible(array $colors, string $backgroundColor = '#ffffff'): bool {
foreach ($colors as $color) {
$contrast = calculateContrast($color->hex, $backgroundColor);
if ($contrast < 4.5) return false;
}
return true;
}
Server-Side Validation:
Validate color selections in the save() method of your resource:
public function save(): void {
$this->validateColorPalette();
parent::save();
}
protected function validateColorPalette(): void {
$allowedColors = [Color::Red->value, Color::Blue->value];
if (!in_array($this->color, $allowedColors)) {
throw ValidationException::withMessages([
'color' => 'Invalid color selected.',
]);
}
}
How can I help you explore Laravel packages today?