Skip to content

Commit d291e3d

Browse files
committed
feat: make marker and pointer replaceable
1 parent 75d0fc3 commit d291e3d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/components/Timeline.css

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@
4444
}
4545

4646
.timeline-item__marker {
47+
margin-top: var(--marker-offset);
48+
}
49+
50+
.timeline-item__marker:not(.timeline-item__marker--custom) {
4751
border-radius: var(--marker-radius);
4852
width: var(--marker-size);
4953
height: var(--marker-size);
5054
background-color: var(--marker-color);
51-
margin-top: var(--marker-offset);
5255
}
5356

5457
.timeline-item--left .timeline-item__marker {
@@ -83,13 +86,16 @@
8386
}
8487

8588
.timeline-card__pointer {
89+
top: var(--marker-offset);
90+
transform: translate(100%, -50%);
91+
position: absolute;
92+
}
93+
94+
.timeline-card__pointer:not(.timeline-card__pointer--custom) {
8695
clip-path: polygon(0% 0%, 0% 100%, 100% 50%);
8796
background-color: var(--card-background);
88-
top: var(--marker-offset);
8997
width: var(--pointer-width);
9098
height: var(--pointer-height);
91-
transform: translate(100%, -50%);
92-
position: absolute;
9399
}
94100

95101
.timeline-item--left .timeline-card__pointer {

src/components/Timeline.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './Timeline.css';
2-
import { Key, useEffect, useRef } from 'react';
2+
import { Key, ReactElement, useEffect, useRef } from 'react';
33
import { PropsWithKey, TimelineItem, TimelineItemProps } from './TimelineItem';
44
import { OffsetConfig, resolveOffsets } from '../models/offset';
55
import { Positioning } from '../models/positioning';
@@ -12,19 +12,24 @@ export type TimelineProps = {
1212
minMarkerGap?: number;
1313
dateFormat?: string;
1414
dateLocale?: Locale;
15+
customMarker?: ReactElement;
16+
customPointer?: ReactElement;
1517
className?: string;
1618
};
1719

1820
const defaultTimelineConfig: Partial<TimelineProps> = {
1921
positioning: 'alternating',
2022
gap: 50,
2123
offset: 50,
22-
minMarkerGap: 50,
24+
minMarkerGap: 100,
2325
dateFormat: 'P',
2426
};
2527

2628
export function Timeline(props: TimelineProps) {
27-
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale } = { ...defaultTimelineConfig, ...props };
29+
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer } = {
30+
...defaultTimelineConfig,
31+
...props,
32+
};
2833

2934
const timelineRef = useRef<HTMLDivElement>(null);
3035
const itemsRef = useRef<Map<Key, HTMLElement>>();
@@ -93,6 +98,8 @@ export function Timeline(props: TimelineProps) {
9398
<TimelineItem
9499
dateFormat={dateFormat}
95100
dateLocale={dateLocale}
101+
customMarker={customMarker}
102+
customPointer={customPointer}
96103
{...item}
97104
ref={(node) => {
98105
const map = getRefMap();

src/components/TimelineItem.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forwardRef, Key, PropsWithChildren } from 'react';
1+
import { forwardRef, Key, PropsWithChildren, ReactElement } from 'react';
22
import { format } from 'date-fns';
33

44
export type PropsWithKey<T> = T & {
@@ -11,15 +11,17 @@ export type TimelineItemProps = PropsWithChildren<{
1111
dateFormat?: string;
1212
dateLocale?: Locale;
1313
title: string;
14+
customMarker?: ReactElement;
15+
customPointer?: ReactElement;
1416
}>;
1517

1618
export const TimelineItem = forwardRef<HTMLDivElement, TimelineItemProps>(
17-
({ className, title, date, children, dateFormat, dateLocale }, ref) => {
19+
({ className, title, date, children, dateFormat, dateLocale, customMarker, customPointer }, ref) => {
1820
return (
1921
<div ref={ref} className={['timeline-item', className].join(' ')}>
20-
<div className="timeline-item__marker" />
22+
<div className={['timeline-item__marker', customMarker && 'timeline-item__marker--custom'].join(' ')}>{customMarker}</div>
2123
<div className="timeline-card">
22-
<div className="timeline-card__pointer" />
24+
<div className={['timeline-card__pointer', customPointer && 'timeline-card__pointer--custom'].join(' ')}>{customPointer}</div>
2325
<p className="timeline-card__date">{date instanceof Date ? format(date, dateFormat ?? 'P', { locale: dateLocale }) : date}</p>
2426
<p className="timeline-card__title">{title}</p>
2527
{children}

0 commit comments

Comments
 (0)