0 follower

Final Class Yiisoft\Bootstrap5\Collapse

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

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

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

Hide inherited methods

addAttributes() public method

Adds a sets of attributes.

public addAttributes( array $attributes ): self
$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 $collapse->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 addClass( \BackedEnum|string|null $class ): self
$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 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, ['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 $collapse->addCssStyle('color: red');

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

            
attribute() public method

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: `php $collapse->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 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;
}

            
cardBodyAttributes() public method

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

            
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 class( \BackedEnum|string|null $class ): self
$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 $collapse->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

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

            
container() public method

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: `php $collapse->container(true); `

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

            
containerAttributes() public method

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

            
items() public method

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: `php $collapse->items(

Toggler::widget('Item 1'),
Toggler::widget()->content('Item 2'),

); `

                public function items(Toggler ...$items): self
{
    $new = clone $this;
    $new->items = $items;
    return $new;
}

            
render() public method

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

            
togglerContainerAttributes() public method

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

            
togglerContainerTag() public method

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: `php $collapse->togglerContainerTag('div'); `

                public function togglerContainerTag(string $tag): self
{
    $new = clone $this;
    $new->togglerContainerTag = $tag;
    return $new;
}