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..3aa2a478a6b2ec --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/index.tsx @@ -0,0 +1,43 @@ +import clsx from 'clsx'; +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. Describe the inactive state in nearby + * text too. + * + * @default false + */ + dimmed?: boolean; +}; + +/** + * The `@wordpress/ui` `Avatar` with an inactive state for collaborators. + * Props other than `dimmed` go to the `Avatar`. + * + * ```jsx + * + * ``` + */ +export default function CollaboratorAvatar( { + dimmed = false, + color, + className, + ...props +}: CollaboratorAvatarProps ) { + return ( + + ); +} 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..ef5bcdd8774379 --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/stories/index.story.tsx @@ -0,0 +1,35 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; +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. + */ +const meta: Meta< typeof CollaboratorAvatar > = { + id: 'editor-collaboratoravatar', + title: 'Editor/Collaborator Avatar', + component: CollaboratorAvatar, + args: { name: 'Alex Morgan', color: '#008a20' }, +}; +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 ) => ( +
+ + + + +
+ ), +}; 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..6de2117529c717 --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/style.module.scss @@ -0,0 +1,6 @@ +// 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); +} 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..8e0fbcaff1acbc --- /dev/null +++ b/packages/editor/src/components/collaborators-presence/collaborator-avatar/test/index.browser.test.tsx @@ -0,0 +1,54 @@ +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 '..'; + +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' ); + } ); + + 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 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( 'passes other props to the Avatar', async () => { + await render( + + ); + const avatar = screen.getByTestId( 'avatar' ); + + expect( avatar ).toHaveAccessibleName( 'Alex Morgan' ); + expect( avatar ).toHaveClass( 'custom' ); + expect( getComputedStyle( avatar ).opacity ).toBe( '0.5' ); + } ); +} );