universe:react-grid-layout

v0.10.0Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Universe React Grid Layout

React Grid Layout is a grid layout system built with React. RGL is React-only and does not require jQuery. This package is a meteor port of STRML/react-grid-layout.

1import {ReactGridLayout} from '{universe:react-grid-layout}';
2export default React.createClass({
3    displayName: 'Demo',
4    render: function () {
5        return (
6            <ReactGridLayout className="layout" cols={12} rowHeight={30}>
7                <div key={1} _grid={{x: 0, y: 0, w: 1, h: 2}}>1</div>
8                <div key={2} _grid={{x: 1, y: 0, w: 1, h: 2}}>2</div>
9                <div key={3} _grid={{x: 2, y: 0, w: 1, h: 2}}>3</div>
10            </ReactGridLayout>
11        )
12    }
13});

Installation

meteor add universe:react-grid-layout

Demos

  1. Showcase
  2. Basic
  3. No Dragging/Resizing (Layout Only)
  4. Messy Layout Autocorrect
  5. Layout Defined on Children
  6. Static Elements
  7. Adding/Removing Elements
  8. Saving Layout to LocalStorage
  9. Saving a Responsive Layout to LocalStorage
  10. Minimum and Maximum Width/Height
  11. Dynamic Minimum and Maximum Width/Height
  12. No Vertical Compacting (Free Movement)

Responsive Usage

To make RGL responsive, use the <ResponsiveReactGridLayout> element:

1import {ResponsiveReactGridLayout} from '{universe:react-grid-layout}';
2//...
3render: function() {
4  // {lg: layout1, md: layout2, ...}
5  var layouts = getLayoutsFromSomewhere();
6  return (
7    <ResponsiveReactGridLayout className="layout" layouts={layouts}
8      breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
9      cols={{lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}}>
10      <div key={1}>1</div>
11      <div key={2}>2</div>
12      <div key={3}>3</div>
13    </ResponsiveReactGridLayout>
14  )
15}

When in responsive mode, you should supply at least one breakpoint via the layouts property.

When using layouts, it is best to supply as many breakpoints as possible, especially the largest one. If the largest is provided, RGL will attempt to interpolate the rest.

Grid Layout Props

RGL supports the following properties (see the source for the final word on this):

1//
2// Basic props
3//
4
5// If true, the container height swells and contracts to fit contents
6autoSize: React.PropTypes.bool,
7
8// {name: pxVal}, e.g. {lg: 1200, md: 996, sm: 768, xs: 480}
9breakpoints: React.PropTypes.object,
10
11// Number of columns in this layout.
12cols: React.PropTypes.number,
13
14// A selector that will not be draggable.
15draggableCancel: React.PropTypes.string,
16// A selector for the draggable handler
17draggableHandle: React.PropTypes.string,
18
19// If true, the layout will compact vertically
20verticalCompact: React.PropTypes.bool,
21
22// Layout is an array of object with the format:
23// {x: Number, y: Number, w: Number, h: Number}
24// The index into the layout must match the key used on each item component.
25// If you choose to use custom keys, you can specify that key in the layout
26// array objects like so:
27// {i: String, x: Number, y: Number, w: Number, h: Number}
28layout: React.PropTypes.array,
29
30// This allows setting the initial width on the server side.
31initialWidth: React.PropTypes.number,
32
33// Margin between items [x, y] in px.
34margin: React.PropTypes.array,
35
36// Rows have a static height, but you can change this based on breakpoints
37// if you like.
38rowHeight: React.PropTypes.number,
39
40//
41// Flags
42//
43isDraggable: React.PropTypes.bool,
44isResizable: React.PropTypes.bool,
45// Uses CSS3 translate() instead of position top/left.
46// This makes about 6x faster paint performance
47useCSSTransforms: React.PropTypes.bool,
48
49// If false, you should supply width yourself. Good if you want to debounce
50// resize events or reuse a handler from somewhere else.
51listenToWindowResize: React.PropTypes.bool,
52
53//
54// Callbacks
55//
56
57// Callback so you can save the layout.
58// Calls back with (currentLayout, allLayouts). allLayouts are keyed by breakpoint.
59onLayoutChange: React.PropTypes.func,
60
61//
62// All callbacks below have signature (layout, oldItem, newItem, placeholder, e).
63// 'start' and 'stop' callbacks pass `undefined` for 'placeholder'.
64//
65
66// Calls when drag starts.
67onDragStart: React.PropTypes.func,
68// Calls on each drag movement.
69onDrag: React.PropTypes.func,
70// Calls when drag is complete.
71onDragStop: React.PropTypes.func,
72// Calls when resize starts.
73onResizeStart: React.PropTypes.func,
74// Calls when resize movement happens.
75onResize: React.PropTypes.func,
76// Calls when resize is complete.
77onResizeStop: React.PropTypes.func

