Skip to content

Instantly share code, notes, and snippets.

@lukeraymonddowning
Created March 26, 2021 09:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeraymonddowning/a6151d5ca9abf580307c91b1f4241ee9 to your computer and use it in GitHub Desktop.
Save lukeraymonddowning/a6151d5ca9abf580307c91b1f4241ee9 to your computer and use it in GitHub Desktop.
Tailwind specificity helper for Laravel Blade
ComponentAttributeBag::macro('fallback', function($classes) {
$fallbacks = Collection::wrap($classes)
->mapWithKeys(fn($partial, $fallback) => is_int($fallback) ? [$partial => Str::of($partial)->before("-")->when(Str::contains("-", $partial), fn($string) => $string->append('-'))->__toString()] : [$fallback => $partial])
->toArray();
return $this->class($fallbacks);
});
@lukeraymonddowning
Copy link
Author

Usage:

<div {{ $attributes->fallback(['rounded-md', 'p-3', 'bg-white']) }}>{{ $slot }}</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment