Skip to content

Instantly share code, notes, and snippets.

@cododel
Created October 27, 2024 14:08
Show Gist options
  • Save cododel/6383973eb0a4997c0271e9c2eda8b2fd to your computer and use it in GitHub Desktop.
Save cododel/6383973eb0a4997c0271e9c2eda8b2fd to your computer and use it in GitHub Desktop.
Tailwind PHP
<?php
namespace App\Services\HTML\Modules\Tailwind;
trait InteractsWithTailwind
{
abstract public function class(string $class): static;
// Text utilities
public function bold()
{
return $this->class('font-bold');
}
public function italic()
{
return $this->class('italic');
}
public function underline()
{
return $this->class('underline');
}
public function textCenter()
{
return $this->class('text-center');
}
public function textLeft()
{
return $this->class('text-left');
}
public function textRight()
{
return $this->class('text-right');
}
// Color utilities
public function textRed($shade = 500)
{
return $this->class("text-red-$shade");
}
public function textBlue($shade = 500)
{
return $this->class("text-blue-$shade");
}
public function bgRed($shade = 500)
{
return $this->class("bg-red-$shade");
}
public function bgBlue($shade = 500)
{
return $this->class("bg-blue-$shade");
}
// Margin and padding utilities
public function p($size = 4)
{
return $this->class("p-$size");
}
public function m($size = 4)
{
return $this->class("m-$size");
}
public function px($size = 4)
{
return $this->class("px-$size");
}
public function py($size = 4)
{
return $this->class("py-$size");
}
public function mx($size = 4)
{
return $this->class("mx-$size");
}
public function my($size = 4)
{
return $this->class("my-$size");
}
// Display utilities
public function flex()
{
return $this->class('flex');
}
public function grid()
{
return $this->class('grid');
}
public function hidden()
{
return $this->class('hidden');
}
public function block()
{
return $this->class('block');
}
public function inlineBlock()
{
return $this->class('inline-block');
}
// Width and height utilities
public function wFull()
{
return $this->class('w-full');
}
public function hFull()
{
return $this->class('h-full');
}
public function w($size)
{
return $this->class("w-$size");
}
public function h($size)
{
return $this->class("h-$size");
}
// Border utilities
public function border()
{
return $this->class('border');
}
public function borderColor($color = 'gray-500')
{
return $this->class("border-$color");
}
public function borderWidth($width = 1)
{
return $this->class("border-$width");
}
public function rounded($size = 'md')
{
return $this->class("rounded-$size");
}
// Flexbox utilities
public function justifyCenter()
{
return $this->class('justify-center');
}
public function itemsCenter()
{
return $this->class('items-center');
}
public function justifyBetween()
{
return $this->class('justify-between');
}
public function itemsStart()
{
return $this->class('items-start');
}
// Position utilities
public function absolute()
{
return $this->class('absolute');
}
public function relative()
{
return $this->class('relative');
}
public function fixed()
{
return $this->class('fixed');
}
public function top($value)
{
return $this->class("top-$value");
}
public function left($value)
{
return $this->class("left-$value");
}
// Shadow utilities
public function shadow($size = 'md')
{
return $this->class("shadow-$size");
}
}
<?php
namespace App\Services\HTML\Modules\Tailwind;
use App\Services\HTML\HTMLElement;
use App\Services\HTML\Modules\Module;
use Closure;
class Tailwind extends Module
{
use InteractsWithTailwind;
public $element;
public function __construct(HTMLElement $element)
{
$this->element = $element;
}
public static function getModuleName(): string
{
return 'tailwind';
}
/** Adapter method to call the class method from the InteractsWithTailwind trait */
protected function class(string $class): static
{
$this->element->class($class);
return $this;
}
public static function getCallback(HTMLElement $element): callable
{
$instance = new static($element);
return function (string|Closure $arg) use ($instance) {
if (is_callable($arg)) {
$arg($instance);
} else {
$instance->class($arg);
}
return $instance->element;
};
}
}
@cododel
Copy link
Author

cododel commented Oct 27, 2024

Планировалось как встраиваемый модуль в библиотеку рендеринга, но не учел, что Tailwind не парсит подобные структуры:

    Table::registerModule(Tailwind::class);

    $table = Table::make([
      Row::make([
        Cell::make("Финансовый результат по видам деятельности, связанным с исполнением договора №")
          ->tailwind(
            fn(Tailwind $t) => $t
              ->bgBlue(300)
          )
          ->width(200)
          ->colSpan(2),
        Cell::make($contract_number),
        Cell::make("от"),
        Cell::make($contract_date),
      ]),
      Row::make([
        Cell::make("Параметры: ИГК")->colSpan(2),
        Cell::make($igk),
      ])
  ]);

Ну и будто это бесполезный модуль, так как есть fluent интерфейс ->class("bg-blue-300")
Плюс придется делать стилизовать страницу вручную из-за первой причины

@cododel
Copy link
Author

cododel commented Oct 30, 2024

Subscribe to my telegram channel, don’t miss new interesting solutions

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