Responsive Grid Layout Props

The responsive grid layout can be used instead. It supports all of the props above, excepting layout. The new properties and changes are:

1// {name: pxVal}, e.g. {lg: 1200, md: 996, sm: 768, xs: 480}
2// Breakpoint names are arbitrary but must match in the cols and layouts objects.
3breakpoints: React.PropTypes.object,
4
5// # of cols. This is a breakpoint -> cols map, e.g. {lg: 12, md: 10, ...}
6cols: React.PropTypes.object,
7
8// layouts is an object mapping breakpoints to layouts.
9// e.g. {lg: Layout, md: Layout, ...}
10layouts: React.PropTypes.object
11
12//
13// Callbacks
14//
15
16// Calls back with breakpoint and new # cols
17onBreakpointChange: React.PropTypes.func,
18
19// Callback so you can save the layout.
20// Calls back with (currentLayout, allLayouts). allLayouts are keyed by breakpoint.
21onLayoutChange: React.PropTypes.func

Grid Item Props

RGL supports the following properties on grid items or layout items. When initializing a grid, build a layout array (as in the first example above), or attach this object as the _grid property to each of your child elements (as in the second example).

Note that if a grid item is provided but incomplete (missing one of x, y, w, or h), an error will be thrown so you can correct your layout.

If no properties are provided for a grid item, one will be generated with a width and height of 1.

You can set minimums and maximums for each dimension. This is for resizing; it of course has no effect if resizing is disabled. Errors will be thrown if your mins and maxes overlap incorrectly, or your initial dimensions are out of range.

Any GridItem properties defined directly on the layout item will take precedence over globally-set options. For example, if the layout has the property isDraggable: false, but the grid item has isDraggable: true, the item will be draggable.

1{
2  // These are all in grid units, not pixels
3  x: React.PropTypes.number.isRequired,
4  y: React.PropTypes.number.isRequired,
5  w: React.PropTypes.number.isRequired,
6  h: React.PropTypes.number.isRequired,
7  minW: React.PropTypes.number,
8  maxW: React.PropTypes.number,
9  minH: React.PropTypes.number,
10  maxH: React.PropTypes.number,
11
12  // If true, equal to `isDraggable: false, isResizable: false`.
13  static: React.PropTypes.bool,
14  // If false, will not be draggable. Overrides `static`.
15  isDraggable: React.PropTypes.bool,
16  // If false, will not be resizable. Overrides `static`.
17  isResizable: React.PropTypes.bool,
18
19  className: React.PropTypes.string,
20  // Selector for draggable handle
21  handle: React.PropTypes.string,
22  // Selector for draggable cancel (see react-draggable)
23  cancel: React.PropTypes.string
24}

Grid Layout Defaults

1{
2  autoSize: true,
3  breakpoints: {lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0},
4  cols: 10,
5  rowHeight: 150,
6  initialWidth: 1280,
7  margin: [10, 10],
8  minH: 1,
9  minW: 1,
10  maxH: Infinity,
11  maxW: Infinity,
12  isDraggable: true,
13  isResizable: true,
14  useCSSTransforms: true,
15  listenToWindowResize: true,
16  verticalCompact: true
17}