1- import React , { useEffect , useRef , useState } from "react" ;
1+ import React , { useEffect , useLayoutEffect , useRef , useState } from "react" ;
22import { HoneycombGrid } from "./Honeycomb.style" ;
33
44// Hexagon (cell) size for each viewport band. Keeping the hexagons sized to
@@ -13,65 +13,75 @@ const hexSizeForWidth = (width) => {
1313 return 150 ;
1414} ;
1515
16+ // useLayoutEffect is a no-op (with a warning) during Gatsby's server-side
17+ // render, so fall back to useEffect there and only measure in the browser.
18+ const useIsomorphicLayoutEffect =
19+ typeof window !== "undefined" ? useLayoutEffect : useEffect ;
20+
21+ // The resize event fires continuously while the user drags a window border
22+ // or resizes on mobile. layoutHoneycomb measures every child via
23+ // getBoundingClientRect, so running it on every tick is expensive. Debounce
24+ // to a trailing call so layout only recomputes once resizing settles.
25+ const debounce = ( fn , delay ) => {
26+ let timer = null ;
27+ return ( ...args ) => {
28+ clearTimeout ( timer ) ;
29+ timer = setTimeout ( ( ) => fn ( ...args ) , delay ) ;
30+ } ;
31+ } ;
32+
1633const Honeycomb = ( props ) => {
1734 const { items, renderItem } = props ;
1835 const [ height , setHeight ] = useState ( 0 ) ;
1936 const [ hexSize , setHexSize ] = useState ( 150 ) ;
2037 const gridRef = useRef ( null ) ;
2138
22- const setHoneycombHeight = ( ) => {
39+ const layoutHoneycomb = ( ) => {
2340 const grid = gridRef . current ;
24- // Total horizontal available space for hexagons (offsetWidth includes
25- // the ul horizontal padding, so subtract it to get the usable width).
26- const availableWidth = grid ? grid . offsetWidth : window . innerWidth ;
27- let usableWidth = availableWidth ;
28- if ( grid && typeof window !== "undefined" && window . getComputedStyle ) {
29- const computedStyle = window . getComputedStyle ( grid ) ;
30- const paddingLeft = parseFloat ( computedStyle . paddingLeft ) || 0 ;
31- const paddingRight = parseFloat ( computedStyle . paddingRight ) || 0 ;
32- usableWidth = availableWidth - paddingLeft - paddingRight ;
33- }
41+ if ( ! grid ) return ;
3442
43+ // The ul width depends on the current hex size. Use the viewport width
44+ // that the breakpoints are defined for.
45+ const availableWidth = window . innerWidth ;
3546 const W = hexSizeForWidth ( availableWidth ) ;
36- // Horizontal pitch (hexagon width + left/right margins)
37- const P = W + 6 ;
3847
39- // No. of hexagons that can be adjusted in first and second row
40- const firstRow = Math . max ( 1 , Math . floor ( ( usableWidth - ( W / 2 + 25 ) ) / P ) ) ;
41- const secondRow = Math . max ( 1 , Math . floor ( ( usableWidth - ( W + 28 ) ) / P ) ) ;
42-
43- // Vertical pitch per row: hexagon height + margin-top + margin-bottom
44- const rowHeight = Math . round ( W * 0.8662 + 6 ) ;
45- // Height of a first+second row pair / of a single leftover row
46- const pairHeight = 2 * rowHeight ;
47- const singleHeight = rowHeight - 6 ;
48+ if ( ! grid . children . length ) {
49+ setHexSize ( W ) ;
50+ setHeight ( 0 ) ;
51+ return ;
52+ }
4853
49- // No. of first-second row pairs possible
50- const pairsCount = Math . floor ( items . length / ( firstRow + secondRow ) ) ;
54+ // Make sure the hexagons are sized for this viewport, then temporarily
55+ // grow the grid tall enough for every hexagon to wrap into place (the
56+ // shape-outside float pattern repeats indefinitely, so any sufficiently
57+ // tall value lays the rows out identically). Measuring the real content
58+ // afterwards and shrinking the grid to fit avoids leaving blank space
59+ // below the mesh whenever the row count is hard to predict analytically.
60+ grid . style . setProperty ( "--hex-size" , `${ W } px` ) ;
61+ const upperBoundHeight = items . length * ( W * 1.3 ) + W ;
62+ grid . style . height = `${ upperBoundHeight } px` ;
5163
52- let newHeight ;
53- if ( pairsCount * ( firstRow + secondRow ) < items . length ) {
54- // Calculate left off hexagons
55- const left = items . length - pairsCount * ( firstRow + secondRow ) ;
56- newHeight =
57- left <= firstRow
58- ? pairsCount * pairHeight + singleHeight
59- : pairsCount * pairHeight + pairHeight ;
60- } else {
61- // All hexagons are covered in n pairs of 2 rows (first row with a
62- // hexagon and second row with b hexagons).
63- newHeight = pairsCount * pairHeight ;
64- }
64+ const gridTop = grid . getBoundingClientRect ( ) . top ;
65+ let maxBottom = 0 ;
66+ Array . from ( grid . children ) . forEach ( ( li ) => {
67+ const bottom = li . getBoundingClientRect ( ) . bottom - gridTop ;
68+ if ( bottom > maxBottom ) maxBottom = bottom ;
69+ } ) ;
6570
66- if ( ! isNaN ( newHeight ) ) setHeight ( newHeight ) ;
71+ grid . style . height = "" ;
6772 setHexSize ( W ) ;
73+ setHeight ( Math . ceil ( maxBottom ) ) ;
6874 } ;
6975
76+ useIsomorphicLayoutEffect ( ( ) => {
77+ layoutHoneycomb ( ) ;
78+ } , [ items ] ) ;
79+
7080 useEffect ( ( ) => {
71- window . addEventListener ( "resize" , setHoneycombHeight ) ;
72- setHoneycombHeight ( ) ;
81+ const debouncedLayout = debounce ( layoutHoneycomb , 150 ) ;
82+ window . addEventListener ( "resize" , debouncedLayout ) ;
7383 return ( ) => {
74- window . removeEventListener ( "resize" , setHoneycombHeight ) ;
84+ window . removeEventListener ( "resize" , debouncedLayout ) ;
7585 } ;
7686 } , [ items ] ) ;
7787
0 commit comments