0.7.0 • Published 3 years ago

object-dom v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

object-dom

Declarative dom with 1:1 mapping of objects and tags, typed css, reactive updates and no bundler needed.

styled with prettier Tests Published on npm

  • ✅ No Dependencies
  • ✅ ES Modules
  • ✅ Full Browser Support
  • ✅ Full types for CSS
  • ✅ Full types for DOM Events
  • ✅ Full types for Attributes
  • ✅ 100% Typescript

Supported Tags

https://www.w3schools.com/TAGS/default.ASP

TagClassDescription
<a>ADefines a hyperlink
<abbr>AbbrDefines an abbreviation or an acronym
<address>AddressDefines contact information for the author/owner of a document
<area>AreaDefines an area inside an image map
<article>ArticleDefines an article
<aside>AsideDefines content aside from the page content
<audio>AudioDefines embedded sound content
<b>BDefines bold text
<base>BaseSpecifies the base URL/target for all relative URLs in a document
<bdi>BdiIsolates a part of text that might be formatted in a different direction from other text outside it
<bdo>BdoOverrides the current text direction
<blockquote>BlockquoteDefines a section that is quoted from another source
<body>BodyDefines the document's body
<br>BrDefines a single line break
<button>ButtonDefines a clickable button
<canvas>CanvasUsed to draw graphics, on the fly, via scripting (usually JavaScript)
<caption>CaptionDefines a table caption
<cite>CiteDefines the title of a work
<code>CodeDefines a piece of computer code
<col>ColSpecifies column properties for each column within a <colgroup> element
<colgroup>ColgroupSpecifies a group of one or more columns in a table for formatting
<data>DataAdds a machine-readable translation of a given content
<datalist>DatalistSpecifies a list of pre-defined options for input controls
<dd>DdDefines a description/value of a term in a description list
<del>DelDefines text that has been deleted from a document
<details>DetailsDefines additional details that the user can view or hide
<dfn>DfnSpecifies a term that is going to be defined within the content
<dialog>DialogDefines a dialog box or window
<div>DivDefines a section in a document
<dl>DlDefines a description list
<dt>DtDefines a term/name in a description list
<em>EmDefines emphasized text
<embed>EmbedDefines a container for an external application
<fieldset>FieldsetGroups related elements in a form
<figcaption>FigcaptionDefines a caption for a <figure> element
<figure>FigureSpecifies self-contained content
<footer>FooterDefines a footer for a document or section
<form>FormDefines an HTML form for user input
<h1>H1Defines HTML headings
<h2>H2Defines HTML headings
<h3>H3Defines HTML headings
<h4>H4Defines HTML headings
<h5>H5Defines HTML headings
<h6>H6Defines HTML headings
<head>HeadContains metadata/information for the document
<header>HeaderDefines a header for a document or section
<hr>HrDefines a thematic change in the content
<html>HtmlDefines the root of an HTML document
<i>IDefines a part of text in an alternate voice or mood
<iframe>IframeDefines an inline frame
<img>ImgDefines an image
<input>InputDefines an input control
<ins>InsDefines a text that has been inserted into a document
<kbd>KbdDefines keyboard input
<label>LabelDefines a label for an <input> element
<legend>LegendDefines a caption for a <fieldset> element
<li>LiDefines a list item
<link>LinkDefines the relationship between a document and an external resource (most used to link to style sheets)
<main>MainSpecifies the main content of a document
<map>MapDefines an image map
<mark>MarkDefines marked/highlighted text
<meta>MetaDefines metadata about an HTML document
<meter>MeterDefines a scalar measurement within a known range (a gauge)
<nav>NavDefines navigation links
<noscript>NoscriptDefines an alternate content for users that do not support client-side scripts
<object>ObjDefines a container for an external application
<ol>OlDefines an ordered list
<optgroup>OptgroupDefines a group of related options in a drop-down list
<option>OptionDefines an option in a drop-down list
<output>OutputDefines the result of a calculation
<p>PDefines a paragraph
<param>ParamDefines a parameter for an object
<picture>PictureDefines a container for multiple image resources
<pre>PreDefines preformatted text
<progress>ProgressRepresents the progress of a task
<q>QDefines a short quotation
<rp>RpDefines what to show in browsers that do not support ruby annotations
<rt>RtDefines an explanation/pronunciation of characters (for East Asian typography)
<ruby>RubyDefines a ruby annotation (for East Asian typography)
<s>SDefines text that is no longer correct
<samp>SampDefines sample output from a computer program
<script>ScriptDefines a client-side script
<section>SectionDefines a section in a document
<select>SelectDefines a drop-down list
<small>SmallDefines smaller text
<source>SourceDefines multiple media resources for media elements (<video> and <audio>)
<span>SpanDefines a section in a document
<strong>StrongDefines important text
<style>StyleDefines style information for a document
<sub>SubDefines subscripted text
<summary>SummaryDefines a visible heading for a <details> element
<sup>SupDefines superscripted text
<svg>SvgDefines a container for SVG graphics
<table>TableDefines a table
<tbody>TbodyGroups the body content in a table
<td>TdDefines a cell in a table
<template>TemplateDefines a container for content that should be hidden when the page loads
<textarea>TextareaDefines a multiline input control (text area)
<tfoot>TfootGroups the footer content in a table
<th>ThDefines a header cell in a table
<thead>TheadGroups the header content in a table
<time>TimeDefines a specific time (or datetime)
<title>TitleDefines a title for the document
<tr>TrDefines a row in a table
<track>TrackDefines text tracks for media elements (<video> and <audio>)
<u>UDefines some text that is unarticulated and styled differently from normal text
<ul>UlDefines an unordered list
<var>VarDefines a variable
<video>VideoDefines embedded video content
<wbr>WbrDefines a possible line-break

