File "index.ts"

Full Path: /home/warrior1/public_html/plugins/jetpack/jetpack_vendor/automattic/jetpack-videopress/src/client/admin/hooks/use-unload-prevent/index.ts
File size: 814 bytes
MIME-type: text/x-java
Charset: utf-8

/**
 * External dependencies
 */
import { useEffect } from 'react';

const useUnloadPrevent = ( {
	shouldPrevent = false,
	message,
}: {
	shouldPrevent?: boolean;
	message: string;
} ) => {
	useEffect( () => {
		if ( ! shouldPrevent ) {
			return;
		}

		const beforeUnloadListener = event => {
			event.preventDefault();
			// Note: The message only shows on older browsers, with a standard non-customizable message on current browsers
			// ref https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#compatibility_notes
			event.returnValue = message;
			return;
		};

		window.addEventListener( 'beforeunload', beforeUnloadListener );

		return () => {
			window.removeEventListener( 'beforeunload', beforeUnloadListener );
		};
	}, [ shouldPrevent ] );
};

export default useUnloadPrevent;