@wordpress/block-library
Version:
Block library for the WordPress editor.
115 lines (105 loc) • 2.48 kB
JavaScript
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
InspectorControls,
useBlockProps,
Warning,
} from '@wordpress/block-editor';
import { ToggleControl, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import HeadingLevelDropdown from '../heading/heading-level-dropdown';
const SUPPORTED_TYPES = [ 'archive', 'search' ];
export default function QueryTitleEdit( {
attributes: { type, level, textAlign, showPrefix, showSearchTerm },
setAttributes,
} ) {
const TagName = `h${ level }`;
const blockProps = useBlockProps( {
className: classnames( 'wp-block-query-title__placeholder', {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
if ( ! SUPPORTED_TYPES.includes( type ) ) {
return (
<div { ...blockProps }>
<Warning>{ __( 'Provided type is not supported.' ) }</Warning>
</div>
);
}
let titleElement;
if ( type === 'archive' ) {
titleElement = (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
label={ __( 'Show archive type in title' ) }
onChange={ () =>
setAttributes( { showPrefix: ! showPrefix } )
}
checked={ showPrefix }
/>
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>
{ showPrefix
? __( 'Archive type: Name' )
: __( 'Archive title' ) }
</TagName>
</>
);
}
if ( type === 'search' ) {
titleElement = (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
label={ __( 'Show search term in title' ) }
onChange={ () =>
setAttributes( {
showSearchTerm: ! showSearchTerm,
} )
}
checked={ showSearchTerm }
/>
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>
{ showSearchTerm
? __( 'Search results for: “search term”' )
: __( 'Search results' ) }
</TagName>
</>
);
}
return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
selectedLevel={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
{ titleElement }
</>
);
}