-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-board): add card animation in panel
- Loading branch information
Showing
6 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
packages/game-app/src/_shared/components/AnimatedGrid/AnimatedGrid.tsx
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,78 @@ | ||
import React, { Children, useCallback } from 'react'; | ||
import { calculateCoordinates, calculateItemPosition, calculateItemsPerRow } from './animatedGridUtils'; | ||
|
||
type Props = { | ||
containerWidth: number; | ||
itemWidth: number; | ||
itemHeight: number; | ||
margin: number; | ||
marginVertical: number; | ||
childClassName?: string; | ||
className?: string; | ||
transitionTime?: string; | ||
transitionTimingFunction?: string; | ||
children: React.ReactChild[]; | ||
}; | ||
|
||
const AnimatedGrid: React.FC<Props> = ({ | ||
margin, | ||
children, | ||
childClassName, | ||
className, | ||
containerWidth, | ||
itemHeight, | ||
itemWidth, | ||
marginVertical, | ||
transitionTime = '400ms', | ||
transitionTimingFunction = 'ease-out', | ||
}) => { | ||
const getChildStyles = useCallback( | ||
(top: number, left: number) => { | ||
return { | ||
position: 'absolute' as const, | ||
top: `${top}px`, | ||
left: `${left}px`, | ||
transition: `${transitionTime} top ${transitionTimingFunction}, ${transitionTime} left ${transitionTimingFunction}`, | ||
}; | ||
}, | ||
[transitionTime, transitionTimingFunction], | ||
); | ||
|
||
const renderChild = useCallback( | ||
(child: React.ReactElement, top: number = 0, left: number = 0) => { | ||
return ( | ||
<div className={childClassName} key={child.key} style={getChildStyles(top, left)}> | ||
{child} | ||
</div> | ||
); | ||
}, | ||
[childClassName, getChildStyles], | ||
); | ||
|
||
const parseChildren = useCallback(() => { | ||
const elementsPerRow = calculateItemsPerRow(itemWidth, margin, containerWidth); | ||
const newMargin = margin; | ||
return Children.map(children, (child, index) => { | ||
const { row, col } = calculateItemPosition(index, elementsPerRow); | ||
const { top, left } = calculateCoordinates(row, col, itemWidth, itemHeight, newMargin, marginVertical); | ||
return renderChild(child as React.ReactElement, top, left); | ||
}); | ||
}, [children, containerWidth, itemHeight, itemWidth, margin, marginVertical, renderChild]); | ||
|
||
return ( | ||
<div | ||
className={className} | ||
style={{ | ||
position: 'relative', | ||
width: '100%', | ||
height: '100%', | ||
}} | ||
> | ||
{parseChildren()} | ||
</div> | ||
); | ||
}; | ||
|
||
AnimatedGrid.displayName = 'AnimatedGrid'; | ||
|
||
export default React.memo(AnimatedGrid); |
36 changes: 36 additions & 0 deletions
36
packages/game-app/src/_shared/components/AnimatedGrid/animatedGridUtils.ts
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,36 @@ | ||
/** | ||
* | ||
* | ||
* | containerWidth | | ||
* _______________________________________________________ | ||
* | itemWidth | margin | itemWidth | margin | itemWidth | | ||
* | ||
* Calculates the max number of columns | ||
* | ||
* @param itemWidth the width of the child | ||
* @param margin the margin between the columns | ||
* @param containerWidth the width of the container | ||
*/ | ||
export function calculateItemsPerRow(itemWidth: number, margin: number, containerWidth: number) { | ||
return Math.floor((containerWidth - margin) / itemWidth); | ||
} | ||
|
||
export function calculateItemPosition(elementIndex: number, itemsPerRow: number) { | ||
const row = Math.floor(elementIndex / itemsPerRow); | ||
return { row, col: elementIndex - row * itemsPerRow }; | ||
} | ||
|
||
export function calculateCoordinates( | ||
rowIndex: number, | ||
colIndex: number, | ||
itemWidth: number, | ||
itemHeight: number, | ||
margin: number, | ||
verticalMargin?: number, | ||
) { | ||
const vMargin = verticalMargin === undefined ? margin : verticalMargin; | ||
return { | ||
left: (itemWidth + margin) * colIndex, | ||
top: (itemHeight + vMargin) * rowIndex, | ||
}; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/game-app/src/_shared/components/AnimatedGrid/index.ts
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,3 @@ | ||
import AnimatedGrid from './AnimatedGrid'; | ||
|
||
export default AnimatedGrid; |
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