css

package
v2.0.3+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2017 License: MIT Imports: 8 Imported by: 0

README

CSS

CSS provides a library which greatly simplify how we write css styles in a more flexible way by using the power of Go templates.

Install

go get -u github.com/gu-io/gu/css

Example

  • Create a new css style with properties fed in
csr := css.New(`

    $:hover {
      color: red;
    }

    $::before {
      content: "bugger";
    }

    $ div a {
      color: black;
      font-family: {{ .Font }}
    }

    @media (max-width: 400px){

      $:hover {
        color: blue;
        font-family: {{ .Font }}
      }

    }
`, nil)

  sheet, err := csr.Stylesheet(struct {
    Font string
  }{Font: "Helvetica"}, "#galatica")

  sheet.String() // => "#galatica:hover {\n  color: red;\n}\n#galatica::before {\n  content: \"bugger\";\n}\n#galatica div a {\n  color: black;\n  font-family: Helvetica;\n}\n@media (max-width: 400px) {\n  #galatica:hover {\n    color: blue;\n    font-family: Helvetica;\n  }\n}"

  • Extend parts of another css rule into a giving style selector
	csr := css.New(`
    block {
      font-family: {{ .Font }};
      color: {{ .Color }};
    }
  `, nil)

	csx := css.New(`

    ::before {
      content: "bugger";
    }

    div a {
			{{ extend "block" }}
			border: 1px solid #000;
    }

    @media (max-width: 400px){

      :hover {
        color: blue;
        font-family: {{ .Font }};
      }

    }
`, csr)

	sheet, err := csx.Stylesheet(struct {
		Font  string
		Color string
	}{
		Font:  "Helvetica",
		Color: "Pink",
	}, "#galatica")

  sheet.String() /*=>

#galatica::before {
  content: "bugger";
}
div a {
  font-family: Helvetica;
  color: Pink;
  border: 1px solid #000;
}
@media (max-width: 400px) {
  #galatica:hover {
    color: blue;
    font-family: Helvetica;
  }
}

*/

Gratitude

Thanks to the awesome work of the CSS tokenizer by the Gorilla team
and Aymerick's css parser through all whom by God's grace made this library possible.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule struct {
	// contains filtered or unexported fields
}

Rule defines the a single css rule which will be transformed and converted into a usable stylesheet during rendering.

func New

func New(rules string, extension *Rule, rs ...*Rule) *Rule

New returns a new instance of a Rule which provides capability to parse and extrapolate the giving content using the provided binding. - Arguments:

  • rules : text containing css values.
  • extension: A instance of a Rule, that may contain certain styles which can be extended into current rule styles using the `extend` template function.
  • rules: A slice of rules which should be built with this, they will also inherit this rules parents, a nice way to extend a rule sets property.

func Plain

func Plain(rule string, extension *Rule, rs ...*Rule) *Rule

Plain returns a new instance of a Rule which uses the raw rule string instead of parsing with a template has the source of the stylesheet to be parsed. No processing will be done on it.

func (*Rule) Add

func (r *Rule) Add(c *Rule) *Rule

Add adds the giving rule into the rules depends list.

func (*Rule) Stylesheet

func (r *Rule) Stylesheet(bind interface{}, parentNode string) (*bcss.Stylesheet, error)

Stylesheet returns the provided styles using the binding as the argument for the provided css template.

func (*Rule) UseExtension

func (r *Rule) UseExtension(c *Rule) *Rule

UseExtension sets the css.Rule to be used for extensions and returns the rule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL