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

Show user's avatar in post author dropdown #41430

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions packages/components/src/custom-select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ export default function CustomSelectControl( {
describedBy: getDescribedBy(),
} ) }
>
{ selectedItem.__experimentalImage && (
<img
className="components-custom-select-control__button-image"
alt=""
width={ 32 }
height={ 32 }
{ ...selectedItem.__experimentalImage }
/>
) }
{ itemToString( selectedItem ) }
<Icon
icon={ chevronDown }
Expand Down Expand Up @@ -179,13 +188,23 @@ export default function CustomSelectControl( {
{
'is-highlighted':
index === highlightedIndex,
'has-image': !! item.__experimentalImage,
'has-hint': !! item.__experimentalHint,
'is-next-36px-default-size': __next36pxDefaultSize,
}
),
style: item.style,
} ) }
>
{ item.__experimentalImage && (
<img
className="components-custom-select-control__item-image"
alt=""
width={ 32 }
height={ 32 }
{ ...item.__experimentalImage }
/>
) }
{ item.name }
{ item.__experimentalHint && (
<span className="components-custom-select-control__item-hint">
Expand Down
57 changes: 57 additions & 0 deletions packages/components/src/custom-select-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,60 @@ WithHints.args = {
},
],
};

export const WithImages = CustomSelectControl.bind( {} );
WithImages.args = {
...Default.args,
options: [
{
key: 'adam',
name: 'Adam Zielinski',
__experimentalImage: {
src:
'https://www.gravatar.com/avatar/3b7ea537531208d83deed8f3e78bc771?s=100&r=g',
width: 48,
height: 48,
},
},
{
key: 'greg',
name: 'Greg Ziółkowski',
__experimentalImage: {
src:
'https://www.gravatar.com/avatar/475d323ceec2e73597729eef1c5bf263?s=100&r=g',
width: 48,
height: 48,
},
},
{
key: 'robert',
name: 'Robert Anderson',
__experimentalImage: {
src:
'https://www.gravatar.com/avatar/c9ae983c4a94490f209c06dd46b801e4?s=100&r=g',
width: 48,
height: 48,
},
},
{
key: 'george',
name: 'George Mamadashvili',
__experimentalImage: {
src:
'https://www.gravatar.com/avatar/ddda3dc3a8502b3e1889905a9d500f3f?s=100&r=g',
width: 48,
height: 48,
},
},
{
key: 'isabel',
name: 'Isabel Brison',
__experimentalImage: {
src:
'https://www.gravatar.com/avatar/0236f3f6facfcca37aa798f9c6766116?s=100&r=g',
width: 48,
height: 48,
},
},
],
};
23 changes: 22 additions & 1 deletion packages/components/src/custom-select-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// For all button sizes allow sufficient space for the
// dropdown "arrow" icon to display.
&.components-custom-select-control__button {
padding-top: $grid-unit-05;
padding-bottom: $grid-unit-05;
// TODO: Some of these can be removed after some internal inconsistencies are addressed, such as the chevron
// (https://github.com/WordPress/gutenberg/issues/39400) and Button padding (https://github.com/WordPress/gutenberg/issues/39431)
padding-left: $grid-unit-20;
Expand All @@ -37,6 +39,12 @@
box-shadow: 0 0 0 ($border-width-focus - $border-width) var(--wp-admin-theme-color);
}

.components-custom-select-control__button-image {
max-height: 100%;
width: auto;
margin-right: $grid-unit-05;
}

.components-custom-select-control__button-icon {
height: 100%;
padding: 0;
Expand Down Expand Up @@ -74,7 +82,7 @@
.components-custom-select-control__item {
align-items: center;
display: grid;
grid-template-columns: auto auto;
grid-template-columns: auto 30px;
list-style-type: none;
padding: $grid-unit-10 $grid-unit-20;
cursor: default;
Expand All @@ -84,12 +92,25 @@
padding: $grid-unit-10;
}

&.has-image {
grid-template-columns: max-content auto 30px;
}
&.has-hint {
grid-template-columns: auto auto 30px;
}
&.has-image.has-hint {
grid-template-columns: max-content auto auto 30px;
}

&.is-highlighted {
background: $gray-300;
}

.components-custom-select-control__item-image {
max-height: $button-size - $grid-unit-05 * 2 - $border-width * 2;
width: auto;
margin-right: $grid-unit-05;
}
.components-custom-select-control__item-hint {
color: $gray-700;
text-align: right;
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-author/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const AUTHORS_QUERY = {
who: 'authors',
per_page: 50,
_fields: 'id,name',
_fields: 'id,name,avatar_urls',
context: 'view', // Allows non-admins to perform requests.
};
25 changes: 17 additions & 8 deletions packages/editor/src/components/post-author/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { SelectControl } from '@wordpress/components';
import { CustomSelectControl } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand All @@ -27,25 +27,34 @@ function PostAuthorSelect() {

const authorOptions = useMemo( () => {
return ( authors ?? [] ).map( ( author ) => {
const [ avatarSize, avatarURL ] = Object.entries(
author.avatar_urls
).find( ( [ size ] ) => size >= 26 * 2 );
return {
value: author.id,
label: decodeEntities( author.name ),
key: author.id,
name: decodeEntities( author.name ),
__experimentalImage: {
src: avatarURL,
width: avatarSize / 2,
height: avatarSize / 2,
},
};
} );
}, [ authors ] );

const setAuthorId = ( value ) => {
const author = Number( value );
const setAuthorId = ( option ) => {
const author = Number( option.key );
editPost( { author } );
};

return (
<SelectControl
className="post-author-selector"
<CustomSelectControl
className="editor-post-author-select"
label={ __( 'Author' ) }
options={ authorOptions }
onChange={ setAuthorId }
value={ postAuthor }
value={ authorOptions.find( ( { key } ) => key === postAuthor ) }
__next36pxDefaultSize
/>
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/components/post-author/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.editor-post-author-select,
.editor-post-author-select .components-custom-select-control__button {
width: 100%;
}
1 change: 1 addition & 0 deletions packages/editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@import "./components/entities-saved-states/style.scss";
@import "./components/error-boundary/style.scss";
@import "./components/page-attributes/style.scss";
@import "./components/post-author/style.scss";
@import "./components/post-excerpt/style.scss";
@import "./components/post-featured-image/style.scss";
@import "./components/post-format/style.scss";
Expand Down