diff --git a/src/index.tsx b/src/index.tsx index deef5da..7e39139 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -24,6 +24,10 @@ interface SnuggleProps { > } +export const SnuggleChild: React.FC<{ span?: number }> = ({ children }) => ( + <>{children} +) + /** * Helpers */ @@ -69,13 +73,18 @@ const Snuggle: React.FC = ({ // Methods const settle = useCallback(() => { - elementsRef.current?.forEach((item: HTMLElement) => { + const childrenArr = Array.isArray(children) ? children : [children] + + elementsRef.current?.forEach((item, index) => { + const { span } = childrenArr?.[index]?.props + if (item && item.firstElementChild) { const firstElement = item.firstElementChild const itemHeight = firstElement.getBoundingClientRect().height const rowSpan = Math.ceil((itemHeight + rowGap) / rowGap) item.style.gridRowEnd = `span ${rowSpan}` + item.style.gridColumn = `span ${span}` } }) @@ -83,7 +92,7 @@ const Snuggle: React.FC = ({ window.requestAnimationFrame(settle) reposition.current = true } - }, [rowGap]) + }, [children, rowGap]) // Effects useEffect(() => { diff --git a/stories/index.stories.tsx b/stories/index.stories.tsx index 0a0922c..12b58c5 100644 --- a/stories/index.stories.tsx +++ b/stories/index.stories.tsx @@ -2,7 +2,7 @@ import React, { useRef, createElement } from 'react' import { storiesOf } from '@storybook/react' import './style.css' -import Snuggle from '../src' +import Snuggle, { SnuggleChild } from '../src' import { ItemStyled, listElements, @@ -14,21 +14,26 @@ storiesOf('Snuggle', module) .add('default', () => (
- Placeholder + + Placeholder + + + + Placeholder + Placeholder - Placeholder