Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

detect-grid

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-grid

Detect and mark grid cells for easy styling

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
increased by333.33%
Maintainers
0
Weekly downloads
 
Created
Source

Detect Grid

NPM version Bundle size GitHub license

Detect and mark grid cells for easy styling.

What does it do?

This package detects the rows and columns of a layout visually by comparing the offset from the top-left edge of the parent container. Each element is assigned a row and column index.

It will mark each detected cell with data attributes for easy targeting in CSS. This makes styling cells by index feasible even in flexbox or grid layouts where visual position doesn't always follow source order.

Installation

npm install detect-grid

Usage

Detect grid cells

Returns an array of rows and columns for further processing. Indices are zero-based.

import detectGrid from 'detect-grid'

const grid = document.querySelector('.grid')

const rows = detectGrid(grid)

rows.forEach((cols, rowIndex) => {
  cols.forEach((cell, colIndex) => {
    console.log(`Cell at row ${rowIndex} and col ${colIndex}`, cell.textContent)
  })
})

Mark grid cells

Detects rows and columns and mark them with data attributes for CSS styling.

import { markGrid } from 'detect-grid'

const grid = document.querySelector('.grid')

markGrid(grid, { selector: '.cell' })

Before

<div class="grid">
  <div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
</div>

After

:warning: While the detectGrid function returns zero-based arrays, the data attributes added by markGrid start counting from 1 to keep compatibility with CSS nth-child selectors.

<div class="grid">
  <div>
    <div class="cell" data-nth-row="1" data-nth-col="1" data-first-row data-first-col></div>
    <div class="cell" data-nth-row="1" data-nth-col="2" data-first-row data-last-col></div>
  </div>
  <div>
    <div class="cell" data-nth-row="2" data-nth-col="1" data-first-col></div>
    <div class="cell" data-nth-row="2" data-nth-col="2" data-last-col></div>
  </div>
  <div>
    <div class="cell" data-nth-row="2" data-nth-col="1" data-last-row data-first-col></div>
    <div class="cell" data-nth-row="2" data-nth-col="2" data-last-row data-last-col></div>
  </div>
</div>

CSS variables

Some visual effects like gradients require CSS custom properties for calculating the correct value for each row and cell. You can enable those behind an optional feature flag when marking grid cells:

markGrid(grid, { cssVariables: true })

You can now use the following CSS properties like --col-fraction or --row-fraction for calculating styles:

<div class="grid">
  <div>
    <div class="cell" style="--col-index: 0; --col-count: 3; --col-fraction: 0; --row-index: 0; --row-count: 2; --row-fraction: 0;"></div>
    <div class="cell" style="--col-index: 1; --col-count: 3; --col-fraction: 0.5; --row-index: 0; --row-count: 2; --row-fraction: 0;"></div>
    <div class="cell" style="--col-index: 2; --col-count: 3; --col-fraction: 1; --row-index: 0; --row-count: 2; --row-fraction: 0;"></div>
  </div>
  <div>
    <div class="cell" style="--col-index: 0; --col-count: 3; --col-fraction: 0; --row-index: 1; --row-count: 2; --row-fraction: 1;"></div>
    <div class="cell" style="--col-index: 1; --col-count: 3; --col-fraction: 0.5; --row-index: 1; --row-count: 2; --row-fraction: 1;"></div>
    <div class="cell" style="--col-index: 2; --col-count: 3; --col-fraction: 1; --row-index: 1; --row-count: 2; --row-fraction: 1;"></div>
  </div>
</div>

Options

Configure how cells are detected by passing an options object as second parameter.

const rows = detectGrid(grid, {
  selector: '.cell',
  align: 'bottom'
})
OptionDescriptionType/OptionsDefault
selectorDOM selector to find grid cellsSelector stringUse direct childnodes
justifyHorizontal alignment of measuring pointString: left, center, rightleft
alignVertical alignment of measuring pointString: top, center, bottomtop
toleranceTolerance to group rows/columns byNumber (px)0

License

MIT

Keywords

FAQs

Package last updated on 08 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc