-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify DwellerCard and TreeCard components
- Loading branch information
Showing
4 changed files
with
137 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as _ from "lodash-es"; | ||
import React, { ReactNode } from "react"; | ||
|
||
import { Card, CardContent } from "@mui/joy"; | ||
import { SxProps } from "@mui/joy/styles/types"; | ||
|
||
import { DwellerCard, TreeCard } from "@/game/types"; | ||
import { getBackgroundForCardTypes } from "@/styles/colors"; | ||
import { CARD_SIZES } from "@/styles/sizes"; | ||
import { hasHorizontalSplit, hasVerticalSplit } from "@/utils/cards"; | ||
import { mergeSx } from "@/utils/sx"; | ||
|
||
export type AttachPosition = "top" | "bottom" | "left" | "right"; | ||
|
||
interface ForestCardProps { | ||
attached?: AttachPosition; | ||
card: TreeCard | DwellerCard; | ||
children: ReactNode; | ||
compact?: boolean; | ||
onClick?: () => void; | ||
sx?: SxProps; | ||
} | ||
|
||
const getAttachedStyles = (attached?: AttachPosition) => { | ||
if (!attached) { | ||
return {}; | ||
} | ||
|
||
const cssAttached = _.upperFirst(attached); | ||
return { | ||
[`padding${cssAttached}`]: "calc(var(--Card-padding) + var(--Card-radius))", | ||
[`margin${cssAttached}`]: "calc(-1 * var(--Card-radius))", | ||
...(["top", "bottom"].includes(attached) && { | ||
[`border${cssAttached}LeftRadius`]: 0, | ||
[`border${cssAttached}RightRadius`]: 0, | ||
}), | ||
...(["left", "right"].includes(attached) && { | ||
[`borderTop${cssAttached}Radius`]: 0, | ||
[`borderBottom${cssAttached}Radius`]: 0, | ||
}), | ||
}; | ||
}; | ||
|
||
const ForestCard: React.FC<ForestCardProps> = ({ | ||
attached, | ||
card, | ||
children, | ||
compact, | ||
onClick, | ||
sx, | ||
}) => ( | ||
<Card | ||
variant="plain" | ||
onClick={onClick} | ||
sx={mergeSx(sx, getAttachedStyles(attached), { | ||
boxShadow: "card", | ||
background: getBackgroundForCardTypes( | ||
card.types, | ||
hasHorizontalSplit(card) ? "horizontal" : "vertical", | ||
), | ||
width: !compact || hasHorizontalSplit(card) ? CARD_SIZES.width : "auto", | ||
height: !compact || hasVerticalSplit(card) ? CARD_SIZES.height : "auto", | ||
})} | ||
> | ||
<CardContent>{children}</CardContent> | ||
</Card> | ||
); | ||
|
||
export default ForestCard; |
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { DwellerPosition } from "@/game"; | ||
import { DwellerCard, DwellerPosition, TreeCard } from "@/game"; | ||
|
||
export const isPositionX = (position: DwellerPosition) => | ||
[DwellerPosition.Left, DwellerPosition.Right].includes(position); | ||
|
||
export const isPositionY = (position: DwellerPosition) => | ||
[DwellerPosition.Top, DwellerPosition.Bottom].includes(position); | ||
|
||
export const hasHorizontalSplit = (card: DwellerCard | TreeCard) => | ||
"position" in card && isPositionY(card.position); | ||
|
||
export const hasVerticalSplit = (card: DwellerCard | TreeCard) => | ||
"position" in card && isPositionX(card.position); |