Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: update the navbar to match the new designs #15964

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 15 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
8 changes: 8 additions & 0 deletions site/.storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export const parameters = {
},
type: "tablet",
},
iphone12: {
name: "iPhone 12",
styles: {
height: "844px",
width: "390px",
},
type: "mobile",
},
terminal: {
name: "Terminal",
styles: {
Expand Down
38 changes: 22 additions & 16 deletions site/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const buttonVariants = cva(
text-sm font-semibold font-medium cursor-pointer
focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-content-link
disabled:pointer-events-none disabled:text-content-disabled
[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0`,
[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0
px-3 py-2`,
{
variants: {
variant: {
Expand All @@ -25,10 +26,15 @@ export const buttonVariants = cva(
"border-none bg-transparent text-content-secondary hover:text-content-primary",
warning:
"border border-border-error text-content-primary bg-surface-error hover:bg-transparent",
ghost:
"text-content-primary bg-transparent border-0 hover:bg-surface-secondary",
},

size: {
default: "h-9 px-3 py-2",
sm: "h-8 px-2 text-xs",
lg: "h-10",
default: "h-9",
sm: "h-8 px-2 py-1.5 text-xs",
icon: "h-10 w-10",
},
},
defaultVariants: {
Expand All @@ -44,16 +50,16 @@ export interface ButtonProps
asChild?: boolean;
}

export const Button: FC<ButtonProps> = forwardRef<
HTMLButtonElement,
ButtonProps
>(({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
});
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just now realizing: do we want the ref type to always be HTMLButtonElement? I'm thinking about cases like this:

<Button asChild>
  <a href="#">Not actually a button in the output</a>
</Button>

Where TypeScript will think it's a button, but the actual HTML node will be for an anchor. I'm not sure what properties are on one but not the other, but calling a button method that doesn't exist could blow up the app

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only thing is, I'm not actually sure how you would add a generic ref type to forwardRef, since it's just a function call, and not a full function signature

I don't think this is worth blocking over, but I'll try to figure out a fix next week

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your concern is totally valid, but this is the default implementation from the shadcn/ui Button. I’d be open to changing it only if necessary, as I’d prefer not to add complexity without bringing justifiable value to us.

({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
},
);
Button.displayName = "Button";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there were some conversations about getting rid of displayName, but we weren't sure if it was necessary for forwarded anonymous components. Does the component not get a name in the dev tools if the name isn't set here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay, I wasn’t aware of that. Thanks for letting me know! I’ve just been copying and pasting the default implementation and making changes only when necessary.

26 changes: 24 additions & 2 deletions site/src/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
*/

import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
import { Check, ChevronRight, Circle } from "lucide-react";
import { Button } from "components/Button/Button";
import { Check, ChevronDownIcon, ChevronRight, Circle } from "lucide-react";
import {
type ComponentPropsWithoutRef,
type ElementRef,
type FC,
type HTMLAttributes,
forwardRef,
} from "react";
Expand Down Expand Up @@ -196,7 +198,7 @@ export const DropdownMenuSeparator = forwardRef<
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.Separator
ref={ref}
className={cn(["-mx-1 my-1 h-px bg-border"], className)}
className={cn(["-mx-1 my-3 h-px bg-border"], className)}
{...props}
/>
));
Expand All @@ -214,3 +216,23 @@ export const DropdownMenuShortcut = ({
);
};
DropdownMenuShortcut.displayName = "DropdownMenuShortcut";

export interface DropdownMenuButtonProps
extends ComponentPropsWithoutRef<typeof Button> {}

export const DropdownMenuButton = forwardRef<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's a little confusing to have a file export both a trigger and a button, especially since the button doesn't use the trigger internally

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I hadn’t thought of that 🤔. Honestly—maybe it’s just personal—I don’t see any confusion since a trigger is a behavioral component and a button is a visual component. To me, it feels okay to have both. What would you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrunoQuaresma It looks like you are adding DropdownMenuButton to the shadcn DropdownMenu component? Im curious why it is necessary to add this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this component is to make dropdown button styling consistent. For example, instead of composing a Button + Icon + styling in every component or page where you want to add a dropdown, you could use a consistent component to reduce boilerplate. While I don’t mind boilerplate in general, in this case, since we already have a consistent design, it could help avoid inconsistencies.

Before:

<Button ref={ref} variant="outline">
	Admin settings
	<DropdownMenuChevronDown />
</Button>

After:

<DropdownMenuButton>Admin settings</DropdownMenuButton>

However, while writing this, I thought of another approach that might be better—creating a new dropdown variant in the Button component:

<Button ref={ref} variant="dropdown">
	Admin settings
</Button>

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If every DropdownMenu component will always use the DropdownMenuButton with this styling then it could make sense. However. that may not always be the case. I personally like having a dropdown variant for the button component more than altering the DropdownMenu component.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to remove the DropdownMenuButton component for now until we actually need it.

HTMLButtonElement,
DropdownMenuButtonProps
>(({ children, ...props }, ref) => {
return (
<Button ref={ref} variant="outline" {...props}>
{children}
<DropdownMenuChevronDown />
</Button>
);
});
DropdownMenuButton.displayName = "DropdownMenuButton";

export const DropdownMenuChevronDown: FC = () => {
return <ChevronDownIcon className="text-content-primary !size-icon-xs" />;
};
6 changes: 4 additions & 2 deletions site/src/contexts/ProxyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import { useQuery } from "react-query";
import { type ProxyLatencyReport, useProxyLatency } from "./useProxyLatency";

export type Proxies = readonly Region[] | readonly WorkspaceProxy[];
export type ProxyLatencies = Record<string, ProxyLatencyReport>;
export interface ProxyContextValue {
// proxy is **always** the workspace proxy that should be used.
// The 'proxy.selectedProxy' field is the proxy being used and comes from either:
Expand Down Expand Up @@ -43,15 +45,15 @@ export interface ProxyContextValue {
// WorkspaceProxy[] is returned if the user is an admin. WorkspaceProxy extends Region with
// more information about the proxy and the status. More information includes the error message if
// the proxy is unhealthy.
proxies?: readonly Region[] | readonly WorkspaceProxy[];
proxies?: Proxies;
// isFetched is true when the 'proxies' api call is complete.
isFetched: boolean;
isLoading: boolean;
error?: unknown;
// proxyLatencies is a map of proxy id to latency report. If the proxyLatencies[proxy.id] is undefined
// then the latency has not been fetched yet. Calculations happen async for each proxy in the list.
// Refer to the returned report for a given proxy for more information.
proxyLatencies: Record<string, ProxyLatencyReport>;
proxyLatencies: ProxyLatencies;
// refetchProxyLatencies will trigger refreshing of the proxy latencies. By default the latencies
// are loaded once.
refetchProxyLatencies: () => Date;
Expand Down
6 changes: 6 additions & 0 deletions site/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@
* {
@apply border-border;
}

/** Related to https://github.com/radix-ui/primitives/issues/3251 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the comment also be updated to list what components rely on this patch?

html body[data-scroll-locked] {
--removed-body-scroll-bar-size: 0 !important;
margin-right: 0 !important;
}
}
20 changes: 2 additions & 18 deletions site/src/modules/dashboard/Navbar/DeploymentDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Interpolation, type Theme, css, useTheme } from "@emotion/react";
import Button from "@mui/material/Button";
import MenuItem from "@mui/material/MenuItem";
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow";
import { DropdownMenuButton } from "components/DropdownMenu/DropdownMenu";
import { FeatureStageBadge } from "components/FeatureStageBadge/FeatureStageBadge";
import {
Popover,
Expand All @@ -16,15 +15,13 @@ import { NavLink } from "react-router-dom";
interface DeploymentDropdownProps {
canViewDeployment: boolean;
canViewOrganizations: boolean;
canViewAllUsers: boolean;
canViewAuditLog: boolean;
canViewHealth: boolean;
}

export const DeploymentDropdown: FC<DeploymentDropdownProps> = ({
canViewDeployment,
canViewOrganizations,
canViewAllUsers,
canViewAuditLog,
canViewHealth,
}) => {
Expand All @@ -34,7 +31,6 @@ export const DeploymentDropdown: FC<DeploymentDropdownProps> = ({
!canViewAuditLog &&
!canViewOrganizations &&
!canViewDeployment &&
!canViewAllUsers &&
!canViewHealth
) {
return null;
Expand All @@ -43,18 +39,7 @@ export const DeploymentDropdown: FC<DeploymentDropdownProps> = ({
return (
<Popover>
<PopoverTrigger>
<Button
size="small"
endIcon={
<DropdownArrow
color={theme.experimental.l2.fill.solid}
close={false}
margin={false}
/>
}
>
Admin settings
</Button>
<DropdownMenuButton size="lg">Admin settings</DropdownMenuButton>
</PopoverTrigger>

<PopoverContent
Expand All @@ -70,7 +55,6 @@ export const DeploymentDropdown: FC<DeploymentDropdownProps> = ({
<DeploymentDropdownContent
canViewDeployment={canViewDeployment}
canViewOrganizations={canViewOrganizations}
canViewAllUsers={canViewAllUsers}
canViewAuditLog={canViewAuditLog}
canViewHealth={canViewHealth}
/>
Expand Down
146 changes: 146 additions & 0 deletions site/src/modules/dashboard/Navbar/MobileMenu.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import type { Meta, StoryObj } from "@storybook/react";
import { fn, userEvent, within } from "@storybook/test";
import { PointerEventsCheckLevel } from "@testing-library/user-event";
import type { FC } from "react";
import { chromaticWithTablet } from "testHelpers/chromatic";
import {
MockPrimaryWorkspaceProxy,
MockProxyLatencies,
MockSupportLinks,
MockUser,
MockUser2,
MockWorkspaceProxies,
} from "testHelpers/entities";
import { MobileMenu } from "./MobileMenu";

const meta: Meta<typeof MobileMenu> = {
title: "modules/dashboard/MobileMenu",
parameters: {
layout: "fullscreen",
viewport: {
defaultViewport: "iphone12",
},
},
component: MobileMenu,
args: {
proxyContextValue: {
proxy: {
preferredPathAppURL: "",
preferredWildcardHostname: "",
proxy: MockPrimaryWorkspaceProxy,
},
isLoading: false,
isFetched: true,
setProxy: fn(),
clearProxy: fn(),
refetchProxyLatencies: fn(),
proxyLatencies: MockProxyLatencies,
proxies: MockWorkspaceProxies,
},
user: MockUser,
supportLinks: MockSupportLinks,
docsHref: "https://coder.com/docs",
onSignOut: fn(),
isDefaultOpen: true,
canViewAuditLog: true,
canViewDeployment: true,
canViewHealth: true,
canViewOrganizations: true,
},
decorators: [withNavbarMock],
};

export default meta;
type Story = StoryObj<typeof MobileMenu>;

export const Closed: Story = {
args: {
isDefaultOpen: false,
},
};

export const Admin: Story = {
play: openAdminSettings,
};

export const Auditor: Story = {
args: {
user: MockUser2,
canViewAuditLog: true,
canViewDeployment: false,
canViewHealth: false,
canViewOrganizations: false,
},
play: openAdminSettings,
};

export const OrgAdmin: Story = {
args: {
user: MockUser2,
canViewAuditLog: true,
canViewDeployment: false,
canViewHealth: false,
canViewOrganizations: true,
},
play: openAdminSettings,
};

export const Member: Story = {
args: {
user: MockUser2,
canViewAuditLog: false,
canViewDeployment: false,
canViewHealth: false,
canViewOrganizations: false,
},
};

export const ProxySettings: Story = {
play: async ({ canvasElement }) => {
const user = setupUser();
const body = within(canvasElement.ownerDocument.body);
const menuItem = await body.findByRole("menuitem", {
name: /workspace proxy settings/i,
});
await user.click(menuItem);
},
};

export const UserSettings: Story = {
play: async ({ canvasElement }) => {
const user = setupUser();
const body = within(canvasElement.ownerDocument.body);
const menuItem = await body.findByRole("menuitem", {
name: /user settings/i,
});
await user.click(menuItem);
},
};

function withNavbarMock(Story: FC) {
return (
<div className="h-[72px] border-0 border-b border-solid px-6 flex items-center justify-end">
<Story />
</div>
);
}

function setupUser() {
// It seems the dropdown component is disabling pointer events, which is
// causing Testing Library to throw an error. As a workaround, we can
// disable the pointer events check.
return userEvent.setup({
pointerEventsCheck: PointerEventsCheckLevel.Never,
});
}

async function openAdminSettings({
canvasElement,
}: { canvasElement: HTMLElement }) {
const user = setupUser();
const body = within(canvasElement.ownerDocument.body);
const menuItem = await body.findByRole("menuitem", {
name: /admin settings/i,
});
await user.click(menuItem);
}
Loading
Loading