How to create a ShareButton component with Blazorise!
Are you ready to sprinkle some Blazorise magic into your Blazor app?
Adding share buttons for social media platforms can give your users an easy
way to spread the word about your awesome content. It's easier than you think, thanks to Blazorise!
Install-Package Blazorise.Bootstrap
Install-Package Blazorise.Icons.FontAwesome
Alternatively you can use your favorite IDE's nuget package manager and add the packages manually!
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"
integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn"
crossorigin="anonymous">
<link href="_content/Blazorise.Icons.FontAwesome/v6/css/all.min.css" rel="stylesheet">
<link href="_content/Blazorise/blazorise.css" rel="stylesheet" />
<link href="_content/Blazorise.Bootstrap/blazorise.bootstrap.css" rel="stylesheet" />
</head>
</html>
@using Blazorise
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
builder.Services
.AddBlazorise()
.AddTailwindProviders()
.AddFontAwesomeIcons();
<Button TextColor="@TextColor" Background="@(new Background(BackgroundColor))" To="@To"
Type="@ButtonType.Link" Size="Size.Large" @attributes="@AdditionalAttributes">
@ChildContent
<Icon Name="@IconName" IconStyle="IconStyle.Light"/>
</Button>
@code
{
[Parameter]
public string TextColor { get; set; }
[Parameter]
public string BackgroundColor { get; set; }
[Parameter]
public string IconName { get; set; }
[Parameter, EditorRequired]
public string To { get; set; }
[Parameter, EditorRequired]
public RenderFragment ChildContent { get; set; }
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> AdditionalAttributes { get; set; } = [];
}
First thing's first, we have the Button component, notice that it is typed as <Button> and not <button>, that is because it is not an ordinary HTML button, it is a Blazorise button component! This means it can take parameters, to allow us to customize it further!
INFO: Just like other Blazorise components, this button is framework agnostic, meaning you may use Bootstrap, TailwindCSS, or Material.
We have just enough parameters to allow for the exact customization necessary,
Here is a breakdown of what each one does:
TextColor
- The button's text color.BackgroundColor
- The button's background color, we will define custom brand colors soon.IconName
- The name of the icon displayed on the button, in this case we are using FontAwesomeTo
- The link where the user is navigated to, same as thehref
attribute on a regular linkChildContent
- The markup displayed inside the button. See blazor-universityAdditionalAttributes
- Any additional attributes the user passes to the button. Will directly be applied to the underlying button component. See blazor-university
Here are some colors, you may expand this further as you need
.bg-snapchat {
background-color: #FFFC00 !important;
}
.bg-discord {
background-color: #5865F2 !important;
}
.bg-github {
background-color: #0D1117 !important;
}
/* your colors */
NOTE: the
!important
property, this is necessary as, by default the Bootstrap icons will have theColor
property set to primary, this will shadow our custom background colors, so adding!important
at the end of them will fix this
<html>
<head>
<link href="brands.css" rel="stylesheet" />
...
</head>
</html>
Inside your page, add the freshly created icon
<ShareButton TextColor="white" BackgroundColor="github" IconName="fa-brands fa-github" To="https://github.com/ddjerqq">
Share on GitHub
</ShareButton>
NOTE notice how the
BackgroundColor
is assigned"github"
and not"bg-github"
, this is because Blazorise inserts thebg-
prefix automatically for us. This is happening because for normal components, we would have Bootstrap colors so if we passed"primary"
to theBackgroundColor
, we would getbg-primary
.
Sharing your content with the world is important, it allows your users to show their friends what your page is all about! Blazorise makes it easy to develop framework agnostic UI quickly.
Thanks for reading this blog 💗