Options
All
  • Public
  • Public/Protected
  • All
Menu

css-grid-template-parser

styled with prettier Greenkeeper badge Travis Coveralls

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

Index

Type aliases

Unit

Unit: "fr" | "%" | "px" | "vw" | "vh" | "em" | "rem" | "auto"

Variables

Const sep

sep: RegExp = /['"]\s*['"]?/g

Const ws

ws: RegExp = /\s+/g

Functions

area

Const cleanLine

  • cleanLine(l: string): string
  • Parameters

    • l: string

    Returns string

Const cleanTpl

  • cleanTpl(t: string): string
  • Parameters

    • t: string

    Returns string

find

  • find(fn: (...args: number[]) => number, direction: "row" | "column", extremum: "start" | "end", __namedParameters: { areas: Record<string, Area> }): number
  • Parameters

    • fn: (...args: number[]) => number
        • (...args: number[]): number
        • Parameters

          • Rest ...args: number[]

          Returns number

    • direction: "row" | "column"
    • extremum: "start" | "end"
    • __namedParameters: { areas: Record<string, Area> }
      • areas: Record<string, Area>

    Returns number

flatten

  • flatten(entries: Entry[]): string

getColumns

  • getColumns(areas: string[], grid: Grid, row: number, current?: number, cols?: string): string
  • Gets columns

    Parameters

    • areas: string[]
    • grid: Grid
    • row: number
    • Default value current: number = 0
    • Default value cols: string = ""

    Returns string

    columns

Const getRow

  • getRow(l: string): Array<string>
  • Parameters

    • l: string

    Returns Array<string>

getRows

  • getRows(areas: Array<string>, grid: Grid, current?: number, rows?: string): string
  • Gets rows

    Parameters

    • areas: Array<string>
    • grid: Grid
    • Default value current: number = 0
    • Default value rows: string = ""

    Returns string

    rows

Const getTpl

  • getTpl(t: string): Array<string>
  • Parameters

    • t: string

    Returns Array<string>

grid

  • grid(tpl: string): Grid
  • grid

    example

    grid(". ." ". .");

    Parameters

    • tpl: string

      string

    Returns Grid

    { width: 2, height: 2, areas: {}, }

matchingArea

  • matchingArea(areas: Grid["areas"], row: number, column: number): (Anonymous function)
  • Matching area

    Parameters

    • areas: Grid["areas"]
    • row: number
    • column: number

    Returns (Anonymous function)

maxColumnEnd

  • maxColumnEnd(grid: Grid): number
  • Maxs column end

    Parameters

    Returns number

    column end

maxColumnStart

  • maxColumnStart(grid: Grid): number
  • Maxs column start

    Parameters

    Returns number

    column start

maxRowEnd

  • maxRowEnd(grid: Grid): number
  • Maxs row end

    Parameters

    Returns number

    row end

maxRowStart

  • maxRowStart(grid: Grid): number
  • Maxs row start

    Parameters

    Returns number

    row start

minColumnEnd

  • minColumnEnd(grid: Grid): number
  • Mins column end

    Parameters

    Returns number

    column end

minColumnStart

  • minColumnStart(grid: Grid): number
  • Mins column start

    Parameters

    Returns number

    column start

minRowEnd

  • minRowEnd(grid: Grid): number
  • Mins row end

    Parameters

    Returns number

    row end

minRowStart

  • minRowStart(grid: Grid): number
  • Mins row start

    Parameters

    Returns number

    row start

rect

Const reduceLines

  • reduceLines(acc: {}, line: string, r: number): {}
  • Parameters

    • acc: {}
      • [key: string]: Area
    • line: string
    • r: number

    Returns {}

    • [key: string]: Area

template

  • template(grid: Grid): string
  • example

    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"

    Parameters

    Returns string

track

  • track(start: number, end: number): Track

Legend

  • Property

Generated using TypeDoc