# cartflows/3.3.0/modules/gutenberg/src/controls/scrollBlockToView.js

CartFlows – Funnel Builder &amp; Checkout Plugin for WooCommerce, version 3.3.0. 48 lines.

- Page: https://pluginprobe.com/plugins/cartflows/3.3.0/code/modules/gutenberg/src/controls/scrollBlockToView.js
- Raw: https://pluginprobe.com/plugins/cartflows/3.3.0/raw/modules/gutenberg/src/controls/scrollBlockToView.js
- Modified: 2022-11-07T12:36:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cartflows/3.3.0/code/modules/gutenberg/src/controls/scrollBlockToView.js#L10-L20`.

```javascript
import { select } from '@wordpress/data';

const scrollBlockToView = () => {

	// Scroll the view to the selected block after device type change in editor.
	const { getSelectedBlock } = select( 'core/block-editor' );
	const selectedBlockID = getSelectedBlock()?.clientId ? `block-${getSelectedBlock()?.clientId}` : false;

	if ( ! selectedBlockID ) {
		return;
	}

	setTimeout( ()=> {
		const currentDocument = getCurrentDocument();
		const selectedBlockElementToScroll = currentDocument.getElementById	( selectedBlockID );

		if ( selectedBlockElementToScroll ) {
			selectedBlockElementToScroll.scrollIntoView( {behavior: 'smooth'} );
		}
	}, 500 )
}

const getCurrentDocument = () => {

	const tabletPreview = document.getElementsByClassName( 'is-tablet-preview' );
	const mobilePreview = document.getElementsByClassName( 'is-mobile-preview' );
	if ( 0 !== tabletPreview.length || 0 !== mobilePreview.length ) {

		const preview = tabletPreview[0] || mobilePreview[0];

		let iframe = false;

		if ( preview ) {
			iframe = preview.getElementsByTagName( 'iframe' )[0];
		}

		const iframeDocument = iframe?.contentWindow.document || iframe?.contentDocument;

		if ( iframeDocument ) {
			return iframeDocument;
		}
	}

	return document;
};

export default scrollBlockToView;

```
