Vanilla quickstart
The core is a set of pure functions. You own the DOM, the pointer events, and the state; Gridla answers one question per pointer move: "given this layout and this intent, what is the next layout?"
This page builds a canvas you can drag on and resize in about eighty lines. It also projects the layout onto the element's measured size, so it is responsive from the start.
1. Describe a layout
A layout is a canvas plus items in canvas-relative pixel coordinates. Nothing else.
sizeMode: 'fixed-h' keeps the header 80px tall when the layout is projected; everything else scales. See sizing modes.
2. Render, project, solve
Three things to notice:
- Solvers are pure.
moveItemandresizeItemreturn a new layout and never mutate their input. During a drag you paintresult.layoutdirectly; on release you keep the last accepted result. Cancel by painting the old one. acceptedis the contract. When the solver cannot honor a request,acceptedisfalseandresult.layoutequals the input. You never end up with an invalid layout.strategytells you why. Every result names the strategy that produced it (push-x,swap,free,rejected, and so on). Show it in a debug overlay while you tunegapandsnapDistance.
3. Insert, remove, rearrange
Removing an item is plain array filtering; the model is data. placeItem with a pointer centers the item on the pointer and falls back through smaller sizes and sibling shrinking until something fits; with a position it treats the top-left as the intent instead. See the place recipe.
Try it
Next
- Mental model explains the layout/render split used above.
- Solver behavior lists every strategy in order.
- The React quickstart does all of this with
GridProvider.