Skip to content

Commit

Permalink
feat: make css properties configurable with props
Browse files Browse the repository at this point in the history
  • Loading branch information
stampaaaron committed Feb 1, 2023
1 parent d291e3d commit 425145f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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 { PropsWithKey, TimelineItem, TimelineItemProps } from './TimelineItem';
import { OffsetConfig, resolveOffsets } from '../models/offset';
import { Positioning } from '../models/positioning';
import { convertToCssVariable, StyleConfig } from '../models/style';

export type TimelineProps = {
items: PropsWithKey<TimelineItemProps>[];
Expand All @@ -14,6 +15,7 @@ export type TimelineProps = {
dateLocale?: Locale;
customMarker?: ReactElement;
customPointer?: ReactElement;
styleConfig?: StyleConfig;
className?: string;
};

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

export function Timeline(props: TimelineProps) {
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer } = {
const { items, positioning, gap, offset, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer, styleConfig } = {
...defaultTimelineConfig,
...props,
};
Expand Down Expand Up @@ -85,6 +87,11 @@ export function Timeline(props: TimelineProps) {
}
}

useEffect(() => {
if (!styleConfig) return;
Object.entries(convertToCssVariable(styleConfig)).forEach((prop) => timelineRef.current?.style.setProperty(...prop));
}, [styleConfig]);

useEffect(() => {
window.addEventListener('resize', positionTimelineItems);
return () => window.removeEventListener('resize', positionTimelineItems);
Expand Down
17 changes: 17 additions & 0 deletions src/helper/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const flattenObject = <O extends Record<string, T>, T extends O | string | undefined>(ob: Record<string, T>, delimiter?: string) => {
const result: Record<string, string> = {};

Object.keys(ob).forEach((i) => {
const value = ob[i];
if (typeof value === 'object') {
const temp = flattenObject(value, delimiter);
Object.keys(temp).forEach((j) => {
result[`${i}${delimiter ?? '.'}${j}`] = temp[j];
});
} else if (value) {
result[i] = value;
}
});

return result;
};
31 changes: 31 additions & 0 deletions src/models/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { flattenObject } from '../helper/object';

export type StyleConfig = {
line?: {
width?: string;
color?: string;
};
marker?: {
size?: string;
color?: string;
radius?: string;
offset?: string;
};
pointer?: {
height?: string;
width?: string;
};
card?: {
background?: string;
radius?: string;
offset?: string;
shadow?: string;
padding?: string;
};
};

export const convertToCssVariable = (styleConfig: StyleConfig) =>
Object.entries(flattenObject(styleConfig, '-')).reduce(
(prev: Record<string, string>, [key, value]) => ({ ...prev, [`--${key}`]: value }),
{},
);

0 comments on commit 425145f

Please sign in to comment.