styled-css-grid icon indicating copy to clipboard operation
styled-css-grid copied to clipboard

Example for responsive layout ?

Open ThunderDev1 opened this issue 7 years ago • 4 comments

Hi,

I'm using a holy grail type layout. How would I go about hiding the left and right columns on on small screen devices ?

Thanks

ThunderDev1 avatar Sep 08 '18 22:09 ThunderDev1

I've been wrestling with this too. How do you make them responsive?

blurymind avatar May 27 '19 15:05 blurymind

https://styled-css-grid.js.org/#responsive

gunar avatar Aug 01 '19 17:08 gunar

To me, that example for responsiveness is too simple. The "holy grail" is a more complex layout. Like @ThunderDev1 was saying, how would you for example make the "holy grail" layout look like this (after a certain break point):

header menu content ads footer

derekcannon avatar Nov 11 '19 04:11 derekcannon

I've managed to make this work for me pretty well

import { Cell as C } from 'styled-css-grid';

const Cell = ({ widths = {}, offset = {}, top = {}, children }) => {
  // returns an object with boolean values for xs, sm, md, lg matches using
  // window.matchMedia(`(min-width: ${params}px)`).matches
  // I can share my solution if people want.
  const dimensions = useMedia();

  const widthMatches = Object.keys(widths)
    .reverse()
    .map((w) => ({ key: w, matches: Boolean(dimensions[w]) }));
  
  // Finds the first media query that matches the widths passed,
  // reserving it means lg down ⬇️, (lg, md, sm, xs)
  const match = widthMatches.find((media) => media.matches);

  // I prefer using an "offset" to be offsets of columns instead of the left behaviour which defaults to "start at" n which is why I've added one to the left value
  return (
    <C width={(match && widths[match.key]) || 12} left={match && offset[match.key] + 1} top={match && top[match.key]}>
      {children}
    </C>
  );
};


// Usage example
<Cell
  widths={{
    xs: 12,
    sm: 4,
    md: 3,
    lg: 9,
  }}
  offset={{ sm: 4, lg: 3 }}
>
  <p>
    Responsive widths are 🔥 
  </p>
</Cell>

omonk avatar May 20 '20 23:05 omonk