Code Monkey home page Code Monkey logo

sharp.css's Introduction

Sharp.CSS

Write strongly typed CSS in C#.

deployment

NuGet Badge NuGet Badge NuGet Badge

Usage

Blazor

  1. Install Sharp.CSS.Blazor and Sharp.CSS.Generators packages
  2. Add the following namespaces to the _Import.razor file.
@using Sharp.CSS.Interfaces
@using Sharp.CSS.CssStyleSets
@using Sharp.CSS.Blazor
@using Sharp.CSS.Types
@using Sharp.CSS.Types.Generated // if using generators to generate output types
  1. Add <SharpCssStyles /> component to the App.razor file.
  2. In the Program.cs register SharpCSS with DI by adding builder.Services.AddSharpCss();

Generating component styles

Sharp.CSS is able to generate multiple CSS classes for your component at once:

Styles = Css.GetClassNames<CounterStyles>(new
{
    Header = new StyleSet
    {
        FontSize = "40px"
    },
    Counter = new StyleSet
    {
        Padding = 10,
        FontWeight = "bold",
        Border = new Border(3, BorderSideStyle.Dotted, Color.Green)
    },
    Button = new StyleSet
    {
        BackgroundColor = Color.DeepSkyBlue
    }
});

By default Sharp.CSS will generate output class automatically if you have installed Sharp.CSS.Generators. Type the name of the class in the generic parameter of GetClassNames and it will be generated. In the example above, generated CounterStyles class will look like:

public class CounterStyles
{
    public string Header { get; private set; }
    public string Counter { get; private set; }
    public string Button { get; private set; }
}

Make sure to include using Sharp.CSS.Types.Generated everywhere you plan to use generated types.

Alternatively, if you don't want Sharp.CSS to generate output class, specify an existing class. Make sure that each StyleSet property on the input object, have a corresponding string property on the output class.

Here is an example of how to use output class in the markup:

<h1 class="@Styles.Header">Counter</h1>
<p class="@Styles.Counter">Current count: @currentCount</p>
<button class="btn btn-primary @Styles.Button" @onclick="IncrementCount">Click me</button>

Injecting styles into components

The most convenient way to use styles is to inject them via dependency injection. To do that, first the style needs to be registered with the DI. This is done in the Program.cs where container is configured. AddSharpCss takes an optional parameter that allows to provide styles configuration.

builder.Services.AddSharpCss(cfg =>
{
    cfg.RegisterStyles<InjectedCounterStyles>(new
    {
        Header = new StyleSet
        {
            FontSize = "80px"
        },
        Counter = new StyleSet
        {
            Padding = 40,
            FontWeight = "bold",
            Border = new Border(3, BorderSideStyle.Groove, Color.Navy)
        },
        Button = new StyleSet
        {
            BackgroundColor = Color.YellowGreen
        }
    });
});

Now InjectedCounterStyles can be injected into component via @inject attribute.

@inject InjectedCounterStyles iStyles

<h1 class="@iStyles.Header">Counter</h1>
<p class="@iStyles.Counter">Current count: @currentCount</p>
<button class="btn btn-primary @iStyles.Button" @onclick="IncrementCount">Click me</button>

Styles configuration can be extracted into modules to simplify registration code. Module is a class that implements IStylesModule for example:

public class CounterStylesModule : IStylesModule
{
    public void Configure(SharpCssConfigurator configurator)
    {
        configurator.RegisterStyles<ModuleCounterStyles>(new
        {
            Header = new StyleSet
            {
                FontSize = "55px"
            },
            Counter = new StyleSet
            {
                Padding = 20,
                FontWeight = "bold",
                Border = new Border(3, BorderSideStyle.Dashed, Color.Pink)
            },
            Button = new StyleSet
            {
                BackgroundColor = Color.Magenta
            }
        });
    }
}

To register a module, call:

builder.Services.AddSharpCss(cfg =>
{
    cfg.RegisterStylesModule(new CounterStylesModule());
});

Generating single CSS class

ICssStyleService type exposes GetClassName capable of generating a single CSS class.

@inject ICssStyleService Css

<h1 class="@Css.GetClassName(new() { FontSize = "80px" })">Counter</h1>

Refer to samples for more details.

sharp.css's People

Contributors

berserkerdotnet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.