Final Class Yiisoft\Bootstrap5\ButtonToolbar
| Inheritance | Yiisoft\Bootstrap5\ButtonToolbar » Yiisoft\Widget\Widget |
|---|
ButtonToolbar Combines sets of button groups into button toolbars for more complex components.
Use utility classes as needed to space out groups, buttons, and more.
For example,
echo ButtonToolbar::widget()
->ariaLabel('Toolbar with button groups')
->buttonGroups(
ButtonGroup::widget()
->addClass('me-2')
->ariaLabel('First group')
->buttons(
Button::widget()->label('1')->variant(ButtonVariant::PRIMARY),
Button::widget()->label('2')->variant(ButtonVariant::PRIMARY),
Button::widget()->label('3')->variant(ButtonVariant::PRIMARY),
Button::widget()->label('4')->variant(ButtonVariant::PRIMARY),
),
ButtonGroup::widget()
->addClass('me-2')
->ariaLabel('Second group')
->buttons(
Button::widget()->label('5'),
Button::widget()->label('6'),
Button::widget()->label('7'),
),
ButtonGroup::widget()
->ariaLabel('Third group')
->buttons(
Button::widget()->label('8')->variant(ButtonVariant::INFO),
)
)
->render();
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addAttributes() | Adds a sets of attributes. | Yiisoft\Bootstrap5\ButtonToolbar |
| addClass() | Adds one or more CSS classes to the existing classes. | Yiisoft\Bootstrap5\ButtonToolbar |
| addCssStyle() | Adds a CSS style. | Yiisoft\Bootstrap5\ButtonToolbar |
| ariaLabel() | Sets the ARIA label. | Yiisoft\Bootstrap5\ButtonToolbar |
| attribute() | Adds a sets attribute value. | Yiisoft\Bootstrap5\ButtonToolbar |
| attributes() | Sets the HTML attributes. | Yiisoft\Bootstrap5\ButtonToolbar |
| buttonGroups() | List of buttons groups. | Yiisoft\Bootstrap5\ButtonToolbar |
| class() | Replaces all existing CSS classes with the specified one(s). | Yiisoft\Bootstrap5\ButtonToolbar |
| id() | Sets the ID. | Yiisoft\Bootstrap5\ButtonToolbar |
| render() | Run the widget. | Yiisoft\Bootstrap5\ButtonToolbar |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| NAME | 'btn-toolbar' | Yiisoft\Bootstrap5\ButtonToolbar |
Method Details
Adds a sets 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 = [...$new->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 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 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, |
| $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
$buttonToolbar->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 the ARIA label.
| public self ariaLabel ( string $label ) | ||
| $label | string |
The ARIA label. |
| return | self |
A new instance with the specified ARIA label. |
|---|---|---|
public function ariaLabel(string $label): self
{
return $this->attribute('aria-label', $label);
}
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:
|
|---|---|---|
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;
}
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 |
| 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 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;
}
Run the widget.
| public string render ( ) | ||
| return | string |
The HTML representation of the element. |
|---|---|---|
public function render(): string
{
$attributes = $this->attributes;
$classes = $attributes['class'] ?? null;
unset($attributes['class'], $attributes['id']);
$buttonGroup = implode("\n", $this->buttonGroups);
$buttonsGroups = $buttonGroup === '' ? '' : "\n" . $buttonGroup . "\n";
if ($buttonsGroups === '') {
return '';
}
return Div::tag()
->attributes($attributes)
->attribute('role', 'toolbar')
->addClass(
self::NAME,
$classes,
...$this->cssClasses,
)
->content($buttonsGroups)
->encode(false)
->id($this->getId())
->render();
}
Signup or Login in order to comment.