Giter VIP home page Giter VIP logo

compnet2.0's Introduction

Compnet2.0

A Easy to learn And Minimal Library to create DOM Elements with JavaScript.

Importation

<script src="/path/to/compnet.js"></script>

compnet.js can be downloaded from here

Usage


Simple DOM Elements

const button = new Component({
    tag: "button",
    innerHTML: "Click Me",
    styles: {
        minWidth: "fit-content",
        minHeight: "fit-content",
        padding: "10px",
        outline: "none",
        border: "none",
        borderRadius: "4px",
        backgroundColor: "#2195F1",
        color: "white",
        fontWeight: "700",
        cursor: "pointer"
    },
})


document.body.append(button.DOM);


Hover and Focus Effects

const button = new Component({
    tag: "button",
    innerHTML: "Make me Red",
    styles: {
        ...
        $hover: {
            backgroundColor: "#FE0000",
        },
        $focus: {
            backgroundColor: "#FE0000"
        }
    },
})

hover-focus-element


$hover$focus state

This Will result same as previous Example

const button = new Component({
    tag: "button",
    innerHTML: "Make me Red",
    styles: {
        ...
        $hover$focus: {
            backgroundColor: "#FE0000",
        }
    }
})

Using id

  • You can pass id key in Config object to set the id of the Element
new Component({
    ...
    id: "my_own_id"
    ...
})

Using randomId

  • By Default randomId is set to true
new Component({
    ...
    randomId: false
    ...
})

Using className

new Component({
    ...
    className: "btn btn-primary m-5"
    ...
})

Using styles

new Component({
    ...

    styles: {
        // Normal Styles Goes Here
        $hover: {
            // All styles in here will apply when in hovering state
        },
        $focus: {
            // All Styles in here will apply when element is focused
        },
        $hover$focus: {
            // All styles in here will Apply At Both Hovering and Focused State
        }
    }

    ...
})

Using Media Query

new Component({
    ...
    styles: {
        ...
        $media: {
            '(max-width: 767px)': {
                // All Styles in here will be applyed when the query matches

                // And styles in here will be removed when media query changes to false
            }
        }
        ...
    },
    ...
})
  • Library Uses matchMedia function in javascript

Using Attributes

new Component({
    ...
    attributes: [
        {
            name: "src",
            value: "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        }
    ]
    ...
})

Using Events

new Component({
    ...
    events: [
        {
            event: "click",
            callback: (e)=>{
                console.log("I was Clicked");
            }
        }
    ]
    ...
})

Embedding Children Elements

Passing JSON Object

    new Component({
        ...

        childrens: [
            {
                tag: "button",
                innerHTML: "Hello I'm Children Element",
                styles: {
                    ...
                }
            }
        ]
        ...
    })

Passing Component Object

    let button = new Component({
        ...
        tag: "button",
        innerHTML: "Hello I'm Children Element",
        styles: {
            ...
        }
        ...
    })

    new Component({
        ...
        childrens: [
            button
        ]
        ...
    })

Preventing Embedding

  • By Using embeddable, You can choose if the Component should be embedded anywhere or not.
    new Component({
        ...
        tag: "button",
        innerHTML: "Hello I'm Children Element",
        styles: {
            ...
        }
        embeddable: false
        ...
    })

Using Built-In Event Emitter

let button = new Component({
    ...
})

button.Emitter.on("message", (msg)=>{
    console.log(msg) // It Will Print "Hello World" after 3 seconds
})

setTimeout(()=>{
    button.Emitter.emit("Hello World")
}, 3000)
  • More About Event Emitter can be Found here

Coded With ❤️ By Henil Malaviya

compnet2.0's People

Contributors

henil0604 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.