Applying

Deliver rules as static, generated and dynamic stylesheets, and drive them from actions.

This page covers delivering rules to the browser: the three kinds of stylesheet, the handler that writes them, the engine that inserts dynamic rules, the actions that drive it, and the errors. The Styling page covers writing rules.

Every example on this page is live. Its bound rule is in this site's Dynamic style, and the controls change it through the same actions your app would fire. The CSS tab shows the text of the rules for the demo's starting state.

§1 Stylesheets

                  
                    type
                     
                    Style
                     
                    =
                     
                    app
                    .
                    Stylesheet
                    


                    func
                     
                    Static
                    (
                    path
                     
                    string
                    )
                     
                    Style
                    

                    func
                     
                    Generated
                    (
                    path
                     
                    string
                    ,
                     
                    items
                     
                    ...
                    Item
                    )
                     
                    Style
                    

                    func
                     
                    Dynamic
                    (
                    items
                     
                    ...
                    Item
                    )
                     
                    Style
                  
                

Style is app.Stylesheet , the interface app.Handler.Styles takes, and these three constructors make the kinds the handler understands. The handler turns a list of them into links and files, under Handler and Build . Item has unexported methods too. *Rule , *KeyframesRule , *FontFaceRule , *PropertyRule , *ImportRule , *LayerRule and *LayerOrderRule implement it, and each has Err() error . A nil Style in a handler's list, and a nil item passed to Generated or Dynamic , are ignored.

native

On the server

Handler.Styles []style.Style
Static →
check the file a /web/ path, or a URL
→
<link>
Generated →
render the items write <WebDir>/….css
→
<link>
Dynamic →
validate only no file, no link
wasm

In the browser

style.Run(styles...) before RunWhenOnBrowser
→
dynamic <style> rules follow the actions
Three kinds of stylesheet in one list. On the server, Build turns them into files and links; in the browser, Run inserts the dynamic rules.

The three kinds are chosen per stylesheet, and one handler can mix them:

  • Static(path) links a .css file that already exists.
    • A path starting with /web/ is looked up in WebDir . If the file is missing, Build returns app.ErrMissingStyle .
    • A URL starting with http:// , https:// or // is linked as given and is not checked.
    • Any other path is app.ErrPathOutsideWeb .
    • The content of the file is never read or validated.
  • Generated(path, items...) writes a .css file from the items. The path must start with /web/ (otherwise app.ErrPathOutsideWeb ) and end in .css (otherwise app.ErrNotCSSPath ). Build renders the items and writes the text to <WebDir>/… , creating directories as needed, only if the bytes differ from what is already on disk. The file is then linked like a Static one. A Generated style with no items, or whose items have no properties, still writes an empty file.
  • Dynamic(items...) adds no link and writes no file. It takes *Rule and *KeyframesRule values that carry a binding. Build only validates them. They are inserted into and removed from the page in the browser, as described under Run and Actions .

A Generated file writes every @layer order statement first, then every @import , then everything else, each group in slice order, with one blank line between items.

Paths are cleaned before use, so /web/./app.css and /web/x/../app.css both mean /web/app.css , and the link written into the page is the cleaned path. A path that leaves /web/ once cleaned, such as /web/../secret.css , is app.ErrPathOutsideWeb . URLs are not cleaned.

Use Static for CSS that has no typed form, such as a property the generator leaves out, and for a stylesheet hosted elsewhere.

Resolve

                  
                    type
                     
                    Kind
                     
                    =
                     
                    app
                    .
                    StyleKind
                    


                    const
                     
                    (
                    
	
                    StaticStyle
                        
                    =
                     
                    app
                    .
                    StaticStyle
                    
	
                    GeneratedStyle
                     
                    =
                     
                    app
                    .
                    GeneratedStyle
                    
	
                    DynamicStyle
                       
                    =
                     
                    app
                    .
                    DynamicStyle
                    

                    )
                    


                    type
                     
                    Source
                     
                    =
                     
                    app
                    .
                    StyleSource
                    


                    func
                     
                    Resolve
                    (
                    s
                     
                    Style
                    )
                     
                    (
                    Source
                    ,
                     
                    error
                    )
                  
                

Resolve is what the handler calls on each style. It checks the items and returns the style's kind, its path as given, and for a Generated style the rendered bytes, which are empty but not nil when the style has no items. A Static style is not checked beyond that, and a Dynamic one has no path. The errors it returns are the item errors under Errors , and on an error the Source is zero. A nil Style resolves to a zero Source with no error. It is the same as calling the style's own StyleSource method. You need Resolve only to deliver styles through something other than app.Handler .

A complete app

