npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objective-rendering

v2.1.3

Published

Objective Rendering lets you structure your document as an object.

Downloads

9

Readme

Objective Rendering

Objective Rendering simplifies your interactions with the DOM by rendering elements based on objects you define.

Installation

CDN (reccomended)

<script src="https://unpkg.com/[email protected]/render.js"></script>
NPM not reccomended

Implementation

To implement, call the render() function. It takes four arguments: | name | type | required or default | description | | --- | --- | --- | --- | | contents | array | required | The objects to render. See below for how to define. | | parent | DOM element object | required | the parent element to render within | | clear | boolean | default: true | If clear is true, all of parent's children will be cleared before rendering | documentCtx | object | default: document | Reference to the document object. No need to change as long as you are using CDN

render() returns an object with references to some DOM elements. See refName below for how to get an element included in this object.

The contents array

This is an array of element objects. The element objects can be either DOM element objects or user-defined objects. User-defined objects can have any of the properties below. The type property is required, all others are optional. | name | type | description | | --- | --- | --- | | type | string | The HTML element type, same as you would use in document.createElement()| | style | object | CSS styling, in {property: value} format. For properties with hyphens (e.g. background-color), use an underscore (e.g. background_color) | | textContent | string | Sets the text content of the element | | children | array | All children of the element. Can be DOM elements or user-defined | | listeners | object | Event listeners to attatch to the element, in {eventType: onFire} format. For example: {click: myFunction} | | contentEditable | boolean | Corresponds to the HTML attribute contenteditable | | id | string | Corresponds to the HTML attribute id | | className | string | Corresponds to the HTML attribute class | | refName | string | Define this in order to get the element included in the references returned by render(). The name you give it will be the name you can use to access it later | | attributes | object | Use this to set any other html attrubutes. Use {name: value} format for any attrubute. For example: {src: "htpps://www.website.com/image"}|

Examples

Hello world

// define the object
// only one element, a <p> with the text 'Hello, world'
myDoc = [
    {
        type: 'p',
        textContent: 'Hello, world'
    }
];
// render myDoc within the document body
render(myDoc, document.body);

Adding children, styles, and listeners

const myDoc = [
    {
        type: 'div',
        children: [
            {
                type: 'button',
                id: 'my_button',
                textContent: 'Press Me',
                listeners: {
                    click: () => console.log('Button clicked'),
                    mouseover: (e) => {
                        e.target.style.backgroundColor = 'red'
                    },
                    mouseout: (e) => {
                        e.target.style.backgroundColor = 'white'
                    }
                }
            },
            {type: 'hr'},
            {
                type: 'p',
                className: 'myClass',
                textContent: 'Hello, World',
                contentEditable: true,
                style: {
                    background_color: 'green',
                    border: '1px solid black',
                    border_radius: '10px'
                }
            },
            myP
        ]
    }
];

render(myDoc, document.body);

Getting references and rendering in different places

const myDoc = [
    {
        type: 'div',
        refName: 'myDiv',
        children: [
            {
                type: 'button',
                id: 'my_button',
                refName: 'myButton',
                textContent: 'Press Me',
                listeners: {
                    click: () => console.log('Button clicked'),
                    mouseover: (e) => {
                        e.target.style.backgroundColor = 'red'
                    },
                    mouseout: (e) => {
                        e.target.style.backgroundColor = 'white'
                    }
                }
            },
            {type: 'hr'},
            {
                type: 'p',
                className: 'myClass',
                textContent: 'Hello, World',
                contentEditable: true,
                style: {
                    background_color: 'green',
                    border: '1px solid black',
                    border_radius: '10px'
                }
            },
            myP
        ]
    }
];

const refs = render(myDoc, document.body);
/*
Notice the refName property in the div and the button.
refs will now contain this:
{
    myButton: <reference to the button>,
    myDiv: <reference to the div>
}
*/

// add another <p> to the <div>, without erasing the existing content
// the second argument specifies to render within refs.myDiv
// the third argument specifies not to erase the existing content
render(
    {
        type: 'p',
        textContent: 'More text'
    }, 
    refs.myDiv, 
    false
);

// you can perform normal DOM manipulation on references
refs.myButton.classList.add('myClass');

Render a normal DOM element

domDiv = document.createElement('div');
domDiv.textContent = 'This is a normal DOM element';
myDoc = [
    {
        type: 'p',
        textContent: 'This is a user-defined object'
    },
    {
        type: 'div',
        children: [
            domDiv
        ]
    }
];

render(myDoc, document.body);