fix: sync sidebar unread badge mode and positioning

This commit is contained in:
hazre committed 2026-03-25 14:06:15 +01:00
1 parent e8559efc8b
commit 638e4d06d2
14 files changed
+216 -140

No files matched your search

+4 -37
View File
@@ -128,53 +128,20 @@ export const SidebarItemBadge = recipe({
},
],
variants: {
hasCount: {
true: {
mode: {
count: {
top: toRem(-6),
left: toRem(-6),
},
false: {
dot: {
top: toRem(-4),
left: toRem(-4),
},
},
},
defaultVariants: {
hasCount: false,
mode: 'dot',
},
// Adjust positioning when badge is on a folder (closed folder button)
compoundVariants: [
{
variants: {
hasCount: true,
},
style: {
selectors: {
// When the badge is within an item containing a closed folder, adjust position
// to align with the folder's square border
'div:has(> button[data-id]) &': {
top: toRem(0),
left: 'auto',
right: toRem(0),
},
},
},
},
{
variants: {
hasCount: false,
},
style: {
selectors: {
'div:has(> button[data-id]) &': {
top: toRem(0),
left: 'auto',
right: toRem(0),
},
},
},
},
],
});
export type SidebarItemBadgeVariants = RecipeVariants<typeof SidebarItemBadge>;
+2 -2
View File
@@ -14,9 +14,9 @@ export const SidebarItem = as<'div', css.SidebarItemVariants>(
);
export const SidebarItemBadge = as<'div', css.SidebarItemBadgeVariants>(
({ as: AsSidebarBadgeBox = 'div', className, hasCount, ...props }, ref) => (
({ as: AsSidebarBadgeBox = 'div', className, mode, ...props }, ref) => (
<AsSidebarBadgeBox
className={classNames(css.SidebarItemBadge({ hasCount }), className)}
className={classNames(css.SidebarItemBadge({ mode }), className)}
{...props}
ref={ref}
/>
@@ -0,0 +1,46 @@
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { SidebarUnreadBadge } from './SidebarUnreadBadge';
const settings = {
showUnreadCounts: true,
badgeCountDMsOnly: false,
showPingCounts: false,
};
vi.mock('$state/hooks/settings', () => ({
useSetting: (_atom: unknown, key: keyof typeof settings) => [settings[key]],
}));
vi.mock('$state/settings', () => ({
settingsAtom: {},
}));
vi.mock('./SidebarItem', () => ({
SidebarItemBadge: ({ mode, children }: { mode: 'dot' | 'count'; children: ReactNode }) => (
<div data-testid="sidebar-item-badge" data-mode={mode}>
{children}
</div>
),
}));
describe('SidebarUnreadBadge', () => {
it('uses count mode when room unread counts are enabled', () => {
settings.showUnreadCounts = true;
render(<SidebarUnreadBadge count={4} />);
expect(screen.getByTestId('sidebar-item-badge')).toHaveAttribute('data-mode', 'count');
expect(screen.getByText('4')).toBeInTheDocument();
});
it('uses dot mode when room unread counts are disabled', () => {
settings.showUnreadCounts = false;
render(<SidebarUnreadBadge count={4} />);
expect(screen.getByTestId('sidebar-item-badge')).toHaveAttribute('data-mode', 'dot');
expect(screen.queryByText('4')).not.toBeInTheDocument();
});
});
@@ -0,0 +1,38 @@
import { UnreadBadge, UnreadBadgeMode, resolveUnreadBadgeMode } from '$components/unread-badge';
import { useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { SidebarItemBadge } from './SidebarItem';
type SidebarUnreadBadgeProps = {
highlight?: boolean;
count: number;
dm?: boolean;
mode?: UnreadBadgeMode;
};
export function SidebarUnreadBadge({
highlight,
count,
dm,
mode,
}: Readonly<SidebarUnreadBadgeProps>) {
const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts');
const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly');
const [showPingCounts] = useSetting(settingsAtom, 'showPingCounts');
const resolvedMode =
mode ??
resolveUnreadBadgeMode({
highlight,
count,
dm,
showUnreadCounts,
badgeCountDMsOnly,
showPingCounts,
});
return (
<SidebarItemBadge mode={resolvedMode}>
<UnreadBadge highlight={highlight} count={count} dm={dm} mode={resolvedMode} />
</SidebarItemBadge>
);
}
+1
View File
@@ -1,5 +1,6 @@
export * from './Sidebar';
export * from './SidebarItem';
export * from './SidebarUnreadBadge';
export * from './SidebarContent';
export * from './SidebarStack';
export * from './SidebarStackSeparator';
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest';
import { resolveUnreadBadgeMode } from './UnreadBadge';
describe('resolveUnreadBadgeMode', () => {
it('returns count for a room unread when unread counts are enabled', () => {
expect(
resolveUnreadBadgeMode({
count: 4,
showUnreadCounts: true,
badgeCountDMsOnly: false,
showPingCounts: false,
})
).toBe('count');
});
it('returns dot for a room unread when unread counts are disabled', () => {
expect(
resolveUnreadBadgeMode({
count: 4,
showUnreadCounts: false,
badgeCountDMsOnly: false,
showPingCounts: false,
})
).toBe('dot');
});
it('returns count for a DM unread when DM counts are enabled', () => {
expect(
resolveUnreadBadgeMode({
count: 4,
dm: true,
showUnreadCounts: false,
badgeCountDMsOnly: true,
showPingCounts: false,
})
).toBe('count');
});
it('returns count for a highlight when ping counts are enabled', () => {
expect(
resolveUnreadBadgeMode({
count: 2,
highlight: true,
showUnreadCounts: false,
badgeCountDMsOnly: false,
showPingCounts: true,
})
).toBe('count');
});
});
+38 -13
View File
@@ -9,7 +9,32 @@ type UnreadBadgeProps = {
count: number;
/** Whether this badge belongs to a DM room. Used with the badgeCountDMsOnly setting. */
dm?: boolean;
mode?: UnreadBadgeMode;
};
type ResolveUnreadBadgeModeOptions = Omit<UnreadBadgeProps, 'mode'> & {
showUnreadCounts: boolean;
badgeCountDMsOnly: boolean;
showPingCounts: boolean;
};
export type UnreadBadgeMode = 'dot' | 'count';
export function resolveUnreadBadgeMode({
highlight,
count,
dm,
showUnreadCounts,
badgeCountDMsOnly,
showPingCounts,
}: ResolveUnreadBadgeModeOptions): UnreadBadgeMode {
const showNumber =
count > 0 &&
((dm && badgeCountDMsOnly) || (!dm && showUnreadCounts) || (highlight && showPingCounts));
return showNumber ? 'count' : 'dot';
}
const styles: CSSProperties = {
minWidth: toRem(16),
};
@@ -21,30 +46,30 @@ export function UnreadBadgeCenter({ children }: { children: ReactNode }) {
);
}
export function UnreadBadge({ highlight, count, dm }: UnreadBadgeProps) {
export function UnreadBadge({ highlight, count, dm, mode }: UnreadBadgeProps) {
const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts');
const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly');
const [showPingCounts] = useSetting(settingsAtom, 'showPingCounts');
/**
* Show a number if there is a count and:
* - Its a dm and dm counts are enabled
* - Its a normal room and unread counts are enabled
* - Its a ping and ping counts are enabled
*/
const showNumber =
count > 0 &&
((dm && badgeCountDMsOnly) || (!dm && showUnreadCounts) || (highlight && showPingCounts));
const resolvedMode =
mode ??
resolveUnreadBadgeMode({
highlight,
count,
dm,
showUnreadCounts,
badgeCountDMsOnly,
showPingCounts,
});
return (
<Badge
variant={highlight ? 'Success' : 'Secondary'}
size={showNumber ? '400' : '200'}
size={resolvedMode === 'count' ? '400' : '200'}
fill="Solid"
radii="Pill"
outlined={false}
>
{showNumber && (
{resolvedMode === 'count' && (
<Text as="span" size="L400">
{millify(count)}
</Text>
@@ -30,7 +30,7 @@ import {
SidebarItem,
SidebarItemTooltip,
SidebarAvatar,
SidebarItemBadge,
SidebarUnreadBadge,
} from '$components/sidebar';
import { UserAvatar } from '$components/user-avatar';
import { nameInitials } from '$utils/common';
@@ -287,12 +287,10 @@ export function AccountSwitcherTab() {
)}
</SidebarItemTooltip>
{(totalBackgroundUnread > 0 || anyBackgroundHighlight) && (
<SidebarItemBadge hasCount style={{ left: toRem(-6), right: 'auto' }}>
<UnreadBadge
highlight={anyBackgroundHighlight}
count={anyBackgroundHighlight ? totalBackgroundHighlight : totalBackgroundUnread}
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={anyBackgroundHighlight}
count={anyBackgroundHighlight ? totalBackgroundHighlight : totalBackgroundUnread}
/>
)}
<PopOut
+7 -16
View File
@@ -1,7 +1,7 @@
import { useMemo, useRef, useEffect } from 'react';
import * as Sentry from '@sentry/react';
import { useNavigate } from 'react-router-dom';
import { Avatar, Text, Box, toRem } from 'folds';
import { Avatar, Text, Box } from 'folds';
import { useAtomValue } from 'jotai';
import { Room } from '$types/matrix-sdk';
import { useMatrixClient } from '$hooks/useMatrixClient';
@@ -10,10 +10,9 @@ import { getDirectRoomPath } from '$pages/pathUtils';
import {
SidebarAvatar,
SidebarItem,
SidebarItemBadge,
SidebarUnreadBadge,
SidebarItemTooltip,
} from '$components/sidebar';
import { UnreadBadge } from '$components/unread-badge';
import { RoomAvatar } from '$components/room-avatar';
import { UserAvatar } from '$components/user-avatar';
import { getDirectRoomAvatarUrl } from '$utils/room';
@@ -139,19 +138,11 @@ function DMItem({ room, selected }: DMItemProps) {
)}
</SidebarItemTooltip>
{unread && (unread.total > 0 || unread.highlight > 0) && (
<SidebarItemBadge
hasCount={unread.total > 0}
style={{
left: unread.total > 0 ? toRem(-6) : toRem(-4),
right: 'auto',
}}
>
<UnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
dm
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
dm
/>
)}
</SidebarItem>
);
+6 -15
View File
@@ -13,11 +13,10 @@ import { useRoomsUnread } from '$state/hooks/unread';
import {
SidebarAvatar,
SidebarItem,
SidebarItemBadge,
SidebarUnreadBadge,
SidebarItemTooltip,
} from '$components/sidebar';
import { useDirectSelected } from '$hooks/router/useDirectSelected';
import { UnreadBadge } from '$components/unread-badge';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useNavToActivePathAtom } from '$state/hooks/navToActivePath';
import { markAsRead } from '$utils/notifications';
@@ -115,19 +114,11 @@ export function DirectTab() {
)}
</SidebarItemTooltip>
{directUnread && (
<SidebarItemBadge
hasCount={directUnread.total > 0}
style={{
left: directUnread.total > 0 ? toRem(-6) : toRem(-4),
right: 'auto',
}}
>
<UnreadBadge
highlight={directUnread.highlight > 0}
count={directUnread.highlight > 0 ? directUnread.highlight : directUnread.total}
dm
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={directUnread.highlight > 0}
count={directUnread.highlight > 0 ? directUnread.highlight : directUnread.total}
dm
/>
)}
{menuAnchor && (
<PopOut
+5 -14
View File
@@ -14,11 +14,10 @@ import { useRoomsUnread } from '$state/hooks/unread';
import {
SidebarAvatar,
SidebarItem,
SidebarItemBadge,
SidebarUnreadBadge,
SidebarItemTooltip,
} from '$components/sidebar';
import { useHomeSelected } from '$hooks/router/useHomeSelected';
import { UnreadBadge } from '$components/unread-badge';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useNavToActivePathAtom } from '$state/hooks/navToActivePath';
import { markAsRead } from '$utils/notifications';
@@ -109,18 +108,10 @@ export function HomeTab() {
)}
</SidebarItemTooltip>
{homeUnread && (
<SidebarItemBadge
hasCount={homeUnread.total > 0}
style={{
left: homeUnread.total > 0 ? toRem(-6) : toRem(-4),
right: 'auto',
}}
>
<UnreadBadge
highlight={homeUnread.highlight > 0}
count={homeUnread.highlight > 0 ? homeUnread.highlight : homeUnread.total}
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={homeUnread.highlight > 0}
count={homeUnread.highlight > 0 ? homeUnread.highlight : homeUnread.total}
/>
)}
{menuAnchor && (
<PopOut
+3 -8
View File
@@ -1,10 +1,10 @@
import { useNavigate } from 'react-router-dom';
import { Icon, Icons, toRem } from 'folds';
import { Icon, Icons } from 'folds';
import { useAtomValue } from 'jotai';
import {
SidebarAvatar,
SidebarItem,
SidebarItemBadge,
SidebarUnreadBadge,
SidebarItemTooltip,
} from '$components/sidebar';
import { allInvitesAtom } from '$state/room-list/inviteList';
@@ -15,7 +15,6 @@ import {
joinPathComponent,
} from '$pages/pathUtils';
import { useInboxSelected } from '$hooks/router/useInbox';
import { UnreadBadge } from '$components/unread-badge';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useNavToActivePathAtom } from '$state/hooks/navToActivePath';
@@ -51,11 +50,7 @@ export function InboxTab() {
</SidebarAvatar>
)}
</SidebarItemTooltip>
{inviteCount > 0 && (
<SidebarItemBadge hasCount style={{ left: toRem(-6), right: 'auto' }}>
<UnreadBadge highlight count={inviteCount} />
</SidebarItemBadge>
)}
{inviteCount > 0 && <SidebarUnreadBadge highlight count={inviteCount} />}
</SidebarItem>
);
}
+9 -26
View File
@@ -51,7 +51,7 @@ import { getSpaceLobbyPath, getSpacePath, joinPathComponent } from '$pages/pathU
import {
SidebarAvatar,
SidebarItem,
SidebarItemBadge,
SidebarUnreadBadge,
SidebarItemTooltip,
SidebarStack,
SidebarStackSeparator,
@@ -60,7 +60,6 @@ import {
} from '$components/sidebar';
import { RoomUnreadProvider, RoomsUnreadProvider } from '$components/RoomUnreadProvider';
import { useSelectedSpace } from '$hooks/router/useSelectedSpace';
import { UnreadBadge } from '$components/unread-badge';
import { getCanonicalAliasOrRoomId, isRoomAlias } from '$utils/matrix';
import { RoomAvatar } from '$components/room-avatar';
import { nameInitials, randomStr } from '$utils/common';
@@ -465,18 +464,10 @@ function SpaceTab({
)}
</SidebarItemTooltip>
{unread && (
<SidebarItemBadge
hasCount={unread.total > 0}
style={{
left: unread.total > 0 ? toRem(-6) : toRem(-4),
right: 'auto',
}}
>
<UnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
/>
)}
{menuAnchor && (
<PopOut
@@ -606,18 +597,10 @@ function ClosedSpaceFolder({
)}
</SidebarItemTooltip>
{unread && (
<SidebarItemBadge
hasCount={unread.total > 0}
style={{
left: unread.total > 0 ? toRem(-6) : toRem(-4),
right: 'auto',
}}
>
<UnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
/>
</SidebarItemBadge>
<SidebarUnreadBadge
highlight={unread.highlight > 0}
count={unread.highlight > 0 ? unread.highlight : unread.total}
/>
)}
</SidebarItem>
)}
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { Badge, color, Icon, Icons, Text, toRem } from 'folds';
import { Badge, color, Icon, Icons, Text } from 'folds';
import {
SidebarAvatar,
SidebarItem,
@@ -66,7 +66,7 @@ function UnverifiedIndicator() {
)}
</SidebarItemTooltip>
{!unverified && unverifiedDeviceCount && unverifiedDeviceCount > 0 && (
<SidebarItemBadge hasCount style={{ left: toRem(-6), right: 'auto' }}>
<SidebarItemBadge mode="count">
<Badge variant="Warning" size="400" fill="Solid" radii="Pill" outlined={false}>
<Text as="span" size="L400">
{unverifiedDeviceCount}