Skip to content

🎥 Make videos programmatically with React

1.6 KB
Raw Blame
import React, {useContext, useMemo} from 'react';
import {WHITE_ALPHA_80} from '../../helpers/colors';
import {
	getTimelineFieldLabelRowStyle,
	TIMELINE_STACKED_FIELD_HEADER_HEIGHT,
} from './timeline-field-row-layout';
import {TimelineRowLayoutContext} from './TimelineRowLayoutContext';
import {
	getTimelineColor,
	getTimelineSelectedLabelStyle,
} from './TimelineSelection';

const fieldNameBase: React.CSSProperties = {
	fontSize: 12,
	lineHeight: `${TIMELINE_STACKED_FIELD_HEADER_HEIGHT}px`,
	color: WHITE_ALPHA_80,
	userSelect: 'none',
	WebkitUserSelect: 'none',
	whiteSpace: 'nowrap',
	overflow: 'hidden',
	textOverflow: 'ellipsis',
	flex: 1,
	minWidth: 0,
};

export const TimelineFieldLabel: React.FC<{
	readonly rowDepth: number;
	readonly selected: boolean;
	readonly label: string;
	readonly stacked?: boolean;
}> = ({rowDepth, selected, label, stacked = false}) => {
	const {basePadding, highlightSelectedLabel} = useContext(
		TimelineRowLayoutContext,
	);
	const labelRowStyle = useMemo(
		(): React.CSSProperties => ({
			...getTimelineFieldLabelRowStyle(rowDepth, basePadding),
			...getTimelineSelectedLabelStyle(
				selected && highlightSelectedLabel,
				true,
			),
			...(stacked
				? {flex: `0 0 ${TIMELINE_STACKED_FIELD_HEADER_HEIGHT}px`}
				: null),
			alignSelf: 'stretch',
		}),
		[basePadding, highlightSelectedLabel, rowDepth, selected, stacked],
	);

	const fieldNameStyle = useMemo(
		(): React.CSSProperties => ({
			...fieldNameBase,
			color: getTimelineColor(selected, true),
		}),
		[selected],
	);

	return (
		<div style={labelRowStyle}>
			<span role="group" style={fieldNameStyle} aria-label={label}>
				{label}
			</span>
		</div>
	);
};