-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…nry-layout-in-bea [CARE-5671] Feature - Add support for masonry layout
- Loading branch information
Showing
13 changed files
with
185 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.container { | ||
display: grid; | ||
grid-template-columns: 1fr 1fr; | ||
column-gap: $grid-gutter-tablet; | ||
|
||
&.desktop { | ||
grid-template-columns: 1fr 1fr 1fr; | ||
column-gap: $grid-gutter-desktop; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client'; | ||
|
||
import classNames from 'classnames'; | ||
import { Children, useMemo, useRef } from 'react'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import { useDevice } from '@/hooks'; | ||
|
||
import styles from './StaggeredLayout.module.scss'; | ||
|
||
export function StaggeredLayout({ children }: PropsWithChildren<{}>) { | ||
const isLayoutInitializedRef = useRef(false); | ||
const { isMobile, isTablet } = useDevice(); | ||
|
||
const columnCount = useMemo(() => { | ||
if (isMobile) { | ||
isLayoutInitializedRef.current = false; | ||
return 0; | ||
} | ||
|
||
// Only use the calculated layout if we're not on mobile screen | ||
isLayoutInitializedRef.current = true; | ||
|
||
if (isTablet) { | ||
return 2; | ||
} | ||
|
||
return 3; | ||
}, [isMobile, isTablet]); | ||
|
||
const childrenInColumns = useMemo(() => { | ||
const itemsCols = new Array(columnCount); | ||
const items = Children.toArray(children); | ||
|
||
items.forEach((item, i) => { | ||
const columnIndex = i % columnCount; | ||
|
||
if (!itemsCols[columnIndex]) { | ||
itemsCols[columnIndex] = []; | ||
} | ||
|
||
itemsCols[columnIndex].push(item); | ||
}); | ||
|
||
return itemsCols; | ||
}, [children, columnCount]); | ||
|
||
if (!isLayoutInitializedRef.current) { | ||
return <div>{children}</div>; | ||
} | ||
|
||
return ( | ||
<div | ||
className={classNames(styles.container, { | ||
[styles.desktop]: columnCount === 3, | ||
})} | ||
> | ||
{childrenInColumns.map((columnItems, i) => ( | ||
<div key={`column-${i}`}>{columnItems}</div> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { StaggeredLayout } from './StaggeredLayout'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.