Skip to content
Prev Previous commit
Next Next commit
Remove focus handling code.
  • Loading branch information
alexstine committed Sep 13, 2023
commit 7f70f29bd76a0c69df526a14918b0afba0b4dd79
22 changes: 1 addition & 21 deletions packages/block-library/src/html/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useContext, useEffect, useRef, useState } from '@wordpress/element';
import { useContext, useState } from '@wordpress/element';
import {
BlockControls,
PlainText,
Expand All @@ -23,41 +23,22 @@ import Preview from './preview';

export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
const [ isPreview, setIsPreview ] = useState();
const [ shouldFocus, setShouldFocus ] = useState( false );
const isDisabled = useContext( Disabled.Context );

const htmlTextareaRef = useRef();
const blockRef = useRef();

const instanceId = useInstanceId( HTMLEdit );
const instanceIdDesc = sprintf( 'html-edit-%d-desc', instanceId );

function switchToPreview() {
setIsPreview( true );
setShouldFocus( true );
}

function switchToHTML() {
setIsPreview( false );
setShouldFocus( true );
}

// Effect for managing focus.
useEffect( () => {
if ( ! shouldFocus ) {
return;
}
if ( isPreview ) {
blockRef?.current?.focus();
} else {
htmlTextareaRef?.current?.focus();
}
}, [ shouldFocus, isPreview ] );

const blockProps = useBlockProps( {
className: 'block-library-html__edit',
'aria-describedby': isPreview ? instanceIdDesc : undefined,
ref: blockRef,
} );

return (
Expand Down Expand Up @@ -98,7 +79,6 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
ref={ htmlTextareaRef }
/>
) }
</div>
Expand Down