Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/sections/Meshery/Meshery-integrations/Honeycomb/Honeycomb.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from "react";
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
import { HoneycombGrid } from "./Honeycomb.style";

// Hexagon (cell) size for each viewport band. Keeping the hexagons sized to
Expand All @@ -13,65 +13,60 @@ const hexSizeForWidth = (width) => {
return 150;
};

// useLayoutEffect is a no-op (with a warning) during Gatsby's server-side
// render, so fall back to useEffect there and only measure in the browser.
const useIsomorphicLayoutEffect =
typeof window !== "undefined" ? useLayoutEffect : useEffect;

const Honeycomb = (props) => {
const { items, renderItem } = props;
const [height, setHeight] = useState(0);
const [hexSize, setHexSize] = useState(150);
const gridRef = useRef(null);

const setHoneycombHeight = () => {
const layoutHoneycomb = () => {
const grid = gridRef.current;
// Total horizontal available space for hexagons (offsetWidth includes
// the ul horizontal padding, so subtract it to get the usable width).
const availableWidth = grid ? grid.offsetWidth : window.innerWidth;
let usableWidth = availableWidth;
if (grid && typeof window !== "undefined" && window.getComputedStyle) {
const computedStyle = window.getComputedStyle(grid);
const paddingLeft = parseFloat(computedStyle.paddingLeft) || 0;
const paddingRight = parseFloat(computedStyle.paddingRight) || 0;
usableWidth = availableWidth - paddingLeft - paddingRight;
}
if (!grid) return;

const availableWidth = grid.offsetWidth || window.innerWidth;
const W = hexSizeForWidth(availableWidth);
// Horizontal pitch (hexagon width + left/right margins)
const P = W + 6;

// No. of hexagons that can be adjusted in first and second row
const firstRow = Math.max(1, Math.floor((usableWidth - (W / 2 + 25)) / P));
const secondRow = Math.max(1, Math.floor((usableWidth - (W + 28)) / P));
if (!grid.children.length) {
setHexSize(W);
setHeight(0);
return;
}

// Vertical pitch per row: hexagon height + margin-top + margin-bottom
const rowHeight = Math.round(W * 0.8662 + 6);
// Height of a first+second row pair / of a single leftover row
const pairHeight = 2 * rowHeight;
const singleHeight = rowHeight - 6;
// Make sure the hexagons are sized for this viewport, then temporarily
// grow the grid tall enough for every hexagon to wrap into place (the
// shape-outside float pattern repeats indefinitely, so any sufficiently
// tall value lays the rows out identically). Measuring the real content
// afterwards and shrinking the grid to fit avoids leaving blank space
// below the mesh whenever the row count is hard to predict analytically.
grid.style.setProperty("--hex-size", `${W}px`);
const upperBoundHeight = items.length * (W * 1.3) + W;
grid.style.height = `${upperBoundHeight}px`;

// No. of first-second row pairs possible
const pairsCount = Math.floor(items.length / (firstRow + secondRow));
const gridTop = grid.getBoundingClientRect().top;
let maxBottom = 0;
Array.from(grid.children).forEach((li) => {
const bottom = li.getBoundingClientRect().bottom - gridTop;
if (bottom > maxBottom) maxBottom = bottom;
});

let newHeight;
if (pairsCount * (firstRow + secondRow) < items.length) {
// Calculate left off hexagons
const left = items.length - pairsCount * (firstRow + secondRow);
newHeight =
left <= firstRow
? pairsCount * pairHeight + singleHeight
: pairsCount * pairHeight + pairHeight;
} else {
// All hexagons are covered in n pairs of 2 rows (first row with a
// hexagon and second row with b hexagons).
newHeight = pairsCount * pairHeight;
}

if (!isNaN(newHeight)) setHeight(newHeight);
grid.style.height = "";
setHexSize(W);
setHeight(Math.ceil(maxBottom));
};

useIsomorphicLayoutEffect(() => {
layoutHoneycomb();
}, [items]);

useEffect(() => {
window.addEventListener("resize", setHoneycombHeight);
setHoneycombHeight();
window.addEventListener("resize", layoutHoneycomb);
return () => {
window.removeEventListener("resize", setHoneycombHeight);
window.removeEventListener("resize", layoutHoneycomb);
};
}, [items]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export const HoneycombGrid = styled.div`

display: flex;
text-align: left;
margin: 0;

ul {
font-size: 0;
line-height: 0;
height: ${(props) => (props.$height === 0 ? "unset" : `calc(${props.$height}px + var(--hex-size, 150px) * 0.31)`)};
height: ${(props) => (props.$height === 0 ? "unset" : `${props.$height}px`)};

&:before {
content: "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const HoneycombGrid = styled.div`
overflow: hidden;
.title {
line-height: 1.375rem;
color: ${(props) => props.theme.white};
color: ${(props) => props.theme.whiteToBlack};
// margin-bottom: -2.7rem;
font-size: 1.2rem;
opacity: 0;
Expand Down
Loading