brique
Version:
Create cascading layout grids like Pinterest with the power of CSS Grid Layout.
188 lines (156 loc) • 4.92 kB
Markdown
Create cascading layout grids like [Pinterest](https://www.pinterest.com/) with the power of **CSS Grid Layout**.
[](https://raphpare.github.io/brique/)
<img alt="Brique example" style="width: 100%; margin: 0 auto; max-width: 800px;" src="https://github.com/raphpare/brique/blob/master/img/example.png?raw=true">
1. [Install](
2. [Instantiate](
3. [HTML markup example](
4. [Parameters](
5. [Properties](
6. [Methods](
7. [Responsive grid](
```
$ npm i brique --save
```
``` ts
import { Brique } from './node_modules/brique/lib';
const refGrid = document.getElementById('grid');
new Brique(refGrid);
```
HTML `script` tag requires the `type="module"` attribute.
``` html
<script type="module" src="scripts/main.js"></script>
```
Create the grid in the JavaScript file (`scripts/main.js`) and import the `esm` version of the library (`index.esm.js`).
``` js
import { Brique } from './node_modules/brique/lib/index.esm.js';
const refGrid = document.getElementById('grid');
new Brique(refGrid);
```
``` html
<div id="grid">
<div>
<h2>Box 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div>
<h2>Box 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut laboret.</p>
</div>
<div>
<h2>Box 3</h2>
</div>
<div>
<h2>Box 4</h2>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div>
<h2>Box 5</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</p>
</div>
</div>
```
3 parameters can be passed to the `Brique` class.
```ts
new Brique(gridElement, options, observeGridResize);
```
Target HTML element which is the container for grid items.
| Property | Type | Required | Description |
| --- | --- | --- | --- |
| columns | number | true | Number of columns |
| columnGap | string | false | Spacing between columns |
| rowGap | string | false | Spacing between row |
``` ts
const refGrid = document.getElementById('grid');
const options = {
columns: 4,
rowGap: '24px',
columnGap: '16px',
};
new Brique(refGrid, options);
```
The default value for this parameter is `true`.
If the value is `true`, the grid items will be updated when the viewport is resized.
If the value is `false`, the grid will **never** be updated when the viewport is resized.
Static property which returns the options defined by default in the `Brique` class.
```ts
Brique.DEFAULT_OPTIONS;
```
Reference array of HTML elements of all grid items.
Update the rendering of the entire grid on demand.
``` ts
briqueGrid.update();
```
Update the rendering of the grid items on demand.
``` ts
briqueGrid.updateItems();
```
Update the dimension of grid items when the grid element is resized.
``` ts
const refGrid = document.getElementById('grid');
const briqueGrid = new Brique(refGrid);
briqueGrid.updateOnResize();
```
Stop update the dimension of grid items when the grid element is resized.
``` ts
briqueGrid.stopUpdateOnResize();
```
Return current options object.
``` ts
briqueGrid.getOptions(); // output: { columns: 3, rowGap: '32px', columnGap: '32px'}
```
Change all properties of options object.
``` ts
briqueGrid.setOptions({
columns: 5
});
```
Updates only changed properties.
``` ts
briqueGrid.updateOptions({
columns: 5
});
```
Can be used to create a [responsive grid](
Removes all events listened to on the HTML elements handled by the `Brique` class.
The `destroy()` method must be called when the grid is removed from HTML.
Update options object on media queries change.
``` ts
const refGrid = document.getElementById('grid');
const mediaQueryMobile = window.matchMedia('(max-width: 767px)');
const getColumnsNumber = () => mediaQueryMobile.matches ? 2 : 3;
const briqueGrid = new Brique(refGrid, {
columns: getColumnsNumber(),
columnGap: '32px',
rowGap: '32px',
});
mediaQueryMobile.addEventListener('change', () => {
briqueGrid.updateOptions({
columns: getColumnsNumber(),
});
});
```