This repository provides a flexible and draggable bento grid template, ideal for creating dynamic layouts. It’s currently used on my portfolio site. Feel free to use and modify this project, just credit me.
demo.mp4
The grid functionality is powered by react-grid-layout, a powerful library for building responsive grids in React. This template adds smooth transitions and animations to the layout changes using CSS.
The react-grid-layout library manages the grid’s core behavior, including item placement and resizing. By integrating React state management, the layout configuration changes dynamically based on user actions, such as clicking navigation buttons.
CSS animations are applied to .react-grid-item elements, enabling smooth transitions during layout changes. The key here is CSS properties like transform, transition, and will-change.
Below are some critical CSS styles used to create the smooth appearance and functionality of the grid, found in index.css
/* Style when dragging */
.react-grid-item.react-grid-placeholder {
background: rgba(211, 211, 211, 0.99); /* Light gray placeholder color */
transition-duration: 100ms; /* Short transition for quick feedback */
z-index: 2;
user-select: none; /* Disable text selection */
border-radius: 25px;
transition: all 400ms ease 0s !important; /* Smooth animation */
will-change: transform;
}
/* Grid item style */
.react-grid-item {
transition: 400ms ease 0s; /* Smooth transitions between layouts */
}React manages the grid’s layout configuration by dynamically updating the props passed to react-grid-layout. Here’s a basic flow of how the grid adjusts between the predefined layouts:
- Initial Layout: Define the starting grid configuration.
- User Interaction: Capture interactions (e.g., navigation clicks).
- State Updates: Update the grid layout stored in React state.
- Render: Pass the new layout to
react-grid-layoutfor rendering.
To create grid layouts for various states/views, see this example (utils/layout.helper.ts):
const layout1 = [
{ i: 'item1', x: 0, y: 0, w: 2, h: 2 },
{ i: 'item2', x: 2, y: 0, w: 2, h: 2 },
// Add more items as needed
];
const layout2 = [
{ i: 'item1', x: 0, y: 0, w: 3, h: 1 },
{ i: 'item2', x: 3, y: 0, w: 1, h: 2 },
// Add more items as needed
];-
Clone or fork the repo:
git clone https://github.com/jsz-05/bento-grid.git -
Install
node_modulesby runningnpm i -
Run the Vite app
npm run dev
- CSS Imports: Failing to import the required styles from
react-grid-layoutcan break the grid rendering. - Manual Configuration: In this project, grid layouts are manually configured for each view. You might want to explore automating this process to reduce redundancy, since complex layouts can be a pain to edit in
layout.helper.ts. For my website, I rigged a jank setup to grab the positions of the tiles from the console debugger once I had them in a layout I liked. - Performance: Adding too many animations or using large grid layouts can impact performance. Use CSS properties like
will-changeto optimize rendering.
This project is licensed under the MIT License.