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

css-grid-template-parser

v0.1.0

Published

A simple CSS Grid template parser

Downloads

410

Readme

css-grid-template-parser

A simple CSS Grid template parser

Installation

npm install --save css-grid-template-parser

Basic usage

Parse a grid template

import { grid } from 'css-grid-template-parser'

const areas = grid(`
  "a a a b b"
  "a a a b b"
  ". . c c c"
  "d d d d d"
`)

// → {
//   width: 5,
//   height: 4,
//   areas: {
//     a: {
//       column: {start: 1, end: 4, span: 3},
//       row: {start: 1, end: 3, span: 2},
//     },
//     b: {
//       column: {start: 4, end: 6, span: 2},
//       row: {start: 1, end: 3, span: 2},
//     },
//     c: {
//       column: {start: 3, end: 6, span: 3},
//       row: {start: 3, end: 4, span: 1},
//     },
//     d: {
//       column: {start: 1, end: 6, span: 5},
//       row: {start: 4, end: 5, span: 1},
//     },
//   },
// }

Build a grid template

import { template } from 'css-grid-template-parser'

const areas = template({
  width: 5,
  height: 4,
  areas: {
    a: {
      column: { start: 1, end: 4, span: 3 },
      row: { start: 1, end: 3, span: 2 },
    },
    b: {
      column: { start: 3, end: 6, span: 3 },
      row: { start: 3, end: 5, span: 2 },
    },
  },
})

// → `"a a a . ."
//    "a a a . ."
//    ". . b b b"
//    ". . b b b"`

An helper is provided to declare areas more intuitively. The following example is equivalent to the previous:

import { template, area } from 'css-grid-template-parser'

const a = area({
  x: 0,
  y: 0,
  width: 3,
  height: 2,
})

const b = area({
  x: 2,
  y: 2,
  width: 3,
  height: 2,
})

const areas = template({
  width: 5,
  height: 4,
  areas: { a, b },
})

// → `"a a a . ."
//    "a a a . ."
//    ". . b b b"
//    ". . b b b"`

API

grid(template)

Parses a grid template and returns an object representation.

Arguments

  1. template string The grid template to parse.

Returns

Grid An object representation of the grid template.

Example

import { grid } from 'css-grid-template-parser'

const areas = grid(`
  "a a a b b"
  "a a a b b"
  ". . c c c"
  "d d d d d"
`)

// → {
//   width: 5,
//   height: 4,
//   areas: {
//     a: {
//       column: {start: 1, end: 4, span: 3},
//       row: {start: 1, end: 3, span: 2},
//     },
//     b: {
//       column: {start: 4, end: 6, span: 2},
//       row: {start: 1, end: 3, span: 2},
//     },
//     c: {
//       column: {start: 3, end: 6, span: 3},
//       row: {start: 3, end: 4, span: 1},
//     },
//     d: {
//       column: {start: 1, end: 6, span: 5},
//       row: {start: 4, end: 5, span: 1},
//     },
//   },
// }

template(grid)

Builds a grid template from an object representation.

Arguments

  1. grid Grid The grid to build.

Returns

string The equivalent grid template.

Example

import { template } from 'css-grid-template-parser'

const areas = template({
  width: 5,
  height: 4,
  areas: {
    a: {
      column: { start: 1, end: 4, span: 3 },
      row: { start: 1, end: 3, span: 2 },
    },
    b: {
      column: { start: 3, end: 6, span: 3 },
      row: { start: 3, end: 5, span: 2 },
    },
  },
})

// → `"a a a . ."
//    "a a a . ."
//    ". . b b b"
//    ". . b b b"`

rect(area)

Converts an area into a rect.

Arguments

  1. area Area The area to convert.

Returns

Rect The equivalent rect.

Example

import { rect } from 'css-grid-template-parser'

const r = rect({
  column: { start: 1, end: 4, span: 3 },
  row: { start: 1, end: 3, span: 2 },
})

// → {
//     x: 0,
//     y: 0,
//     width: 3,
//     height: 2,
//   }

area(rect)

Converts a rect into an area.

Arguments

  1. rect Rect The rect to convert.

Returns

Area The equivalent area.

Example

import { area } from 'css-grid-template-parser'

const a = area({
  x: 0,
  y: 0,
  width: 3,
  height: 2,
})

// → {
//     column: {start: 1, end: 4, span: 3},
//     row: {start: 1, end: 3, span: 2},
//   }

minColumnStart(grid)

Finds the min column start of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The min column start.

Example

import { grid, minColumnStart } from 'css-grid-template-parser'

const min = minColumnStart(
  grid(`
  ". . a a a"
  ". b b b b"
  ". . . c c"
`)
)

// → 2

maxColumnStart(grid)

Finds the max column start of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The max column start.

Example

import { grid, maxColumnStart } from 'css-grid-template-parser'

const max = maxColumnStart(
  grid(`
  ". . a a a"
  ". b b b b"
  ". . . c c"
`)
)

// → 4

minColumnEnd(grid)

Finds the min column end of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The min column end.

Example

import { grid, minColumnEnd } from 'css-grid-template-parser'

const min = minColumnEnd(
  grid(`
  "a a . . ."
  "b b b b ."
  "c c c . ."
`)
)

// → 3

maxColumnEnd(grid)

Finds the max column end of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The max column end.

Example

import { grid, maxColumnEnd } from 'css-grid-template-parser'

const max = maxColumnEnd(
  grid(`
  "a a . . ."
  "b b b b ."
  "c c c . ."
`)
)

// → 5

minRowStart(grid)

Finds the min row start of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The min row start.

Example

import { grid, minRowStart } from 'css-grid-template-parser'

const min = minRowStart(
  grid(`
  ". . . ."
  "a a . ."
  "a a b b"
  "a a b b"
`)
)

// → 2

maxRowStart(grid)

Finds the max row start of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The max row start.

Example

import { grid, maxRowStart } from 'css-grid-template-parser'

const max = maxRowStart(
  grid(`
  ". . . ."
  "a a . ."
  "a a b b"
  "a a b b"
`)
)

// → 3

minRowEnd(grid)

Finds the min row end of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The min row end.

Example

import { grid, minRowEnd } from 'css-grid-template-parser'

const min = minRowEnd(
  grid(`
  "a a b b"
  "a a b b"
  ". . b b"
  ". . . ."
`)
)

// → 3

maxRowEnd(grid)

Finds the max row end of all grid areas.

Arguments

  1. grid Grid The grid to analyze.

Returns

number The max row end.

Example

import { grid, maxRowEnd } from 'css-grid-template-parser'

const max = maxRowEnd(
  grid(`
  "a a b b"
  "a a b b"
  ". . b b"
  ". . . ."
`)
)

// → 4

Types

Track

export interface Track {
  start: number
  end: number
  span: number
}

Area

type Area = {
  row: Track
  column: Track
}

Rect

export interface Rect {
  x: number
  y: number
  width: number
  height: number
}

Grid

export interface Grid {
  width: number
  height: number
  areas: Record<string, Area>
}

License

MIT