[New post] Create an accordion component with Blazor
Enrico posted: " In this post, I'm going to explain how to create an accordion component with Blazor WebAssembly in a very simple way. My goal is to create a component to display a list of element in the page. Accordion close When the user clicks on one of the elem"
In this post, I'm going to explain how to create an accordion component with BlazorWebAssembly in a very simple way. My goal is to create a component to display a list of element in the page.
When the user clicks on one of the element, it opens and shows its content.
Implementation
So, first things first. I'm using Open Iconic to display the icon in the accordion. For that, I have to add it in the project. For that, you can download it from the website or add a Client-side library. After that, add the CSS to your index.html.
Collapsable component
First, under the Shared folder add a new Razor component and its name is Collapsable.razor. So, copy the following code and paste it in your Razor file.
How you can see, the code is pretty easy. The variable Collapsed gives to the page the input to display or not the main content in the collapsable-panel. In the first div in line 2, when the user clicks on it, the function Toggle is called and changes the value of the Collapsed variable.
Obviously, the Title and the content of this accordion is always different and I want to add whatever I want. For this reason, I add a Title variable to use as parameter. Also, RenderFragment ChildContent allows us to add in the tag Collapsable any code.
Add the CSS
Now, next step to create an accordion component with Blazor is to add a bit of style for the component. We have 2 ways to do that:
add the CSS in the global CSS
create a specific CSS for this component. For that, just add a new CSS file with name Collapsable.razor.css
Either way, copy the following CSS and add it to yours.
Finally, I want to use this Razor component. In a Razor Page, add the following code
<Collapsable Title="Test" Collapsed="true" Value=""> This is my content </Collapsable>
Now, if you run your project, you can see the collapsed bar with the title Test. If you click on it, you can see the content.
Bonus
So, you see in the component a Value property. You can remove it. The reason for that is that I want to display a percentage (or any other text) on the right of the accordion like in the following image.
Conclusion
I hope this post "Create an accordion component with Blazor" it will be useful. Please leave your comment at the end of this post or in our forum.
More documentation and examples
For more documentation and examples about Blazor and other components, visit my blog PureSourceCode. Here a quick list of popular links:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.