Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Change avatar setting component to use a menu #12585

Merged
merged 58 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
06896dc
New user profile UI in User Settings
dbkr May 21, 2024
f16cb80
Show avatar upload error
dbkr May 22, 2024
1b60ea8
Fix avatar upload error
dbkr May 23, 2024
d0624af
Wire up errors & feedback for display name setting
dbkr May 24, 2024
ac46104
Implement avatar upload / remove progress toast
dbkr May 24, 2024
a01d94a
Add 768px breakpoint
dbkr May 24, 2024
5da2969
Fix display of no avatar in avatar setting controls
dbkr May 28, 2024
236de62
Fix room profile display
dbkr May 28, 2024
aba6a6a
Update edit icon on avatarsetting comnponent
dbkr May 28, 2024
8e0b017
Change avatarsetting componment to use a menu
dbkr May 30, 2024
6786697
Update to released compund-web with required components / fixes
dbkr May 30, 2024
237570e
Require compound-web 4.4.0
dbkr May 30, 2024
500fb3a
Update snapshots
dbkr May 30, 2024
f0c9d83
Fix duplicate import
dbkr May 30, 2024
513687c
Fix CSS comment
dbkr May 30, 2024
c93ab3d
Update snapshot
dbkr May 30, 2024
be18de1
Run all the tests so the ids stay the same
dbkr May 30, 2024
7330b34
Start of a test for ProfileSettings
dbkr May 30, 2024
b244866
More tests
dbkr May 30, 2024
137df66
Test that a toast appears
dbkr May 30, 2024
48a3d39
Test ToastRack
dbkr May 30, 2024
a99dced
Update snapshots
dbkr Jun 4, 2024
605555a
Add the usernamee control
dbkr Jun 4, 2024
0f56520
Fix playwright tests
dbkr Jun 5, 2024
96802a7
Put ^ back on compound-web version
dbkr Jun 5, 2024
304836d
Split CSS for room & user profile settings
dbkr Jun 5, 2024
d97d8d9
Fix playwright test
dbkr Jun 5, 2024
6a8a981
Update room settings screenshot
dbkr Jun 5, 2024
b2a7f8a
Use original screenshot instead
dbkr Jun 5, 2024
41bee22
Merge branch 'dbkr/new_profilesettings' into dbkr/fix_blank_avatar_di…
dbkr Jun 5, 2024
072681d
Merge branch 'develop' into dbkr/new_profilesettings
dbkr Jun 5, 2024
85b2266
Merge branch 'dbkr/new_profilesettings' into dbkr/fix_blank_avatar_di…
dbkr Jun 5, 2024
8d05f12
Add required props in test
dbkr Jun 5, 2024
54cb655
Fix test
dbkr Jun 5, 2024
3bd2aa1
Also here
dbkr Jun 5, 2024
6082f97
Update screenshots
dbkr Jun 5, 2024
52fbe95
Remove user icon
dbkr Jun 5, 2024
f0c6f94
Fix styling of unrelated buttons
dbkr Jun 5, 2024
ecb6730
Add copyright year
dbkr Jun 5, 2024
672d7cf
Fix copyright year
dbkr Jun 5, 2024
f72d7e7
Merge remote-tracking branch 'origin/develop' into dbkr/new_profilese…
dbkr Jun 6, 2024
145fc75
Merge branch 'dbkr/new_profilesettings' into dbkr/fix_blank_avatar_di…
dbkr Jun 6, 2024
2d7672d
Merge branch 'develop' into dbkr/fix_blank_avatar_display
dbkr Jun 6, 2024
a40340f
Switch to useMatrixClientContext
dbkr Jun 6, 2024
7f08efd
Fix other test
dbkr Jun 6, 2024
b6a897b
Merge branch 'dbkr/new_profilesettings' into dbkr/avatarsetting_menu
dbkr Jun 6, 2024
78bc01a
Make clickable with no avatar again and fix tests
dbkr Jun 7, 2024
f935d12
Merge branch 'dbkr/fix_blank_avatar_display' into dbkr/avatarsetting_…
dbkr Jun 7, 2024
2f2297b
Merge remote-tracking branch 'origin/develop' into dbkr/avatarsetting…
dbkr Jun 7, 2024
a16f3f3
Put back missing CSS to make the menu entry red
dbkr Jun 7, 2024
112ec3f
Fix type error
dbkr Jun 7, 2024
d178f30
Fix tests
dbkr Jun 7, 2024
2829ab6
Supply open / onOpenChange props
dbkr Jun 7, 2024
76f7974
Fix tests
dbkr Jun 7, 2024
f6993fb
There is no hover anymore
dbkr Jun 7, 2024
39e23da
Use the computed name, not the name which may be null
dbkr Jun 7, 2024
cf36913
Fix room avatar remove behaviour
dbkr Jun 7, 2024
d14a7b0
Remove redundant else
dbkr Jun 7, 2024
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
27 changes: 9 additions & 18 deletions playwright/e2e/settings/general-user-settings-tab.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ test.describe("General user settings tab", () => {
// Assert that a userId is rendered
expect(uut.getByLabel("Username")).toHaveText(user.userId);

// Check avatar setting
const avatar = profile.locator(".mx_AvatarSetting_avatar");
await avatar.hover();

// Hover effect
await expect(avatar.locator(".mx_AvatarSetting_hoverBg")).toBeVisible();
await expect(avatar.locator(".mx_AvatarSetting_hover span").getByText("Upload")).toBeVisible();

// Wait until spinners disappear
await expect(uut.getByTestId("accountSection").locator(".mx_Spinner")).not.toBeVisible();
await expect(uut.getByTestId("discoverySection").locator(".mx_Spinner")).not.toBeVisible();
Expand Down Expand Up @@ -128,21 +120,20 @@ test.describe("General user settings tab", () => {
await expect(uut).toMatchScreenshot("general-smallscreen.png");
});

test("should support adding and removing a profile picture", async ({ uut }) => {
test("should support adding and removing a profile picture", async ({ uut, page }) => {
const profileSettings = uut.locator(".mx_UserProfileSettings");
// Upload a picture
await profileSettings.getByAltText("Upload").setInputFiles("playwright/sample-files/riot.png");

// Find and click "Remove" link button
await profileSettings
.locator(".mx_UserProfileSettings_profile")
.getByRole("button", { name: "Remove" })
.click();
// Image should be visible
await expect(profileSettings.locator(".mx_AvatarSetting_avatar img")).toBeVisible();

// Assert that the link button disappeared
await expect(
profileSettings.locator(".mx_AvatarSetting_avatar .mx_AccessibleButton_kind_link_sm"),
).not.toBeVisible();
// Open the menu & click remove
await profileSettings.getByRole("button", { name: "Profile Picture" }).click();
await page.getByRole("menuitem", { name: "Remove" }).click();

// Assert that the image disappeared
await expect(profileSettings.locator(".mx_AvatarSetting_avatar img")).not.toBeVisible();
});

test("should set a country calling code based on default_country_code", async ({ uut }) => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 15 additions & 44 deletions res/css/views/settings/_AvatarSetting.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,6 @@ limitations under the License.
margin-top: 8px;
position: relative;

.mx_AvatarSetting_hover {
transition: opacity var(--hover-transition);
opacity: 0;

/* position to place the hover bg over the entire thing */
position: absolute;
inset: 0;

pointer-events: none; /* let the pointer fall through the underlying thing */

line-height: 90px;
text-align: center;

> span {
color: $primary-content;
position: relative; /* tricks the layout engine into putting this on top of the bg */
font-weight: 500;
}

.mx_AvatarSetting_hoverBg {
/* absolute position to lazily fill the entire container */
position: absolute;
inset: 0;

opacity: 0.5;
background-color: $quinary-content;
border-radius: 90px;
}
}

&.mx_AvatarSetting_avatarDisplay:hover .mx_AvatarSetting_hover {
opacity: 1;
}
Expand All @@ -77,25 +47,26 @@ limitations under the License.
}

.mx_AvatarSetting_uploadButton {
width: 32px;
height: 32px;
width: 28px;
height: 28px;
border-radius: 32px;
background-color: $secondary-content;
border: 1px solid var(--cpd-color-bg-canvas-default);
background-color: var(--cpd-color-bg-subtle-primary);

position: absolute;
bottom: 0;
right: 0;
}
text-align: center;
cursor: pointer;

.mx_AvatarSetting_uploadButton::before {
content: "";
display: block;
width: 100%;
height: 100%;
mask-repeat: no-repeat;
mask-position: center;
mask-size: 55%;
background-color: $quinary-content;
mask-image: url("$(res)/img/feather-customised/edit.svg");
svg {
position: relative;
top: 3px;
}
}
}

.mx_AvatarSetting_removeMenuItem svg,
.mx_AvatarSetting_removeMenuItem span {
color: var(--cpd-color-text-critical-primary) !important;
}
8 changes: 6 additions & 2 deletions src/components/views/room_settings/RoomProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
);
}

const canRemove = this.state.profileFieldsTouched.avatar
? Boolean(this.state.avatarFile)
: Boolean(this.state.originalAvatarUrl);

return (
<form onSubmit={this.saveProfile} autoComplete="off" noValidate={true} className="mx_RoomProfileSettings">
<div className="mx_RoomProfileSettings_profile">
Expand Down Expand Up @@ -254,9 +258,9 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
avatarAltText={_t("room_settings|general|avatar_field_label")}
disabled={!this.state.canSetAvatar}
onChange={this.onAvatarChanged}
removeAvatar={this.removeAvatar}
removeAvatar={canRemove ? this.removeAvatar : undefined}
placeholderId={idNameForRoom(MatrixClientPeg.safeGet().getRoom(this.props.roomId)!)}
placeholderName={this.state.displayName}
placeholderName={MatrixClientPeg.safeGet().getRoom(this.props.roomId)!.name}
/>
</div>
{profileSettingsButtons}
Expand Down
105 changes: 70 additions & 35 deletions src/components/views/settings/AvatarSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,59 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { createRef, useCallback, useEffect, useState } from "react";
import React, { ReactNode, createRef, useCallback, useEffect, useState } from "react";
import { Icon as EditIcon } from "@vector-im/compound-design-tokens/icons/edit.svg";
import { Icon as UploadIcon } from "@vector-im/compound-design-tokens/icons/share.svg";
import { Icon as DeleteIcon } from "@vector-im/compound-design-tokens/icons/delete.svg";
import { Menu, MenuItem } from "@vector-im/compound-web";

import { _t } from "../../../languageHandler";
import AccessibleButton from "../elements/AccessibleButton";
import { mediaFromMxc } from "../../../customisations/Media";
import { chromeFileInputFix } from "../../../utils/BrowserWorkarounds";
import { useId } from "../../../utils/useId";
import AccessibleButton from "../elements/AccessibleButton";
import BaseAvatar from "../avatars/BaseAvatar";

interface MenuProps {
trigger: ReactNode;
onUploadSelect: () => void;
onRemoveSelect?: () => void;
}

const AvatarSettingContextMenu: React.FC<MenuProps> = ({ trigger, onUploadSelect, onRemoveSelect }) => {
const [menuOpen, setMenuOpen] = useState(false);

const onOpenChange = useCallback((newOpen: boolean) => {
setMenuOpen(newOpen);
}, []);

return (
<Menu
trigger={trigger}
title={_t("action|set_avatar")}
showTitle={false}
open={menuOpen}
onOpenChange={onOpenChange}
>
<MenuItem
as="div"
Icon={<UploadIcon width="24px" height="24px" />}
label={_t("action|upload_file")}
onSelect={onUploadSelect}
/>
{onRemoveSelect && (
<MenuItem
as="div"
Icon={<DeleteIcon width="24px" height="24px" />}
className="mx_AvatarSetting_removeMenuItem"
label={_t("action|remove")}
onSelect={onRemoveSelect}
/>
)}
</Menu>
);
};

interface IProps {
/**
* The current value of the avatar URL, as an mxc URL or a File.
Expand Down Expand Up @@ -136,48 +180,39 @@ const AvatarSetting: React.FC<IProps> = ({
}

let uploadAvatarBtn: JSX.Element | undefined;
if (uploadAvatar) {
// insert an empty div to be the host for a css mask containing the upload.svg
if (!disabled) {
uploadAvatarBtn = (
<>
<AccessibleButton
onClick={uploadAvatar}
className="mx_AvatarSetting_uploadButton"
aria-labelledby={a11yId}
/>
<input
type="file"
style={{ display: "none" }}
ref={fileInputRef}
onClick={chromeFileInputFix}
onChange={onFileChanged}
accept="image/*"
alt={_t("action|upload")}
/>
</>
);
}

let removeAvatarBtn: JSX.Element | undefined;
if (avatarURL && removeAvatar && !disabled) {
removeAvatarBtn = (
<AccessibleButton onClick={removeAvatar} kind="link_sm">
{_t("action|remove")}
</AccessibleButton>
<div className="mx_AvatarSetting_uploadButton">
<EditIcon width="20px" height="20px" />
</div>
);
}

return (
const content = (
<div className="mx_AvatarSetting_avatar" role="group" aria-label={avatarAltText}>
{avatarElement}
<div className="mx_AvatarSetting_hover" aria-hidden="true">
<div className="mx_AvatarSetting_hoverBg" />
{!disabled && <span id={a11yId}>{_t("action|upload")}</span>}
</div>
{uploadAvatarBtn}
{removeAvatarBtn}
</div>
);

if (disabled) {
return content;
}

return (
<>
<AvatarSettingContextMenu trigger={content} onUploadSelect={uploadAvatar} onRemoveSelect={removeAvatar} />
<input
type="file"
style={{ display: "none" }}
ref={fileInputRef}
onClick={chromeFileInputFix}
onChange={onFileChanged}
accept="image/*"
alt={_t("action|upload")}
/>
</>
);
};

export default AvatarSetting;
2 changes: 1 addition & 1 deletion src/components/views/settings/UserProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const UserProfileSettings: React.FC = () => {
avatar={avatarURL ?? undefined}
avatarAltText={_t("common|user_avatar")}
onChange={onAvatarChange}
removeAvatar={onAvatarRemove}
removeAvatar={avatarURL ? onAvatarRemove : undefined}
placeholderName={displayName}
placeholderId={client.getUserId() ?? ""}
/>
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"save": "Save",
"search": "Search",
"send_report": "Send report",
"set_avatar": "Set profile picture",
"share": "Share",
"show": "Show",
"show_advanced": "Show advanced",
Expand All @@ -132,6 +133,7 @@
"update": "Update",
"upgrade": "Upgrade",
"upload": "Upload",
"upload_file": "Upload file",
"verify": "Verify",
"view": "View",
"view_all": "View all",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describe("RoomProfileSetting", () => {

render(<RoomProfileSettings roomId="!floob:itty" />);

await user.click(screen.getByRole("button", { name: "Remove" }));
await user.click(screen.getByRole("button", { name: "Room avatar" }));
await user.click(screen.getByRole("menuitem", { name: "Remove" }));
await user.click(screen.getByRole("button", { name: "Save" }));

await waitFor(() =>
Expand Down
32 changes: 0 additions & 32 deletions test/components/views/settings/AvatarSetting-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,6 @@ describe("<AvatarSetting />", () => {
expect(imgElement).toBeInTheDocument();
});

it("renders avatar with remove button", async () => {
const { queryByText } = render(
<AvatarSetting
avatarAltText="Avatar of Peter Fox"
avatar="mxc://example.org/my-avatar"
removeAvatar={jest.fn()}
placeholderId="blee"
placeholderName="boo"
/>,
);

const removeButton = queryByText("Remove");
expect(removeButton).toBeInTheDocument();
});

it("renders avatar without remove button", async () => {
const { queryByText } = render(
<AvatarSetting
placeholderId="blee"
placeholderName="boo"
disabled={true}
avatarAltText="Avatar of Peter Fox"
/>,
);

const removeButton = queryByText("Remove");
expect(removeButton).toBeNull();
});

it("renders a file as the avatar when supplied", async () => {
render(
<AvatarSetting
Expand Down Expand Up @@ -102,9 +73,6 @@ describe("<AvatarSetting />", () => {
/>,
);

// not really necessary, but to follow the expected user flow as much as possible
await user.click(screen.getByRole("button", { name: "Avatar of Peter Fox" }));

const fileInput = screen.getByAltText("Upload");
await user.upload(fileInput, AVATAR_FILE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe("ProfileSettings", () => {
});

it("removes avatar", async () => {
jest.spyOn(OwnProfileStore.instance, "avatarMxc", "get").mockReturnValue("mxc://example.org/my-avatar");
renderProfileSettings(toastRack, client);

expect(await screen.findByText("Mocked AvatarSetting")).toBeInTheDocument();
Expand Down
Loading