Skip to content

🎥 Make videos programmatically with React

1.0 KB
Raw Blame
import {
	REACT_REFRESH_FINISHED_EVENT,
	REACT_REFRESH_STARTED_EVENT,
} from '@remotion/studio-shared';

type ReactRefreshRuntime = {
	performReactRefresh: () => unknown;
	__remotionReactRefreshWrapped: boolean | null;
};

const RefreshRuntime = require('react-refresh/runtime') as ReactRefreshRuntime;
RefreshRuntime.__remotionReactRefreshWrapped ??= null;

if (RefreshRuntime.__remotionReactRefreshWrapped === null) {
	const originalPerformReactRefresh = RefreshRuntime.performReactRefresh;
	RefreshRuntime.__remotionReactRefreshWrapped = true;
	RefreshRuntime.performReactRefresh = () => {
		window.dispatchEvent(new Event(REACT_REFRESH_STARTED_EVENT));
		// The refresh integration calls this after applying the update. Emitting
		// here gives Studio a boundary after React refreshed its roots.
		const result = originalPerformReactRefresh();
		if (result !== null) {
			window.dispatchEvent(new Event(REACT_REFRESH_FINISHED_EVENT));
		}

		return result;
	};
}

window.remotion_performReactRefresh = () =>
	RefreshRuntime.performReactRefresh();