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

@mordech/dynamic-grid-core

v0.3.0

Published

A dynamic grid component

Downloads

14

Readme

Dynamic Grid Core

This package contains the core functionality for a Dynamic Grid. The grid can be used to display a large number of elements in a compact and organized way, with the number of columns and rows adjusting dynamically based on the available space.

Installation To use the dynamic grid in your web application, you can install the dynamic-grid-core package from yarn or npm:

yarn add @mordech/dynamic-grid-core

or

npm install @mordech/dynamic-grid-core

calcColumns

This is a function that calculates the number of columns that can fit in a given space, taking into account the minimum width of each column, the width of the elements, and any gaps between the columns.

Usage

To use this function, import it into your project and call it with an object that contains the following properties:

  • minWidth: The minimum width of each column.

  • elementWidth: The width of the container element.

  • gap: The gap between each column in px (optional, defaults to 0).

  • dividedBy: The number to divide the number of columns by (optional, defaults to 1). For example, if you want the number of columns to be even, you can set this to 2.

  • maxColumns: The maximum number of columns that can be displayed (optional, defaults to Infinity).

  • scrollHint: A number between 0 and 1 that indicates how much of the next column should be shown in a scroll (optional, defaults to 0). For example, if you want to show half of the next column, you can set this to 0.5.

The function returns the number of columns that can fit in the given space.

To import the calcColumns function into your project, you can follow these steps:

import { calcColumns } from '@mordech/dynamic-grid-core';

You can now use the calcColumns function in your code.

Here is an example of how you could use the calcColumns function:

import { calcColumns } from '@mordech/dynamic-grid-core';

const params = {
  minWidth: 100,
  elementWidth: 200,
  gap: 10,
  dividedBy: 2,
  maxColumns: 4,
  scrollHint: 0.5,
};

const numColumns = calcColumns(params);

console.log(numColumns); // Output: 2

core.css

This file contains the core styles for a dynamic grid layout.

Usage

To use the dynamic grid in your web application, you can import the core.css file into your project and use the .grid and .is-scroll classes.

To import the core.css file into your project:

import '@mordech/dynamic-grid-core/css/core.css';

To use the .dg-grid class, add it to the container element of the grid:

<div class="dg-grid">
  <!-- Grid elements -->
</div>

To make the grid scrollable, use the .dg-is-scroll class, and add it to the container element of the grid:

<div class="dg-grid dg-is-scroll">
  <!-- Grid elements -->
</div>

.dg-is-scroll cannot work with --dg-repeat-count set to auto-fit or auto-fill.

To make the grid shrink to fit below the minimum width, add the .dg-is-shrink class to the container element of the grid:

<div class="dg-grid dg-is-shrink">
  <!-- Grid elements -->
</div>

Custom Properties

The core.css file uses the following CSS custom properties (also known as CSS variables):

  • --dg-repeat-count: Specifies the number of columns in the grid. Defaults to auto-fit.

  • --dg-min-width: Specifies the minimum width of each grid element. Defaults to 200px.

  • --dg-gap: Specifies the gap between grid elements. Defaults to unset.

  • --dg-gap-inline: Used to calculate the gap between grid elements. Defaults to var(--dg-gap). This property is used when --dg-gap has two values, one for the horizontal gap and one for the vertical gap. This value is needed only in .dg-is-scroll mode. For example, if --dg-gap is set to 10px 20px, then --dg-gap-inline should be set to 20px.

  • --dg-scroll-rows: Specifies the number of rows in the scrollable grid. Defaults to 1.

  • --dg-scroll-hint: Specifies the number of columns to show as a hint when scrolling. Defaults to 0.

These custom properties are used to define the grid layout and scroll behavior of the dynamic grid. They can be customized by setting their values in the CSS for the .grid and .is-scroll classes.

For example, to set the number of columns to 3 and the gap between elements to 10 pixels, you could add the following CSS:

.dg-grid {
  --dg-repeat-count: 3;
  --dg-gap: 10px;
}

CSS modules

If you are using CSS modules in your project, you can import the core.module.css file instead of the core.css file:

import '@mordech/dynamic-grid-core/css/core.module.css';

You can then use the dg-grid and dg-is-scroll classes in your code:

import styles from '@mordech/dynamic-grid-core/css/core.module.css';

<div className={styles.dgGrid}>
  <!-- Grid elements -->
</div>

Browser Support

The dynamic grid is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge.

License

This project is licensed under the MIT License - see the LICENSE file for details.