Skip to content

Commit

Permalink
Only add RichText component if the figcaption is clicked to prevent i…
Browse files Browse the repository at this point in the history
…t stealing focus every time block is selected (#31216)

Co-authored-by: Glen Davies <[email protected]>
  • Loading branch information
glendaviesnz and Glen Davies authored Apr 28, 2021
1 parent fc06639 commit 1067a0c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
77 changes: 71 additions & 6 deletions packages/block-library/src/gallery/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
} from '@wordpress/block-editor';
import { VisuallyHidden } from '@wordpress/components';
import {
useState,
useEffect,
useRef,
useLayoutEffect,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';

Expand Down Expand Up @@ -46,6 +52,30 @@ export const Gallery = ( props ) => {
__experimentalLayout: { type: 'default', alignments: [] },
} );

const [ captionFocused, setCaptionFocused ] = useState( false );

const captionRef = useRef();

// Need to use a layout effect here as we can't focus the RichText element
// until the DOM render cycle is finished.
useLayoutEffect( () => {
if ( captionFocused && captionRef.current ) {
captionRef.current.focus();
}
}, [ captionFocused ] );

function onFocusCaption() {
if ( ! captionFocused ) {
setCaptionFocused( true );
}
}

useEffect( () => {
if ( ! isSelected ) {
setCaptionFocused( false );
}
}, [ isSelected ] );

return (
<figure
{ ...innerBlocksProps }
Expand All @@ -60,8 +90,12 @@ export const Gallery = ( props ) => {
) }
>
{ children }
{ mediaPlaceholder }
<RichTextVisibilityHelper
captionRef={ captionRef }
isHidden={ ! isSelected && RichText.isEmpty( caption ) }
captionFocused={ captionFocused }
onFocusCaption={ onFocusCaption }
tagName="figcaption"
className="blocks-gallery-caption"
aria-label={ __( 'Gallery caption text' ) }
Expand All @@ -73,16 +107,47 @@ export const Gallery = ( props ) => {
insertBlocksAfter( createBlock( 'core/paragraph' ) )
}
/>
{ mediaPlaceholder }
</figure>
);
};

function RichTextVisibilityHelper( { isHidden, ...richTextProps } ) {
return isHidden ? (
<VisuallyHidden as={ RichText } { ...richTextProps } />
) : (
<RichText { ...richTextProps } />
function RichTextVisibilityHelper( {
isHidden,
captionFocused,
onFocusCaption,
className,
value,
placeholder,
tagName,
captionRef,
...richTextProps
} ) {
if ( isHidden ) {
return <VisuallyHidden as={ RichText } { ...richTextProps } />;
}

if ( captionFocused ) {
return (
<RichText
ref={ captionRef }
value={ value }
placeholder={ placeholder }
className={ className }
tagName={ tagName }
isSelected={ captionFocused }
{ ...richTextProps }
/>
);
}

return (
<figcaption
role="presentation"
onClick={ onFocusCaption }
className={ className }
>
{ RichText.isEmpty( value ) ? placeholder : value }
</figcaption>
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
figcaption {
flex-grow: 1;
flex-basis: 100%;
text-align: center;
}

// Non cropped images.
Expand Down

0 comments on commit 1067a0c

Please sign in to comment.