Giter Site home page Giter Site logo

bricky's Introduction

Bricky

Note

This library is a fork of macy.js. This library was created as a successor to it was there was no maintenance to it for 4 years.

Roadmap

  • Typescript support (#1)
  • container option to use actual HTML elements. (#2)

Bricky is a lightweight dependency-free JavaScript library designed to sort items vertically into columns by finding an optimum layout with a minimum height.

Installing

Install with npm:

npm install bricky

Install with Bower:

bower install bricky

Include via jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/bricky@2"></script>

Usage

var brickyInstance = Bricky({
  // See below for all available options.
});

Options

container

Default: None

Use this option to specify your target container element to apply Bricky too. All direct children of an element with this selector will become sortable items and a height applied to the target container.

columns

Default: 4

Define the default amount of columns to work with. Use the breakAt option to specify breakpoints for this value.

trueOrder

Default: false

Setting this to false will prioritise equalising the height of each column over the order of the items themselves.

margin

Default: 0 Adjust the margin between columns with a pixel value. Don’t forget you can still apply padding to the elements with standard CSS.

Note: Due to the way the container height is calculated, using anything other than integer in the Y property will cause an error.

margin: {
  x: 10,
  y: 16
}

When declaring the default margin as an object it requires both and x and y values unlike the breakAt object.

waitForImages

Default: false If set to true, Bricky will wait for all images on the page to load before running. Set to false by default, it will run every time an image loads.

useOwnImageLoader

Default: false

Set this to true if you would prefer to use a different image loaded library.

mobileFirst

Default: false

Setting this value to true will alter how the breakAt options will work. Bricky will now work in a mobile first way so the default columns will be the default then if for example you have 400: 2 in your breakAt object, if the document is greater or equal to 400px the column count will be 2.

breakAt

Default: None This object allows you to specify how the total number of columns will change based on the width of the viewport. Setting an option to 780: 3 for example will adjust the column count to 3 when the viewport is <= 780px. If the viewport resizes after the page has loaded, Bricky will rerun to ensure optimum sorting

If the column is set to one then Bricky will remove all styling to leave you to style it perfectly on mobile.

Added in v2.1 breakAt now supports changing margin within these breakpoints.

For example

{
  breakAt: {
    760: {
      margin: {
        x: 20,
        y: 10,
      },
      columns: 4
    }
  }
}

If you do not need the modify the margin you can leave it as 760: 4 and bricky will set the columns to 4. You can also just define a change in just one marginal direction for example:

{
  breakAt: {
    760: {
      margin: {
        x: 20,
      },
      columns: 4
    }
  }
}

This would change the xMargin to 20px when screens are smaller than 760, but the instance will use a previously declared y value.

cancelLegacy

Default: false - If enabled this will cause the script to not run on browsers that don't support native Promises and when there isn't a polyfill present.

useContainerForBreakpoints

Default: false - When enabled the breakpoint options are based on the container elements width instead of the document width.

Methods

Bricky

Parameters: {Object} args - required

This is the initializing function. The function takes an object of properties listed above. The only required property is container which would be the selector for the element that contains all the elements you want to be layed out:

var bricky = Bricky({
  container: "#bricky-container",
  trueOrder: false,
  waitForImages: false,
  margin: 24,
  columns: 6,
  breakAt: {
    1200: 5,
    940: 3,
    520: 2,
    400: 1,
  },
});

From this point on whenever 'Bricky' is specified it is referencing the variable you assign bricky to when making the initial call.

recalculate

_Parameters: {Boolean} refresh - can be null & {Boolean} loaded -can be null _

When called this recalculates the entire layout, this becomes useful if you just used ajax to pull in more content:

brickyInstance.recalculate();

runOnImageLoad

Parameters: {Function} - Function to run on image load & {Boolean} If true it will run everytime an image loads

runOnImageLoad is a method used to do something each time and image loads or after all images have been loaded. This helps when using Ajax to make sure the layout is worked out correctly when images are loading. Using this in conjunction with the recalculate function makes your layouts look great no matter how long it takes to load in your images:

brickyInstance.runOnImageLoad(function () {
  brickyInstance.recalculate(true);
}, true);

If you only require it to run once all the images have loaded you can achieve this by ommiting the second parameter as this defaults to false:

brickyInstance.runOnImageLoad(function () {
  console.log("I only get called when all images are loaded");
  brickyInstance.recalculate(true, true);
});

If you only require the during function to run then only pass it one function:

brickyInstance.runOnImageLoad(function () {
  console.log("Every time an image loads I get fired");
  brickyInstance.recalculate(true);
}, true);

The callback function receives an event as its first and only property, if you are running the callback on every image load then the function has access to the image that has just loaded.

brickyInstance.runOnImageLoad(function (event) {
  if (event.data.img) {
    // note: this parameter can be undefined if it is the final completion event that is emitted.
    console.log(event.data.img);
  }
}, true);

remove

Parameters: None

Remove does exactly what it says on the tin, it removes all styling and event listeners that Bricky added to the DOM:

brickyInstance.remove();

reInit

Parameters: None

Reinitialises the current bricky instance;

brickyInstance.reInit();

on

Parameters: {String} - Event key, {Function} the function to run when the event occurs

This would console log when all images are loaded.

brickyInstance.on(
  brickyInstance.constants.EVENT_IMAGE_COMPLETE,
  function (ctx) {
    console.log("all images have loaded");
  },
);

emit

Parameters: {String} - Event key

Emit an event, although bricky does not utilise most of these events, these are more to trigger your own functions.


Constants

Bricky now has some constants available to be used with in the events system. This is to make sure the functions are targetting the correct event as the naming may be subject to change They are all accessible under brickyInstance.constants

Currently available constants

Key Value Description
EVENT_INITIALIZED 'bricky.initialized' This is the event constant for when bricky is initialized/reinitialized
EVENT_RECALCULATED 'bricky.recalculated' This is the event constant for every time the layout is recalculated
EVENT_IMAGE_LOAD 'bricky.images.load' This is the event constant for when an image loads
EVENT_IMAGE_COMPLETE 'bricky.images.complete' This is the event constant for when all images are complete
EVENT_RESIZE 'bricky.resize' This is the event constant for when the document is resized

Notes

  • Browser support for all major browsers excluding IE11
  • IE11 requires a Promise polyfill for image loading to work
  • To support IE10 a dataset polyfill will have to be added

bricky's People

Contributors

stevenjpx2 avatar

Stargazers

 avatar

Watchers

 avatar

bricky's Issues

[FEATURE] Allow selector to select more than just a string.

Is your feature request related to a problem? Please describe.
Currently, you can only use a single selector string which uses a special method called $e to specially select only particular elements.

Describe the solution you'd like
Use querySelectorAll as fallback, but default to using HTMLElement | SVGElement.

[FEATURE] Typescript support

Is your feature request related to a problem? Please describe.
It is not a problem, but there is no Typescript support.

Describe the solution you'd like

  • Add typescript.
  • Convert all .js files to .ts with appropriate typing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.