0 follower

Final Class Yiisoft\Bootstrap5\Offcanvas

InheritanceYiisoft\Bootstrap5\Offcanvas » Yiisoft\Widget\Widget

Offcanvas renders a Bootstrap offcanvas component.

For example, `php <?= Offcanvas::widget()

->placement(OffcanvasPlacement::END)
->title('Offcanvas Title')
->togglerContent('Toggle Offcanvas')
->begin()

?>

// content of the offcanvas 'Offcanvas content here'

<?= Offcanvas::end ?> `

Public Methods

Hide inherited methods

Method Description Defined By
addAttributes() Adds a set of attributes. Yiisoft\Bootstrap5\Offcanvas
addClass() Adds one or more CSS classes to the existing classes. Yiisoft\Bootstrap5\Offcanvas
addCssStyle() Adds a CSS style. Yiisoft\Bootstrap5\Offcanvas
addTogglerAttribute() Adds toggler attribute value. Yiisoft\Bootstrap5\Offcanvas
addTogglerClass() Adds one or more CSS classes to the existing toggler classes. Yiisoft\Bootstrap5\Offcanvas
addTogglerCssStyle() Adds a toggler CSS style. Yiisoft\Bootstrap5\Offcanvas
attribute() Adds a sets attribute value. Yiisoft\Bootstrap5\Offcanvas
attributes() Sets the HTML attributes. Yiisoft\Bootstrap5\Offcanvas
backdrop() Sets whether to use a backdrop. Yiisoft\Bootstrap5\Offcanvas
backdropStatic() Sets whether to use a static backdrop. Yiisoft\Bootstrap5\Offcanvas
begin() Begins the rendering. Yiisoft\Bootstrap5\Offcanvas
bodyAttributes() Sets the HTML attributes for the body. Yiisoft\Bootstrap5\Offcanvas
class() Replaces all existing CSS classes with the specified one(s). Yiisoft\Bootstrap5\Offcanvas
headerAttributes() Sets the HTML attributes for the header. Yiisoft\Bootstrap5\Offcanvas
id() Sets the ID. Yiisoft\Bootstrap5\Offcanvas
placement() Sets the placement. Yiisoft\Bootstrap5\Offcanvas
render() Run the widget. Yiisoft\Bootstrap5\Offcanvas
responsive() Sets the responsive size. Yiisoft\Bootstrap5\Offcanvas
scrollable() Sets whether it is scrollable. Yiisoft\Bootstrap5\Offcanvas
show() Sets whether it is visible. Yiisoft\Bootstrap5\Offcanvas
theme() Sets the theme. Yiisoft\Bootstrap5\Offcanvas
title() Sets the title. Yiisoft\Bootstrap5\Offcanvas
titleAttributes() Sets the HTML attributes for the title. Yiisoft\Bootstrap5\Offcanvas
toggler() Sets the toggle button. Yiisoft\Bootstrap5\Offcanvas
togglerAttributes() Sets the HTML attributes for the toggler. Yiisoft\Bootstrap5\Offcanvas
togglerContent() Sets the content of the toggler. Yiisoft\Bootstrap5\Offcanvas

Constants

Hide inherited constants

Constant Value Description Defined By
BODY_CLASS 'offcanvas-body' Yiisoft\Bootstrap5\Offcanvas
CLOSE_CLASS 'btn-close' Yiisoft\Bootstrap5\Offcanvas
HEADER_CLASS 'offcanvas-header' Yiisoft\Bootstrap5\Offcanvas
NAME 'offcanvas' Yiisoft\Bootstrap5\Offcanvas
SHOW_CLASS 'show' Yiisoft\Bootstrap5\Offcanvas
TITLE_CLASS 'offcanvas-title' Yiisoft\Bootstrap5\Offcanvas
TOGGLER_CLASS 'btn btn-primary' Yiisoft\Bootstrap5\Offcanvas

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-offcanvas'].

return self

A new instance with the specified attributes added.

Example usage: `php $offcanvas->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.

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

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

            
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, 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 $offcanvas->addCssStyle('color: red');

// or $offcanvas->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;
}

            
addTogglerAttribute() public method

Adds toggler attribute value.

public self addTogglerAttribute ( 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 $offcanvas->addTogglerAttribute('data-id', '123'); `

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

            
addTogglerClass() public method

