Final Class Yiisoft\Bootstrap5\Collapse
| Inheritance | Yiisoft\Bootstrap5\Collapse » Yiisoft\Widget\Widget |
|---|
Collapse renders a Bootstrap collapse component.
For example:
<?= Collapse::widget()
->containerAttributes(['class' => 'row'])
->items(
Toggler::to(
'Some placeholder content for the first collapse component of this multi-collapse example. ' .
'This panel is hidden by default but revealed when the user activates the relevant trigger.',
'multiCollapseExample1',
togglerContent: 'Toggle first element',
togglerAsLink: true,
),
Toggler::to(
'Some placeholder content for the second collapse component of this multi-collapse example. ' .
'This panel is hidden by default but revealed when the user activates the relevant trigger.',
'multiCollapseExample2',
togglerContent: 'Toggle second element',
),
Toggler::to(
togglerContent: 'Toggle both elements',
togglerMultiple: true,
ariaControls: 'multiCollapseExample1 multiCollapseExample2',
),
)
?>
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addAttributes() | Adds a sets of attributes. | Yiisoft\Bootstrap5\Collapse |
| addClass() | Adds one or more CSS classes to the existing classes. | Yiisoft\Bootstrap5\Collapse |
| addCssStyle() | Adds a CSS style. | Yiisoft\Bootstrap5\Collapse |
| attribute() | Adds a sets attribute value. | Yiisoft\Bootstrap5\Collapse |
| attributes() | Sets the HTML attributes. | Yiisoft\Bootstrap5\Collapse |
| cardBodyAttributes() | Sets the HTML attributes for the card body. | Yiisoft\Bootstrap5\Collapse |
| class() | Replaces all existing CSS classes with the specified one(s). | Yiisoft\Bootstrap5\Collapse |
| container() | Sets the container for the collapse items. | Yiisoft\Bootstrap5\Collapse |
| containerAttributes() | Sets the HTML attributes for the container of the collapse items. | Yiisoft\Bootstrap5\Collapse |
| items() | Sets the items. | Yiisoft\Bootstrap5\Collapse |
| render() | Run the widget. | Yiisoft\Bootstrap5\Collapse |
| togglerContainerAttributes() | Sets the HTML attributes for the container of the toggler. | Yiisoft\Bootstrap5\Collapse |
| togglerContainerTag() | Sets the tag for the container of the toggler. | Yiisoft\Bootstrap5\Collapse |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| CARD | 'card' | Yiisoft\Bootstrap5\Collapse | |
| CARD_BODY | 'card-body' | Yiisoft\Bootstrap5\Collapse | |
| COLLAPSE_MULTIPLE | 'multi-collapse' | Yiisoft\Bootstrap5\Collapse | |
| NAME | 'collapse' | Yiisoft\Bootstrap5\Collapse |
Method Details
Adds a sets of attributes.
| public addAttributes( array $attributes ): self | ||
| $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.
Multiple classes can be added by passing them as separate arguments. null values are filtered out
automatically.
| public addClass( \BackedEnum|string|null $class ): self | ||
| $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 a CSS style.
| public addCssStyle( array|string $style, boolean $overwrite = true ): self | ||
| $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
$collapse->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;
}
Adds a sets attribute value.
| public attribute( string $name, mixed $value ): self | ||
| $name | string |
The attribute name. |
| $value | mixed |
The attribute value. |
| return | self |
A new instance with the specified attribute added. Example usage:
|
|---|---|---|
public function attribute(string $name, mixed $value): self
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
Sets the HTML attributes.
| public attributes( array $attributes ): self | ||
| $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 HTML attributes for the card body.
| public cardBodyAttributes( array $attributes ): self | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the card body. |
|---|---|---|
public function cardBodyAttributes(array $attributes): self
{
$new = clone $this;
$new->cardBodyAttributes = $attributes;
return $new;
}
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 class( \BackedEnum|string|null $class ): self | ||
| $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 container for the collapse items.
| public container( boolean $enabled ): self | ||
| $enabled | boolean |
Whether to wrap the collapse items in a container. |
| return | self |
A new instance with the specified container. Example usage:
|
|---|---|---|
public function container(bool $enabled): self
{
$new = clone $this;
$new->container = $enabled;
return $new;
}
Sets the HTML attributes for the container of the collapse items.
| public containerAttributes( array $attributes ): self | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the container of the collapse items. |
|---|---|---|
public function containerAttributes(array $attributes): self
{
$new = clone $this;
$new->containerAttributes = $attributes;
return $new;
}
Sets the items.
| public items( Yiisoft\Bootstrap5\Toggler $items ): self | ||
| $items | Yiisoft\Bootstrap5\Toggler |
The items. |
| return | self |
A new instance with the specified items. Example usage:
);
|
|---|---|---|
public function items(Toggler ...$items): self
{
$new = clone $this;
$new->items = $items;
return $new;
}
Run the widget.
| public render( ): string | ||
| return | string |
The HTML representation of the element. |
|---|---|---|
public function render(): string
{
if ($this->items === []) {
return '';
}
$collapse = [];
$isMultiple = count($this->items) > 1;
$toggler = [];
foreach ($this->items as $item) {
if ($item->getContent() !== '') {
$collapseDiv = Div::tag()
->addClass(self::NAME, ...$this->cssClasses)
->addAttributes($this->attributes)
->addContent(
"\n",
Div::tag()
->addAttributes($this->cardBodyAttributes)
->addClass(self::CARD, self::CARD_BODY)
->addContent(
"\n",
$item->getContent(),
"\n",
),
"\n",
)
->id($item->getId());
if ($isMultiple) {
$collapseDiv = $collapseDiv->addClass(self::COLLAPSE_MULTIPLE);
if ($item->getTogglerMultiple() === false) {
$collapse[] = Div::tag()->addClass('col')->addContent("\n", $collapseDiv, "\n");
}
} else {
$collapse[] = $collapseDiv;
}
}
$toggler[] = $item->renderToggler();
}
return $this->renderTogglerContainer($toggler) . "\n" . $this->renderCollapse($collapse);
}
Sets the HTML attributes for the container of the toggler.
| public togglerContainerAttributes( array $attributes ): self | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the container of the toggler. |
|---|---|---|
public function togglerContainerAttributes(array $attributes): self
{
$new = clone $this;
$new->togglerContainerAttributes = $attributes;
return $new;
}
Sets the tag for the container of the toggler.
| public togglerContainerTag( string $tag ): self | ||
| $tag | string |
The tag for the container of the toggler. |
| return | self |
A new instance with the specified tag for the container of the toggler. Example usage:
|
|---|---|---|
public function togglerContainerTag(string $tag): self
{
$new = clone $this;
$new->togglerContainerTag = $tag;
return $new;
}
Signup or Login in order to comment.