Skip to content

Add CSS support for adding Shadows to VisualElement #27157

@sthewissen

Description

@sthewissen

Description

Currently, while all VisualElement objects in .NET MAUI support the Shadow property with attributes such as Offset (x, y), Opacity, Brush (color), and Radius, there is no native support for defining shadows directly through CSS. This feature request proposes the addition of CSS syntax (either through the CSS equivalent box-shadow or a custom MAUI format) and corresponding support for styling shadows in .NET MAUI.

Public API Changes

I would expect the following API changes to be required, please do add to this list as I'm sure it's not entirely complete.

  • Add StyleProperty declarations to AssemblyInfo.cs (also unsure whether the PropertyOwnerType setup is correct here).
[assembly: StyleProperty("shadow", typeof(VisualElement), nameof(VisualElement.ShadowProperty))]
[assembly: StyleProperty("shadow-color", typeof(VisualElement), nameof(Shadow.BrushProperty), PropertyOwnerType = typeof(Shadow))]
[assembly: StyleProperty("shadow-offset", typeof(VisualElement), nameof(Shadow.OffsetProperty), PropertyOwnerType = typeof(Shadow))]
[assembly: StyleProperty("shadow-radius", typeof(VisualElement), nameof(Shadow.RadiusProperty), PropertyOwnerType = typeof(Shadow))]
[assembly: StyleProperty("shadow-opacity", typeof(VisualElement), nameof(Shadow.OpacityProperty), PropertyOwnerType = typeof(Shadow))]
  • Add TypeConverter to allow conversion from a string to a completely defined Shadow.
  • The necessary plumbing to combine the CSS with actually setting the properties and assigning a Shadow object.
  • Support for setting only some of the Shadow properties and combining them through multiple CSS classes.

To support setting individual properties we will probably need to come up with a mechanism that can also initialize a Shadow object, given that all the other CSS-related styling is mapping properties whereas this is setting properties of a property. Perhaps something like an attached property setup that could set Shadow.Offset etc?

Intended Use-Case

Using CSS to style the shadows, we could either use it to set a complete shadow in a single line, or use various classes and combine them accordingly through StyleClass to make a shadow. Given that Shadow translates so nicely to CSS, I feel like it's a missing piece that can go a long way to users styling their apps in a modern fashion.

/* Scenario 1: */
.shadow-singleline {
   shadow: 4 4 8 #000000 0.5; /* OffsetX, OffsetY, Radius, Color, Opacity */
}

/* Scenario 2: */
.shadow-md {
    shadow-offset: 4 4; 
    shadow-opacity: 0.5; 
    shadow-radius: 8;
}

.shadow-black {
    shadow-color: #000000;
}

Which would be used as such in XAML:

<Label Text="Single Line Shadow" StyleClass="shadow-singleline" />
<Label Text="Combined Shadow" StyleClass="shadow-md,shadow-black" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions