macy.js

repository·master·Indexed 23 days ago

https://github.com/bigbite/macy.js

A lightweight, dependency-free JavaScript masonry layout library (version 2.5.1) that sorts items into vertical columns with minimum height. It features responsive breakpoints via the breakAt option, dynamic layout recalculation, and image loading handlers.

Tokens
1.6K
Snippets
8
Records
9
Agent score
30%

What's inside macy.js

  1. Configure responsive breakpoints with breakAt

    master

    The breakAt option allows you to change the number of columns and margins at specific viewport widths. When the viewport resizes, Macy automatically reruns to optimize the layout.

    Changing columns and margins:

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

    Changing only one margin direction:

    {
      breakAt: {
        760: {
          margin: {
            x: 20,
          },
          columns: 4
        }
      }
    }
  2. Configure Macy.js options

    master

    Use the following options to customize the layout behavior:

    • container (Default: None): CSS selector for the target container. All direct children will be treated as sortable items.
    • columns (Default: 4): The default number of columns.
    • trueOrder (Default: false): If false, Macy prioritizes equalizing column heights over maintaining the original item order.
    • margin (Default: 0): Pixel value for spacing between columns. Can be an object { x: number, y: number }. Note: y must be an integer.
    • waitForImages (Default: false): If true, Macy waits for all images to load before running. If false, it runs every time an image loads.
    • useOwnImageLoader (Default: false): If true, uses a different image loading library.
    • mobileFirst (Default: false): If true, breakAt logic is inverted (default columns is the base, and breakpoints trigger when the viewport is greater than or equal to the specified width).
    • breakAt (Default: None): An object defining column/margin changes based on viewport width. Example: { 780: 3 } sets columns to 3 when viewport is <= 780px.
    • cancelLegacy (Default: false): If true, prevents execution on browsers without native Promise support (unless a polyfill is present).
    • useContainerForBreakpoints (Default: false): If true, breakpoints are calculated based on the container's width instead of the document width.
  3. Initialize Macy.js

    master

    To use Macy.js, call the Macy() function with a configuration object. The container property is required and should be a CSS selector for the element containing the items you want to sort.

    var macyInstance = Macy({
      container: '#macy-container',
      trueOrder: false,
      waitForImages: false,
      margin: 24,
      columns: 6,
      breakAt: {
        1200: 5,
        940: 3,
        520: 2,
        400: 1
      }
    });
  4. Handle image loading with runOnImageLoad()

    master

    To ensure the layout is correct when images load (especially with AJAX), use runOnImageLoad().

    • Run on every image load: Pass a callback and true as the second argument.
    • Run once after all images load: Pass a callback and omit the second argument (defaults to false).

    The callback receives an event object. If running on every load, event.data.img contains the image that just loaded (may be undefined on the final completion event).

    Example: Recalculate on every image load:

    macyInstance.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);
      }
      macyInstance.recalculate(true);
    }, true);
    macyInstance.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);
  5. Listen to Macy events with on()

    master

    You can listen to specific lifecycle events using the on(eventKey, callback) method. Use the constants provided on the instance to ensure accuracy.

    macyInstance.on(macyInstance.constants.EVENT_IMAGE_COMPLETE, function (ctx) {
      console.log('all images have loaded');
    });
  6. Manage Macy instance lifecycle with remove() and reInit()

    master

    Use remove() to strip all styling and event listeners added by Macy from the DOM. Use reInit() to re-initialize the current instance.

    macyInstance.remove();
    
    macyInstance.reInit();
  7. Reference Macy.js event constants

    master

    Access these constants via macyInstance.constants to avoid hardcoding event strings.

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