Available Methods

  • jsonTable
  • render
  • generateHtml
  • parseHtml
  • textContent

Getting Started

Functional Approach

<div id="root"></div>
<script type="module">
  import { Div, P, Button, Row, Column, render } from "object-dom";

  const label = new P({ text: "Hello World!" });
  const button = new Button({
    text: "Update",
    styles: { width: "100px" },
  });
  const app = new Div({
    children: [
      new Column({
        children: [
          label,
          button,
          new Row({
            styles: { padding: "10px" },
            children: ["A", "B", "C"],
          }),
        ],
      }),
    ],
  });
  button.addEventListener('click', () => {
     label.text = "New Update!";
  }, false);
  render(app, document.body.querySelector("#root"));
</script>

Class Approach

import { ObjectDom, Div, H1, Button, Row, Canvas } from "object-dom";

export class MyApp extends ObjectDom {
  render = () => {
    return new Div({
      children: [new H1({ text: "Counter Example" }), new Counter(), new CanvasExample()],
    });
  };
}

class Counter extends ObjectDom {
  value = 0;
  render() {
    return new Div({
      styles: { margin: "5px" },
      children: [
        `${this.value}`,
        new Row({
          children: [
            new Button({
              text: "-",
              styles: { width: "50px" },
              events: {
                click: () => {
                  this.value -= 1;
                  this.update();
                },
              },
            }),
            new Button({
              text: "+",
              styles: { width: "50px", marginLeft: "5px" },
              events: {
                click: () => {
                  this.value += 1;
                  this.update();
                },
              },
            }),
          ],
        }),
      ],
    });
  }
}

class CanvasExample extends ObjectDom {
  render() {
    return new Canvas({
      styles: { width: "200px", height: "200px" },
      onCreate: (node) => {
        const canvas = node as HTMLCanvasElement;
        const ctx = canvas.getContext("2d")!;

        // Create gradient
        const grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
        grd.addColorStop(0, "red");
        grd.addColorStop(1, "white");

        // Fill with gradient
        ctx.fillStyle = grd;
        ctx.fillRect(10, 10, 150, 80);
      },
    });
  }
}

render(new MyApp(), document.body.querySelector("#root"));

JSON Table

jsonTable(
  [
    {
      first_name: "John",
      last_name: "Smith",
      company: "N/A",
    },
    {
      first_name: "Steve",
      last_name: "Jobs",
      company: "Apple",
    },
    {
      first_name: "Bill",
      last_name: "Gates",
      company: "Microsoft",
    },
    {
      first_name: "Elon",
      last_name: "Musk",
      company: "Tesla",
    },
  ],
  { style: { margin: "10px" } }
);

Which renders the following html:

<table>
  <tr>
    <th>first_name</th>
    <th>last_name</th>
    <th>company</th>
  </tr>
  <tr>
    <td>John</td>
    <td>Smith</td>
    <td>N/A</td>
  </tr>
  <tr>
    <td>Steve</td>
    <td>Jobs</td>
    <td>Apple</td>
  </tr>
  <tr>
    <td>Bill</td>
    <td>Gates</td>
    <td>Microsoft</td>
  </tr>
  <tr>
    <td>Elon</td>
    <td>Musk</td>
    <td>Tesla</td>
  </tr>
</table>
0.6.6

3 years ago

0.1.0

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.1.2

3 years ago

0.0.3

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.0.2

3 years ago

0.4.5

3 years ago

0.6.2

3 years ago

0.4.4

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.4.6

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.3.2

3 years ago

0.2.3

3 years ago

0.1.4

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.2.2

3 years ago

0.1.3

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.5.2

3 years ago

0.4.3

3 years ago

0.3.4

3 years ago

0.1.6

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.4.2

3 years ago

0.3.3

3 years ago

0.2.4

3 years ago

0.1.5

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago