From 3ca388fb9df9cbf5e07a485d70c68be2d875c1ef Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 3 Dec 2026 10:45:12 +0100 Subject: [PATCH 1/5] Editor: Add a collaborator avatar with an inactive state Wrap the @wordpress/ui Avatar in the editor and add the inactive state that the editor Avatar has: `dimmed` drops the collaborator color, turns the avatar gray and fades it, and `statusIndicator` shows an icon over a dimmed avatar at full opacity. The collaborator UI can then move to the ui Avatar without adding these props to the ui component. Co-Authored-By: Claude Opus 5.5 --- .../collaborator-avatar/index.tsx | 56 +++++++++++++ .../stories/index.story.tsx | 51 ++++++++++++ .../collaborator-avatar/style.module.scss | 31 ++++++++ .../test/index.browser.test.tsx | 79 +++++++++++++++++++ 4 files changed, 217 insertions(+) create mode 100644 packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx create mode 100644 packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx create mode 100644 packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss create mode 100644 packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx new file mode 100644 index 00000000000000..252c3f20559b64 --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx @@ -0,0 +1,56 @@ +import clsx from 'clsx'; +import { Icon as WCIcon, type IconType } from '@wordpress/components'; +import { + // eslint-disable-next-line @wordpress/use-recommended-components -- `Avatar` is marked "use with caution" while its API settles; the collaborator UI adopts it ahead of that, see #87841. + Avatar, +} from '@wordpress/ui'; +import styles from './style.module.scss'; + +export type CollaboratorAvatarProps = React.ComponentPropsWithoutRef< + typeof Avatar +> & { + /** + * Whether to dim the avatar to show that the collaborator is inactive, + * for example after they disconnect. The avatar drops its color, the image + * turns gray, and the avatar fades. + * + * @default false + */ + dimmed?: boolean; + /** + * An icon shown over the center of a dimmed avatar, at full opacity. + * It is decorative: describe the inactive state in nearby text. + */ + statusIndicator?: IconType | null; +}; + +/** + * The `@wordpress/ui` `Avatar` with an inactive state for collaborators. + * Props other than `dimmed` and `statusIndicator` go to the `Avatar`. + * + * ```jsx + * + * ``` + */ +export default function CollaboratorAvatar( { + dimmed = false, + statusIndicator, + color, + className, + ...props +}: CollaboratorAvatarProps ) { + return ( + + + { dimmed && !! statusIndicator && ( + + + + ) } + + ); +} diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx new file mode 100644 index 00000000000000..86072d3485e2cd --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx @@ -0,0 +1,51 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; +import { linkOff } from '@wordpress/icons'; +import CollaboratorAvatar from '..'; + +// Served from `storybook/static`. +const IMAGE_SRC = 'profile-picture.svg'; + +/** + * The `@wordpress/ui` `Avatar` with an inactive state for collaborators who + * have disconnected. A dimmed avatar drops its color, turns gray and fades. + * An optional status indicator shows over it at full opacity. + */ +const meta: Meta< typeof CollaboratorAvatar > = { + id: 'editor-collaboratoravatar', + title: 'Editor/Collaborator Avatar', + component: CollaboratorAvatar, + args: { name: 'Alex Morgan', color: '#008a20' }, + argTypes: { + statusIndicator: { control: false }, + }, +}; +export default meta; + +type Story = StoryObj< typeof CollaboratorAvatar >; + +export const Default: Story = { + args: { src: IMAGE_SRC, active: true }, +}; + +/** Active and dimmed avatars side by side, with initials and with an image. */ +export const Dimmed: Story = { + render: ( args ) => ( +
+ + + + +
+ ), +}; + +/** The status indicator shows only on a dimmed avatar. */ +export const WithStatusIndicator: Story = { + args: { dimmed: true, statusIndicator: linkOff }, + render: ( args ) => ( +
+ + +
+ ), +}; diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss new file mode 100644 index 00000000000000..c6cf70d3c9c804 --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss @@ -0,0 +1,31 @@ +// The wrapper keeps the status indicator out of the dimmed avatar, so the +// indicator stays at full opacity. +.root { + position: relative; + display: inline-flex; + flex-shrink: 0; + vertical-align: middle; +} + +// Without its color, the avatar uses the neutral fill and ring of the theme. +// The filter turns the image gray too. +.dimmed { + opacity: 0.5; + filter: grayscale(1); +} + +.status-indicator { + position: absolute; + inset: 0; + display: flex; + align-items: center; + justify-content: center; + color: var(--wpds-color-foreground-content-neutral); + fill: currentColor; + pointer-events: none; + + svg { + width: 75%; + height: 75%; + } +} diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx new file mode 100644 index 00000000000000..a7a8423d225ab8 --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -0,0 +1,79 @@ +import { describe, expect, it } from 'vitest'; +import { screen } from '@testing-library/react'; +import { render } from 'vitest-browser-react'; +import '@wordpress/theme/design-tokens.css'; +import CollaboratorAvatar from '..'; + +const indicator = ; + +describe( 'CollaboratorAvatar', () => { + it( 'renders the Avatar at full opacity by default', async () => { + await render( + + ); + const avatar = screen.getByRole( 'img', { name: 'Alex Morgan' } ); + + expect( getComputedStyle( avatar ).opacity ).toBe( '1' ); + expect( getComputedStyle( avatar ).filter ).toBe( 'none' ); + expect( screen.queryByTestId( 'indicator' ) ).not.toBeInTheDocument(); + } ); + + it( 'fades a dimmed avatar, turns it gray and drops its color', async () => { + await render( + <> + + + + ); + const avatar = screen.getByRole( 'img', { name: 'Dimmed' } ); + const reference = getComputedStyle( screen.getByText( 'R' ) ); + const fallback = getComputedStyle( screen.getByText( 'D' ) ); + + expect( getComputedStyle( avatar ).opacity ).toBe( '0.5' ); + expect( getComputedStyle( avatar ).filter ).toBe( 'grayscale(1)' ); + expect( fallback.backgroundColor ).toBe( reference.backgroundColor ); + expect( fallback.color ).toBe( reference.color ); + } ); + + it( 'shows the status indicator over a dimmed avatar at full opacity', async () => { + await render( + + ); + const avatar = screen.getByRole( 'img', { name: 'Alex Morgan' } ); + const indicatorElement = screen.getByTestId( 'indicator' ); + const avatarRect = avatar.getBoundingClientRect(); + const indicatorRect = indicatorElement.getBoundingClientRect(); + + expect( avatar ).not.toContainElement( indicatorElement ); + expect( getComputedStyle( indicatorElement ).opacity ).toBe( '1' ); + expect( indicatorRect.left + indicatorRect.width / 2 ).toBeCloseTo( + avatarRect.left + avatarRect.width / 2 + ); + expect( indicatorRect.top + indicatorRect.height / 2 ).toBeCloseTo( + avatarRect.top + avatarRect.height / 2 + ); + } ); + + it( 'passes other props to the Avatar', async () => { + await render( + + ); + const avatar = screen.getByTestId( 'avatar' ); + + expect( avatar ).toHaveAccessibleName( 'Alex Morgan' ); + expect( avatar ).toHaveClass( 'custom' ); + } ); +} ); From 046b9a7e63eb69bfc6736689fa83f762dcc7dfa2 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 3 Dec 2026 11:04:32 +0100 Subject: [PATCH 2/5] Editor: Hide the collaborator status indicator from assistive technology The status indicator is decorative, but custom JSX could add its text to the accessible name of a surrounding control. Also check that an avatar that isn't dimmed keeps its color. --- .../collaborator-avatar/index.tsx | 5 ++++- .../test/index.browser.test.tsx | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx index 252c3f20559b64..21969836e14d08 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx @@ -47,7 +47,10 @@ export default function CollaboratorAvatar( { className={ clsx( className, dimmed && styles.dimmed ) } /> { dimmed && !! statusIndicator && ( - + ) } diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx index a7a8423d225ab8..ce6f367364694c 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -26,19 +26,39 @@ describe( 'CollaboratorAvatar', () => { await render( <> + ); const avatar = screen.getByRole( 'img', { name: 'Dimmed' } ); const reference = getComputedStyle( screen.getByText( 'R' ) ); + const colored = getComputedStyle( screen.getByText( 'C' ) ); const fallback = getComputedStyle( screen.getByText( 'D' ) ); expect( getComputedStyle( avatar ).opacity ).toBe( '0.5' ); expect( getComputedStyle( avatar ).filter ).toBe( 'grayscale(1)' ); + expect( colored.backgroundColor ).not.toBe( reference.backgroundColor ); expect( fallback.backgroundColor ).toBe( reference.backgroundColor ); expect( fallback.color ).toBe( reference.color ); } ); + it( 'keeps the status indicator out of the accessible name', async () => { + await render( + + ); + + expect( screen.getByText( 'Offline' ) ).toBeVisible(); + expect( screen.getByRole( 'button' ) ).toHaveAccessibleName( + 'Alex Morgan' + ); + } ); + it( 'shows the status indicator over a dimmed avatar at full opacity', async () => { await render( Date: Thu, 3 Dec 2026 11:09:09 +0100 Subject: [PATCH 3/5] Editor: Check every ancestor of the collaborator status indicator for opacity Opacity fades a whole group, so the indicator's own opacity doesn't show whether it is faded. --- .../collaborator-avatar/test/index.browser.test.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx index ce6f367364694c..74fa80f9a2d08a 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -73,7 +73,13 @@ describe( 'CollaboratorAvatar', () => { const indicatorRect = indicatorElement.getBoundingClientRect(); expect( avatar ).not.toContainElement( indicatorElement ); - expect( getComputedStyle( indicatorElement ).opacity ).toBe( '1' ); + // Opacity applies to a whole group, so check every ancestor too. + let element: Element | null = indicatorElement; + while ( element ) { + expect( getComputedStyle( element ).opacity ).toBe( '1' ); + // eslint-disable-next-line testing-library/no-node-access -- No query reaches the ancestors, which can fade the indicator. + element = element.parentElement; + } expect( indicatorRect.left + indicatorRect.width / 2 ).toBeCloseTo( avatarRect.left + avatarRect.width / 2 ); From a6b732435de68caf9abf115b04a491d728c6c60f Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 3 Dec 2026 11:13:10 +0100 Subject: [PATCH 4/5] Editor: Keep the collaborator status indicator centered in stretching containers A flex or grid container stretched the wrapper, but not the avatar inside it, so the indicator centered on the wrapper instead of the avatar. Keep the wrapper at the size of the avatar. --- .../collaborator-avatar/style.module.scss | 6 +++++- .../collaborator-avatar/test/index.browser.test.tsx | 13 ++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss index c6cf70d3c9c804..54567896e5e1e9 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss @@ -1,9 +1,13 @@ // The wrapper keeps the status indicator out of the dimmed avatar, so the -// indicator stays at full opacity. +// indicator stays at full opacity. It keeps the size of the avatar, even in +// a flex or grid container that stretches it, so the indicator stays +// centered on the avatar. .root { position: relative; display: inline-flex; flex-shrink: 0; + inline-size: fit-content; + block-size: fit-content; vertical-align: middle; } diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx index 74fa80f9a2d08a..cca7844f19c882 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -60,12 +60,15 @@ describe( 'CollaboratorAvatar', () => { } ); it( 'shows the status indicator over a dimmed avatar at full opacity', async () => { + // A grid cell stretches its item in both directions by default. await render( - +
+ +
); const avatar = screen.getByRole( 'img', { name: 'Alex Morgan' } ); const indicatorElement = screen.getByTestId( 'indicator' ); From d4dd04b99e72fcaa1194e0c0f78ef71000e05ed9 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 3 Dec 2026 11:20:38 +0100 Subject: [PATCH 5/5] Editor: Drop the status indicator from the collaborator avatar No caller has ever passed `statusIndicator`, to this wrapper or to the editor Avatar it comes from. Without it, the wrapper returns the ui Avatar directly, with no extra element to position or size. --- .../collaborator-avatar/index.tsx | 32 +++------- .../stories/index.story.tsx | 16 ----- .../collaborator-avatar/style.module.scss | 29 --------- .../test/index.browser.test.tsx | 60 +------------------ 4 files changed, 11 insertions(+), 126 deletions(-) diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx index 21969836e14d08..3aa2a478a6b2ec 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx @@ -1,5 +1,4 @@ import clsx from 'clsx'; -import { Icon as WCIcon, type IconType } from '@wordpress/components'; import { // eslint-disable-next-line @wordpress/use-recommended-components -- `Avatar` is marked "use with caution" while its API settles; the collaborator UI adopts it ahead of that, see #87841. Avatar, @@ -12,21 +11,17 @@ export type CollaboratorAvatarProps = React.ComponentPropsWithoutRef< /** * Whether to dim the avatar to show that the collaborator is inactive, * for example after they disconnect. The avatar drops its color, the image - * turns gray, and the avatar fades. + * turns gray, and the avatar fades. Describe the inactive state in nearby + * text too. * * @default false */ dimmed?: boolean; - /** - * An icon shown over the center of a dimmed avatar, at full opacity. - * It is decorative: describe the inactive state in nearby text. - */ - statusIndicator?: IconType | null; }; /** * The `@wordpress/ui` `Avatar` with an inactive state for collaborators. - * Props other than `dimmed` and `statusIndicator` go to the `Avatar`. + * Props other than `dimmed` go to the `Avatar`. * * ```jsx * @@ -34,26 +29,15 @@ export type CollaboratorAvatarProps = React.ComponentPropsWithoutRef< */ export default function CollaboratorAvatar( { dimmed = false, - statusIndicator, color, className, ...props }: CollaboratorAvatarProps ) { return ( - - - { dimmed && !! statusIndicator && ( - - ) } - + ); } diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx index 86072d3485e2cd..ef5bcdd8774379 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx @@ -1,5 +1,4 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; -import { linkOff } from '@wordpress/icons'; import CollaboratorAvatar from '..'; // Served from `storybook/static`. @@ -8,16 +7,12 @@ const IMAGE_SRC = 'profile-picture.svg'; /** * The `@wordpress/ui` `Avatar` with an inactive state for collaborators who * have disconnected. A dimmed avatar drops its color, turns gray and fades. - * An optional status indicator shows over it at full opacity. */ const meta: Meta< typeof CollaboratorAvatar > = { id: 'editor-collaboratoravatar', title: 'Editor/Collaborator Avatar', component: CollaboratorAvatar, args: { name: 'Alex Morgan', color: '#008a20' }, - argTypes: { - statusIndicator: { control: false }, - }, }; export default meta; @@ -38,14 +33,3 @@ export const Dimmed: Story = { ), }; - -/** The status indicator shows only on a dimmed avatar. */ -export const WithStatusIndicator: Story = { - args: { dimmed: true, statusIndicator: linkOff }, - render: ( args ) => ( -
- - -
- ), -}; diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss index 54567896e5e1e9..6de2117529c717 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss @@ -1,35 +1,6 @@ -// The wrapper keeps the status indicator out of the dimmed avatar, so the -// indicator stays at full opacity. It keeps the size of the avatar, even in -// a flex or grid container that stretches it, so the indicator stays -// centered on the avatar. -.root { - position: relative; - display: inline-flex; - flex-shrink: 0; - inline-size: fit-content; - block-size: fit-content; - vertical-align: middle; -} - // Without its color, the avatar uses the neutral fill and ring of the theme. // The filter turns the image gray too. .dimmed { opacity: 0.5; filter: grayscale(1); } - -.status-indicator { - position: absolute; - inset: 0; - display: flex; - align-items: center; - justify-content: center; - color: var(--wpds-color-foreground-content-neutral); - fill: currentColor; - pointer-events: none; - - svg { - width: 75%; - height: 75%; - } -} diff --git a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx index cca7844f19c882..8e0fbcaff1acbc 100644 --- a/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -4,22 +4,15 @@ import { render } from 'vitest-browser-react'; import '@wordpress/theme/design-tokens.css'; import CollaboratorAvatar from '..'; -const indicator = ; - describe( 'CollaboratorAvatar', () => { it( 'renders the Avatar at full opacity by default', async () => { await render( - + ); const avatar = screen.getByRole( 'img', { name: 'Alex Morgan' } ); expect( getComputedStyle( avatar ).opacity ).toBe( '1' ); expect( getComputedStyle( avatar ).filter ).toBe( 'none' ); - expect( screen.queryByTestId( 'indicator' ) ).not.toBeInTheDocument(); } ); it( 'fades a dimmed avatar, turns it gray and drops its color', async () => { @@ -42,55 +35,6 @@ describe( 'CollaboratorAvatar', () => { expect( fallback.color ).toBe( reference.color ); } ); - it( 'keeps the status indicator out of the accessible name', async () => { - await render( - - ); - - expect( screen.getByText( 'Offline' ) ).toBeVisible(); - expect( screen.getByRole( 'button' ) ).toHaveAccessibleName( - 'Alex Morgan' - ); - } ); - - it( 'shows the status indicator over a dimmed avatar at full opacity', async () => { - // A grid cell stretches its item in both directions by default. - await render( -
- -
- ); - const avatar = screen.getByRole( 'img', { name: 'Alex Morgan' } ); - const indicatorElement = screen.getByTestId( 'indicator' ); - const avatarRect = avatar.getBoundingClientRect(); - const indicatorRect = indicatorElement.getBoundingClientRect(); - - expect( avatar ).not.toContainElement( indicatorElement ); - // Opacity applies to a whole group, so check every ancestor too. - let element: Element | null = indicatorElement; - while ( element ) { - expect( getComputedStyle( element ).opacity ).toBe( '1' ); - // eslint-disable-next-line testing-library/no-node-access -- No query reaches the ancestors, which can fade the indicator. - element = element.parentElement; - } - expect( indicatorRect.left + indicatorRect.width / 2 ).toBeCloseTo( - avatarRect.left + avatarRect.width / 2 - ); - expect( indicatorRect.top + indicatorRect.height / 2 ).toBeCloseTo( - avatarRect.top + avatarRect.height / 2 - ); - } ); - it( 'passes other props to the Avatar', async () => { await render( { className="custom" data-testid="avatar" size="sm" + dimmed /> ); const avatar = screen.getByTestId( 'avatar' ); expect( avatar ).toHaveAccessibleName( 'Alex Morgan' ); expect( avatar ).toHaveClass( 'custom' ); + expect( getComputedStyle( avatar ).opacity ).toBe( '0.5' ); } ); } );