Skip to content

Commit

Permalink
[docs][CardActionArea] Added demo in docs of cards for adding props t…
Browse files Browse the repository at this point in the history
…o CardActionArea (#44789)

Signed-off-by: siddhantantil39 <[email protected]>
Co-authored-by: siriwatknp <[email protected]>
Co-authored-by: Sycamore <[email protected]>
Co-authored-by: ZeeshanTamboli <[email protected]>
  • Loading branch information
4 people authored Dec 24, 2024
1 parent 04ac13a commit 3ae7a04
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docs/data/material/components/cards/SelectActionCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import CardActionArea from '@mui/material/CardActionArea';

const cards = [
{
id: 1,
title: 'Plants',
description: 'Plants are essential for all life.',
},
{
id: 2,
title: 'Animals',
description: 'Animals are a part of nature.',
},
{
id: 3,
title: 'Humans',
description: 'Humans depend on plants and animals for survival.',
},
];

function SelectActionCard() {
const [selectedCard, setSelectedCard] = React.useState(0);
return (
<Box
sx={{
width: '100%',
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(min(200px, 100%), 1fr))',
gap: 2,
}}
>
{cards.map((card, index) => (
<Card>
<CardActionArea
onClick={() => setSelectedCard(index)}
data-active={selectedCard === index ? '' : undefined}
sx={{
height: '100%',
'&[data-active]': {
backgroundColor: 'action.selected',
'&:hover': {
backgroundColor: 'action.selectedHover',
},
},
}}
>
<CardContent sx={{ height: '100%' }}>
<Typography variant="h5" component="div">
{card.title}
</Typography>
<Typography variant="body2" color="text.secondary">
{card.description}
</Typography>
</CardContent>
</CardActionArea>
</Card>
))}
</Box>
);
}

export default SelectActionCard;
67 changes: 67 additions & 0 deletions docs/data/material/components/cards/SelectActionCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import CardActionArea from '@mui/material/CardActionArea';

const cards = [
{
id: 1,
title: 'Plants',
description: 'Plants are essential for all life.',
},
{
id: 2,
title: 'Animals',
description: 'Animals are a part of nature.',
},
{
id: 3,
title: 'Humans',
description: 'Humans depend on plants and animals for survival.',
},
];

function SelectActionCard() {
const [selectedCard, setSelectedCard] = React.useState(0);
return (
<Box
sx={{
width: '100%',
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(min(200px, 100%), 1fr))',
gap: 2,
}}
>
{cards.map((card, index) => (
<Card>
<CardActionArea
onClick={() => setSelectedCard(index)}
data-active={selectedCard === index ? '' : undefined}
sx={{
height: '100%',
'&[data-active]': {
backgroundColor: 'action.selected',
'&:hover': {
backgroundColor: 'action.selectedHover',
},
},
}}
>
<CardContent sx={{ height: '100%' }}>
<Typography variant="h5" component="div">
{card.title}
</Typography>
<Typography variant="body2" color="text.secondary">
{card.description}
</Typography>
</CardContent>
</CardActionArea>
</Card>
))}
</Box>
);
}

export default SelectActionCard;
6 changes: 6 additions & 0 deletions docs/data/material/components/cards/cards.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ Here's an example of a media control card.

{{"demo": "MediaControlCard.js", "bg": true}}

## Active state styles

To customize a Card's styles when it's in an active state, you can attach a `data-active` attribute to the Card Action Area component and apply styles with the `&[data-active]` selector, as shown below:

{{"demo": "SelectActionCard.js", "bg": true}}

🎨 If you are looking for inspiration, you can check [MUI Treasury's customization examples](https://mui-treasury.com/?path=/docs/card-introduction--docs).

0 comments on commit 3ae7a04

Please sign in to comment.