DEV Community

Ahmad Rafsanjani
Ahmad Rafsanjani

Posted on • Edited on

1 1

Box Layout In Reusable Components for Mobile App Development

Hello everyone, I am Ahmad. It's my second post and my first post about React. Here's my thought:

How I design the layout of react.js app?

Before, I always use anything convenient to make all elements fit the screen. I use grid, flex, and absolute positioning anywhere without rules.

But recently I stuck to this rule( especially for mobile development ):

Grid for layout and flex for reusable components positioning

Grid for Layout

You can use one column grid layout for group-box of components in general. Then you can specify the inner layout of a group-box, if there is a horizontal( read: grid-column ) layout or even a vertical( grid-row ) layout.
Alt Text
Alternatively you can combine vertical and horizontal layout using grid-span or grid-template-areas. Just do what convenient to your app.

/* Template_Example: css */
.view  { display: grid; grid-template-rows: 1fr 2fr 9fr 3fr; }
.modal { display: grid; grid-template-rows: 1fr 2fr 2fr 3fr; }
.nav   { display: grid; grid-template-columns:  1fr 1fr 1fr; }
.control {
  display: grid;
  grid-template-areas: "left right" "left right" "footer footer";
}
Enter fullscreen mode Exit fullscreen mode

Flex for Component Positioning

I wrapped every small component or composite component in a box( div ), and add flexbox to the box so we could positioning its content relative to the box.

/* Component_Pattern */
function Component(props) {
  return (
    <div className={props.boxClass}>
      <ChildComponent  />
    </div>
  )
}
Enter fullscreen mode Exit fullscreen mode

See picture below, I made nine flex class for component positioning from top-left to bottom-right. It doesn't have to be all nine, it depend on your app.
Alt Text

/* Template_Example: css */
.f1 { display: flex; justify-content: flex-start; align-items: flex-start; }
.f2 { display: flex; justify-content: center;     align-items: flex-start; }
.f3 { display: flex; justify-content: flex-end;   align-items: flex-start; }
.f4 { display: flex; justify-content: flex-start; align-items: center;     }
.f5 { display: flex; justify-content: center;     align-items: center;     }
.f6 { display: flex; justify-content: flex-end;   align-items: center;     }
.f7 { display: flex; justify-content: flex-start; align-items: flex-end;   }
.f8 { display: flex; justify-content: center;     align-items: flex-end;   }
.f9 { display: flex; justify-content: flex-end;   align-items: flex-end;   }
Enter fullscreen mode Exit fullscreen mode

Here's an example in CodePen.

That's it. Feel free to leave a comment below.

Edit:
Actually There's a better solution for better readability (minimize div usage) using Grid only and place-self property. Check this [article]

on place-self section. By using place-self, flexbox for positioning won't be necessary.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay