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