0 follower

Final Class Yiisoft\Bootstrap5\Toast

InheritanceYiisoft\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

Hide inherited 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

Hide inherited 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

Hide inherited methods

addAttributes() public method

Adds a set of attributes.

public self addAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names. for example, ['id' => 'my-id'].

return self

A new instance with the specified attributes added.

Example usage: `php $toast->addAttributes(['data-id' => '123']); `

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = [...$this->attributes, ...$attributes];
    return $new;
}

            
addClass() public method

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 null to skip adding a class.

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;
}

            
addCloseButtonAttribute() public method

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: `php $toast->addCloseButtonAttribute('data-id', '123'); `

                public function addCloseButtonAttribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->closeButtonAttributes[$name] = $value;
    return $new;
}

            
addCloseButtonClass() public method

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 null to skip adding a class.

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;
}

            
addCloseButtonCssStyle() public method

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, ['color' => 'red', 'font-weight' => 'bold'] will be rendered as color: red; font-weight: bold;. If it is a string, it will be added as is, for example, color: red.

$overwrite boolean

Whether to overwrite existing styles with the same name. If false, the new value will be appended to the existing one.

return self

A new instance with the specified CSS style value added.

Example usage: `php $alert->addCloseButtonCssStyle('color: red');

// 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;
}

            
addCssStyle() public method

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., ['color' => 'red', 'font-weight' => 'bold'] will be rendered as color: red; font-weight: bold;. If it is a string, it will be added as is. For example, color: red.

$overwrite boolean

Whether to overwrite existing styles with the same name. If false, the new value will be appended to the existing one.

return self

A new instance with the specified CSS style value added.

Example usage: `php $toast->addCssStyle('color: red');

// 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;
}

            
attribute() public method

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: `php $toast->attribute('data-id', '123'); `

                public function attribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->attributes[$name] = $value;
    return $new;
}

            
attributes() public method

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;
}

            
body() public method
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 <div> tag.

$attributes array

Additional HTML attributes for the <div> tag. Used only when $content is a string.

$class \BackedEnum|string|null

CSS class names to add to the <div> tag. Pass null to skip adding a class.

return self

A new instance with the specified body content.

Example usage: `php // Basic usage $toast->body('Hello, world! This is a toast message.');

// 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;
}

            
class() public method

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 null to skip setting a class.

return self

A new instance with the specified CSS classes set.

Example usage: `php $toast->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

                public function class(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    $new->cssClasses = $class;
    return $new;
}

            
closeButton() public method

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: `php // Use default close button $toast->closeButton();

// 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;
}

            
container() public method

Whether to render container <div> tag.

public self container ( boolean $enabled )
$enabled boolean

Whether to render container <div> tag. By default, the widget will be rendered in a container <div> tag. If set to false, the container will not be rendered.

return self

A new instance with the specified container setting.

Example usage: `php $toast->container(false); `

                public function container(bool $enabled): self
{
    $new = clone $this;
    $new->container = $enabled;
    return $new;
}

            
content() public method

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: `php // Using a string $toast->content('

My custom toast implementation
');

// Using multiple strings $toast->content(

'<div class="header">',
'Toast Header', '</div>',
'<div class="body">',
'Toast Body',
'</div>',

);

// Using a Stringable object $toast->content($customToastHtml); `

                public function content(string|Stringable ...$content): self
{
    $new = clone $this;
    $new->content = implode("\n", $content);
    return $new;
}

            
headerAttributes() public method

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;
}

            
id() public method

Sets the ID.

public self id ( boolean|string $id )
$id boolean|string

The ID of the component. If true, an ID will be generated automatically.

return self

A new instance with the specified ID.

Example usage: `php $toast->id('my-id'); `

                public function id(bool|string $id): self
{
    $new = clone $this;
    $new->id = $id;
    return $new;
}

            
image() public method

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 <img> tag. Used only when $content is a string.

$attributes array

Additional HTML attributes for the <img> tag. Used only when $content is a string.

return self

A new instance with the specified image for the header section.

Example usage: `php // Using a URL $toast->image('path/to/image.jpg', 'Bootstrap logo', ['class' => 'rounded me-2']);

// 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;
}

            
render() public method

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(),
    };
}

            
time() public method

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 <small> tag.

$attributes array

Additional HTML attributes for the <small> tag. Used only when $content is a string.

$class \BackedEnum|string|null

CSS class names to add to the <small> tag. Pass null to skip adding a class.

return self

A new instance with the specified time display for the header section.

Example usage: `php // Basic usage $toast->time('11 minutes ago');

// 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;
}

            
title() public method

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 <strong> tag.

$attributes array

Additional HTML attributes for the <strong> tag. Used only when $content is a string.

$class \BackedEnum|string|null

CSS class names to add to the <strong> tag. Pass null to skip adding a class.

return self

A new instance with the specified title for the header section.

Example usage: `php // Basic usage $toast->title('Bootstrap');

// 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;
}

            
triggerButton() public method

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: `php $toast->triggerButton('Show notification'); `

                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);
}