Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Only add RichText component if the figcaption is clicked to prevent i…
…t stealing focus every time block is selected
  • Loading branch information
Glen Davies committed Apr 27, 2021
commit 84849dda532a1ca777ac7134edc05682add2c094
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