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

@gebruederheitz/wp-frontend-utils

v1.1.1

Published

Some JS helpers for easier Wordpress frontend development.

Downloads

69

Readme

WP Frontend Utilities

Some JS helpers for easier Wordpress frontend development

Installation

> npm i @gebruederheitz/wp-frontend-utils

Usage

Element creator

import { createDomElement, createSvgElement } from '@gebruederheitz/wp-frontend-utils';

const newDiv = createDomElement({
    innerText: 'Hello',
    parent: window.document.body,
});

const list = createDomElement({
    type: 'UL',
    classNames: ['my-pretty-list', 'mt5'],
});
for (var i = 0; i < 5; i++) {
    createDomElement({
        type: 'LI',
        classNames: ['my-pretty-list__item'],
        attributes: {
            dataIndex: i,
        },
        innerHtml: `<span>List item number <em>${i}</em>!</span>`,
        parent: list,
    });
}
document.body.appendChild(list);

// Uses createElementNS() to create a namespaced, valid SVG element
const svg = createSvgElement({
    parent: document.querySelector('.svg-container'),
    classNames: [],
    attributes: {},
});

Debuggable class

Convenient toggle for namespaced debug output as an extensible class.

import { Debuggable } from '@gebruederheitz/wp-frontend-utils';

class MyClass extends Debuggable {
    constructor() {
        // Set the namespace for the class – debug output will be prefixed with this.
        super('MyClass');
    }
    
    logSomething() {
        // Will log "[MyClass]: Test" when (and only if) debug output is enabled
        this.debug.log('Test');
    }
}

// Toggle debug output for all classes inheriting from Debuggable
Debuggable.setGlobalDebug(true);
Debuggable.setGlobalDebug(false);

// Setting debug output per instance
class OtherClass extends Debuggable {
    constructor(options = {debug: false}) {
        super('OtherClass');
        this.options = options;
    }
    
    logSomething() {
        this.debug.log('Test');
    }
}

const otherSilent = new OtherClass();
const otherLoud = new OtherClass({debug: true});

otherSilent.logSomething() // no output
otherLoud.logSomething() // "[OtherClass]: Test"

Debuggable.setGlobalDebug(true);
otherSilent.logSomething() // "[OtherClass]: Test"

From v1.1.0 on you can also enable debug output at runtime (e.g. through the browser console):

// Enable debug output for all modules
window.ghDebuggableDebugEnabled = true;
// Target one specific module
window.ghDebuggableDebugEnabled = 'OtherClass';

Key Codes

A simple map of key codes to keys:

import { keyCodes } from '@gebruederheitz/wp-frontend-utils';

console.log(keyCodes[82]); // "r"

Query Selector

I see you'd still rather be writing jQuery

Shorthands for querySelector[All]:

import { $, $$ } from '@gebruederheitz/wp-frontend-utils';

const one = $()('.classname');  // = document.querySelector('.classname')
const two = $(one)('[data-attribute]'); // = one.querySelelctor('[data-attribute]')
const three = $$()('#id'); // = document.querySelectorAll('#id')
const four = $$(two)('li'); // = two.querySelelctorAll('li')

Show/Hide

Simple functions to show or hide elements.

import { $, isItOpen, toggleIt, openIt, closeIt } from '@gebruederheitz/wp-frontend-utils';

const element = $()('.test-element');

// Will return true if the element has the class 'show-it' and an attribute 
// 'aria-expanded' which is true – or if it has an attribute 'aria-controls'
// referencing a valid element which has the class 'show-it' or has a sibling
// element with that class.
// Will throw an error if none of the aria-attributes are present and no
// sibling element can be found.
// Will return false otherwise.
const initiallyOpen = isItOpen(element);

// Same thing, but with a custom class name instead of 'show-it'.
const isThisOpen = isItOpen(element, 'visible');

// To be used on the controlling element: Will set 'aria-expanded' to the 
// opposite of the current state and toggle the class 'show-it' on the target / 
// controlled element.
toggleIt(element);
toggleIt(element, 'visible');

 isItOpen(element) === !initiallyOpen; // true

openIt(element);
 isItOpen(element); // true

closeIt(element);
 isItOpen(element); // false

Development

(tbd)