Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Latest commit

 

History

History
89 lines (67 loc) · 2.07 KB

README.md

File metadata and controls

89 lines (67 loc) · 2.07 KB

Class Namespaces

Build Status

Abstraction for easier work with conventional namespaced class names in Elm + TEA. Currently only our in-house WeakCss style is supported. We're considering adding classic BEM style support in future though.

This package is highly experimental and might change a lot over time.

Feedback and contributions to both code and documentation are very welcome.

Installation

Usual elm package install:

$ elm package install GlobalWebIndex/class-namespaces

Usage

Just simply use WeakCss module's API instead of using Html.Attributes.class directly.

module Main exposing (..)

import Html exposing (Attribute, Html)
import WeakCss exposing (ClassName)


moduleClass : ClassName
moduleClass =
    WeakCss.namespace "menu"


main : Html msg
main =
    let
        navClass =
            moduleClass
                |> WeakCss.add "nav"

        itemClass =
            navClass
                |> WeakCss.add "item"

        isActive =
            -- presumably some actual logic here
            True
    in
    Html.aside
        [ moduleClass |> WeakCss.toClass ]
        [ Html.nav
            [ navClass |> WeakCss.toClass ]
            [ Html.ul
                [ navClass |> WeakCss.nest "list" ]
                [ Html.li [ itemClass |> WeakCss.withStates [] ] [ Html.text "first item" ]
                , Html.li [ itemClass |> WeakCss.withStates [ ( "active", isActive ) ] ] [ Html.text "second active item" ]
                ]
            ]
        ]

This is structure of corresponding CSS file written in SCSS:

.menu {
    &__nav {
        &--list {
        }

        &--item {
            &.active {
            }
        }
    }
}

And this is how selectors in compiled CSS file will look like:

.menu {}
.menu__nav {}
.menu__nav--list {}
.menu__nav--item {}
.menu__nav--item.active {}