Skip to content

Commit

Permalink
feat: introduce custom timeline ends
Browse files Browse the repository at this point in the history
  • Loading branch information
stampaaaron committed Jun 16, 2023
1 parent 4310e66 commit 33c1bfa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/components/Timeline.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@
display: flex;
}

.timeline__line {
.timeline-line {
background-color: var(--line-color);
min-width: var(--line-width);
position: relative;
}

.timeline-line__end {
position: absolute;
left: 50%;
}

.timeline-line__end--opening {
transform: translate(-50%, -100%);
top: 0;
}

.timeline-line__end--closing {
transform: translate(-50%, 100%);
bottom: 0;
}

.timeline__items-container {
Expand Down
13 changes: 11 additions & 2 deletions src/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Key, ReactElement, useEffect, useRef } from 'react';
import { TimelineItem, TimelineItemRefs, TimelineItemsProps } from './TimelineItem';
import { Positioning } from '../models/positioning';
import { convertToCssVariable, StyleConfig } from '../models/style';
import { isElement } from '../helper/element';

export type TimelineProps = {
items: TimelineItemsProps;
Expand All @@ -11,6 +12,7 @@ export type TimelineProps = {
formatDate?: (date: Date) => string;
customMarker?: ReactElement;
customPointer?: ReactElement;
customTimelineEnds?: ReactElement | { opening?: ReactElement; closing?: ReactElement };
styleConfig?: StyleConfig;
className?: string;
};
Expand All @@ -21,7 +23,7 @@ export const defaultTimelineConfig: Partial<TimelineProps> = {
};

export function Timeline(props: TimelineProps) {
const { items, positioning, minMarkerGap, className, customMarker, customPointer, styleConfig, formatDate } = {
const { items, positioning, minMarkerGap, className, customMarker, customPointer, customTimelineEnds, styleConfig, formatDate } = {
...defaultTimelineConfig,
...props,
};
Expand Down Expand Up @@ -121,7 +123,14 @@ export function Timeline(props: TimelineProps) {
))}
</div>

<div className="timeline__line" />
<div className="timeline-line">
<div className="timeline-line__end timeline-line__end--opening">
{isElement(customTimelineEnds) ? customTimelineEnds : customTimelineEnds?.opening}
</div>
<div className="timeline-line__end timeline-line__end--closing">
{isElement(customTimelineEnds) ? customTimelineEnds : customTimelineEnds?.closing}
</div>
</div>

<div ref={rightContainer} className="timeline__items-container timeline__items-container--right" />
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/helper/element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ReactElement, isValidElement } from 'react';

export const isElement = (element: any): element is ReactElement => isValidElement(element);

0 comments on commit 33c1bfa

Please sign in to comment.