Skip to content

feat(site): improve icon compatibility across themes #11457

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

Merged
merged 8 commits into from
Jan 8, 2024
Merged
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
6 changes: 3 additions & 3 deletions examples/examples.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "",
"name": "AWS EC2 (Devcontainer)",
"description": "Provision AWS EC2 VMs with a devcontainer as Coder workspaces",
"icon": "/icon/aws.png",
"icon": "/icon/aws.svg",
"tags": [
"vm",
"linux",
Expand All @@ -20,7 +20,7 @@
"url": "",
"name": "AWS EC2 (Linux)",
"description": "Provision AWS EC2 VMs as Coder workspaces",
"icon": "/icon/aws.png",
"icon": "/icon/aws.svg",
"tags": [
"vm",
"linux",
Expand All @@ -34,7 +34,7 @@
"url": "",
"name": "AWS EC2 (Windows)",
"description": "Provision AWS EC2 VMs as Coder workspaces",
"icon": "/icon/aws.png",
"icon": "/icon/aws.svg",
"tags": [
"vm",
"windows",
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/aws-devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
display_name: AWS EC2 (Devcontainer)
description: Provision AWS EC2 VMs with a devcontainer as Coder workspaces
icon: ../../../site/static/icon/aws.png
icon: ../../../site/static/icon/aws.svg
maintainer_github: coder
verified: true
tags: [vm, linux, aws, persistent, devcontainer]
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/aws-linux/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
display_name: AWS EC2 (Linux)
description: Provision AWS EC2 VMs as Coder workspaces
icon: ../../../site/static/icon/aws.png
icon: ../../../site/static/icon/aws.svg
maintainer_github: coder
verified: true
tags: [vm, linux, aws, persistent-vm]
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/aws-windows/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
display_name: AWS EC2 (Windows)
description: Provision AWS EC2 VMs as Coder workspaces
icon: ../../../site/static/icon/aws.png
icon: ../../../site/static/icon/aws.svg
maintainer_github: coder
verified: true
tags: [vm, windows, aws]
Expand Down
8 changes: 1 addition & 7 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import WorkspacesPage from "./pages/WorkspacesPage/WorkspacesPage";
import UserSettingsLayout from "./pages/UserSettingsPage/Layout";
import { TemplateSettingsLayout } from "./pages/TemplateSettingsPage/TemplateSettingsLayout";
import { WorkspaceSettingsLayout } from "./pages/WorkspaceSettingsPage/WorkspaceSettingsLayout";
import { ThemeOverride } from "contexts/ThemeProvider";
import themes from "theme";

// Lazy load pages
// - Pages that are secondary, not in the main navigation or not usually accessed
Expand Down Expand Up @@ -421,11 +419,7 @@ export const AppRouter: FC = () => {
/>
<Route
path="/:username/:workspace/terminal"
element={
<ThemeOverride theme={themes.dark}>
<TerminalPage />
</ThemeOverride>
}
element={<TerminalPage />}
/>
<Route path="/cli-auth" element={<CliAuthenticationPage />} />
<Route path="/icons" element={<IconsPage />} />
Expand Down
2 changes: 1 addition & 1 deletion site/src/__mocks__/react-markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, PropsWithChildren } from "react";

const ReactMarkdown: FC<PropsWithChildren<unknown>> = ({ children }) => {
const ReactMarkdown: FC<PropsWithChildren> = ({ children }) => {
return <div data-testid="markdown">{children}</div>;
};

Expand Down
1 change: 1 addition & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const me = (): UseQueryOptions<User> & {
queryKey: meKey,
initialData: initialUserData,
queryFn: API.getAuthenticatedUser,
refetchOnWindowFocus: true,
};
};

Expand Down
14 changes: 13 additions & 1 deletion site/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import MuiAvatar, {
type AvatarProps as MuiAvatarProps,
} from "@mui/material/Avatar";
import { type FC, useId } from "react";
import { css, type Interpolation, type Theme } from "@emotion/react";
import { css, type Interpolation, type Theme, useTheme } from "@emotion/react";
import { visuallyHidden } from "@mui/utils";
import { getExternalImageStylesFromUrl } from "theme/externalImages";

export type AvatarProps = MuiAvatarProps & {
size?: "xs" | "sm" | "md" | "xl";
Expand Down Expand Up @@ -67,6 +68,17 @@ export const Avatar: FC<AvatarProps> = ({
);
};

export const ExternalAvatar: FC<AvatarProps> = (props) => {
const theme = useTheme();

return (
<Avatar
css={getExternalImageStylesFromUrl(theme.externalImages, props.src)}
{...props}
/>
);
};

type AvatarIconProps = {
src: string;
alt: string;
Expand Down
8 changes: 4 additions & 4 deletions site/src/components/AvatarCard/AvatarCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactNode } from "react";
import { type FC, type ReactNode } from "react";
import { Avatar } from "components/Avatar/Avatar";
import { type CSSObject, useTheme } from "@emotion/react";

Expand All @@ -12,14 +12,14 @@ type AvatarCardProps = {
maxWidth?: number | "none";
};

export function AvatarCard({
export const AvatarCard: FC<AvatarCardProps> = ({
header,
imgUrl,
altText,
background,
subtitle,
maxWidth = "none",
}: AvatarCardProps) {
}) => {
const theme = useTheme();

return (
Expand Down Expand Up @@ -77,4 +77,4 @@ export function AvatarCard({
</Avatar>
</div>
);
}
};
36 changes: 0 additions & 36 deletions site/src/components/ExternalIcon/ExternalIcon.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions site/src/components/ExternalImage/ExternalImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useTheme } from "@emotion/react";
import { type ImgHTMLAttributes, forwardRef } from "react";
import { getExternalImageStylesFromUrl } from "theme/externalImages";

export const ExternalImage = forwardRef<
HTMLImageElement,
ImgHTMLAttributes<HTMLImageElement>
>((attrs, ref) => {
const theme = useTheme();

return (
<img
ref={ref}
alt=""
css={getExternalImageStylesFromUrl(theme.externalImages, attrs.src)}
{...attrs}
/>
);
});
22 changes: 11 additions & 11 deletions site/src/components/FullPageLayout/Topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { css } from "@emotion/css";
import Button, { ButtonProps } from "@mui/material/Button";
import IconButton, { IconButtonProps } from "@mui/material/IconButton";
import { useTheme } from "@mui/material/styles";
import { Avatar, AvatarProps } from "components/Avatar/Avatar";
import { AvatarProps, ExternalAvatar } from "components/Avatar/Avatar";
import {
ForwardedRef,
HTMLAttributes,
PropsWithChildren,
ReactElement,
type FC,
type ForwardedRef,
type HTMLAttributes,
type ReactElement,
cloneElement,
forwardRef,
} from "react";

export const Topbar = (props: HTMLAttributes<HTMLDivElement>) => {
export const Topbar: FC<HTMLAttributes<HTMLElement>> = (props) => {
const theme = useTheme();

return (
Expand Down Expand Up @@ -70,7 +70,7 @@ export const TopbarButton = forwardRef<HTMLButtonElement, ButtonProps>(
},
);

export const TopbarData = (props: HTMLAttributes<HTMLDivElement>) => {
export const TopbarData: FC<HTMLAttributes<HTMLDivElement>> = (props) => {
return (
<div
{...props}
Expand All @@ -84,7 +84,7 @@ export const TopbarData = (props: HTMLAttributes<HTMLDivElement>) => {
);
};

export const TopbarDivider = (props: HTMLAttributes<HTMLSpanElement>) => {
export const TopbarDivider: FC<HTMLAttributes<HTMLSpanElement>> = (props) => {
const theme = useTheme();
return (
<span {...props} css={{ color: theme.palette.divider }}>
Expand All @@ -93,9 +93,9 @@ export const TopbarDivider = (props: HTMLAttributes<HTMLSpanElement>) => {
);
};

export const TopbarAvatar = (props: AvatarProps) => {
export const TopbarAvatar: FC<AvatarProps> = (props) => {
return (
<Avatar
<ExternalAvatar
{...props}
variant="square"
fitImage
Expand All @@ -104,7 +104,7 @@ export const TopbarAvatar = (props: AvatarProps) => {
);
};

type TopbarIconProps = PropsWithChildren<HTMLAttributes<HTMLOrSVGElement>>;
type TopbarIconProps = HTMLAttributes<HTMLOrSVGElement>;

export const TopbarIcon = forwardRef<HTMLOrSVGElement, TopbarIconProps>(
(props: TopbarIconProps, ref) => {
Expand Down
2 changes: 1 addition & 1 deletion site/src/components/GroupAvatar/GroupAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const GroupAvatar: FC<GroupAvatarProps> = ({ name, avatarURL }) => {
badgeContent={<Group />}
classes={{ badge }}
>
<Avatar src={avatarURL} background>
<Avatar background src={avatarURL}>
{name}
</Avatar>
</Badge>
Expand Down
3 changes: 2 additions & 1 deletion site/src/components/IconField/IconField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PopoverContent,
PopoverTrigger,
} from "components/Popover/Popover";
import { ExternalImage } from "components/ExternalImage/ExternalImage";

// See: https://github.com/missive/emoji-mart/issues/51#issuecomment-287353222
const urlFromUnifiedCode = (unified: string) =>
Expand Down Expand Up @@ -60,7 +61,7 @@ export const IconField: FC<IconFieldProps> = ({
},
}}
>
<img
<ExternalImage
alt=""
src={textFieldProps.value}
// This prevent browser to display the ugly error icon if the
Expand Down
16 changes: 13 additions & 3 deletions site/src/components/Resources/ResourceAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { type FC } from "react";
import { type FC, useId } from "react";
import { visuallyHidden } from "@mui/utils";
import type { WorkspaceResource } from "api/typesGenerated";
import { Avatar, AvatarIcon } from "components/Avatar/Avatar";
import { Avatar } from "components/Avatar/Avatar";
import { ExternalImage } from "components/ExternalImage/ExternalImage";
import { getResourceIconPath } from "utils/workspace";

export type ResourceAvatarProps = { resource: WorkspaceResource };

export const ResourceAvatar: FC<ResourceAvatarProps> = ({ resource }) => {
const avatarSrc = resource.icon || getResourceIconPath(resource.type);
const altId = useId();

return (
<Avatar background>
<AvatarIcon src={avatarSrc} alt={resource.name} />
<ExternalImage
src={avatarSrc}
css={{ maxWidth: "50%" }}
aria-labelledby={altId}
/>
<div id={altId} css={{ ...visuallyHidden }}>
{resource.name}
</div>
</Avatar>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const Options: Story = {
name: "Third option",
value: "third_option",
description: "",
icon: "/icon/aws.png",
icon: "/icon/aws.svg",
},
],
}),
Expand Down Expand Up @@ -138,7 +138,7 @@ Very big.

> Wow, that description is straight up large. –Some guy, probably
`,
icon: "/icon/aws.png",
icon: "/icon/aws.svg",
},
],
}),
Expand Down
5 changes: 3 additions & 2 deletions site/src/components/RichParameterInput/RichParameterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TemplateVersionParameter } from "api/typesGenerated";
import { MemoizedMarkdown } from "components/Markdown/Markdown";
import { Stack } from "components/Stack/Stack";
import { MultiTextField } from "./MultiTextField";
import { ExternalImage } from "components/ExternalImage/ExternalImage";

const isBoolean = (parameter: TemplateVersionParameter) => {
return parameter.type === "bool";
Expand Down Expand Up @@ -106,7 +107,7 @@ const ParameterLabel: FC<ParameterLabelProps> = ({ parameter }) => {
<Stack direction="row" alignItems="center">
{parameter.icon && (
<span css={styles.labelIconWrapper}>
<img
<ExternalImage
css={styles.labelIcon}
alt="Parameter icon"
src={parameter.icon}
Expand Down Expand Up @@ -213,7 +214,7 @@ const RichParameterField: FC<RichParameterInputProps> = ({
label={
<Stack direction="row" alignItems="center">
{option.icon && (
<img
<ExternalImage
css={styles.optionIcon}
src={option.icon}
alt="Parameter icon"
Expand Down
Loading