Streamline your UI by tucking secondary button actions into a dropdown. See how to customize the button style and set individual icons for each action option.
DropDownButton for Blazor is the perfect combination of the features that a regular button has to offer with the capabilities of a dropdown component.
This is an ideal option to provide to the end user of your .NET Blazor app a simple UI to display hidden options to choose from with a single click. Typical scenarios for using such a component include:
This simple but extremely useful component is now part of Progress Telerik UI for Blazor suite and comes with:
Now, let’s see it in action! 👇
If this is the first time, you are adding a Telerik component, you need to prepare your .NET Blazor application. To use Telerik UI for Blazor, you need to:
Full details can be found in Telerik documentation.
Add a <TelerikDropDownButton>
tag to your HTML page similar to the rest of Telerik UI for Blazor components:
<TelerikDropDownButton >
</TelerikDropDownButton>
This is enough to display the button, but it will not be useful at this stage, right? 😊
To make it look like it should, we can set up its content by defining the <DropDownButtonContent>
tag and we add the choices we need to be displayed when the button is clicked with the <DropDownItems>
tag. Let’s continue with that.
In order to add content to the dropdown area of the Telerik DropDownButton, you have to use its DropDownContent tag. It is of type object so you can add any other component as content of the dropdown. The content can be plain text, HTML or another component.
Example:
<TelerikDropDownButton>
<DropDownButtonContent>Paste</DropDownButtonContent>
</TelerikDropDownButton>
To add dropdown items or “secondary buttons,” simply define them inside the DropDownButtonItems tag like this:
<TelerikDropDownButton>
…
<DropDownButtonItems>
<DropDownButtonItem >Paste</DropDownButtonItem>
<DropDownButtonItem >Paste as HTML</DropDownButtonItem>
<DropDownButtonItem >Paste MarkDown</DropDownButtonItem>
<DropDownButtonItem > Set Default Paste </DropDownButtonItem>
</DropDownButtonItems>
</TelerikDropDownButton>
The items above are very useful, but still, we can make them look better and extend their functionality by:
Icon
attributeEnabled
attributed to false if we want an item to be disabled for some reason.OnClick
eventNote: The
Icon
,OnClick
andEnable
attributes can be assigned to the DropDownComponent itself too.
Here is the full definition of the component:
<TelerikDropDownButton Icon="@SvgIcon.Clipboard" OnClick="@(() => OnButtonClick("Paste"))">
<DropDownButtonContent>Paste</DropDownButtonContent>
<DropDownButtonItems>
<DropDownButtonItem Icon="@SvgIcon.ClipboardText" OnClick="@(() => OnItemClick("Paste Text"))">Paste Text</DropDownButtonItem>
<DropDownButtonItem Icon="@SvgIcon.ClipboardCode" OnClick="@(() => OnItemClick("Paste as HTML"))">Paste as HTML</DropDownButtonItem>
<DropDownButtonItem Icon="@SvgIcon.ClipboardMarkdown" OnClick="@(() => OnItemClick("Paste Markdown"))">Paste Markdown</DropDownButtonItem>
<DropDownButtonItem OnClick="@(() => OnItemClick("Set Default Paste"))">Set Default Paste</DropDownButtonItem>
</DropDownButtonItems>
<DropDownButtonSettings>
<DropDownButtonPopupSettings MaxHeight="500px" MinWidth="150px">
</DropDownButtonPopupSettings>
</DropDownButtonSettings>
</TelerikDropDownButton>
Next, let’s review the attributes in detail.
The <TelerikDropDownButton>
and <DropDownButtonItem>
provide an Icon
parameter that allows you to add an icon to the main button and all secondary action items.
The Icon parameter is of type object and it accepts:
Both the primary button and the secondary buttons offer the OnClick
event, which fires when the user clicks or taps the button. Each of them executes a separate OnClick
handler.
The example above executes the following event handler:
private void OnItemClick(string item)
{
Console.WriteLine($"User clicked {item} option");
}
Telerik DropDownButton comes with 18 predefined variations of the Default, Bootstrap, Fluent and Material built-in themes. You can easily choose one of them and edit it or create a new one with Progress ThemeBuilder.
In our example, we will set the theme to Bootstrap with Nordic color variations.
The DropDownButton provides the same appearance parameters as the regular Button component. We can set:
<TelerikDropDownButton FillMode="outline"
Rounded="full"
Size="lg"
ThemeColor="success”>
...
</TelerikDropDownButton>
Explore more styling options with the appearance demo.
The dropdown popup can also be customized by using its DropDownButtonPopupSettings
, like what’s shown below:
<TelerikDropDownButton Icon="@SvgIcon.Clipboard" OnClick="@(() => OnButtonClick("Paste"))">
...
<DropDownButtonSettings>
<DropDownButtonPopupSettings MaxHeight="500px" MinWidth="150px">
</DropDownButtonPopupSettings>
</DropDownButtonSettings>
</TelerikDropDownButton>
The Telerik UI for Blazor DropDownButton comes with built-in right-to-left (RTL) support and offers right-to-left rendering. By setting the EnableRtl
parameter of the TelerikRootComponent to true
, Telerik DropDown will change its direction:
<TelerikRootComponent EnableRtl="true">
@Body
</TelerikRootComponent>
By default, the KeyboardNavigation is enabled and allows us to use the keys below as described:
As you saw, this simple UI component is actually very powerful. See more of it in Telerik UI for Blazor demos, play with it directly in Telerik REPL for Blazor and Telerik documentation, or customize with ThemeBuilder.
You can check out Telerik UI for Blazor yourself: download it for free now, play with it and share your feedback and ideas with the Telerik team on Telerik Feedback Portal or Telerik Forums.
Happy coding!
Rossitza Fakalieva is a Technical Manager, Microsoft MVP in Developer Technologies and a Director of the Bulgarian chapter of the global Women Who Code organization. She previously worked on the Telerik engineering team and defines herself as .NET enthusiast. She loves to empower others to grow in their career and in the tech field—by teaching, by delivering courses and presentations, and as part of her daily job.