Skip to content
Open
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
15 changes: 2 additions & 13 deletions dotcom-rendering/src/components/Dropdown.island.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ interface Props {
links: DropdownLinkType[];
dataLinkName: string;
cssOverrides?: SerializedStyles;
linkCssOverrides?: SerializedStyles;
buttonCssOverrides?: SerializedStyles;
children?: React.ReactNode;
renderTrigger?: (isExpanded: boolean) => React.ReactNode;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

leftover props from the previous sign-in header test, that we don't need any more

ariaLabel?: string;
}

Expand Down Expand Up @@ -283,9 +280,8 @@ const NotificationMessage = ({ notification }: NotificationMessageProps) => {
type DropdownLinkProps = {
link: DropdownLinkType;
index: number;
linkCssOverrides?: SerializedStyles;
};
const DropdownLink = ({ link, index, linkCssOverrides }: DropdownLinkProps) => {
const DropdownLink = ({ link, index }: DropdownLinkProps) => {
const ophanComponent = useMemo(
() => buildOphanComponentWithNotifications(link),
[link],
Expand Down Expand Up @@ -362,7 +358,6 @@ const DropdownLink = ({ link, index, linkCssOverrides }: DropdownLinkProps) => {
href={url}
css={[
linkStyles,
linkCssOverrides,
!!link.isActive && linkActive,
index === 0 && linkFirst,
]}
Expand Down Expand Up @@ -401,10 +396,7 @@ export const Dropdown = ({
links,
dataLinkName,
cssOverrides,
linkCssOverrides,
buttonCssOverrides,
children,
renderTrigger,
ariaLabel,
}: Props) => {
const [isExpanded, setIsExpanded] = useState(false);
Expand Down Expand Up @@ -500,7 +492,6 @@ export const Dropdown = ({
href={l.url}
css={[
linkStyles,
linkCssOverrides,
!!l.isActive && linkActive,
index === 0 && linkFirst,
]}
Expand All @@ -519,7 +510,6 @@ export const Dropdown = ({
css={[
buttonStyles,
cssOverrides,
buttonCssOverrides,
isExpanded && buttonExpanded,
]}
aria-label={ariaLabel}
Expand All @@ -529,7 +519,7 @@ export const Dropdown = ({
type="button"
ref={setButtonRef}
>
{renderTrigger ? renderTrigger(isExpanded) : label}
{label}
{notificationCount > 0 && (
<div css={dropdownButtonNotificationBadgeStyles}>
<NotificationBadge diameter={18} />
Expand All @@ -550,7 +540,6 @@ export const Dropdown = ({
key={link.id}
link={link}
index={index}
linkCssOverrides={linkCssOverrides}
/>
))}
</ul>
Expand Down
38 changes: 0 additions & 38 deletions dotcom-rendering/src/components/Dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,44 +136,6 @@ describe('Dropdown', () => {
expect(ulElement).toBeVisible();
});

it('should pass the expanded state to a nested trigger and toggle once per click', () => {
const renderTrigger = jest.fn(() => (
<span>
<span>Nested trigger</span>
</span>
));
const { getByRole, getByText } = render(
<ConfigProvider
value={{
renderingTarget: 'Web',
darkModeAvailable: false,
assetOrigin: '/',
editionId: 'UK',
}}
>
<Dropdown
id="abc"
label={LABEL}
links={links}
dataLinkName="linkname"
renderTrigger={renderTrigger}
/>
</ConfigProvider>,
);

const button = getByRole('button');
expect(renderTrigger).toHaveBeenLastCalledWith(false);
expect(button).toHaveAttribute('aria-expanded', 'false');

fireEvent.click(getByText('Nested trigger'));
expect(renderTrigger).toHaveBeenLastCalledWith(true);
expect(button).toHaveAttribute('aria-expanded', 'true');

fireEvent.click(getByText('Nested trigger'));
expect(renderTrigger).toHaveBeenLastCalledWith(false);
expect(button).toHaveAttribute('aria-expanded', 'false');
});

it('should close the expanded menu when they click away', () => {
const { container, getByRole } = render(
<ConfigProvider
Expand Down
27 changes: 26 additions & 1 deletion dotcom-rendering/src/components/TopBar.island.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ interface Props {
showReaderRevenueLinks?: boolean;
}

const SIGN_IN_FALLBACK_TIMEOUT_MS = 2000;

const topBarStyles = css`
grid-column: content-start / main-column-end;
background-color: ${themePalette('--masthead-top-bar-background')};
Expand Down Expand Up @@ -137,12 +139,28 @@ export const TopBar = ({
const pageViewId = usePageViewId(renderingTarget);

const [referrerUrl, setReferrerUrl] = useState('');
const [showSignInTextOnMobile, setShowSignInTextOnMobile] = useState<
boolean | undefined
>();
const hasHydrated = useHydrated();

useEffect(() => {
setReferrerUrl(window.location.origin + window.location.pathname);
}, []);

useEffect(() => {
if (!showReaderRevenueLinks || showSignInTextOnMobile !== undefined) {
return;
}

const timeout = window.setTimeout(() => {
console.warn('Support header timed out; showing sign-in text.');
setShowSignInTextOnMobile((current) => current ?? true);
}, SIGN_IN_FALLBACK_TIMEOUT_MS);

return () => window.clearTimeout(timeout);
}, [showReaderRevenueLinks, showSignInTextOnMobile]);

useEffect(() => {
if (authStatus.kind === 'SignedOut') {
clearSubscriptionCache();
Expand Down Expand Up @@ -173,6 +191,9 @@ export const TopBar = ({
tagIds={tagIds}
sectionId={sectionId}
contentType={contentType}
onShowSignInTextOnMobileChange={
setShowSignInTextOnMobile
}
/>
</TopBarLinkContainer>
)}
Expand Down Expand Up @@ -216,7 +237,11 @@ export const TopBar = ({
discussionApiUrl={discussionApiUrl}
idApiUrl={idApiUrl}
authStatus={authStatus}
showSignInTextOnMobile={true}
showSignInTextOnMobile={showSignInTextOnMobile ?? true}
isSignInReady={
!showReaderRevenueLinks ||
showSignInTextOnMobile !== undefined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need this prop to avoid showing the wrong sign-in presentation while the support header is loading

}
Comment thread
Copilot marked this conversation as resolved.
Resolved
/>
</TopBarLinkContainer>
</div>
Expand Down
44 changes: 0 additions & 44 deletions dotcom-rendering/src/components/TopBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { AccessToken, IDToken } from '@guardian/identity-auth';
import { breakpoints, from } from '@guardian/source/foundations';
import type { Meta } from '@storybook/react-webpack5';
import type { PropsWithChildren } from 'react';
import { expect, userEvent, within } from 'storybook/test';
import {
contributionsHeaderResponse,
contributionsSignInPromptHeaderResponse,
Expand Down Expand Up @@ -180,49 +179,6 @@ export const SignedInText = () => {
};
SignedInText.parameters = accountStoryParameters;

export const SignedInNoText = () => {
return (
<AccountStoryFrame>
<TopBarMyAccount
mmaUrl={''}
idUrl={''}
discussionApiUrl={''}
idApiUrl={''}
authStatus={{
kind: 'SignedIn',
accessToken: mockAccessToken as AccessToken<never>,
idToken: mockIdToken,
}}
showSignInTextOnMobile={false}
/>
</AccountStoryFrame>
);
};
SignedInNoText.parameters = accountStoryParameters;

export const SignedInNoTextExpanded = () => <SignedInNoText />;
SignedInNoTextExpanded.parameters = accountStoryParameters;
SignedInNoTextExpanded.play = async ({
canvasElement,
}: {
canvasElement: HTMLElement;
}) => {
const canvas = within(canvasElement);
const button = await canvas.findByTestId('dropdown-button');

await userEvent.click(button);
await expect(button).toHaveAttribute('aria-expanded', 'true');

const billingLink = await canvas.findByRole('link', { name: 'Billing' });
await expect(billingLink).toBeVisible();

const expectedDividerLeft =
window.innerWidth < breakpoints.tablet ? '24px' : '30px';
await expect(window.getComputedStyle(billingLink, '::before').left).toBe(
expectedDividerLeft,
);
};

export const SignedOutText = () => {
return (
<AccountStoryFrame>
Expand Down
Loading
Loading