Skip to content

🎥 Make videos programmatically with React

1.8 KB
Raw Blame
import React, {useCallback} from 'react';
import {
	BACKGROUND,
	SHADOW_BLACK,
	WHITE,
	WHITE_ALPHA_20,
} from '../helpers/colors';
import {HigherZIndex} from '../state/z-index';

const padding = 20;

export const getMaxModalWidth = (width: number) => {
	return `min(calc(100vw - ${padding * 2}px), calc(${width}px - ${
		padding * 2
	}px))`;
};

export const getMaxModalHeight = (height: number) => {
	return `min(calc(100vh - ${padding * 2}px), calc(${height}px - ${
		padding * 2
	}px))`;
};

const backgroundOverlay: React.CSSProperties = {
	backgroundColor: WHITE_ALPHA_20,
	backdropFilter: `blur(1px)`,
	position: 'fixed',
	height: '100%',
	width: '100%',
	display: 'flex',
	padding,
};

const panel: React.CSSProperties = {
	backgroundColor: BACKGROUND,
	boxShadow: SHADOW_BLACK,
	color: WHITE,
	margin: 'auto',
};

export const ModalContainer: React.FC<{
	readonly ariaLabel?: string;
	readonly ariaLabelledBy?: string;
	readonly onEscape: () => void;
	readonly onOutsideClick: () => void;
	readonly children: React.ReactNode;
	readonly noZIndex?: boolean;
	readonly panelStyle?: React.CSSProperties;
}> = ({
	ariaLabel,
	ariaLabelledBy,
	children,
	onEscape,
	onOutsideClick,
	noZIndex,
	panelStyle,
}) => {
	const onPointerDown = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
		// Prevent deselection of currently selected items
		e.stopPropagation();
	}, []);

	return (
		<div
			aria-label={ariaLabel}
			aria-labelledby={ariaLabelledBy ?? undefined}
			aria-modal="true"
			className="css-reset"
			role="dialog"
			style={backgroundOverlay}
			onPointerDown={onPointerDown}
		>
			<HigherZIndex
				disabled={noZIndex}
				onOutsideClick={onOutsideClick}
				onEscape={onEscape}
			>
				<div style={{...panel, ...panelStyle}}>{children}</div>
			</HigherZIndex>
		</div>
	);
};