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

ui-utilities

v0.5.1

Published

JavaScript utilities for implementing common web user interfaces

Downloads

6

Readme

Utility Scripts

JavaScript utilities for implementing common web user interfaces

Installation

npm install --save ui-utilities

Usage

import { scroll, text, throttle } from 'ui-utilities';

const anchorLinks = document.querySelectorAll('a[href*="#"]');

const handleResize = throttle(() => {
    // Do something expensive on resize throttled to every 300ms
}, 300); // Only fire this event every 300ms
function handleDOMConentLoaded() {
    // Balance all lines of text in headings
    text.balance();
}
function handleClick(e) {
    // Use smooth scroll instead of default functionality for anchor link
    e.preventDefault();

    const id = e.target.href.split('#')[1];

    scroll.to(id);
}

window.addEventListener('resize', handleResize);
document.addEventListener('DOMContentLoaded', handleDOMConentLoaded);
anchorLinks.forEach(l => {
    l.addEventListener('click', handleClick);
});

Functions

Events - Emit

This is a wrapper method around a CustomEvent to send a named event with an optional payload

import { events } from 'ui-utilities';

events.emit('event-name', { data: [] });

Forms - Sync Dates

import { forms } from 'ui-utilities';

const dates = document.querySelectorAll('[type="date"]');

// Sync paired dates together as start and end
if (dates.length === 2) {
    forms.syncDates(...dates);
}

Router - Get, Post

Wrapper methods for XHR get and post request with options history state and FormData passing

import { router } from 'ui-utilities';

const link = document.querySelector('a');
const form = document.querySelector('form');

function handleClick(e) {
    // Intercept get request with JS and update history state
    e.preventDefault();

    const url = e.target.href;
    const query { query_key: "query_value" };
    const state = { url, query };

    function cb(res) {
        // Do something with response from server
    }

    router.get({ url, query, state, cb }) {
}
function handleSubmit(e) {
    // Intercept form post with JS and pass formData
    e.preventDefault();

    const url = '/form-handler';
    const formData = new FormData(form);

    function cb(res) {
        // Do something with response from back end handler
    }

    router.post({ url, formData, cb });
}

link.addEventListener('click', handleClick);
form.addEventListener('submit', handleSubmit);

Scroll - To, Top, Bottom

Smooth scrolling functionality for navigating to anchor links, and the top and bottom of elements.

import { scroll } from 'ui-utilities';

const hero = document.querySelector('.hero');
const button = hero.querySelector('button');

function handleClick() {
    // Scroll to the bottom of the hero element and offset by 100px to compensate for the fixed header
    scroll.bottom(hero, 100);
}

heroButton.addEventListener('click', handleClick);

Spy - Anchors, Images, Nav, Sections

TODO

Text - Balance

This function will balance lines of text such that lines stacked on top of each other will be of similar length. This prevents widow words on new lines inside of a responsive design where content is dynamic and subject to change. A default step size tolerance of 10px can be overridden by supplying a data attribute to the element to be balanced.

import { text } from 'ui-utilities';

text.balance(document, '.heading-class');

Throttle

This function throttles the execution of a given function to only fire at a given interval

import { throttle } from 'ui-utilities';

const handleResize = throttle(() => {
    // Expensive function to only fire every 300ms during resize events firing
}, 300);

window.addEventListener('resize', handleResize);