Final Class Yiisoft\Bootstrap5\Dropdown
| Inheritance | Yiisoft\Bootstrap5\Dropdown » Yiisoft\Widget\Widget |
|---|
Dropdown renders a Bootstrap dropdown menu component.
For example,
<?= Dropdown::widget()
->items(
DropdownItem::link('Action', '#'),
DropdownItem::link('Another action', '#'),
DropdownItem::link('Something else here', '#'),
DropdownItem::divider(),
DropdownItem::link('Separated link', '#'),
)
->toggleContent('Toggle dropdown')
->toggleVariant(ButtonVariant::DANGER)
->toggleSplit()
->toggleSplitContent('Danger')
->toggleSizeLarge()
?>
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DROPDOWN_CLASS | 'dropdown' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_ACTIVE_CLASS | 'active' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_CLASS | 'dropdown-item' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_DISABLED_CLASS | 'disabled' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_DIVIDER_CLASS | 'dropdown-divider' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_HEADER_CLASS | 'dropdown-header' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_ITEM_TEXT_CLASS | 'dropdown-item-text' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_LIST_CLASS | 'dropdown-menu' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_TOGGLER_BUTTON_CLASS | 'btn' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_TOGGLER_CLASS | 'dropdown-toggle' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_TOGGLER_CONTAINER_CLASS | 'btn-group' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_TOGGLER_SPAN_CLASS | 'visually-hidden' | Yiisoft\Bootstrap5\Dropdown | |
| DROPDOWN_TOGGLER_SPLIT_CLASS | 'dropdown-toggle-split' | Yiisoft\Bootstrap5\Dropdown | |
| NAME | 'dropdown' | Yiisoft\Bootstrap5\Dropdown |
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 = [...$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 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
$dropdown->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 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:
|
|---|---|---|
public function addTogglerAttribute(string $name, mixed $value): self
{
$new = clone $this;
$new->togglerAttributes[$name] = $value;
return $new;
}
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 |
| 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;
}
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, |
| $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
$dropdown->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;
}
Sets the alignment.
| public self alignment ( \Yiisoft\Bootstrap5\DropdownAlignment|null $alignment ) | ||
| $alignment | \Yiisoft\Bootstrap5\DropdownAlignment|null |
The alignment. If |
| return | self |
A new instance with the specified alignment. Example usage:
|
|---|---|---|
public function alignment(DropdownAlignment|null ...$alignment): self
{
$new = clone $this;
$new->alignmentClasses = $alignment;
return $new;
}
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;
}
Sets the auto-close setting.
| public self autoClose ( \Yiisoft\Bootstrap5\DropdownAutoClose $autoClose ) | ||
| $autoClose | \Yiisoft\Bootstrap5\DropdownAutoClose |
The auto-close setting. |
| return | self |
A new instance with the specified auto-close setting. Example usage:
|
|---|---|---|
public function autoClose(DropdownAutoClose $autoClose): self
{
return $this->addTogglerAttribute('data-bs-auto-close', $autoClose->value);
}
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;
}
Whether to render in a container <div> tag.
| public self container ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to render in a container |
| return | self |
A new instance with the specified container setting. Example usage:
|
|---|---|---|
public function container(bool $enabled): self
{
$new = clone $this;
$new->container = $enabled;
return $new;
}
Sets the CSS classes for the container.
| public self containerClasses ( \BackedEnum|string $classes ) | ||
| $classes | \BackedEnum|string |
The CSS class for the container. |
| return | self |
A new instance with the specified CSS class for the container. Example usage:
|
|---|---|---|
public function containerClasses(BackedEnum|string ...$classes): self
{
$new = clone $this;
$new->containerClasses = $classes;
return $new;
}
Set the direction.
| public self direction ( \Yiisoft\Bootstrap5\DropdownDirection $direction ) | ||
| $direction | \Yiisoft\Bootstrap5\DropdownDirection |
The direction. |
| return | self |
A new instance with the specified direction. Example usage:
|
|---|---|---|
public function direction(DropdownDirection $direction): self
{
$new = clone $this;
$new->containerClasses = [$direction];
return $new;
}
Returns the CSS classes for the container.
| public array getCssClasses ( ) | ||
| return | array |
The CSS classes for the container. |
|---|---|---|
public function getCssClasses(): array
{
return $this->cssClasses;
}
Returns the list of links to appear in the dropdown.
| public Yiisoft\Bootstrap5\DropdownItem[] getItems ( ) | ||
| return | Yiisoft\Bootstrap5\DropdownItem[] |
The links to appear in the dropdown. |
|---|---|---|
public function getItems(): array
{
return $this->items;
}
List of links. If this property is empty, the widget will not render anything.
| public self items ( array $items ) | ||
| $items | array |
The links. |
| return | self |
A new instance with the specified links. |
|---|---|---|
public function items(DropdownItem ...$items): self
{
$new = clone $this;
$new->items = $items;
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;
$containerClasses = $this->containerClasses;
if ($this->items === []) {
return '';
}
$togglerId = $this->getTogglerId();
unset($attributes['class'], $attributes['id']);
if ($this->togglerSplit) {
$containerClasses = [self::DROPDOWN_TOGGLER_CONTAINER_CLASS];
}
$renderToggler = match ($this->togglerSplit) {
true => $this->renderTogglerSplit() . "\n" . $this->renderToggler($togglerId),
false => $this->renderToggler($togglerId),
};
$renderItems = $this->renderItems($togglerId);
return match ($this->container) {
true => Div::tag()
->addAttributes($attributes)
->addClass(...$containerClasses)
->addClass($classes)
->addClass(...$this->cssClasses)
->addContent(
"\n",
$renderToggler,
"\n",
$renderItems,
"\n",
)
->encode(false)
->render(),
false => $renderToggler . "\n" . $renderItems,
};
}
Sets the theme.
| public self theme ( string $theme ) | ||
| $theme | string |
The theme. |
| return | self |
A new instance with the specified theme. Example usage:
|
|---|---|---|
public function theme(string $theme): self
{
return $this->addAttributes(['data-bs-theme' => $theme === '' ? null : $theme]);
}
Sets the toggler custom element.
| public self toggler ( string|\Stringable $tag ) | ||
| $tag | string|\Stringable |
The toggler custom element. |
| return | self |
A new instance with the specified toggler custom element. Example usage:
);
|
|---|---|---|
public function toggler(string|Stringable $tag): self
{
$new = clone $this;
$new->toggler = (string) $tag;
return $new;
}
Whether to render the toggler as a link.
| public self togglerAsLink ( boolean $enable = true ) | ||
| $enable | boolean |
Whether to render the toggler as a link. If set to |
| return | self |
A new instance with the specified toggler as a link setting. Example usage:
|
|---|---|---|
public function togglerAsLink(bool $enable = true): self
{
$new = clone $this;
$new->togglerLink = $enable;
return $new;
}
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 attributes for the toggler. |
|---|---|---|
public function togglerAttributes(array $attributes): self
{
$new = clone $this;
$new->togglerAttributes = $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 togglerClass ( \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 for the toggler. Example usage:
|
|---|---|---|
public function togglerClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->togglerClasses = $class;
return $new;
}
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 content of the toggler. Example usage:
|
|---|---|---|
public function togglerContent(string|Stringable $content): self
{
$new = clone $this;
$new->togglerContent = (string) $content;
return $new;
}
Sets the ID for the toggler.
| public self togglerId ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the component. If |
| return | self |
A new instance with the specified ID for the toggler. Example usage:
|
|---|---|---|
public function togglerId(bool|string $id): self
{
$new = clone $this;
$new->togglerId = $id;
return $new;
}
Sets the size for the toggler.
| public self togglerSize ( \Yiisoft\Bootstrap5\ButtonSize|null $size ) | ||
| $size | \Yiisoft\Bootstrap5\ButtonSize|null |
The size. If |
| return | self |
A new instance with the specified size for the toggler. Example usage:
|
|---|---|---|
public function togglerSize(ButtonSize|null $size): self
{
$new = clone $this;
$new->togglerSize = $size?->value;
return $new;
}
Whether to render the toggler as a split.
| public self togglerSplit ( boolean $enable = true ) | ||
| $enable | boolean |
Whether to render the toggler as a split. If set to |
| return | self |
A new instance with the specified the toggler split setting. Example usage:
|
|---|---|---|
public function togglerSplit(bool $enable = true): self
{
$new = clone $this;
$new->togglerSplit = $enable;
return $new;
}
Sets the content of the toggler split.
| public self togglerSplitContent ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The content of the toggler split. |
| return | self |
A new instance with the specified content of the toggler split. Example usage:
|
|---|---|---|
public function togglerSplitContent(string|Stringable $content): self
{
$new = clone $this;
$new->togglerSplitContent = (string) $content;
return $new;
}
Sets the URL for the toggler link.
| public self togglerUrl ( string $url ) | ||
| $url | string |
The URL for the toggler link. |
| return | self |
A new instance with the specified URL for the toggler link. Example usage:
|
|---|---|---|
public function togglerUrl(string $url): self
{
$new = clone $this;
$new->togglerUrl = $url;
return $new;
}
Sets the variant for the toggler.
| public self togglerVariant ( \Yiisoft\Bootstrap5\ButtonVariant|null $variant ) | ||
| $variant | \Yiisoft\Bootstrap5\ButtonVariant|null |
The variant for the toggler. If |
| return | self |
A new instance with the specified variant for the toggler. |
|---|---|---|
public function togglerVariant(ButtonVariant|null $variant): self
{
$new = clone $this;
$new->togglerVariant = $variant;
return $new;
}
Signup or Login in order to comment.