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

@webarthur/kingdom

v1.0.1

Published

A lightweight DOM manipulation library

Downloads

1

Readme

KingDom.js: A lightweight DOM manipulation library

KingDom.js is a lightweight, utility-driven JavaScript library designed to simplify DOM manipulation, event handling, and dynamic content management in your web projects. With a fluent and intuitive API, KingDom.js empowers developers to write cleaner and more efficient code when interacting with the Document Object Model (DOM).

Installation

You can install KingDom.js using a package manager like npm or yarn:

npm install @webarthur/kingdom

You can also include KingDom.js in your project by downloading the file and linking it directly in your HTML or by importing it into your JavaScript modules. Download KingDom.js and include it in your HTML file:

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

Usage

KingDom.js exposes a set of utility functions that can be used to perform common DOM manipulation tasks. Below are examples of how to use each function:

  • get(target): Accepts a CSS selector string or an existing DOM element and returns the selected element(s).
  • each(target, callback): Iterates over a collection of elements selected using a CSS selector and executes the callback function for each element.
  • on(target, event, callback): Attaches an event listener to the specified element. Provides a convenient shorthand for common events:
  • update(target, content, type='html'): Updates the content of an element. type can be 'html' or 'text'.
  • show(target): Makes an element visible by removing the d-none class (assuming a Bootstrap-like class naming convention).
  • hide(target): Hides an element by adding the d-none class.
  • append(target, code, position = 'beforeend'): Appends content (string or element) to an existing element at a specified position.
  • set(target, attribute, value): Sets or retrieves an attribute value for an element.
  • setStyle(target, style, value): Sets a CSS style property for an element. You can pass an object for multiple styles.
  • create(tag, attributes = {}, parent, position = 'beforeend'): Creates a new DOM element with the specified tag, attributes, and optional parent and position for insertion.
  • remove(target): Removes an element from the DOM.
  • toggle(target, className = 'd-none', force): Toggles a CSS class for an element, optionally adding or removing it based on a force flag.
  • exists(target, parent = document): Checks if an element exists in the DOM.
  • dispatch(target, event): Dispatches a custom event on an element.
  • load(src, props = {}, parent = document.head): Loads a script or stylesheet from the specified source with optional properties and parent element.

Examples

Adding Event Listeners

Using on() to attach an event listener to the specified element.

import { on } from '@webarthur/kingdom'

// Attaches a callback to button on click event
on('#button-id', 'click', () => {
  console.log('Button clicked!')
})

// Attaches a callback to window on load
on('load', () => {
  console.log('Page loaded!')
})

Updating Element Content

Using update() topdate the content of an element.

import { update } from '@webarthur/kingdom'

update('#myDiv', 'New content here')

Showing and Hiding Elements

import { hide, showw } from '@webarthur/kingdom'

hide('#element-id') // Hide element
show('#element-id') // Show element

Toggling Classes

import { toggle } from '@webarthur/kingdom'

toggle('#element-id', 'my-class')

Creating and Appending Elements

import { get, create } from '@webarthur/kingdom'

// Get container ref
const container = get('#container-id')

// Create new div and append to container
const newDiv = create('div', { 
  class: 'my-new-div', 
  text: 'Hello, world!' 
}, container)

Removing Elements

remove('#element-id')

Loading External Resources

load('path/to/script.js') // Load an external JavaScript file
load('path/to/style.css') // Load an external CSS file

Contributing

Contributions to KingDom.js are always welcome! Whether it's bug reports, feature requests, or pull requests, all contributions help make KingDom.js better for everyone.

License

KingDom.js is open source and released under the MIT License. See the LICENSE file for more details.