| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Widget; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Html\HtmlElement; |
| 8 |
use ipl\Html\Text; |
| 9 |
use ipl\Html\ValidHtml; |
| 10 |
use ipl\Web\Common\CalloutType; |
| 11 |
|
| 12 |
/** |
| 13 |
* Information box with a type specific color and icon |
| 14 |
* |
| 15 |
* The type controls both the color scheme and the icon. An optional title |
| 16 |
* is displayed above the content. |
| 17 |
*/ |
| 18 |
class Callout extends BaseHtmlElement |
| 19 |
{ |
| 20 |
/** @var string Class name for fit content callouts */ |
| 21 |
protected const CLASS_FIT_CONTENT = 'callout-fit-content'; |
| 22 |
|
| 23 |
protected $tag = 'div'; |
| 24 |
|
| 25 |
protected $defaultAttributes = ['class' => 'callout']; |
| 26 |
|
| 27 |
/** |
| 28 |
* Create a new callout |
| 29 |
* |
| 30 |
* The $type parameter determines the color and icon of the callout. |
| 31 |
* |
| 32 |
* @param CalloutType $type The type of the callout |
| 33 |
* @param ValidHtml|string $content The content of the callout |
| 34 |
* @param ?string $title An optional title, displayed above the content |
| 35 |
*/ |
| 36 |
public function __construct( |
| 37 |
protected CalloutType $type, |
| 38 |
protected ValidHtml|string $content, |
| 39 |
protected ?string $title = null |
| 40 |
) { |
| 41 |
$this->addAttributes(Attributes::create(['class' => $type->value])); |
| 42 |
} |
| 43 |
|
| 44 |
protected function assemble(): void |
| 45 |
{ |
| 46 |
$this->addHtml($this->type->getIcon()); |
| 47 |
|
| 48 |
$this->addHtml(HtmlElement::create( |
| 49 |
'div', |
| 50 |
['class' => 'callout-text'], |
| 51 |
[ |
| 52 |
$this->title === null || trim($this->title) === '' |
| 53 |
? null |
| 54 |
: HtmlElement::create('strong', ['class' => 'callout-title'], Text::create($this->title)), |
| 55 |
is_string($this->content) ? Text::create($this->content) : $this->content, |
| 56 |
], |
| 57 |
)); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set the callout width to 100% of its parent container |
| 62 |
* |
| 63 |
* Callouts are by default sized to fill their parent container. |
| 64 |
* |
| 65 |
* @param bool $isFitContent Whether the callout size should be dependent on its content |
| 66 |
* |
| 67 |
* @return $this |
| 68 |
*/ |
| 69 |
public function setFitContent(bool $isFitContent = true): static |
| 70 |
{ |
| 71 |
if ($isFitContent) { |
| 72 |
$this->addAttributes(Attributes::create(['class' => static::CLASS_FIT_CONTENT])); |
| 73 |
} else { |
| 74 |
$this->removeAttribute('class', static::CLASS_FIT_CONTENT); |
| 75 |
} |
| 76 |
|
| 77 |
return $this; |
| 78 |
} |
| 79 |
} |