Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -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
* <CollaboratorAvatar name="Alex Morgan" color="#008a20" dimmed />
* ```
*/
export default function CollaboratorAvatar( {
dimmed = false,
color,
className,
...props
}: CollaboratorAvatarProps ) {
return (
<Avatar
{ ...props }
color={ dimmed ? undefined : color }
className={ clsx( className, dimmed && styles.dimmed ) }
/>
);
}
Original file line number Diff line number Diff line change
@@ -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 ) => (
<div style={ { display: 'flex', gap: 16, alignItems: 'center' } }>
<CollaboratorAvatar { ...args } active />
<CollaboratorAvatar { ...args } dimmed />
<CollaboratorAvatar { ...args } src={ IMAGE_SRC } active />
<CollaboratorAvatar { ...args } src={ IMAGE_SRC } dimmed />
</div>
),
};
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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(
<CollaboratorAvatar name="Alex Morgan" color="#008a20" />
);
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(
<>
<CollaboratorAvatar name="Reference" />
<CollaboratorAvatar name="Colored" color="#008a20" />
<CollaboratorAvatar name="Dimmed" color="#008a20" dimmed />
</>
);
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(
<CollaboratorAvatar
name="Alex Morgan"
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' );
} );
} );
Loading