News for hackers. Stuff that matters.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by hardwaresofton on Monday December 24, 2018 @01:25AM (#18750748)
For those wondering why not just use CSS Grid -- it's not build for that[0] (see @rachelandrew's comment). It's not explained on the repo README but the author goes into it on the explanation blog post.

As mentioned, Masonry[1] is the other famous library for doing this. Magic Grid also has a Vue port[2] if you're into that.

[EDIT] - PR just got merged, explanation of this stuff is on the Github README now

[0]: https://github.com/rachelandrew/cssgrid-ama/issues/19

[1]: https://github.com/desandro/masonry

[2]: https://github.com/imlinus/Vue-Magic-Grid

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by captain_crabs on Monday December 24, 2018 @10:34AM (#18752561)
Although I've read through the "css grid isn't meant to do this details", you can get fairly close to this with pure css grid, see here: https://github.com/wesbos/css-grid/blob/master/20%20-%20CSS%...
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by hardwaresofton on Tuesday December 25, 2018 @12:41AM (#18756127)
Here's the JSfiddle for that demo:

https://jsfiddle.net/cbjzped2/

The problem with that is that the grid rows are determined dynamically:

    .item.v2 {
      grid-row: span 2;
    }
    .item.v3 {
      grid-row: span 3;
    }
    .item.v4 {
      grid-row: span 4;
    }
    .item.h2 {
      grid-column: span 2;
    }
I'd much rather drop in a library like MagicGrid or Masonry and deal with it from there, to be honest, instead of trying to hack grid to do it. I think grid could be extended to handle this kind of design but until it is...
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by WhitneyLand on Monday December 24, 2018 @12:03PM (#18752997)
But, grid and/or flex box could still do these things by nesting I assume?

So do you mean this is a less complex, more sane way, or did you mean it enables things that couldn’t be done with grid/flex?

Of course sanity is good but it’s an important distinction, say if a simple scenario could avoid a library dep without much extra complexity.

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by hardwaresofton on Tuesday December 25, 2018 @12:39AM (#18756118)
I'm not sure -- the problem is that the height is determined across the whole row -- as soon as you have one item bigger than others in a row it falls apart.

Some of the auto sizing features of grid make it seem like it could work but I haven't seen too many examples, and rachelandrews saying "it's not built for that" is more than enough to discourage me from trying to hack around it.

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by humphreybc on Monday December 24, 2018 @02:37AM (#18750967)
I spent some time trying to build a simple grid layout with dynamic width and height items in order for the layout to naturally flow responsively. Most libraries—like this one—require fixed width items or fixed width containers with fluid items.

Fully responsive turned out to be a lot harder than I thought it would be due to lots of rabbit holes, but the algorithm was fairly straightforward. My first approach was to try CSS Flex and Grid, but they didn't cut the mustard since they're not really designed for this sort of thing. I then found CSS Columns and had that working, but it proved too unstable for production use with issues with the sort order of items, wrapping across columns, and cross-browser support.

I ended up building our own library: https://github.com/heydovetail/react-masonry-responsive

There's a nice brief writeup about using CSS for masonry layout here: https://regisphilibert.com/blog/2017/12/pure-css-masonry-lay...

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by gnomodromo on Monday December 24, 2018 @07:31AM (#18751837)
I've experimented with fully responsive layouts too for my website (http://www.uptopnews.com) but at the end I just used css flex gird. I almost build a comment section where the comments are cards in a grid but that rabbit hole was too deep.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by Eli_P on Monday December 24, 2018 @11:39AM (#18752874)
Correct me if I'm wrong, but using window.addEventListener("resize", () => (), 200) is no good, you probably should consider window.requestAnimationFrame() and debouncing. Otherwise, your library will drain battery juice, despite it's really lightweight. Say, on Android, actions like typing into an input field, changing orientation, scrolling backward and triggering url bar appearing, all of them do trigger costly document reflow.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @11:51AM (#18752945)
You're right requestAnimationFrame is more efficient than using the resize listener with a delay. I'm actually working on an update to change that. It'll be fixed soon.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by dakom on Monday December 24, 2018 @02:33AM (#18750948)
How about a grid of equal-height items, but intersperse something between each row- can this library do that?

For example, consider a bookshelf design where the books are the items and each row has an element underneath it (the element would itself be composed of say 3 images for left and right ends plus the middle)

afaik that's not possible with CSS grid either since grid doesn't have an `nth-row` selector

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @02:56AM (#18751031)
No, the library doesn't support that. you should be able to achieve the effect with plain html and css though.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by ThomPete on Monday December 24, 2018 @06:57AM (#18751748)
Now that I have a bunch of knowledgeable people in one thread.

Anyone know of a good grid layout library/framework.

With grid i don't mean the css syntax version but the actual visual design grid that gives you something like:

https://cl.ly/a01ae1db0240

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by mdolon on Monday December 24, 2018 @09:33AM (#18752274)
I made a grid overlay tool years ago, perhaps something in there may be of use? https://devgrow.com/griddy-overlay/
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by WhitneyLand on Monday December 24, 2018 @12:12PM (#18753040)
You said below you wanted the visual lines.

It really looked like you would have been referrring to the text overlapping multiple columns, is that not related to what you meant?

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by molszanski on Monday December 24, 2018 @07:28AM (#18751829)
Not sure what your question is. You want something like a 12 column grid? Or the visual lines?
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by ThomPete on Monday December 24, 2018 @07:51AM (#18751897)
The visual lines. I seem to remember a few frameworks i saw a while back which allow you to control these grid lines and help you do them according to the golden means.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by sandGorgon on Monday December 24, 2018 @10:53AM (#18752650)
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by supernintendo on Monday December 24, 2018 @12:49AM (#18750618)
Neat. We use Masonry [1] although I’m not the biggest fan of it. How does this library compare?

[1] https://github.com/desandro/masonry

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @12:59AM (#18750657)
Depends on the use case. If the grid's items have varying widths then masonry is the better option. However, for a grid with equal width items, I'll definitely go with magic grid. It's just so much lighter (2.3kb minified) than masonry (23.5kb minified).
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by matt4077 on Monday December 24, 2018 @11:11AM (#18752741)
I really don't see what's lacking with CSS Grids for equal-with items. Even accommodating different sizes, and allowing reordering for packing, works fine with just CSS.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by catacombs on Monday December 24, 2018 @01:33AM (#18750785)
Thanks. This answers the very question I was about to post.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by turbolent on Monday December 24, 2018 @05:25AM (#18751449)
If you are looking for a more flexible solution with support for any layout and support for animations, something like iOS' UICollectionView or Android's RecyclerView, have a look at https://github.com/turbolent/collection-view.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by WhitneyLand on Monday December 24, 2018 @11:36AM (#18752858)
Can this, or any solution allow for a cell to overlap into another row or column?

Something like this https://www.pcscoreboards.com/footballscoreboardpro/scoreboa...

Notice the red 8 seems to need to overlap between rows.

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @12:51PM (#18753246)
This doesn't and I think most grid solutions keep the items separate
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by sbr464 on Monday December 24, 2018 @01:38AM (#18750800)
Curious why a lot of grid algorithms/implementations only support items of the same height or width?
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @01:46AM (#18750814)
Makes it much easier to segment the grid into rows or columns
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by rmdashrfstar on Monday December 24, 2018 @12:53AM (#18750633)
And the reason to use this over built-in CSS grids is...
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by jahbrewski on Monday December 24, 2018 @01:09AM (#18750696)
+1 - I recently went looking for a CSS framework (I was still in the “Bootstrap” mindset), and ultimately decided on vanilla CSS grid + flexbox. If anyone is looking for a resource to learn CSS grid, I highly recommend Grid Garden: http://cssgridgarden.com/
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by PavlovsCat on Monday December 24, 2018 @02:03AM (#18750860)
That is awesome. Did you see this in the footer?

https://flexboxfroggy.com/

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by dfa1234 on Monday December 24, 2018 @05:58AM (#18751571)
I just started this tutorial. Thats is a "why", not a "how"... So maybe I can ask here. Why grid-column start include the value and grid column end exclude it... In fact where there is a ressource for understanding css logic on the internet?
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by kilburn on Monday December 24, 2018 @06:16AM (#18751615)
> Why grid-column start include the value and grid column end exclude it

All CSS grid properties are defined in terms of grid "lines", not cells. If you have a grid of 2x2 cells, there are actually 3 vertical lines and 3 horizontal lines:

    1 2 3
  1 ┌─┬─┐
  2 ├─┼─┤
  3 └─┴─┘
"grid-column-start: x" means "this area starts the x vertical line"

"grid-column-end: y" means "this area ends at the y vertical line"

As you can see, there is no possible ambiguity here on whether you mean "inclusive" or "exclusive" like there would be if you referred to cells (which is why you had this question). That's the reason they chose to go with the "lines" model.

> In fact where there is a ressource for understanding css logic on the internet?

Usually each CSS spec has a rationale and some introduction to the "why" and the "thinking model" of the spec. For CSS grid you may look at [1].

[1] https://www.w3.org/TR/css-grid-1/#grid-concepts

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by quickthrower2 on Monday December 24, 2018 @06:02AM (#18751579)
And I guess you can polyfill that back to IE etc.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by e-oj on Monday December 24, 2018 @01:03AM (#18750671)
When the components in a grid have different heights, a single longer or shorter component stretches the whole row or leaves unpleasant whitespace at the bottom.
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by zanny on Monday December 24, 2018 @01:20AM (#18750734)
You can use auto-flow: dense to have the grid backfill gaps. I haven't had much chance to test its limits cross browser yet though but trivial examples have worked for me so far.

It isn't as optimal a layout system as you can get with javascript but generally so long as all elements are at least half the height of other members of a row it doesn't look offensively bad. And there is room for more sophisticated layout algorithms in the future.

And it generally isn't that hard to pad grid items to be multiples of a common height so they can organically fill consistently.

Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by jtmarmon on Monday December 24, 2018 @01:01AM (#18750667)
Looks like it's addressed here: https://codeburst.io/magic-grid-f8e2221e7cef
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by thinkloop on Monday December 24, 2018 @12:56AM (#18750641)
This should have definitely been addressed in the readme
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by jogis on Monday December 24, 2018 @07:03AM (#18751770)
what about ssr?
Re:Show HN: Magic Grid – A simple JavaScript library for dyn... by KenanSulayman on Monday December 24, 2018 @07:52AM (#18751903)
Why do you need yet another library? Not invented here syndrome? A simple Github search gives me literally thousands of results: https://github.com/search?utf8=&q=css+grid