Adds one or more CSS classes to the existing toggler classes.

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self addTogglerClass ( \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 addTogglerClass(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    foreach ($class as $item) {
        Html::addCssClass($new->togglerAttributes, $item);
    }
    return $new;
}

            
addTogglerCssStyle() public method

Adds a toggler CSS style.

public self addTogglerCssStyle ( 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 $offcanvas->addTogglerCssStyle('color: red');

// or $offcanvas->addTogglerCssStyle(['color' => 'red', 'font-weight' => 'bold']); `

                public function addTogglerCssStyle(array|string $style, bool $overwrite = true): self
{
    $new = clone $this;
    Html::addCssStyle($new->togglerAttributes, $style, $overwrite);
    return $new;
}

            
attribute() public method

Adds a 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 added.

Example usage: `php $offcanvas->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;
}

            
backdrop() public method

Sets whether to use a backdrop.

public self backdrop ( )
return self

A new instance with the specified backdrop setting.

                public function backdrop(): self
{
    $new = clone $this;
    $new->backdrop = true;
    return $new;
}

            
backdropStatic() public method

Sets whether to use a static backdrop.

public self backdropStatic ( )
return self

A new instance with the specified static backdrop setting.

                public function backdropStatic(): self
{
    $new = clone $this;
    $new->backdropStatic = true;
    return $new;
}

            
begin() public method

Begins the rendering.

public string begin ( )
return string

The opening HTML tags for the offcanvas.

throws InvalidArgumentException

if the tag is an empty string.

                public function begin(): string
{
    parent::begin();
    $id = $this->getId();
    $html = '';
    if ($this->toggler !== '' && $this->show === false) {
        $html .= (string) $this->toggler . "\n";
    }
    if ($this->togglerContent !== '' && $this->show === false) {
        $html .= $this->renderToggler($id) . "\n";
    }
    $html .= $this->renderOffcanvas($id);
    return $html;
}

            
bodyAttributes() public method

Sets the HTML attributes for the body.

public self bodyAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified body attributes.

                public function bodyAttributes(array $attributes): self
{
    $new = clone $this;
    $new->bodyAttributes = $attributes;
    return $new;
}

            
class() public method

Replaces all existing CSS classes with the specified one(s).

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

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 $offcanvas->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

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

            
headerAttributes() public method

Sets the HTML attributes for the header.

public self headerAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified header attributes.

                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 $offcanvas->id('my-offcanvas'); `

throws InvalidArgumentException

if the ID is an empty string or false.

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

            
placement() public method

Sets the placement.

public self placement ( \Yiisoft\Bootstrap5\OffcanvasPlacement $placement )
$placement \Yiisoft\Bootstrap5\OffcanvasPlacement

The placement.

return self

A new instance with the specified placement setting.

Example usage: `php $offcanvas->placement(OffcanvasPlacement::END); `

                public function placement(OffcanvasPlacement $placement): self
{
    $new = clone $this;
    $new->placement = $placement;
    return $new;
}

            
render() public method

Run the widget.

public string render ( )
return string

The HTML representation of the element.

                public function render(): string
{
    $html = Html::closeTag('div') . "\n";
    $html .= Html::closeTag('div');
    return $html;
}

            
responsive() public method

Sets the responsive size.

public self responsive ( \Yiisoft\Bootstrap5\Utility\Responsive $size )
$size \Yiisoft\Bootstrap5\Utility\Responsive

The responsive size.

return self

A new instance with the specified responsive size setting.

Example usage: `php $offcanvas->responsive(Responsive::SM); `

                public function responsive(Responsive $size): self
{
    $new = clone $this;
    $new->responsive = $size->value;
    return $new;
}

            
scrollable() public method

Sets whether it is scrollable.

public self scrollable ( )
return self

A new instance with the specified scrollable setting.

                public function scrollable(): self
{
    $new = clone $this;
    $new->scrollable = true;
    return $new;
}

            
show() public method

Sets whether it is visible.

public self show ( )
return self

A new instance with the specified visibility setting.

Example usage: `php $offcanvas->show(); `

                public function show(): self
{
    $new = clone $this;
    $new->show = true;
    return $new;
}

            
theme() public method

Sets the theme.

public self theme ( string $theme )
$theme string

The theme. If an empty string, the theme will be removed. Valid values are dark and light.

return self

A new instance with the specified theme.

                public function theme(string $theme): self
{
    return $this->addAttributes(['data-bs-theme' => $theme === '' ? null : $theme]);
}

            
title() public method

Sets the title.

public self title ( string|\Stringable $title )
$title string|\Stringable

The title.

return self

A new instance with the specified title.

Example usage: `php $offcanvas->title('My Offcanvas Title'); `

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

            
titleAttributes() public method

Sets the HTML attributes for the title.

public self titleAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified title attributes.

                public function titleAttributes(array $attributes): self
{
    $new = clone $this;
    $new->titleAttributes = $attributes;
    return $new;
}

            
toggler() public method

Sets the toggle button.

public self toggler ( string|\Stringable $toggle )
$toggle string|\Stringable

The toggle button.

return self

A new instance with the specified toggle button.

Example usage: `php $offcanvas->toggler('Toggle');

// or $offcanvas->toggler(Button::button('Toggle')); `

                public function toggler(string|Stringable $toggle): self
{
    $new = clone $this;
    $new->toggler = $toggle;
    return $new;
}

            
togglerAttributes() public method

Sets the HTML attributes for the toggler.

public self togglerAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified toggler attributes.

                public function togglerAttributes(array $attributes): self
{
    $new = clone $this;
    $new->togglerAttributes = $attributes;
    return $new;
}

            
togglerContent() public method

Sets the content of the toggler.

public self togglerContent ( string|\Stringable $content )
$content string|\Stringable

The content of the toggler.

return self

A new instance with the specified toggler content.

Example usage: `php $offcanvas->togglerContent('Toggle Offcanvas'); `

                public function togglerContent(string|Stringable $content): self
{
    $new = clone $this;
    $new->togglerContent = $content;
    return $new;
}