Skip to content

Commit

Permalink
[Card] Fix CardOverflow in nested cards (#39668)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp authored Oct 31, 2023
1 parent 191c3d8 commit e74a4f5
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 26 deletions.
63 changes: 63 additions & 0 deletions docs/data/joy/components/card/NestedCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as React from 'react';
import Card from '@mui/joy/Card';
import CardContent from '@mui/joy/CardContent';
import Divider from '@mui/joy/Divider';
import CardOverflow from '@mui/joy/CardOverflow';
import Typography from '@mui/joy/Typography';
import BallotIcon from '@mui/icons-material/Ballot';
import CommentOutlinedIcon from '@mui/icons-material/CommentOutlined';
import InboxOutlinedIcon from '@mui/icons-material/InboxOutlined';
import { AspectRatio } from '@mui/joy';

export default function NestedCard() {
return (
<Card sx={{ borderRadius: 0, width: 300, maxWidth: '100%' }}>
<CardContent>
<Typography level="body-xs">IN DESIGN</Typography>
<Typography level="title-lg">AFSL Web App</Typography>
</CardContent>
<Card
orientation="horizontal"
size="sm"
sx={{ bgcolor: 'background.surface', borderRadius: 0, mb: 1 }}
>
<CardOverflow>
<AspectRatio
ratio="1"
sx={{ minWidth: 70, '& img[data-first-child]': { p: 1.5 } }}
>
<img src="https://uilogos.co/img/logomark/lighting.png" alt="" />
</AspectRatio>
</CardOverflow>
<CardContent>
<Typography level="title-md">Sub project</Typography>
<Typography level="body-sm">Next review in 17 days</Typography>
</CardContent>
</Card>
<CardOverflow
variant="soft"
sx={{
display: 'flex',
flexDirection: 'row',
gap: 1,
justifyContent: 'space-around',
py: 1,
borderTop: '1px solid',
borderColor: 'divider',
}}
>
<Typography startDecorator={<BallotIcon color="danger" />} level="title-sm">
13
</Typography>
<Divider orientation="vertical" />
<Typography startDecorator={<CommentOutlinedIcon />} level="title-sm">
9
</Typography>
<Divider orientation="vertical" />
<Typography startDecorator={<InboxOutlinedIcon />} level="title-sm">
32
</Typography>
</CardOverflow>
</Card>
);
}
63 changes: 63 additions & 0 deletions docs/data/joy/components/card/NestedCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as React from 'react';
import Card from '@mui/joy/Card';
import CardContent from '@mui/joy/CardContent';
import Divider from '@mui/joy/Divider';
import CardOverflow from '@mui/joy/CardOverflow';
import Typography from '@mui/joy/Typography';
import BallotIcon from '@mui/icons-material/Ballot';
import CommentOutlinedIcon from '@mui/icons-material/CommentOutlined';
import InboxOutlinedIcon from '@mui/icons-material/InboxOutlined';
import { AspectRatio } from '@mui/joy';

export default function NestedCard() {
return (
<Card sx={{ borderRadius: 0, width: 300, maxWidth: '100%' }}>
<CardContent>
<Typography level="body-xs">IN DESIGN</Typography>
<Typography level="title-lg">AFSL Web App</Typography>
</CardContent>
<Card
orientation="horizontal"
size="sm"
sx={{ bgcolor: 'background.surface', borderRadius: 0, mb: 1 }}
>
<CardOverflow>
<AspectRatio
ratio="1"
sx={{ minWidth: 70, '& img[data-first-child]': { p: 1.5 } }}
>
<img src="https://uilogos.co/img/logomark/lighting.png" alt="" />
</AspectRatio>
</CardOverflow>
<CardContent>
<Typography level="title-md">Sub project</Typography>
<Typography level="body-sm">Next review in 17 days</Typography>
</CardContent>
</Card>
<CardOverflow
variant="soft"
sx={{
display: 'flex',
flexDirection: 'row',
gap: 1,
justifyContent: 'space-around',
py: 1,
borderTop: '1px solid',
borderColor: 'divider',
}}
>
<Typography startDecorator={<BallotIcon color="danger" />} level="title-sm">
13
</Typography>
<Divider orientation="vertical" />
<Typography startDecorator={<CommentOutlinedIcon />} level="title-sm">
9
</Typography>
<Divider orientation="vertical" />
<Typography startDecorator={<InboxOutlinedIcon />} level="title-sm">
32
</Typography>
</CardOverflow>
</Card>
);
}
4 changes: 4 additions & 0 deletions docs/data/joy/components/card/card.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ This is because the variables are also used to calculate a proper radius for the

## Common examples

### Nested cards

{{"demo": "NestedCard.js"}}

### Bio card

{{"demo": "BioCard.js"}}
Expand Down
57 changes: 31 additions & 26 deletions packages/mui-joy/src/CardOverflow/CardOverflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,39 @@ const CardOverflowRoot = styled('div', {
alignSelf: 'stretch', // prevent shrinking if parent's align-items is not initial
position: 'relative',
display: 'flex',
flexDirection: 'var(--_CardOverflow-flexDirection)' as React.CSSProperties['flexDirection'],
margin: 'var(--_CardOverflow-margin)',
borderRadius: 'var(--_CardOverflow-radius)',
padding: 'var(--_CardOverflow-padding)',
[`.${cardClasses.horizontal} &`]: {
[`.${cardClasses.vertical} &, .${cardClasses.horizontal} .${cardClasses.vertical} &, .${modalDialogClasses.root} &`]:
{
'--_CardOverflow-flexDirection': 'column', // required to make AspectRatio works
'--AspectRatio-margin': '0 calc(-1 * var(--Card-padding))',
'--_CardOverflow-margin': '0 var(--CardOverflow-offset)',
'--_CardOverflow-padding': '0 var(--Card-padding)',
'&[data-first-child]': {
'--AspectRatio-radius': `${childRadius} ${childRadius} 0 0`,
'--_CardOverflow-radius': 'var(--CardOverflow-radius) var(--CardOverflow-radius) 0 0',
'--_CardOverflow-margin': 'var(--CardOverflow-offset) var(--CardOverflow-offset) 0',
},
'&[data-last-child]': {
'--AspectRatio-radius': `0 0 ${childRadius} ${childRadius}`,
'--_CardOverflow-radius': '0 0 var(--CardOverflow-radius) var(--CardOverflow-radius)',
'--_CardOverflow-margin': '0 var(--CardOverflow-offset) var(--CardOverflow-offset)',
},
'&[data-last-child][data-first-child]': {
'--AspectRatio-radius': childRadius,
'--_CardOverflow-margin': 'var(--CardOverflow-offset)',
},
[`& > .${buttonClasses.root}:only-child`]: {
zIndex: 1, // prevent button from being covered Link overlay. This can be improved in the future with :has() selector
width: 'calc(100% + -2 * var(--CardOverflow-offset))',
'--Button-margin': '0 var(--CardOverflow-offset)',
'--Button-radius': '0 0 var(--CardOverflow-radius) var(--CardOverflow-radius)',
},
},
[`.${cardClasses.horizontal} &, .${cardClasses.vertical} .${cardClasses.horizontal} &`]: {
'--_CardOverflow-flexDirection': 'row',
'--AspectRatio-margin': 'calc(-1 * var(--Card-padding)) 0px',
'--_CardOverflow-margin': 'var(--CardOverflow-offset) 0px',
'--_CardOverflow-padding': 'var(--Card-padding) 0px',
Expand All @@ -68,38 +97,14 @@ const CardOverflowRoot = styled('div', {
},
'&[data-last-child][data-first-child]': {
'--AspectRatio-radius': childRadius,
'--_CardOverflow-margin': 'var(--CardOverflow-offset)',
},
[`& > .${buttonClasses.root}:only-child`]: {
height: 'calc(100% + -2 * var(--CardOverflow-offset))',
'--Button-margin': 'var(--CardOverflow-offset) 0',
'--Button-radius': '0 var(--CardOverflow-radius) var(--CardOverflow-radius) 0',
},
},
[`.${cardClasses.vertical} &, .${modalDialogClasses.root} &`]: {
flexDirection: 'column', // required to make AspectRatio works
'--AspectRatio-margin': '0 calc(-1 * var(--Card-padding))',
'--_CardOverflow-margin': '0 var(--CardOverflow-offset)',
'--_CardOverflow-padding': '0 var(--Card-padding)',
'&[data-first-child]': {
'--AspectRatio-radius': `${childRadius} ${childRadius} 0 0`,
'--_CardOverflow-radius': 'var(--CardOverflow-radius) var(--CardOverflow-radius) 0 0',
'--_CardOverflow-margin': 'var(--CardOverflow-offset) var(--CardOverflow-offset) 0',
},
'&[data-last-child]': {
'--AspectRatio-radius': `0 0 ${childRadius} ${childRadius}`,
'--_CardOverflow-radius': '0 0 var(--CardOverflow-radius) var(--CardOverflow-radius)',
'--_CardOverflow-margin': '0 var(--CardOverflow-offset) var(--CardOverflow-offset)',
},
'&[data-last-child][data-first-child]': {
'--AspectRatio-radius': childRadius,
},
[`& > .${buttonClasses.root}:only-child`]: {
zIndex: 1, // prevent button from being covered Link overlay. This can be improved in the future with :has() selector
width: 'calc(100% + -2 * var(--CardOverflow-offset))',
'--Button-margin': '0 var(--CardOverflow-offset)',
'--Button-radius': '0 0 var(--CardOverflow-radius) var(--CardOverflow-radius)',
},
},
...theme.variants[ownerState.variant!]?.[ownerState.color!],
};
});
Expand Down

0 comments on commit e74a4f5

Please sign in to comment.