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

jdomjs

v3.1.11

Published

A wrapper for query selector and html elements

Downloads

23

Readme

JDOM 3.1.11

A wrapper for query selector and html elements + templating & reactivity framework

Install

NPM

npm install jdomjs

Module

import { $, $n, $c, $r, $h, JDOM } from 'https://cdn.jsdelivr.net/npm/[email protected]'

HTML import

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jdom.js"></script>

DOM Manipulation

const el = $('#a-div')

el.css({
    background: '#000000'
})

el.each($el => {
    console.log(el.html())
})

el.text('Hello world')

el.html('<span>Hello</span> world')

el.attr('key', value)

el.classes('hello', 'world')

if (el.hasClass('hello')) {}

// Builder pattern
el.text('Example').attr('type', 'text').classes('input-big')


el.click(e => {
    console.log(e)
})

Create Element

$('#app').append(
    // Creates a new div with 'Hey' text
    $n('div').text('Hey')
)

Create element from HTML

const name = 'John'
const el = $h(`<h1>Hello ${name}!</h1>`)
console.log(el)
// -> JDOM(HTMLElement)

Animator

// Single animation
$('#test').animate({
    background: '#000000'
}, 1000)

// Using animator
$('#test').animator([
    {
        duration: 1000,
        css: {
            background: '#FF0000'
        }
    },
    {
        duration: 1000,
        css: {
            background: '#00FF00'
        }
    }
])

Web-Components

<my-component></my-component>

<script>
// Create HTMLElement
const MyComponent = $c((el, component) => {
    el.append(
        $n('span')
            .text('Hello World')
            .click(() => {
                alert('Hey')
            })
    )

    component.addStyle(`span { color: red }`)
}/*, {shadowed: true} */)

// Register component
$r('my-component', MyComponent)
</script>

Typescript Class-Component

import {html, JDOMComponent} from 'jdomjs'
import {CustomElement, State, Attribute} from "jdomjs/decorator.ts";

@CustomElement('example-component')
class ExampleComponent extends JDOMComponent {
  @State()
  private name = new Hook<String>('John')

  @State()
  @Attribute({ name: 'last-name' })
  private lastName = new Hook<String>('default')

  @Computed(s => [s.name])
  private greetings() {
    return comp`Hello ${this.name}`
  }

  render() {
    return html`
            <input :bind=${this.name}>
            <h1>${this.greetings}</h1>
          `
  }
}

Javascript Class-Component

import {html, JDOMComponent, $r} from 'jdomjs'

class ExampleComponent extends JDOMComponent {
  private name = new Hook('John')

  private lastName = new Hook('default')

  constructor() {
    super();
    this.addAttributeListener('lastName', { name: 'last-name' })
  }

  private greetings() {
    return comp`Hello ${this.name}`
  }

  render() {
    return html`
            <input :bind=${this.name}>
            <h1>${this.greetings()}</h1>
          `
  }
}

$r('example-component', ExampleComponent)

JDOM-Template

import { html } from 'jdomjs'

const name = "John"

const myHTML = html`
    <h1>Hello ${name}</h1>
    <button @click=${() => alert('Clicked')}>Click me</button>
`

$(document).append(myHTML)

// binding
const name = state('John')

html`
    name: ${name} <br>
    <input :bind=${name}>
`

Reactivity

import { state, html, $ } from 'jdomjs'

const count = state(0)

const button = html`
    <button @click=${() => count.value++}>The count is ${count}</button>
`
$(document).append(button)

if-confitions

// if-condition
html`
    <!-- Reactive if condition with ternary operator -->
    ${computed(() => 
        isEnabled.value 
            ? html`<div>Now shown!</div>` // If true render this div 
            : null, // If false render nothing
    [isEnabled])}
    
    <!-- :if attribute -->
    <div :if=${isEnabled} @click=${() => alert('Yo')}>
        Now I'm shown :o
    </div>
    <div :else>
        Not shown :(
    </div>
    <div></div>
`

Reactive for-each

html`
    ${computed(() => elements.value.map(user => html`
        <div>
            <span>${user.name}</span>
        </div>
    `), [elements])}

    <button @click=${() => elements.value = [...elements.value, {name: 'Joe'}]}>Add Element</button>
`

// Or use Helper-Component
import { ForEach } from 'jdomjs/template/helper/components.js'
html`
    <${ForEach} 
        :bind=${elements}
        :content=${user => html`
            <div>
                <span>${user.name}</span>
            </div>
        `}
    />
    
    <button @click=${() => elements.value = [...elements.value, {name: 'Joe'}]}>Add Element</button>
`

Function-Components

// Function components
function UserLayout({ exampleProp, $slot }) {
    return html`<div class="user-profile">
        ${$slot}
    </div>`
}

html`<${UserLayout} exampleProp="test">
    Profile
</${UserLayout}>`

Promise-Handling

const promise = fetch('/user/name') 
html`${promise.then(r => r.json()).then(u => u.name)}`

// Or use Helper-Component
import { Awaiting } from 'jdomjs/template/helper/components.js'

const promise = fetch('/api/user')
html`
    <${Awaiting} 
        promise=${promise.then(r => r.json())}
        finished=${user => html`<${User} user=${user} />`}
        awaiting${html`<${LoadingIndicator} />`}
        error="Something went wrong"
    />
`

JDOM-Hooks

import { state, computed, watch, bind, $, $c, $r, html } from 'jdom'

// Create a state
const name = state('John')

// Set value
name.value = 'Jessica'

// Read value
console.log(name.value)

// Add to JDOM
$('#user-name').text(name)

// Add to jdom-template
html`Hello ${name}`



// computed
const lowerCaseName = computed(() => {
    return name.value.toLowerCase()
}, [name]) // <- Dependencies. The function given will be called if one of the dependencies change

// Helper template-string-tag:
const greeting = comp`Hello ${name}!`


// Watch
watch([name], () => {
    console.log(`Name changed to ${name}!`)
})


// Bindings in components
$r('my-example-component', $c((el, component) => {
    const value = bind(component)
    
    return html`Your name is ${value}`
}))
// <my-example-component value="test" /> -> Your name is test