Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Update post excerpt panel with new designs #60894

Merged
merged 10 commits into from
Apr 25, 2024
Prev Previous commit
Next Next commit
update excerpt on blur
  • Loading branch information
ntsekouras committed Apr 24, 2024
commit 9ad36cb753733ed5118b8291b47776e6bd0ca73f
8 changes: 7 additions & 1 deletion packages/editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,13 @@ Undocumented declaration.

### PostExcerpt

Undocumented declaration.
Renders an editable textarea for the post excerpt. Templates, template parts and patterns use the `excerpt` field as a description semantically. Additionally templates and template parts override the `excerpt` field as `description` in REST API. So this component handles proper labeling and updating the edited entity.

_Parameters_

- _props_ `Object`: - Component props.
- _props.hideLabelFromVision_ `[boolean]`: - Whether to visually hide the textarea's label.
- _props.updateOnBlur_ `[boolean]`: - Whether to update the post on change or use local state and update on blur.

### PostExcerptCheck

Expand Down
30 changes: 23 additions & 7 deletions packages/editor/src/components/post-excerpt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@
import { __ } from '@wordpress/i18n';
import { ExternalLink, TextareaControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';

function PostExcerpt( { hideLabelFromVision = false } ) {
/**
* Renders an editable textarea for the post excerpt.
* Templates, template parts and patterns use the `excerpt` field as a description semantically.
* Additionally templates and template parts override the `excerpt` field as `description` in
* REST API. So this component handles proper labeling and updating the edited entity.
*
* @param {Object} props - Component props.
* @param {boolean} [props.hideLabelFromVision=false] - Whether to visually hide the textarea's label.
* @param {boolean} [props.updateOnBlur=false] - Whether to update the post on change or use local state and update on blur.
*/
export default function PostExcerpt( {
hideLabelFromVision = false,
updateOnBlur = false,
} ) {
const { excerpt, shouldUseDescriptionLabel, usedAttribute } = useSelect(
( select ) => {
const { getCurrentPostType, getEditedPostAttribute } =
Expand Down Expand Up @@ -38,7 +52,10 @@ function PostExcerpt( { hideLabelFromVision = false } ) {
[]
);
const { editPost } = useDispatch( editorStore );

const [ localExcerpt, setLocalExcerpt ] = useState( excerpt );
const updatePost = ( value ) => {
editPost( { [ usedAttribute ]: value } );
};
const label = shouldUseDescriptionLabel
? __( 'Write a description (optional)' )
: __( 'Write an excerpt (optional)' );
Expand All @@ -50,10 +67,11 @@ function PostExcerpt( { hideLabelFromVision = false } ) {
label={ label }
hideLabelFromVision={ hideLabelFromVision }
className="editor-post-excerpt__textarea"
onChange={ ( value ) =>
editPost( { [ usedAttribute ]: value } )
onChange={ updateOnBlur ? setLocalExcerpt : updatePost }
onBlur={
updateOnBlur ? () => updatePost( localExcerpt ) : undefined
}
value={ excerpt }
value={ updateOnBlur ? localExcerpt : excerpt }
help={
! shouldUseDescriptionLabel ? (
<ExternalLink
Expand All @@ -71,5 +89,3 @@ function PostExcerpt( { hideLabelFromVision = false } ) {
</div>
);
}

export default PostExcerpt;
7 changes: 5 additions & 2 deletions packages/editor/src/components/post-excerpt/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function PrivateExcerpt() {
<Button
className={ classnames(
'editor-post-excerpt__dropdown__trigger',
excerpt && 'has-excerpt'
{ 'has-excerpt': !! excerpt }
) }
onClick={ onToggle }
label={ excerptText && triggerEditLabel }
Expand All @@ -215,7 +215,10 @@ function PrivateExcerpt() {
<PluginPostExcerpt.Slot>
{ ( fills ) => (
<>
<PostExcerptForm hideLabelFromVision />
<PostExcerptForm
hideLabelFromVision
updateOnBlur
/>
{ fills }
</>
) }
Expand Down
Loading