Skip to content

Commit 425145f

Browse files
committed
feat: make css properties configurable with props
1 parent d291e3d commit 425145f

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/components/Timeline.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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';
6+
import { convertToCssVariable, StyleConfig } from '../models/style';
67

78
export type TimelineProps = {
89
items: PropsWithKey<TimelineItemProps>[];
@@ -14,6 +15,7 @@ export type TimelineProps = {
1415
dateLocale?: Locale;
1516
customMarker?: ReactElement;
1617
customPointer?: ReactElement;
18+
styleConfig?: StyleConfig;
1719
className?: string;
1820
};
1921

@@ -26,7 +28,7 @@ const defaultTimelineConfig: Partial<TimelineProps> = {
2628
};
2729

2830
export function Timeline(props: TimelineProps) {
29-
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer } = {
31+
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer, styleConfig } = {
3032
...defaultTimelineConfig,
3133
...props,
3234
};
@@ -85,6 +87,11 @@ export function Timeline(props: TimelineProps) {
8587
}
8688
}
8789

90+
useEffect(() => {
91+
if (!styleConfig) return;
92+
Object.entries(convertToCssVariable(styleConfig)).forEach((prop) => timelineRef.current?.style.setProperty(...prop));
93+
}, [styleConfig]);
94+
8895
useEffect(() => {
8996
window.addEventListener('resize', positionTimelineItems);
9097
return () => window.removeEventListener('resize', positionTimelineItems);

src/helper/object.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const flattenObject = <O extends Record<string, T>, T extends O | string | undefined>(ob: Record<string, T>, delimiter?: string) => {
2+
const result: Record<string, string> = {};
3+
4+
Object.keys(ob).forEach((i) => {
5+
const value = ob[i];
6+
if (typeof value === 'object') {
7+
const temp = flattenObject(value, delimiter);
8+
Object.keys(temp).forEach((j) => {
9+
result[`${i}${delimiter ?? '.'}${j}`] = temp[j];
10+
});
11+
} else if (value) {
12+
result[i] = value;
13+
}
14+
});
15+
16+
return result;
17+
};

src/models/style.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { flattenObject } from '../helper/object';
2+
3+
export type StyleConfig = {
4+
line?: {
5+
width?: string;
6+
color?: string;
7+
};
8+
marker?: {
9+
size?: string;
10+
color?: string;
11+
radius?: string;
12+
offset?: string;
13+
};
14+
pointer?: {
15+
height?: string;
16+
width?: string;
17+
};
18+
card?: {
19+
background?: string;
20+
radius?: string;
21+
offset?: string;
22+
shadow?: string;
23+
padding?: string;
24+
};
25+
};
26+
27+
export const convertToCssVariable = (styleConfig: StyleConfig) =>
28+
Object.entries(flattenObject(styleConfig, '-')).reduce(
29+
(prev: Record<string, string>, [key, value]) => ({ ...prev, [`--${key}`]: value }),
30+
{},
31+
);

0 commit comments

Comments
 (0)