-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from lsst-sqre/tickets/DM-44392
DM-44392: Drop use of Reach UI
- Loading branch information
Showing
20 changed files
with
495 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'squareone': minor | ||
--- | ||
|
||
Usage of Reach UI is now removed and replaced with Radix UI. The user menu now uses `GafaelfawrUserMenu` from `@lsst-sqre/squared` and is based on Radix UI's Navigation Menu component. It is customized here to work with the Gafaelawr API to show a log in button for the logged out state, and to show the user's menu with a default log out button for the logged in state. Previously we also used Reach UI for showing an accessible validation alert in the Times Square page parameters UI. For now we've dropped this functionality. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lsst-sqre/squared': minor | ||
--- | ||
|
||
Created GafaelfawrUserMenu based on the Radix UI [navigation-menu](https://www.radix-ui.com/primitives/docs/components/navigation-menu) component. That's the right primitive for an accessible menu that uses `<a>` or Next `Link` elements. The existing Gafaelfawr menu is now `GafaelfawrUserDropdown` for reference (it is based on Radix UI's [dropdown menu](https://www.radix-ui.com/primitives/docs/components/dropdown-menu), but is more appropriate as a menu of buttons. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/squared/src/components/GafaelfawrUserDropdown/GafaelfawrUserDropdown.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { rest } from 'msw'; | ||
import { SWRConfig } from 'swr'; | ||
import { within, userEvent, screen } from '@storybook/testing-library'; | ||
import { expect } from '@storybook/jest'; | ||
|
||
import GafaelfawrUserDropdown from './GafaelfawrUserDropdown'; | ||
|
||
const meta: Meta<typeof GafaelfawrUserDropdown> = { | ||
title: 'Components/GafaelfawrUserDropdown', | ||
component: GafaelfawrUserDropdown, | ||
parameters: { | ||
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/react/configure/story-layout | ||
layout: 'centered', | ||
// The user menu always shows up on a dark background. | ||
backgrounds: { | ||
default: 'dark', | ||
values: [{ name: 'dark', value: '#1f2121' }], | ||
}, | ||
}, | ||
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/react/writing-docs/autodocs | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof GafaelfawrUserDropdown>; | ||
|
||
const loggedInAuthHandlers = [ | ||
rest.get('/auth/api/v1/user-info', (req, res, ctx) => { | ||
return res( | ||
ctx.json({ | ||
username: 'someuser', | ||
name: 'Alice Example', | ||
email: 'alice@example.com', | ||
uid: 4123, | ||
gid: 4123, | ||
groups: [ | ||
{ | ||
name: 'g_special_users', | ||
id: 123181, | ||
}, | ||
], | ||
quota: { | ||
api: {}, | ||
notebook: { | ||
cpu: 4, | ||
memory: 16, | ||
}, | ||
}, | ||
}) | ||
); | ||
}), | ||
]; | ||
|
||
const loggedOutAuthHandlers = [ | ||
rest.get('/auth/api/v1/user-info', (req, res, ctx) => { | ||
return res(ctx.status(401)); | ||
}), | ||
]; | ||
|
||
export const Default: Story = { | ||
args: { | ||
currentUrl: 'http://localhost:6006/somepage', | ||
}, | ||
|
||
parameters: { | ||
msw: { | ||
handlers: { | ||
auth: loggedInAuthHandlers, | ||
}, | ||
}, | ||
}, | ||
|
||
render: (args) => ( | ||
<SWRConfig value={{ provider: () => new Map() }}> | ||
<GafaelfawrUserDropdown {...args}> | ||
<GafaelfawrUserDropdown.Item> | ||
<a href="#">Account Settings</a> | ||
</GafaelfawrUserDropdown.Item> | ||
<GafaelfawrUserDropdown.Item> | ||
<a href="#">Security tokens</a> | ||
</GafaelfawrUserDropdown.Item> | ||
</GafaelfawrUserDropdown> | ||
</SWRConfig> | ||
), | ||
}; | ||
|
||
export const LoggedOut: Story = { | ||
args: { ...Default.args }, | ||
|
||
parameters: { | ||
msw: { | ||
handlers: { | ||
auth: loggedOutAuthHandlers, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const OpenedMenu: Story = { | ||
args: { ...Default.args }, | ||
|
||
parameters: { ...Default.parameters }, | ||
|
||
play: async ({ canvasElement }) => { | ||
// Delay so msw can load | ||
const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); | ||
await delay(1000); | ||
|
||
const canvas = within(canvasElement); | ||
await userEvent.click(canvas.getByRole('button')); | ||
// Using screen rather than canvas because Radix renders the dropdown | ||
// outside the scope of the storybook canvas. | ||
await expect(screen.getByText('Log out')).toBeInTheDocument(); | ||
await expect(screen.getByText('Log out')).toHaveAttribute( | ||
'href', | ||
'http://localhost:6006/logout?rd=http%3A%2F%2Flocalhost%3A6006%2F' | ||
); | ||
}, | ||
|
||
render: (args) => ( | ||
<SWRConfig value={{ provider: () => new Map() }}> | ||
<GafaelfawrUserDropdown {...args}> | ||
<GafaelfawrUserDropdown.Item> | ||
<a href="#">Account Settings</a> | ||
</GafaelfawrUserDropdown.Item> | ||
<GafaelfawrUserDropdown.Item> | ||
<a href="#">Security tokens</a> | ||
</GafaelfawrUserDropdown.Item> | ||
</GafaelfawrUserDropdown> | ||
</SWRConfig> | ||
), | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/squared/src/components/GafaelfawrUserDropdown/GafaelfawrUserDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
|
||
import styled from 'styled-components'; | ||
|
||
import Menu from './Menu'; | ||
import Separator from './Separator'; | ||
import MenuItem from './MenuItem'; | ||
import { getLoginUrl, getLogoutUrl } from './authUrls'; | ||
import useGafaelfawrUser from '../../hooks/useGafaelfawrUser'; | ||
|
||
export interface GafaelfawrUserDropdownProps { | ||
children: React.ReactNode; | ||
/** | ||
* The URL of the current page. Used to construct the login and logout URLs | ||
* with appropriate redirects. | ||
*/ | ||
currentUrl: string; | ||
} | ||
|
||
export const GafaelfawrUserDropdown = ({ | ||
children, | ||
currentUrl, | ||
}: GafaelfawrUserDropdownProps) => { | ||
const { user, isLoggedIn } = useGafaelfawrUser(); | ||
// TODO: it'd be nice to integrate the useCurrentUrl hook into | ||
// this component so the user doesn't have to pass this prop. | ||
const logoutUrl = getLogoutUrl(currentUrl); | ||
const loginUrl = getLoginUrl(currentUrl); | ||
if (isLoggedIn && user) { | ||
return ( | ||
<Menu logoutHref={logoutUrl} username={user.username}> | ||
{children} | ||
</Menu> | ||
); | ||
} else { | ||
return <SiteNavLink href={loginUrl}>Log in / Sign up</SiteNavLink>; | ||
} | ||
}; | ||
|
||
const SiteNavLink = styled.a` | ||
color: var(--rsd-component-header-nav-text-color); | ||
&:hover { | ||
color: var(--rsd-component-header-nav-text-hover-color); | ||
} | ||
`; | ||
|
||
// Associate child components with the parent for easier imports. | ||
GafaelfawrUserDropdown.Item = MenuItem; | ||
GafaelfawrUserDropdown.Separator = Separator; | ||
|
||
export default GafaelfawrUserDropdown; |
Oops, something went wrong.