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

ts-dom-utils

v2.2.0

Published

A simple utility library for DOM manipulation. Provides TypeScript typings for enhanced development experience in TS environments.

Downloads

25

Readme

TS Dom Utils

A lightweight TypeScript library that provides simple wrappers around common DOM manipulation functions. It allows you to write less code. Although it's written in TypeScript, it can also be used in JavaScript projects.

Table of Contents

Installation

# npm
npm install ts-dom-utils
# or Yarn
yarn add ts-dom-utils

Features

  • Strong TypeScript typings for better editor autocompletion and error checking.
  • Compatible with both TypeScript and JavaScript projects.
  • Simple and intuitive API.
  • Tree-shakeable

Functions

createElement

Creates a new element with the provided options.

Example

import { createElement } from 'ts-dom-utils';

const button = createElement('button', {
  id: 'my-button',
  class: ['btn', 'btn-primary'],
  text: 'Click me',
  onclick: (event) => {
    console.log('clicked!', event)
  },
  dataset: {
    action: 'open-menu',
  },
  'aria-expandended': 'false',
});

document.body.appendChild(button);
// <button id="my-button" class="btn btn-primary" data-action="open-menu" aria-expandended="false">Click me</button>

| Param | Default | Description | |---------|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | tagName | undefined | The tag name of the element type to create. | | options | {} | The options to use when creating the element. Options can include any attributes that can be passed to setAttribute, with class, and text as special options for enhancement. | | target | document | The Document in which to create the element. |

DOMisReady

DOMisReady is a function that returns a Promise. This Promise resolves when the provided DOM is ready, i.e., when the document's readyState is not 'loading'. This can be used to delay script execution until the DOM is fully constructed and can be safely manipulated.

import { DOMisReady, qs } from 'ts-dom-utils';

// using then
DOMisReady().then(() => {
  // DOM manipulation code here
});

// using async/await
(async function() {
  await DOMisReady();
  // DOM manipulation code here
})();

// checking if an iframe's document is ready
const iframe = qs<HTMLIFrameElement>('iframe');
DOMisReady(iframe.contentDocument).then(() => {
  // iframe DOM manipulation code here
});

| Param | Default | Description | |-------|-----------|-------------------------| | doc | document | The Document to check. |

qs

A wrapper function for document.querySelector.

Example

import { qs } from 'ts-dom-utils';

const container = qs<HTMLDivElement>('.footer > .buttons');
const button = qs<HTMLButtonElement>('button', container);

| Param | Default | Description | |----------|------------|-----------------------------------------------------| | selector | undefined | The selector to match against. | | parent | document | The ParentNode in which to search for the selector. |


qsa

A wrapper function for document.querySelectorAll.

Example

import { qsa } from 'ts-dom-utils';

const buttons = qsa<HTMLButtonElement>('.btn');

const menu = qs<HTMLDivElement>('.menu');
const menuButtons = qsa<HTMLButtonElement>('.btn', menu);

| Param | Default | Description | |----------|-----------|-----------------------------------------------------| | selector | undefined | The selector to match against. | | parent | document | The ParentNode in which to search for the selector. |

How to Contribute

We welcome any contributions! If you have an idea for a new feature or tool, please feel free to open an issue to discuss it. You can also submit a pull request if you've developed a feature or fix that you'd like to contribute back.

Support This Project

This project is developed and maintained by a single developer. If you find it useful and would like to support its continued development, you can do so through GitHub Sponsors. Any amount is appreciated!