@wordpress/block-library
Version:
Block library for the WordPress editor.
83 lines (79 loc) • 2.07 kB
JavaScript
/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
Disabled,
PanelBody,
RangeControl,
ToggleControl,
} from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
import { __ } from '@wordpress/i18n';
/**
* Minimum number of comments a user can show using this block.
*
* @type {number}
*/
const MIN_COMMENTS = 1;
/**
* Maximum number of comments a user can show using this block.
*
* @type {number}
*/
const MAX_COMMENTS = 100;
export default function LatestComments( { attributes, setAttributes } ) {
const { commentsToShow, displayAvatar, displayDate, displayExcerpt } =
attributes;
return (
<div { ...useBlockProps() }>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
label={ __( 'Display avatar' ) }
checked={ displayAvatar }
onChange={ () =>
setAttributes( { displayAvatar: ! displayAvatar } )
}
/>
<ToggleControl
label={ __( 'Display date' ) }
checked={ displayDate }
onChange={ () =>
setAttributes( { displayDate: ! displayDate } )
}
/>
<ToggleControl
label={ __( 'Display excerpt' ) }
checked={ displayExcerpt }
onChange={ () =>
setAttributes( {
displayExcerpt: ! displayExcerpt,
} )
}
/>
<RangeControl
label={ __( 'Number of comments' ) }
value={ commentsToShow }
onChange={ ( value ) =>
setAttributes( { commentsToShow: value } )
}
min={ MIN_COMMENTS }
max={ MAX_COMMENTS }
required
/>
</PanelBody>
</InspectorControls>
<Disabled>
<ServerSideRender
block="core/latest-comments"
attributes={ attributes }
// The preview uses the site's locale to make it more true to how
// the block appears on the frontend. Setting the locale
// explicitly prevents any middleware from setting it to 'user'.
urlQueryArgs={ { _locale: 'site' } }
/>
</Disabled>
</div>
);
}