CurrieTechnologies.Razor.SweetAlert2
5.6.0
dotnet add package CurrieTechnologies.Razor.SweetAlert2 --version 5.6.0
NuGet\Install-Package CurrieTechnologies.Razor.SweetAlert2 -Version 5.6.0
<PackageReference Include="CurrieTechnologies.Razor.SweetAlert2" Version="5.6.0" />
paket add CurrieTechnologies.Razor.SweetAlert2 --version 5.6.0
#r "nuget: CurrieTechnologies.Razor.SweetAlert2, 5.6.0"
// Install CurrieTechnologies.Razor.SweetAlert2 as a Cake Addin #addin nuget:?package=CurrieTechnologies.Razor.SweetAlert2&version=5.6.0 // Install CurrieTechnologies.Razor.SweetAlert2 as a Cake Tool #tool nuget:?package=CurrieTechnologies.Razor.SweetAlert2&version=5.6.0
CurrieTechnologies.Razor.SweetAlert2
<br/> ➕ <br/> <br/> A beautiful, responsive, customizable, accessible (WAI-ARIA) replacement for JavaScript's popup boxes. <br/> All wrapped inside a Razor Component Library for use in Blazor Server and WebAssembly applications. <br/> <br/> See SweetAlert2 in action ↗ |
Master | Develop | Version | Downloads | Mergify |
---|---|---|---|---|
👉 Upgrading from v3.x to v4.x? Read the release notes!
👉 Upgrading from v4.x to v5.x? Read the release notes!
This package is for both Blazor Server Apps and Blazor WebAssembly Apps. It should be used instead of CurrieTechnologies.Blazor.SweetAlert2
which is now deprecated.
🙌 Includes themes from the Official SweetAlert2 Themes project 🙌
Installation
Install-Package CurrieTechnologies.Razor.SweetAlert2
Or install from the NuGet Package Manager
Usage
Register the service in your Startup file.
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSweetAlert2();
...
}
NB: For newer .NET 6 minimal project templates, this will actually be in Program.cs
// Program.cs
...
builder.Services.AddSweetAlert2();
OR
If you want to use one of the Official SweetAlert2 themes:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSweetAlert2(options => {
options.Theme = SweetAlertTheme.Dark;
});
...
}
See Configuration for more information.
Add the script tag
Add this script tag in your root html file (Likely _Host.cshtml for Blazor Server or index.html for Blazor WebAssembly), right under the framework script tag. (i.e <script src="_framework/blazor.server.js"></script>
for Blazor Server or <script src="_framework/blazor.webassembly.js"></script>
for Blazor WebAssembly)
NB: In newer templates, this will be towards the bottom of Pages/_Layout.cshtml
for Blazor Server or wwwroot/index.html
for Blazor WebAssembly.
<script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></script>
If you need to support IE11, this script tag is different. See IE Compatibility.
_Imports.razor
Recommended: Add @using CurrieTechnologies.Razor.SweetAlert2
to your _Imports.razor
file, to avoid having to put the using call in every component that requires it.
Inject the SweetAlertService into any Blazor component.
// Sample.razor
@inject SweetAlertService Swal;
<button class="btn btn-primary"
@onclick="@(async () => await Swal.FireAsync("Any fool can use a computer"))">
Try me!
</button>
Examples
The most basic message:
await Swal.FireAsync("Hello world!");
A message signaling an error:
await Swal.FireAsync("Oops...", "Something went wrong!", "error");
Handling the result of SweetAlert2 modal:
// async/await
SweetAlertResult result = await Swal.FireAsync(new SweetAlertOptions
{
Title = "Are you sure?",
Text = "You will not be able to recover this imaginary file!",
Icon = SweetAlertIcon.Warning,
ShowCancelButton = true,
ConfirmButtonText = "Yes, delete it!",
CancelButtonText = "No, keep it"
});
if (!string.IsNullOrEmpty(result.Value))
{
await Swal.FireAsync(
"Deleted",
"Your imaginary file has been deleted.",
SweetAlertIcon.Success
);
}
else if (result.Dismiss == DismissReason.Cancel)
{
await Swal.FireAsync(
"Cancelled",
"Your imaginary file is safe :)",
SweetAlertIcon.Error
);
}
// Promise/Task based
Swal.FireAsync(new SweetAlertOptions
{
Title = "Are you sure?",
Text = "You will not be able to recover this imaginary file!",
Icon = SweetAlertIcon.Warning,
ShowCancelButton = true,
ConfirmButtonText = "Yes, delete it!",
CancelButtonText = "No, keep it"
}).ContinueWith(swalTask =>
{
SweetAlertResult result = swalTask.Result;
if (!string.IsNullOrEmpty(result.Value))
{
Swal.FireAsync(
"Deleted",
"Your imaginary file has been deleted.",
SweetAlertIcon.Success
);
}
else if (result.Dismiss == DismissReason.Cancel)
{
Swal.FireAsync(
"Cancelled",
"Your imaginary file is safe :)",
SweetAlertIcon.Error
);
}
});
More examples can be found on the SweetAlert2 project site
Configuration
In Startup.cs
you have the opportunity to configure how sweetalert2
will behave in your application.
Theme
With SweetAlertServiceOptions.Theme
you can specify one of the official sweetalert2 themes to apply to your modal throughout your application.
SetThemeForColorSchemePreference()
With the SweetAlertServiceOptions.SetThemeForColorSchemePreference()
method, you can specify which theme the user uses, based on the result of their prefers-color-scheme
CSS media query. Most commonly this can be used to help create a dark version of your application, based on user preference. Browsers that do not support the prefers-color-scheme
media query will fall back to the theme specified in SweetAlertServiceOptions.Theme
Theme Examples
If you want the default theme by default, and the dark theme if the user prefers a dark color scheme:
services.AddSweetAlert2(options => {
options.SetThemeForColorSchemePreference(ColorScheme.Dark, SweetAlertTheme.Dark);
});
A dark theme by default, and a lighter theme if the user prefers a light color scheme:
services.AddSweetAlert2(options => {
options.Theme = SweetAlertTheme.Dark;
options.SetThemeForColorSchemePreference(ColorScheme.Light, SweetAlertTheme.Bootstrap4);
});
A minimal theme as a fallback, and a dark/light theme to match user preference:
services.AddSweetAlert2(options => {
options.Theme = SweetAlertTheme.Minimal;
options.SetThemeForColorSchemePreference(ColorScheme.Light, SweetAlertTheme.Default);
options.SetThemeForColorSchemePreference(ColorScheme.Dark, SweetAlertTheme.Dark);
});
See prefers-color-scheme for more information.
Default Settings
If you want some settings globally applied to all of your SweetAlert2 dialogs, configure your default settings in Startup.cs
services.AddSweetAlert2(options => {
options.DefaultOptions = new SweetAlertOptions {
HeightAuto = false
};
});
These can be overriden in individual FireAsync()
calls.
NB: This will only apply to FireAsync()
calls that take a SweetAlertOptions
object as a parameter. The methods that take in primitive types bypass SweetAlertOptions
entirely on both the C# and JS libraries.
Notable differences from the JavaScript library
- No methods that return an HTMLElement are included (e. g.
Swal.getHtmlContainer()
) - The value of a
SweetAlertResult
(result.Value
) can only be a string. Numbers and booleans must be converted. Object must be parsed to/from JSON in your code. DidOpenAsync()
,WillCloseAsync()
,WillOpenAsync()
, andDidCloseAsync()
can all take asynchronous callbacks. 🎉 (none will return an HTMLElement though.)- No support for
<optgroup>
in the select input type. - No async option for
InputOptions
orInputValue
- Callbacks must be passed inside of objects specifically designed for the given callback property. e.g. the
InputValidator
property takes anInputValidatorCallback
created like so:
new SweetAlertOptions {
...
InputValidator = new InputValidatorCallback((string input) => input.Length == 0 ? "Please provide a value" : null, this),
...
}
this
is passed in so that the Blazor EventCallback
used behind the scenes can trigger a re-render if the state of the calling component was changed in the callback. If the callback does not require the calling component to re-render, passing in this
is optional.
These callbacks are necessary because there is currently no way to create an EventCallback
in Blazor that isn't a component parameter without using the EventCallbackFactory
which is clunky. It also allows the callback to return a value that can be used by the SweetAlert2 library. (e.g. A validation message to show if input validation fails.) Native Blazor EventCallback
s only return generic Task
s.
Browser compatibility
IE11* | Edge | Chrome | Firefox | Safari | Opera | UC Browser |
---|---|---|---|---|---|---|
❌ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
IE Compatibility*
IE Compatibility has been removed in v5
due to sweetalert2@11
dropping their support for it.
If you need to support IE11, use v4
or earlier, and use this script tag instead. (file size is about 35% larger)
<script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.ieCompat.min.js"></script>
You will also likely need to utilize the Blazor.Polyfill library, for general Blazor functionality in IE.
Protestware
Currently, the original sweetalert2
library contains protestware to disable Russian websites using sweetalert2
when visited by Russian-speaking users. This wrapper library bypasses the effects of that protestware, so there should be no undesired effects when using this library.
Related projects
- SweetAlert2 - Original SweetAlert2 project
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Components (>= 3.1.0)
- Microsoft.AspNetCore.Components.Web (>= 3.1.0)
-
net6.0
- Microsoft.AspNetCore.Components (>= 6.0.25)
- Microsoft.AspNetCore.Components.Web (>= 6.0.25)
-
net7.0
- Microsoft.AspNetCore.Components (>= 7.0.14)
- Microsoft.AspNetCore.Components.Web (>= 7.0.14)
-
net8.0
- Microsoft.AspNetCore.Components (>= 8.0.0)
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on CurrieTechnologies.Razor.SweetAlert2:
Package | Downloads |
---|---|
OneLine.Blazor
OneLine.Blazor is a set of components and utilities to be used in blazor context. |
|
Necnat.Abp.NnLibCommon.Blazor
Package Description |
|
Tuic
Web front-end components package for use in Blazor WASM. The components utilize Bootstrap 5.0 and based on Tabler. |
|
TucComponents
Web front-end components package for use in Blazor WASM. The components utilize Bootstrap 5.0. |
|
Zoy.Core.App.Web
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
5.6.0 | 60,729 | 3/19/2024 |
5.5.0 | 82,614 | 3/29/2023 |
5.4.0 | 26,994 | 12/14/2022 |
5.3.0 | 9,767 | 10/11/2022 |
5.2.1 | 47,330 | 4/13/2022 |
5.2.0 | 10,180 | 3/16/2022 |
5.1.0 | 67,909 | 11/8/2021 |
5.0.3 | 7,876 | 10/6/2021 |
5.0.2 | 2,825 | 9/10/2021 |
5.0.1 | 8,257 | 6/23/2021 |
5.0.0 | 4,078 | 6/16/2021 |
5.0.0-preview.1 | 168 | 6/16/2021 |
4.5.0 | 8,079 | 5/7/2021 |
4.4.0 | 4,359 | 4/7/2021 |
4.3.1 | 22,874 | 11/20/2020 |
4.3.0 | 8,349 | 11/5/2020 |
4.2.0 | 8,522 | 11/2/2020 |
4.1.0 | 2,025 | 10/22/2020 |
4.0.0 | 5,896 | 10/14/2020 |
3.0.0 | 2,855 | 9/17/2020 |
2.10.1 | 5,815 | 8/10/2020 |
2.10.0 | 5,614 | 7/14/2020 |
2.9.1 | 1,883 | 7/14/2020 |
2.9.0 | 2,705 | 6/26/2020 |
2.8.2 | 2,568 | 6/16/2020 |
2.8.1 | 3,003 | 6/8/2020 |
2.8.0 | 1,990 | 6/5/2020 |
2.7.0 | 4,356 | 5/21/2020 |
2.6.7 | 2,164 | 5/13/2020 |
2.6.6 | 30,770 | 4/20/2020 |
2.6.5 | 3,616 | 4/6/2020 |
2.6.4 | 2,187 | 4/1/2020 |
2.6.3 | 2,014 | 3/25/2020 |
2.6.2 | 2,001 | 3/23/2020 |
2.6.1 | 1,908 | 3/19/2020 |
2.6.0 | 3,580 | 3/9/2020 |
2.5.4 | 2,538 | 2/24/2020 |
2.5.3 | 2,011 | 2/19/2020 |
2.5.2 | 2,476 | 2/7/2020 |
2.5.1 | 2,091 | 1/30/2020 |
2.5.0 | 2,002 | 1/22/2020 |
2.4.1 | 2,010 | 1/21/2020 |
2.4.0 | 2,070 | 1/15/2020 |
2.3.2 | 1,996 | 1/15/2020 |
2.3.1 | 2,005 | 12/31/2019 |
2.3.0 | 4,600 | 12/12/2019 |
2.2.2 | 1,966 | 12/10/2019 |
2.2.1 | 2,177 | 12/6/2019 |
2.2.0 | 2,096 | 12/3/2019 |
2.1.14 | 1,905 | 12/3/2019 |
2.1.13 | 1,989 | 12/3/2019 |
2.1.12 | 1,920 | 12/2/2019 |
2.1.11 | 2,009 | 11/25/2019 |
2.1.10 | 1,882 | 11/21/2019 |
2.1.9 | 1,915 | 11/19/2019 |
2.1.8 | 1,933 | 11/19/2019 |
2.1.7 | 1,924 | 11/18/2019 |
2.1.6 | 1,890 | 11/15/2019 |
2.1.5 | 1,917 | 11/15/2019 |
2.1.4 | 1,854 | 11/15/2019 |
2.1.3 | 1,924 | 11/14/2019 |
2.1.2 | 1,903 | 11/13/2019 |
2.1.1 | 2,212 | 11/12/2019 |
2.1.0 | 1,956 | 11/11/2019 |
2.0.2 | 1,980 | 11/8/2019 |
2.0.1 | 1,966 | 11/6/2019 |
2.0.0 | 2,048 | 11/5/2019 |
1.2.4 | 2,024 | 11/4/2019 |
1.2.3 | 1,967 | 11/1/2019 |
1.2.2 | 1,967 | 10/23/2019 |
1.2.1 | 1,982 | 10/18/2019 |
1.2.0 | 1,938 | 10/17/2019 |
1.1.1 | 1,949 | 10/16/2019 |
1.1.0 | 1,981 | 10/11/2019 |
1.0.4 | 1,921 | 10/10/2019 |
1.0.3 | 1,872 | 10/9/2019 |
1.0.2 | 1,954 | 10/7/2019 |
1.0.1 | 2,018 | 9/30/2019 |
1.0.0 | 1,986 | 9/23/2019 |
0.10.2 | 340 | 9/19/2019 |
0.10.1 | 362 | 9/17/2019 |
0.10.0 | 325 | 9/16/2019 |
0.9.1 | 319 | 9/16/2019 |
0.9.0 | 338 | 9/4/2019 |
0.8.5 | 321 | 9/4/2019 |
0.8.0 | 321 | 9/3/2019 |
0.7.1 | 346 | 8/22/2019 |
0.7.0 | 340 | 8/19/2019 |
0.6.1 | 331 | 8/16/2019 |
0.6.0 | 329 | 8/13/2019 |
0.5.0 | 325 | 8/12/2019 |
0.4.5 | 342 | 8/9/2019 |
0.4.4 | 340 | 8/5/2019 |
0.4.3 | 338 | 8/2/2019 |
0.4.2 | 337 | 8/2/2019 |
0.4.1 | 331 | 7/29/2019 |
0.4.0 | 342 | 7/23/2019 |
0.3.1 | 1,973 | 7/18/2019 |
0.3.0 | 2,036 | 7/18/2019 |
0.2.9 | 2,026 | 7/17/2019 |
0.2.2 | 1,938 | 7/15/2019 |
0.2.1 | 2,023 | 7/9/2019 |
0.2.0 | 2,008 | 6/17/2019 |
0.1.3 | 2,061 | 6/14/2019 |
0.1.2 | 1,998 | 6/14/2019 |
0.1.0 | 2,060 | 6/14/2019 |
add .NET 8 support