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

containerq

v1.0.12

Published

Easily add classes to elements based on their dimensions

Downloads

10

Readme

ContainerQ

A library to make container/element queries. Add classes or execute an action once an element reaches a desired dimension.

It listens to changes in the dimensions of an element using the ResizeObserver API.

Installation

npm i containerq

Basic usage

import { ContainerQ } from "containerq";

const cq = new ContainerQ();
const container = document.querySelector(".container");
cq.query(container, "width", ">", 300, "px", "container--big");

This bit of code adds the class "container--big" to the element "container" once its width is greater than 300 pixels and removes the class if it's less than or equal to 300 pixels.

Methods

query(element, property, comparison, breakpoint, unit, className): number

element: Element property: "width" | "height" comparison: "=" | "<" | "<=" | ">" | ">=" breakpoint: number unit: "px" | "rem" | "em" | "%" className: string

Adds a class to an element if a dimension (width or height) meets a condition and removes the class if the condition is not being met anymore.

When the percentage (%) unit is used the parent element is listened for size changes too.

Example:

const id = cq.query(container, "width", ">", 300, "px", "container--big");

Returns a queryId, an id number that represents the condition passed to query(). Useful if you later want to remove that query or if you want to check if said query exists.

query(element, property, comparison, breakpoint, unit, onQueryActive, onQueryInactive?): number

element: Element property: "width" | "height" comparison: "=" | "<" | "<=" | ">" | ">=" breakpoint: number unit: "px" | "rem" | "em" | "%" onQueryActive: Function onQueryInactive?: Function

Executes the function onQueryActive if a dimension (width or height) meets a condition. Optionally, another function, onQueryInactive, can be passed to be executed once the desired condition is not being met anymore.

Example:

function active() {
	console.log("I'm big")
}

function inactive() {
	console.log("I'm small")
}

cq.query(container, "width", ">=", 50, "%", active, inactive);

Returns queryId.

stopQuerying(queryId)

queryId: number

Stops a specific query from an element.

stopQuerying(element)

element: Element

Stops every query associated to a specific element.

stopQueryingAll()

Stops every query of every element in the ContainerQ instance.

isQuerying(queryId): QueryInfo | undefined

queryId: number

QueryInfo: {
    property: "width" | "height";
    comparison: "=" | "<" | "<=" | ">" | ">=";
    breakpoint: number;
    unit: "px" | "rem" | "em" | "%";
    onQueryActive: string | Function;
    onQueryInactive?: Function;
    queryId: number;
    active: boolean;
}

Receives the id of a query and returns the QueryInfo object of that query. It has information about a specific query.

Returns undefined if the id is not associated to any query or if its associated query was removed via the stopQuerying method.

onQueryActive is a string if a className string is passed to the query method. Likewise if a function was passed then onQueryActive will be a function.

The active property is a boolean indicating if the condition of the pertaining query is true or false at the moment.

Example:

const id = cq.query(container, "width", ">", 300, "px", "container--big");

console.log(cq.isQuerying(id).property) //width

cq.stopQuerying(id);

console.log(cq.isQuerying(id)) //undefined

isQuerying(element): QueryInfo[] | undefined

element: Element

Takes an Element and returns an array of every QueryInfo object associated to the Element.

Returns undefined if no queries are associated with the passed Element.

Example:

cq.query(container, "height", "<", 10, "rem", "container--short");
cq.query(container, "width", ">=", 550, "px", "container--wide");

console.log(cq.isQuerying(container)) //[{...}, {...}]

React Hook

useCQ(elementRef, queries)

elementRef: React.MutableRefObject<Element> queries: QueryProperties[]

QueryProperties: {
    property: "width" | "height";
    comparison: "=" | "<" | "<=" | ">" | ">=";
    breakpoint: number;
    unit: "px" | "rem" | "em" | "%";
    onQueryActive?: string | Function;
    onQueryInactive?: Function;
    onQueryToggleState?: Function;
}

Example:

import { useCQ } from  "containerq";

function  MyComponent() {
    const [wideSize, setWideSize] = useState(false);
    const [shortSize, setShortSize] = useState(false);
    const  divRef= useRef();

    useCQ(divRef, [
	    { property: "width", comparison: ">", breakpoint: 60, unit: "%", onQueryToggleState: setWideSize },
	    { property: "height", comparison: "<=", breakpoint: 30, unit: "%", onQueryToggleState: setShortSize }
    ]);

    return (
	    <div ref={divRef}>
		    <SubComponent1 isWideSize={wideSize} />
		    <SubComponent2 isWideSize={wideSize} />
		    <SubComponent3 isShortSize={shortSize} />
	    </div>
    );
}
// The first two subcomponents react to the parent container being wide
// The last subcomponent reacts to the parent container being short

The queries object of type QueryProperties has properties very similar to the query method's parameters. The only difference is the property onQueryToggleState which takes a function that's called with true as an argument when the query's condition is being met, otherwise is called with false as the argument.

The idea is to use onQueryToggleState alongside useState but you could come up with different ways of using it.

There is no need to create a ContainerQ instance when using the hook because an instance is created when useCQ is imported. Also, the queries associated with the hook are cleared automatically when the component gets unmounted.