1.6.0 • Published 3 years ago

web-element v1.6.0

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

web-element generates HTML. That's it.

You can pass in a selector, attributes, and contents and it gives you back some HTML.

var element = require("web-element")

element(
  "button.small", 
  {onclick: "doSomething()"},
  "DO IT"
).html()

Should generate:

<button class="small" onclick="doSomething()">DO IT</button>

You can also pass arrays and it will try to turn the elements into HTML.

element([
  "some text",
  ".some.classes",
  "<some>HTML</some>",
  element("a", {href: "/"}, "ok")
]).html()

Should generate:

<div>
  <div>some text</div>
  <div class="some classes"></div>
  <some>HTML</some>
  <a href="/">ok</a>
</div>

Inline styles

element("h1", "Big Big News",
  element.style({
    display: "inline"
  })
)

generates:

<h1 style="display: inline">Big Big News</h1>

Templates

If you want to build an element over and over, element.template will return a function that can be used to generate elements:

var mealTemplate = element.template(
  ".meal",
  element.style({
    "box-shadow": "1px 1px 10px #eee",
    "padding": "10px",
  }),
  function(id, title) {
    var photo = element("img", {src: "/images/"+id+".jpg"})
    var button = element("a.button", {href: "/buy/"+id}, "Deliver")
    this.addChild(photo)
    this.addChild(title)
    this.addChild(button)
  }
)

var page = element("body")

db.query("SELECT id, title FROM meals", function(id, title) {
  var meal = mealTemplate(id, title)
  page.addChild(meal)
})

page.addToHead(element.stylesheet(mealTemplate))

response.send(page.html())

Media queries

var responsive = element.style(
  ".responsive",
  {
    "@media (max-width: 600px)": {
      "font-size": "0.8em"
    }
  }
)

Descendant styles

You can also include second level styles and pseduoelements. Don't forget to escape your content strings!

var callout = element.style(
  ".callout",
  {
    "border-bottom": "2pt solid cyan",

    ".error": {
      "border-color": "red",
    },

    "::after": {
      "content": "\"\"",
      "width": "6pt",
      "height": "6pt",
      "background": "cyan",
      "vertical-align": "-2pt",
    }
  }
)

generates:

.callout {
  border-bottom: 2pt solid cyan;
}

.callout.error {
  border-color: red;
}

.callout::after {
  content: ".";
  width: 6pt;
  height: 6pt;
  background: cyan;
  vertical-align: -2pt;
}

Methods

var el = element(element.tag("zoomable-image"))

el.addSelector(".foo")

el.appendStyles({
  "display": "none"
})

el.onclick("alert(\"hi\")")

el.addChild(element("baby element"))

el.assignId() // el.id == "fj29"

el.addAttribute("src", "foo.png")

el.addAttributes({
  width: 320,
  height: 240,
})

Generates

<zoomable-image class="foo" style="display: none" onclick="alert(\"hi\")" id="fj29" src="foo.png" width="320" height="240">
  <div>baby element</div>
</zoomable-image>

If you have variable content and you want to ensure it is not interpreted as a tagname or a selector, you can use element.raw or element.escape:

var someHtml = "<img/>"
element(
  element.raw(someHtml))

produces:

<div><img/></div>

whereas

var treatAsText = "<img/>"
element(
  element.escape(treatAsText))

produces:

<div>&lt;img/&gt;</div>

Why?

When someone tries to learn how to build web apps, they have to learn Javascript, HTML, and CSS all at once. Web-element lets them build web pages using only JavaScript.

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

4 years ago

1.1.0

4 years ago

0.109.0

5 years ago

0.108.0

5 years ago

0.107.0

5 years ago

0.106.0

5 years ago

0.105.0

5 years ago

0.104.0

5 years ago

0.103.0

6 years ago

0.102.0

6 years ago

0.101.0

6 years ago

0.100.0

6 years ago

0.99.0

6 years ago

0.98.0

6 years ago

0.97.0

6 years ago

0.96.0

7 years ago

0.95.0

7 years ago

0.94.0

7 years ago

0.93.0

7 years ago

0.92.0

7 years ago

0.91.0

7 years ago

0.90.0

7 years ago

0.89.0

7 years ago

0.88.0

7 years ago

0.87.0

7 years ago

0.86.0

7 years ago

0.85.0

7 years ago

0.84.0

7 years ago

0.83.0

7 years ago

0.82.0

7 years ago

0.81.0

7 years ago

0.80.0

7 years ago

0.79.0

7 years ago

0.78.0

7 years ago

0.77.0

7 years ago

0.76.0

7 years ago

0.75.0

7 years ago

0.74.0

7 years ago

0.73.0

7 years ago

0.72.0

7 years ago

0.71.0

7 years ago

0.70.0

7 years ago

0.69.0

7 years ago

0.68.0

7 years ago

0.67.0

7 years ago

0.66.0

7 years ago

0.65.0

7 years ago

0.64.0

7 years ago

0.63.0

7 years ago

0.62.0

7 years ago

0.61.0

7 years ago

0.60.0

7 years ago

0.59.0

7 years ago

0.58.0

7 years ago

0.57.0

7 years ago

0.56.0

8 years ago

0.55.0

8 years ago

0.54.0

8 years ago

0.53.0

8 years ago