picostyle icon indicating copy to clipboard operation
picostyle copied to clipboard

Refer components' class name in the component

Open matype opened this issue 6 years ago • 1 comments

https://github.com/morishitter/picostyle/issues/51

Ex:

const Foo = style("div")({
  // some declarations
  "~ ${this-components-classname}": {
    margin-start: "1em",
  }
})

matype avatar Nov 06 '18 08:11 matype

Do you want to use a keyword and replace the keyword with the generated class name? Maybe handlebars syntax?

Example:

const Foo = style("div")({
  // some declarations
  "~ {class}": {
    margin-start: "1em",
  }
})

//---- index.js ----
function createStyle(rules, cssType, p) {
  var id = p + _id++
  var name = cssType + id
  rules.forEach(function(rule) {
    if (/^@/.test(rule)) {
      var start = rule.indexOf("{") + 1
      rule = rule.slice(0, start) + name + rule.slice(start)
    } else {
      rule = name + rule
    }
   //=========================
   // Replace the {class} keyword with the generated class name
    rule.replace("{class}", name)
   //=========================
    sheet.insertRule(rule, sheet.cssRules.length)
  })
  return id
}

B-Teague avatar Apr 29 '19 02:04 B-Teague