Creating a TimelineItem component using MUI (Material-UI) requires combining several of MUI's components and custom styling to achieve the desired functionality. I will provide a simplified example that meets the specifications you've provided. Note that this example will use MUI version 5, as it's one of the most commonly used versions as of my last update.
First, make sure you have MUI installed in your project. If not, you can add it with:
npm install @mui/material @emotion/react @emotion/styled
Here is an implementation of the TimelineItem component:
import React, { useState } from 'react';
import { Box, Typography, Tooltip, IconButton, Collapse } from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
function TimelineItem({ title, timestamp, isFirst = false, isLast = false, color = '#d9d9d9', children }) {
const [expanded, setExpanded] = useState(false);
const handleExpandClick = () => {
setExpanded(!expanded);
};
return (
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', width: '100%', p: 1, '&:hover': { backgroundColor: '#f0f0f0' } }}>
{/* Timeline line and dot */}
<Box sx={{ position: 'relative', display: 'flex', alignItems: 'center' }}>
{!isFirst && <Box sx={{ position: 'absolute', top: '-20px', bottom: '50%', width: '2px', backgroundColor: '#d9d9d9' }} />}
<Box sx={{ backgroundColor: color, width: '16px', height: '16px', borderRadius: '50%' }} />
{!isLast && <Box sx={{ position: 'absolute', top: '50%', bottom: '-20px', width: '2px', backgroundColor: '#d9d9d9' }} />}
</Box>
{/* Content section */}
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'start', ml: 2, width: '100%' }}>
{/* Title and timestamp */}
<Box sx={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}>
<Typography variant="body1">{title}</Typography>
{timestamp && (
<Tooltip title={new Date(timestamp).toLocaleString()}>
<Typography variant="caption" sx={{ ml: 1 }}>
{new Date(timestamp).toLocaleTimeString()}
</Typography>
</Tooltip>
)}
</Box>
{/* Expand/Collapse icon */}
{children && (
<IconButton onClick={handleExpandClick} aria-expanded={expanded} aria-label="show more" size="small" sx={{ transform: expanded ? 'rotate(180deg)' : 'rotate(0deg)', transition: 'transform 0.2s' }}>
<ExpandMoreIcon />
</IconButton>
)}
{/* Collapsible content */}
<Collapse in={expanded} timeout="auto" unmountOnExit>
{children}
</Collapse>
</Box>
</Box>
);
}
export default TimelineItem;
This component takes in the specified props, managing an expand/collapse state for child content. It displays the timeline dot and optional lines above and below depending on the isFirst and isLast props. The color of the dot is customizable via the color prop. If children are provided, an expand/collapse icon is displayed and controls the visibility of the children content.
Usage examples:
You provided the sample usage already in your question. Just make sure to import and use this component in a React application where MUI is properly set up.
Please note, this example might need adjustments depending on specific project configurations and versions of React and MUI being used.