<commit_before>import { useCallback } from '@wordpress/element';

import PropTypes from 'prop-types';

import Data from 'googlesitekit-data';
import JoyrideTooltip from '../JoyrideTooltip';
import { CORE_UI } from '../../googlesitekit/datastore/ui/constants';

import { useTooltipState } from './useTooltipState';

const { useDispatch } = Data;

export function AdminMenuTooltip( { onDismiss, tooltipStateKey, ...props } ) {
	const { setValue } = useDispatch( CORE_UI );

	const { rehideAdminMenu, rehideAdminSubMenu } =
		useTooltipState( tooltipStateKey );

	const handleDismissTooltip = useCallback( async () => {
		// If the WordPress admin menu was closed, re-close it.
		if ( rehideAdminMenu ) {
			const isAdminMenuOpen =
				document.querySelector( '#adminmenu' ).offsetHeight > 0;

			if ( isAdminMenuOpen ) {
				document.getElementById( 'wp-admin-bar-menu-toggle' )?.click();
			}
		}

		// If the Site Kit admin submenu was hidden, re-hide it.
		if ( rehideAdminSubMenu ) {
			// Click on the body to close the submenu.
			document.querySelector( 'body' ).click();
		}

		await onDismiss?.();

		setValue( tooltipStateKey, undefined );
	}, [
		onDismiss,
		rehideAdminMenu,
		rehideAdminSubMenu,
		setValue,
		tooltipStateKey,
	] );

	return <JoyrideTooltip onDismiss={ handleDismissTooltip } { ...props } />;
}

AdminMenuTooltip.propTypes = {
	...JoyrideTooltip.propTypes,
	target: PropTypes.string,
	tooltipStateKey: PropTypes.string.isRequired,
};

AdminMenuTooltip.defaultProps = {
	// Point to the Site Kit Settings menu item by default.
	target: '#adminmenu [href*="page=googlesitekit-settings"]',
};<commit_msg>Render GA4 admin menu tooltip in Portal to avoid side-effects.<commit_after>import { useCallback } from '@wordpress/element';

import PropTypes from 'prop-types';

import Data from 'googlesitekit-data';
import JoyrideTooltip from '../JoyrideTooltip';
import Portal from '../Portal';
import { CORE_UI } from '../../googlesitekit/datastore/ui/constants';
import { useTooltipState } from './useTooltipState';

const { useDispatch } = Data;

export function AdminMenuTooltip( { onDismiss, tooltipStateKey, ...props } ) {
	const { setValue } = useDispatch( CORE_UI );

	const { rehideAdminMenu, rehideAdminSubMenu } =
		useTooltipState( tooltipStateKey );

	const handleDismissTooltip = useCallback( async () => {
		// If the WordPress admin menu was closed, re-close it.
		if ( rehideAdminMenu ) {
			const isAdminMenuOpen =
				document.querySelector( '#adminmenu' ).offsetHeight > 0;

			if ( isAdminMenuOpen ) {
				document.getElementById( 'wp-admin-bar-menu-toggle' )?.click();
			}
		}

		// If the Site Kit admin submenu was hidden, re-hide it.
		if ( rehideAdminSubMenu ) {
			// Click on the body to close the submenu.
			document.querySelector( 'body' ).click();
		}

		await onDismiss?.();

		setValue( tooltipStateKey, undefined );
	}, [
		onDismiss,
		rehideAdminMenu,
		rehideAdminSubMenu,
		setValue,
		tooltipStateKey,
	] );

	// Render in a Portal to avoid adding an element in the sub-header which has
	// added styles if not empty. This also avoids a tiny shift when dismissing.
	return (
		<Portal slug="ga4-activation-banner-admin-menu-tooltip">
			<JoyrideTooltip onDismiss={ handleDismissTooltip } { ...props } />
		</Portal>
	);
}

AdminMenuTooltip.propTypes = {
	...JoyrideTooltip.propTypes,
	target: PropTypes.string,
	tooltipStateKey: PropTypes.string.isRequired,
};

AdminMenuTooltip.defaultProps = {
	// Point to the Site Kit Settings menu item by default.
	target: '#adminmenu [href*="page=googlesitekit-settings"]',
};
{'fraction_non_alphanumeric': 0.12475524475524476, 'fraction_numerical': 0.0011188811188811189, 'mean_word_length': 4.015427769985974, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788595', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1128, 'n_words': 295}