Skip to content

Commit

Permalink
because of lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Aug 30, 2023
1 parent 3c76d61 commit 50ba488
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Components/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Image extends HtmlComponent
{
public $thumbnail = false;
public $lazy = false;
public $fluid = false;
public $placeholder = false;
public $css = '';
Expand All @@ -16,6 +17,7 @@ class Image extends HtmlComponent

public function __construct(
$thumbnail = false,
$lazy = false,
$fluid = false,
$placeholder = false,
$css = '',
Expand All @@ -24,6 +26,7 @@ public function __construct(
) {
$this->thumbnail = $thumbnail;
$this->fluid = $fluid;
$this->lazy = $lazy;
$this->placeholder = $placeholder;
$this->css = $css;
$this->alt = $alt;
Expand All @@ -41,6 +44,10 @@ public function render()
$image = $image->fluid();
}

if ($this->lazy) {
$image = $image->lazy();
}

if ($this->placeholder) {
$image = $image->placeholder();
}
Expand Down
15 changes: 14 additions & 1 deletion src/Tags/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Image extends HtmlComponent
public static $thumbnail = false;
public static $fluid = false;
public static $placeholder = false;
public static $lazy = false;
public static $css = '';
public static $alt = '';
public static $source;
Expand Down Expand Up @@ -41,6 +42,13 @@ public static function thumbnail()
return new static();
}

public static function lazy()
{
self::$lazy = true;

return new static();
}

public static function placeholder()
{
self::$placeholder = true;
Expand All @@ -58,10 +66,15 @@ public static function fluid()
public static function process()
{
$html = '';
$lazy = '';
$alt = self::$alt;
$source = self::$source;
$class = self::$css;

if (self::$lazy) {
$lazy = ' loading="lazy"';
}

if (self::$thumbnail) {
$class .= " img-thumbnail";
}
Expand All @@ -75,7 +88,7 @@ public static function process()
}

$class = trim($class);
$html .= "<img class=\"{$class}\" src=\"{$source}\" alt=\"{$alt}\" />";
$html .= "<img {$lazy} class=\"{$class}\" src=\"{$source}\" alt=\"{$alt}\" />";

if (self::$placeholder) {
$html .= '</div>';
Expand Down

0 comments on commit 50ba488

Please sign in to comment.