GithubHelp home page GithubHelp logo

bakeemawaytoys / elm-css Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rtfeldman/elm-css

0.0 2.0 0.0 4.34 MB

Elm CSS preprocessor that lets you use union types rather than Strings for your classes and IDs

Home Page: http://package.elm-lang.org/packages/rtfeldman/elm-css/latest

License: BSD 3-Clause "New" or "Revised" License

JavaScript 1.70% HTML 44.94% Elm 53.25% CSS 0.11%

elm-css's Introduction

elm-css Version Travis build Status AppVeyor build status

elm-css lets you define CSS in Elm, like so:

module MyCss exposing (..)

import Css exposing (..)
import Css.Elements exposing (body, li)
import Css.Namespace exposing (namespace)


type CssClasses
    = NavBar


type CssIds
    = Page


css =
    (stylesheet << namespace "dreamwriter")
    [ body
        [ overflowX auto
        , minWidth (px 1280)
        ]
    , (#) Page
        [ backgroundColor (rgb 200 128 64)
        , color (hex "CCFFFF")
        , width (pct 100)
        , height (pct 100)
        , boxSizing borderBox
        , padding (px 8)
        , margin zero
        ]
    , (.) NavBar
        [ margin zero
        , padding zero
        , children
            [ li
                [ (display inlineBlock) |> important
                , color primaryAccentColor
                ]
            ]
        ]
    ]


primaryAccentColor =
    hex "ccffaa"

Note: (.) defines a class selector and (#) defines an ID selector.

Here's what you can do with this code:

  • You can generate a .css file from it.
  • You can use it to generate type-checked inline styles.
  • You can share NavBar and Page with your Elm view code, so your classes and IDs can never get out of sync due to a typo or refactor.
  • You can move this code into your view file and have your styles live side-by-side with your view functions themselves.

elm-css works hard to prevent invalid styles from being generated; for example, if you write color "blah" or margin (rgb 1 2 3), you'll get a type mismatch. If you write (rgb 3000 0 -3) you'll get a build-time validation error (RGB values must be between 0 and 255) if you try to compile it to a stylesheet.

elm-css draws inspiration from the excellent Sass, Stylus, and CSS Modules. It includes popular features like:

There are two popular approaches to use it.

Approach 1: Inline Styles

One way to use elm-css is for inline styles, using the asPairs function:

styles =
    Css.asPairs >> Html.Attributes.style

button [ styles [ position absolute, left (px 5) ] ]
    [ text "Whee!" ]

Approach 2: Generating CSS files

You can also use elm-css as a CSS preprocessor which generates separate .css files.

To do this, you will need to install both the node module and the Elm library:

npm install -g elm-css
elm package install rtfeldman/elm-css

Then you'll need a special module with a port for elm-css to access:

port module Stylesheets exposing (..)

import Css.File exposing (..)
import MyCss
import Html exposing (div)
import Html.App as Html


port files : CssFileStructure -> Cmd msg


cssFiles : CssFileStructure
cssFiles =
    toFileStructure [ ( "styles.css", compile [ MyCss.css ] ) ]


main : Program Never
main =
    Html.program
        { init = ( (), files cssFiles )
        , view = \_ -> (div [] [])
        , update = \_ _ -> ( (), Cmd.none )
        , subscriptions = \_ -> Sub.none
        }

Run elm-css on the file containing this Stylesheets module. Then include that css file in your web page.

The above elm-css stylesheet compiles to the following .css file:

body {
    overflow-x: auto;
    min-width: 1280px;
}

#dreamwriterPage {
    background-color: rgb(200, 128, 64);
    color: #CCFFFF;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 8px;
    margin: 0;
}

.dreamwriterNavBar {
    margin: 0;
    padding: 0;
}

.dreamwriterNavBar > li {
    display: inline-block !important;
    color: #ccffaa;
}

Try it out! (make sure you already have elm installed, e.g. with npm install -g elm)

$ npm install -g elm-css
$ git clone https://github.com/rtfeldman/elm-css.git
$ cd elm-css/examples
$ elm-css src/Stylesheets.elm
$ less homepage.css

Examples

There are a few examples to check out!

  • json-to-elm which can see be seen live here
  • the examples folder, which contains a working project with a README
  • the example above

Using elm-css with Elm view code

Here's how to use elm-css in your projects:

In your Elm code, use the same union types to represent classes and ids. Then they can't get out of sync with your CSS. To do this, you'll need special versions the of id, class, and classList functions from elm-html.

Install the handy package that combines elm-html and elm-css:

 elm package install rtfeldman/elm-css-helpers

The Elm.CssHelpers.withNamespace returns a record full of handy functions. Use that, and then construct Html using classes and ids defined in your union types. For example:

module MyView exposing (..)


import Html.CssHelpers
import MyCss


{ id, class, classList } =
    Html.CssHelpers.withNamespace "dreamwriter"


view =
    Html.div []
        [ Html.div [ class [ MyCss.NavBar ] ] [ Html.text "this has the NavBar class" ]
        , Html.div [ id MyCss.Page ] [ Html.text "this has the Page id" ]
        ]

Releases

Version Notes
5.0.0 Have compile accept multiple files, add more keywords.
4.0.2 Fix for #140
4.0.1 Fix for #136
4.0.0 Fix multiple pseudo-selectors, add cursor properties.
3.1.2 Fix extraneous space in pseudo-element output
3.1.1 Fix missing pseudo-element implementation
3.1.0 Fix bug where namespace was getting applied to ID selectors, add letterSpacing, h5, h6
3.0.0 Upgrade for Elm 0.17
2.2.0 Expose more types
1.1.0 Add Helpers
1.0.0 Initial Release

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.