Skip to content

Commit

Permalink
fix empty columns flash (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
taep96 committed May 22, 2023
1 parent 35bb0f1 commit 261c639
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-elephants-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-auto-columns": minor
---

Fix empty columns rendering in SSR, which caused a flash of empty space before the columns recalculated with window width.
33 changes: 21 additions & 12 deletions packages/react-auto-columns/src/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import React, { Children, useEffect, useState } from "react";

function calculateColumnCount(
columns: ColumnsProps["columns"],
windowWidth: number
windowWidth?: number
) {
// If columns is an object, find the largest breakpoint that is smaller than the window width
if (windowWidth && typeof columns === "object") {
// Filter out breakpoints that are larger than the window width and sort them from largest to smallest
const breakpoints = Object.keys(columns)
.map(Number)
.filter((key) => key <= windowWidth)
.sort((a, b) => b - a);

// If no breakpoints match, return 1 as the smallest column count
return (typeof breakpoints[0] === "number" && columns[breakpoints[0]]) || 1;
if (typeof columns === "object") {
if (windowWidth) {
// Filter out breakpoints that are larger than the window width and sort them from largest to smallest
const breakpoints = Object.keys(columns)
.map(Number)
.filter((key) => key <= windowWidth)
.sort((a, b) => b - a);

// If no breakpoints match, return 1 as the smallest column count
return (
(typeof breakpoints[0] === "number" && columns[breakpoints[0]]) || 1
);
}

// If no window width is provided (e.g. in SSR), return the biggest column count
return Math.max(...Object.values(columns).map(Number));
}

// If columns is a number, return it as is
Expand Down Expand Up @@ -79,7 +86,7 @@ export default function Columns({
columnClassName = "",
...Props
}: ColumnsProps) {
const [columnCount, setColumnCount] = useState(3);
const [columnCount, setColumnCount] = useState(calculateColumnCount(columns));
const [columnArray, setColumnArray] = useState(() =>
getColumnArray(children, columnCount)
);
Expand All @@ -106,7 +113,9 @@ export default function Columns({
<div {...Props}>
{columnArray.map((column, index) => (
// Using index as key here is probably okay since it recalculates columnArray on every render
<div className={columnClassName} key={index}>{column}</div>
<div className={columnClassName} key={index}>
{column}
</div>
))}
</div>
);
Expand Down

1 comment on commit 261c639

@vercel
Copy link

@vercel vercel bot commented on 261c639 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-components – ./

react-components-git-main-taep96.vercel.app
react-components-taep96.vercel.app
react-components.taep96.moe

Please sign in to comment.