Two rules, delivered in a Generated file. The rules live in a package of their own, theme , which also holds the one function that lists the app's stylesheets:

                  
                    package
                     
                    theme
                    


                    import
                     
                    (
                    
	
                    "github.com/Toms223/WebGo/css"
                    
	
                    "github.com/Toms223/WebGo/kw"
                    
	
                    "github.com/Toms223/WebGo/media"
                    
	
                    "github.com/Toms223/WebGo/style"
                    

                    )
                    


                    var
                     
                    Card
                     
                    =
                     
                    style
                    .
                    Class
                    (
                    "card"
                    )
                    .
                    
	
                    Set
                    (
                    css
                    .
                    Padding
                    (
                    css
                    .
                    Rem
                    (
                    1
                    )
                    )
                    ,
                     
                    css
                    .
                    BorderRadius
                    (
                    css
                    .
                    Px
                    (
                    8
                    )
                    )
                    )
                    


                    var
                     
                    Link
                     
                    =
                     
                    style
                    .
                    Class
                    (
                    "nav-link"
                    )
                    .
                    
	
                    Set
                    (
                    css
                    .
                    Color
                    (
                    css
                    .
                    Var
                    (
                    "--ink"
                    )
                    )
                    )
                    .
                    
	
                    Hover
                    (
                    css
                    .
                    TextDecoration
                    (
                    kw
                    .
                    Underline
                    )
                    )
                    .
                    
	
                    Media
                    (
                    media
                    .
                    Screen
                    (
                    )
                    .
                    And
                    (
                    media
                    .
                    MaxWidth
                    (
                    css
                    .
                    Px
                    (
                    600
                    )
                    )
                    )
                    ,
                     
                    css
                    .
                    FontSize
                    (
                    css
                    .
                    Rem
                    (
                    0.9
                    )
                    )
                    )
                    


                    func
                     
                    Styles
                    (
                    )
                     
                    [
                    ]
                    style
                    .
                    Style
                     
                    {
                    
	
                    return
                     
                    [
                    ]
                    style
                    .
                    Style
                    {
                    
		
                    style
                    .
                    Generated
                    (
                    "/web/theme.css"
                    ,
                     
                    Card
                    ,
                     
                    Link
                    )
                    ,
                    
	
                    }
                    

                    }
                  
                

The program that uses it, in a module named example.com/hello :

                  
                    package
                     
                    main
                    


                    import
                     
                    (
                    
	
                    "log"
                    
	
                    "net/http"
                    

	
                    "example.com/hello/theme"
                    
	
                    "github.com/Toms223/WebGo/app"
                    
	
                    "github.com/Toms223/WebGo/style"
                    

                    )
                    


                    type
                     
                    hello
                     
                    struct
                     
                    {
                    
	
                    app
                    .
                    Compo
                    

                    }
                    


                    func
                     
                    (
                    h
                     
                    *
                    hello
                    )
                     
                    Render
                    (
                    )
                     
                    app
                    .
                    UI
                     
                    {
                    
	
                    return
                     
                    app
                    .
                    Div
                    (
                    )
                    .
                    Style
                    (
                    theme
                    .
                    Card
                    )
                    .
                    Body
                    (
                    
		
                    app
                    .
                    A
                    (
                    )
                    .
                    Style
                    (
                    theme
                    .
                    Link
                    )
                    .
                    Href
                    (
                    "/"
                    )
                    .
                    Text
                    (
                    "Home"
                    )
                    ,
                    
	
                    )
                    

                    }
                    


                    func
                     
                    main
                    (
                    )
                     
                    {
                    
	
                    app
                    .
                    Route
                    (
                    "/"
                    ,
                     
                    func
                    (
                    )
                     
                    app
                    .
                    Composer
                     
                    {
                     
                    return
                     
                    &
                    hello
                    {
                    }
                     
                    }
                    )
                    
	
                    style
                    .
                    Run
                    (
                    theme
                    .
                    Styles
                    (
                    )
                    ...
                    )
                    
	
                    app
                    .
                    RunWhenOnBrowser
                    (
                    )
                    

	
                    handler
                     
                    :=
                     
                    &
                    app
                    .
                    Handler
                    {
                    
		
                    Name
                    :
                       
                    "Hello"
                    ,
                    
		
                    Styles
                    :
                     
                    theme
                    .
                    Styles
                    (
                    )
                    ,
                    
	
                    }
                    
	
                    if
                     
                    err
                     
                    :=
                     
                    handler
                    .
                    Build
                    (
                    )
                    ;
                     
                    err
                     
                    !=
                     
                    nil
                     
                    {
                    
		
                    log
                    .
                    Fatal
                    (
                    err
                    )
                    
	
                    }
                    
	
                    http
                    .
                    Handle
                    (
                    "/"
                    ,
                     
                    handler
                    )
                    
	
                    log
                    .
                    Fatal
                    (
                    http
                    .
                    ListenAndServe
                    (
                    ":8080"
                    ,
                     
                    nil
                    )
                    )
                    

                    }
                  
                

Build writes web/theme.css and links /web/theme.css in every page. Calling it yourself surfaces errors before the server starts; without the call, the first request builds, and on an error every request answers with a 500. The file contains:

                  
                    .card {
                    

                    	
                    padding
                    : 
                    1rem;
                    

                    	
                    border-radius
                    : 
                    8px;
                    

                    }
                    


                    

                    .nav-link {
                    

                    	
                    color
                    : 
                    var(--ink);
                    

                    }
                    


                    

                    .nav-link:hover {
                    

                    	
                    text-decoration
                    : 
                    underline;
                    

                    }
                    


                    

                    @media screen and (max-width: 600px) {
                    

                    	
                    .nav-link {
                    

                    		
                    font-size
                    : 
                    0.9rem;
                    

                    	
                    }
                    

                    }
                  
                
01