Socket
Book a DemoInstallSign in
Socket

dom-magic

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dom-magic

simple ES6 utilities to manipulate browsers DOM

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

dom-magic

Simple ES6 utilities to manipulate DOM

Features

  • JSX compatible, standalone createElement method (no react)
  • Show/hide elements
  • Add event listeners
  • Native ES6 module (requires babel/rollup to be used within browser)

Installation

$ npm install dom-magic --save
$ yarn add dom-magic

ES6 Transpiler Setup

File: gulpfile.js


const _rollup_babel = require('rollup-plugin-babel');
const _rollup_resolve = require('@rollup/plugin-node-resolve');

_gulp.task('es6-transpile', async function(){
    const bundle = await _rollup.rollup({
        input: './src/browser/EnlighterJS.js',
        plugins: [
            _rollup_resolve(),
            _rollup_babel()
        ]
    });

    // write the bundle to disk
    await bundle.write({
        format: 'iife',
        name: 'EnlighterJS',
        file: './.tmp/enlighterjs.js'
    });
});

File: babel.config.json

{
    "plugins": [
        ["@babel/plugin-transform-react-jsx", {
        }]
    ],
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "browsers": [
                        "safari >= 7",
                        "IE >= 9",
                        "ios >= 9",
                        "firefox >= 50",
                        "chrome >= 50",
                        "edge >= 11"
                    ]
                },
                "debug": false
            }
        ]
    ]
}

Examples

JSX Component

File: button.jsx

// Internal "ReactDOM"
import * as React from 'dom-magic';

export function Button(props){

    // button css classes
    const classes = ['enlighter-btn'];

    // name set ?
    if (props.name){
        classes.push('enlighter-btn-' + props.name);
    }

    // create button
    return <div 
            className={classes.join(' ')} 
            onClick={props.onClick}
        >
            {props.text||null}
        </div>
}

Transpiled JSX

createElement is provided by dom-magic

function Button(props) {
    // button css classes
    var classes = ['enlighter-btn']; // name set ?

    if (props.name) {
    classes.push('enlighter-btn-' + props.name);
    } // create button


    return createElement("div", {
    className: classes.join(' '),
    onClick: props.onClick
    }, props.text || null);
}

Browser (abstract example)

var domMagic = require('dom-magic');

// get target container
var targetEl = domMagic.getElement('#container-a');

// create element
var buttonEl = Button({
    text: "hello world",
    name: "test"
});

// inject
domMagic.insertBefore(targetEl, buttonEl);

Full Examples

  • EnlighterJS

License

dom-magic is OpenSource and licensed under the Terms of MPL 2.0 - your're welcome to contribute

FAQs

Package last updated on 20 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.