2.2.0 • Published 6 months ago

ts-dom-utils v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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>
ParamDefaultDescription
tagNameundefinedThe 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.
targetdocumentThe 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
});
ParamDefaultDescription
docdocumentThe 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);
ParamDefaultDescription
selectorundefinedThe selector to match against.
parentdocumentThe 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);
ParamDefaultDescription
selectorundefinedThe selector to match against.
parentdocumentThe 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!

2.2.0

6 months ago

2.1.0

8 months ago

2.0.0

10 months ago

1.1.0

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago