Final Class Yiisoft\Bootstrap5\Toast
| Inheritance | Yiisoft\Bootstrap5\Toast » Yiisoft\Widget\Widget |
|---|
Toasts renders a toast bootstrap widget.
For example,
echo Toast::widget()
->body('Hello, world! This is a toast message.')
->image('https://example.com/150', 'Bootstrap5', ['class' => 'rounded me-2'])
->time('11 minutes ago')
->title('Bootstrap')
->render();
See also https://getbootstrap.com/docs/5.0/components/toasts/.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addAttributes() | Adds a set of attributes. | Yiisoft\Bootstrap5\Toast |
| addClass() | Adds one or more CSS classes to the existing classes. | Yiisoft\Bootstrap5\Toast |
| addCloseButtonAttribute() | Adds close button attribute value. | Yiisoft\Bootstrap5\Toast |
| addCloseButtonClass() | Adds one or more CSS classes to the existing close button classes. | Yiisoft\Bootstrap5\Toast |
| addCloseButtonCssStyle() | Adds a close button CSS style. | Yiisoft\Bootstrap5\Toast |
| addCssStyle() | Adds a CSS style. | Yiisoft\Bootstrap5\Toast |
| attribute() | Sets attribute value. | Yiisoft\Bootstrap5\Toast |
| attributes() | Sets the HTML attributes. | Yiisoft\Bootstrap5\Toast |
| body() | Sets the body content. | Yiisoft\Bootstrap5\Toast |
| class() | Replaces all existing CSS classes with the specified one(s). | Yiisoft\Bootstrap5\Toast |
| closeButton() | Sets the close button for the header section. | Yiisoft\Bootstrap5\Toast |
| container() | Whether to render container <div> tag. |
Yiisoft\Bootstrap5\Toast |
| content() | Sets custom content. | Yiisoft\Bootstrap5\Toast |
| headerAttributes() | Sets the HTML attributes for the header section. | Yiisoft\Bootstrap5\Toast |
| id() | Sets the ID. | Yiisoft\Bootstrap5\Toast |
| image() | Sets the image for the header section. | Yiisoft\Bootstrap5\Toast |
| render() | Run the widget. | Yiisoft\Bootstrap5\Toast |
| time() | Sets the time display for the header section. | Yiisoft\Bootstrap5\Toast |
| title() | Sets the title for the header section. | Yiisoft\Bootstrap5\Toast |
| triggerButton() | Sets the trigger button for displaying the toast. | Yiisoft\Bootstrap5\Toast |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| CLASS_CLOSE_BUTTON | 'btn-close' | Yiisoft\Bootstrap5\Toast | |
| NAME | 'toast' | Yiisoft\Bootstrap5\Toast | |
| TOAST_BODY | 'toast-body' | Yiisoft\Bootstrap5\Toast | |
| TOAST_CONTAINER | 'toast-container position-fixed bottom-0 end-0 p-3' | Yiisoft\Bootstrap5\Toast | |
| TOAST_HEADER | 'toast-header' | Yiisoft\Bootstrap5\Toast | |
| TOAST_TITLE_HEADER | 'me-auto' | Yiisoft\Bootstrap5\Toast |
Method Details
Adds a set of attributes.
| public self addAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. for example, |
| return | self |
A new instance with the specified attributes added. Example usage:
|
|---|---|---|
public function addAttributes(array $attributes): self
{
$new = clone $this;
$new->attributes = [...$this->attributes, ...$attributes];
return $new;
}
Adds one or more CSS classes to the existing classes.
| public self addClass ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to add. Pass |
| return | self |
A new instance with the specified CSS classes added to existing ones. |
|---|---|---|
public function addClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->cssClasses = [...$this->cssClasses, ...$class];
return $new;
}
Adds close button attribute value.
| public self addCloseButtonAttribute ( string $name, mixed $value ) | ||
| $name | string |
The attribute name. |
| $value | mixed |
The attribute value. |
| return | self |
A new instance with the specified attribute added. Example usage:
|
|---|---|---|
public function addCloseButtonAttribute(string $name, mixed $value): self
{
$new = clone $this;
$new->closeButtonAttributes[$name] = $value;
return $new;
}
Adds one or more CSS classes to the existing close button classes.
Multiple classes can be added by passing them as separate arguments. null values are filtered out
automatically.
| public self addCloseButtonClass ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to add. Pass |
| return | self |
A new instance with the specified CSS classes added to existing ones. |
|---|---|---|
public function addCloseButtonClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
foreach ($class as $item) {
Html::addCssClass($new->closeButtonAttributes, $item);
}
return $new;
}
Adds a close button CSS style.
| public self addCloseButtonCssStyle ( array|string $style, boolean $overwrite = true ) | ||
| $style | array|string |
The CSS style. If the value is an array, a space will separate the values.
For example, |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// or
$toast->addCloseButtonCssStyle(['color' => 'red', 'font-weight' => 'bold']);
|
|---|---|---|
public function addCloseButtonCssStyle(array|string $style, bool $overwrite = true): self
{
$new = clone $this;
Html::addCssStyle($new->closeButtonAttributes, $style, $overwrite);
return $new;
}
Adds a CSS style.
| public self addCssStyle ( array|string $style, boolean $overwrite = true ) | ||
| $style | array|string |
The CSS style. If the value is an array, the values will be separated by a space.
e.g., |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// or
$toast->addCssStyle(['color' => 'red', 'font-weight' => 'bold']);
|
|---|---|---|
public function addCssStyle(array|string $style, bool $overwrite = true): self
{
$new = clone $this;
Html::addCssStyle($new->attributes, $style, $overwrite);
return $new;
}
Sets attribute value.
| public self attribute ( string $name, mixed $value ) | ||
| $name | string |
The attribute name. |
| $value | mixed |
The attribute value. |
| return | self |
A new instance with the specified attribute set. Example usage:
|
|---|---|---|
public function attribute(string $name, mixed $value): self
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
Sets the HTML attributes.
| public self attributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes. |
|---|---|---|
public function attributes(array $attributes): self
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
Sets the body content.
See also https://getbootstrap.com/docs/5.3/components/toasts/#basic.
| public self body ( string|\Stringable $content, array $attributes = [], \BackedEnum|string|null $class ) | ||
| $content | string|\Stringable |
The body content. If a string, it will be wrapped in a |
| $attributes | array |
Additional HTML attributes for the |
| $class | \BackedEnum|string|null |
CSS class names to add to the |
| return | self |
A new instance with the specified body content. Example usage:
// With additional attributes $toast->body('Custom toast message', ['data-id' => '123']); // With additional classes $toast->body('Custom toast message', [], 'text-primary', 'p-4'); // Using a Stringable object (pre-rendered HTML)
$toast->body($customBodyHtml);
|
|---|---|---|
public function body(string|Stringable $content, array $attributes = [], BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->body = is_string($content)
? Div::tag()
->addAttributes($attributes)
->addClass(
self::TOAST_BODY,
...$class,
)
->content(
"\n",
$content,
"\n",
)
->render()
: (string) $content;
return $new;
}
Replaces all existing CSS classes with the specified one(s).
| public self class ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to set. Pass |
| return | self |
A new instance with the specified CSS classes set. Example usage:
|
|---|---|---|
public function class(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->cssClasses = $class;
return $new;
}
Sets the close button for the header section.
| public self closeButton ( string|\Stringable $content = '' ) | ||
| $content | string|\Stringable |
The close button. If empty, a default close button will be rendered. If provided, this content will be used instead of the default button. |
| return | self |
A new instance with the specified close button for the header section. Example usage:
// Label for the close button $toast->closeButton('Close'); // Custom close button content $toast->closeButton('×'); // Using a Stringable object
$toast->closeButton($customButtonHtml);
|
|---|---|---|
public function closeButton(string|Stringable $content = ''): self
{
$new = clone $this;
$new->closeButton = $content;
return $new;
}
Whether to render container <div> tag.
| public self container ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to render container |
| return | self |
A new instance with the specified container setting. Example usage:
|
|---|---|---|
public function container(bool $enabled): self
{
$new = clone $this;
$new->container = $enabled;
return $new;
}
Sets custom content.
| public self content ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The custom content to display. This will replace all standard content rendering. Multiple parameters will be joined with newlines. |
| return | self |
A new instance with the specified custom content. Example usage:
My custom toast implementation ');
// Using multiple strings $toast->content(
); // Using a Stringable object
$toast->content($customToastHtml);
|
|---|---|---|
public function content(string|Stringable ...$content): self
{
$new = clone $this;
$new->content = implode("\n", $content);
return $new;
}
Sets the HTML attributes for the header section.
| public self headerAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the header section. |
|---|---|---|
public function headerAttributes(array $attributes): self
{
$new = clone $this;
$new->headerAttributes = $attributes;
return $new;
}
Sets the ID.
| public self id ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the component. If |
| return | self |
A new instance with the specified ID. Example usage:
|
|---|---|---|
public function id(bool|string $id): self
{
$new = clone $this;
$new->id = $id;
return $new;
}
Sets the image for the header section.
| public self image ( string|\Stringable $content, string $alt = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
The image content. If a string, it will be treated as the URL for the image source. If a Stringable object, it will be treated as the HTML content for the image. |
| $alt | string |
The alt attribute for the |
| $attributes | array |
Additional HTML attributes for the |
| return | self |
A new instance with the specified image for the header section. Example usage:
// Using a Stringable object
$toast->image($customImageHtml);
|
|---|---|---|
public function image(string|Stringable $content, string $alt = '', array $attributes = []): self
{
$new = clone $this;
$new->image = is_string($content)
? Img::tag()->addAttributes($attributes)->alt($alt)->src($content)->render()
: (string) $content;
return $new;
}
Run the widget.
| public string render ( ) | ||
| return | string |
The HTML representation of the element. |
|---|---|---|
public function render(): string
{
if ($this->body === '' && $this->content === '') {
return '';
}
return match ($this->container) {
true => $this->triggerButton . Div::tag()
->addClass(self::TOAST_CONTAINER)
->content(
"\n",
$this->renderToast(),
"\n",
)
->encode(false)
->render(),
default => $this->renderToast(),
};
}
Sets the time display for the header section.
| public self time ( string|\Stringable $content, array $attributes = [], \BackedEnum|string|null $class ) | ||
| $content | string|\Stringable |
The time content. If a string, it will be wrapped in a |
| $attributes | array |
Additional HTML attributes for the |
| $class | \BackedEnum|string|null |
CSS class names to add to the |
| return | self |
A new instance with the specified time display for the header section. Example usage:
// With additional classes $toast->time('11 minutes ago', [], 'text-body-secondary'); // With attributes $toast->time('11 minutes ago', ['data-id' => '123']); // Using a Stringable object
$toast->time($customTimeHtml);
|
|---|---|---|
public function time(string|Stringable $content, array $attributes = [], BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->time = is_string($content)
? Small::tag()->addAttributes($attributes)->addClass(...$class)->content($content)->render()
: (string) $content;
return $new;
}
Sets the title for the header section.
| public self title ( string|\Stringable $content, array $attributes = [], \BackedEnum|string|null $class ) | ||
| $content | string|\Stringable |
The title content. If a string, it will be wrapped in a |
| $attributes | array |
Additional HTML attributes for the |
| $class | \BackedEnum|string|null |
CSS class names to add to the |
| return | self |
A new instance with the specified title for the header section. Example usage:
// With additional classes $toast->title('Bootstrap', [], 'text-primary'); // With attributes $toast->title('Bootstrap', ['data-id' => '123']); // Using a Stringable object
$toast->title($customTitleHtml);
|
|---|---|---|
public function title(string|Stringable $content, array $attributes = [], BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->title = is_string($content)
? Strong::tag()
->addAttributes($attributes)
->addClass(
self::TOAST_TITLE_HEADER,
...$class,
)
->content($content)
->render()
: (string) $content;
return $new;
}
Sets the trigger button for displaying the toast.
| public self triggerButton ( string|\Stringable $content = 'Show live toast', array $attributes = [] ) | ||
| $content | string|\Stringable |
The content of the trigger button. |
| $attributes | array |
The HTML attributes for the trigger button. |
| return | self |
A new instance with the specified trigger button for displaying the toast. Example usage:
|
|---|---|---|
public function triggerButton(string|Stringable $content = 'Show live toast', array $attributes = []): self
{
if (is_string($content)) {
$classes = $attributes['class'] ?? 'btn btn-primary';
unset($attributes['class']);
$content = Button::button($content)->addAttributes($attributes)->addClass($classes)->render();
}
$new = clone $this;
$new->triggerButton = $content !== '' ? $content . "\n" : '';
return $new->container(true);
}
Signup or Login in order to comment.