Skip to content

Commit 2d23ce2

Browse files
committed
chore(workspaces): move workspace tab theming to provider
1 parent 8e9c13f commit 2d23ce2

File tree

13 files changed

+117
-108
lines changed

13 files changed

+117
-108
lines changed

packages/compass-collection/src/plugin-tab-title.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import toNS from 'mongodb-ns';
44
import {
55
useConnectionInfo,
66
useConnectionsListRef,
7-
useTabConnectionTheme,
87
} from '@mongodb-js/compass-connections/provider';
98
import {
109
WorkspaceTab,
@@ -32,7 +31,6 @@ function _PluginTitle({
3231
namespace,
3332
...tabProps
3433
}: PluginTitleProps) {
35-
const { getThemeOf } = useTabConnectionTheme();
3634
const { getConnectionById } = useConnectionsListRef();
3735
const { id: connectionId } = useConnectionInfo();
3836

@@ -75,7 +73,6 @@ function _PluginTitle({
7573
: 'Folder'
7674
}
7775
data-namespace={ns}
78-
tabTheme={getThemeOf(connectionId)}
7976
isNonExistent={isNonExistent}
8077
/>
8178
);

packages/compass-components/src/components/workspace-container.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function WorkspaceContainer({
120120
children:
121121
| React.ReactNode
122122
| ((
123-
scrollTriggerRef: (node?: Element | null | undefined) => void
123+
scrollTriggerRef: (node?: Element | null) => void
124124
) => React.ReactNode);
125125
}) {
126126
const darkMode = useDarkMode();

packages/compass-components/src/components/workspace-tabs/tab.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useDefaultAction } from '../../hooks/use-default-action';
1313
import { LogoIcon } from '../icons/logo-icon';
1414
import { Tooltip } from '../leafygreen';
1515
import { ServerIcon } from '../icons/server-icon';
16+
import { useTabTheme } from './use-tab-theme';
1617

1718
function focusedChild(className: string) {
1819
return `&:hover ${className}, &:focus-visible ${className}, &:focus-within:not(:focus) ${className}`;
@@ -86,20 +87,6 @@ const tabStyles = css({
8687
},
8788
});
8889

89-
export type TabTheme = {
90-
'--workspace-tab-background-color': string;
91-
'--workspace-tab-selected-background-color': string;
92-
'--workspace-tab-top-border-color': string;
93-
'--workspace-tab-selected-top-border-color': string;
94-
'--workspace-tab-border-color': string;
95-
'--workspace-tab-color': string;
96-
'--workspace-tab-selected-color': string;
97-
'&:focus-visible': {
98-
'--workspace-tab-selected-color': string;
99-
'--workspace-tab-border-color': string;
100-
};
101-
};
102-
10390
const tabLightThemeStyles = css({
10491
'--workspace-tab-background-color': palette.gray.light3,
10592
'--workspace-tab-selected-background-color': palette.white,
@@ -199,7 +186,6 @@ export type WorkspaceTabPluginProps = {
199186
isNonExistent?: boolean;
200187
iconGlyph: GlyphName | 'Logo' | 'Server';
201188
tooltip?: [string, string][];
202-
tabTheme?: Partial<TabTheme>;
203189
};
204190

205191
export type WorkspaceTabCoreProps = {
@@ -224,7 +210,6 @@ function Tab({
224210
onClose,
225211
tabContentId,
226212
iconGlyph,
227-
tabTheme,
228213
className: tabClassName,
229214
...props
230215
}: TabProps & Omit<React.HTMLProps<HTMLDivElement>, 'title'>) {
@@ -233,6 +218,7 @@ function Tab({
233218
const { listeners, setNodeRef, transform, transition } = useSortable({
234219
id: tabContentId,
235220
});
221+
const tabTheme = useTabTheme();
236222

237223
const tabProps = mergeProps<HTMLDivElement>(
238224
defaultActionProps,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { useContext } from 'react';
2+
3+
export type TabTheme = {
4+
'--workspace-tab-background-color': string;
5+
'--workspace-tab-selected-background-color': string;
6+
'--workspace-tab-top-border-color': string;
7+
'--workspace-tab-selected-top-border-color': string;
8+
'--workspace-tab-border-color': string;
9+
'--workspace-tab-color': string;
10+
'--workspace-tab-selected-color': string;
11+
'&:focus-visible': {
12+
'--workspace-tab-selected-color': string;
13+
'--workspace-tab-border-color': string;
14+
};
15+
};
16+
17+
type TabThemeProviderValue = Partial<TabTheme> | undefined;
18+
19+
type TabThemeContextValue = TabThemeProviderValue | null;
20+
21+
const TabThemeContext = React.createContext<TabThemeContextValue>(null);
22+
23+
export const TabThemeProvider: React.FunctionComponent<{
24+
children: React.ReactNode;
25+
theme: Partial<TabTheme> | undefined | null;
26+
}> = ({ children, theme }) => {
27+
return (
28+
<TabThemeContext.Provider value={theme}>
29+
{children}
30+
</TabThemeContext.Provider>
31+
);
32+
};
33+
34+
export function useTabTheme(): Partial<TabTheme> | undefined | null {
35+
const context = useContext(TabThemeContext);
36+
37+
return context;
38+
}

packages/compass-components/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import { Accordion } from './components/accordion';
4040
import { CollapsibleFieldSet } from './components/collapsible-field-set';
4141
export {
4242
Tab as WorkspaceTab,
43-
type TabTheme,
4443
type WorkspaceTabCoreProps,
4544
} from './components/workspace-tabs/tab';
45+
export { TabThemeProvider } from './components/workspace-tabs/use-tab-theme';
4646
import { WorkspaceTabs } from './components/workspace-tabs/workspace-tabs';
4747
import ResizableSidebar, {
4848
defaultSidebarWidth,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { useMemo } from 'react';
2+
import { type ConnectionInfo } from '@mongodb-js/connection-info';
3+
import { useConnectionColor } from '@mongodb-js/connection-form';
4+
import {
5+
palette,
6+
useDarkMode,
7+
TabThemeProvider,
8+
} from '@mongodb-js/compass-components';
9+
import { useConnectionsColorList } from '../stores/store-context';
10+
11+
export const ConnectionThemeProvider: React.FunctionComponent<{
12+
children: React.ReactNode;
13+
connectionId?: ConnectionInfo['id'];
14+
}> = ({ children, connectionId }) => {
15+
const { connectionColorToHex, connectionColorToHexActive } =
16+
useConnectionColor();
17+
const connectionColorsList = useConnectionsColorList();
18+
const darkMode = useDarkMode();
19+
20+
const theme = useMemo(() => {
21+
const color = connectionColorsList.find((connection) => {
22+
return connection.id === connectionId;
23+
})?.color;
24+
const bgColor = connectionColorToHex(color);
25+
const activeBgColor = connectionColorToHexActive(color);
26+
27+
if (!color || !bgColor || !activeBgColor) {
28+
return;
29+
}
30+
31+
return {
32+
'--workspace-tab-background-color': bgColor,
33+
'--workspace-tab-top-border-color': bgColor,
34+
'--workspace-tab-border-color': darkMode
35+
? palette.gray.dark2
36+
: palette.gray.light2,
37+
'--workspace-tab-color': darkMode
38+
? palette.gray.base
39+
: palette.gray.dark1,
40+
'--workspace-tab-selected-background-color': darkMode
41+
? palette.black
42+
: palette.white,
43+
'--workspace-tab-selected-top-border-color': activeBgColor,
44+
'--workspace-tab-selected-color': darkMode
45+
? palette.white
46+
: palette.gray.dark3,
47+
'&:focus-visible': {
48+
'--workspace-tab-border-color': darkMode
49+
? palette.blue.light1
50+
: palette.blue.base,
51+
'--workspace-tab-selected-color': darkMode
52+
? palette.blue.light1
53+
: palette.blue.base,
54+
},
55+
};
56+
}, [
57+
connectionId,
58+
connectionColorsList,
59+
connectionColorToHex,
60+
connectionColorToHexActive,
61+
darkMode,
62+
]);
63+
64+
return <TabThemeProvider theme={theme}>{children}</TabThemeProvider>;
65+
};

packages/compass-connections/src/hooks/use-tab-connection-theme.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.

packages/compass-connections/src/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151
connectionInfoRefLocator,
5252
} from './connection-info-provider';
5353

54-
export { useTabConnectionTheme } from './hooks/use-tab-connection-theme';
54+
export { ConnectionThemeProvider } from './hooks/connection-tab-theme-provider';
5555

5656
export {
5757
useConnectionActions,

packages/compass-serverstats/src/plugin-tab-title.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import {
33
useConnectionInfo,
44
useConnectionsListRef,
5-
useTabConnectionTheme,
65
} from '@mongodb-js/compass-connections/provider';
76
import {
87
WorkspaceTab,
@@ -22,8 +21,6 @@ export function ServerStatsPluginTitleComponent(
2221
const { id: connectionId } = useConnectionInfo();
2322
const connectionName = getConnectionById(connectionId)?.title || '';
2423

25-
const { getThemeOf } = useTabConnectionTheme();
26-
2724
return (
2825
<WorkspaceTab
2926
{...props}
@@ -32,7 +29,6 @@ export function ServerStatsPluginTitleComponent(
3229
title={`Performance: ${connectionName}`}
3330
tooltip={[['Performance', connectionName || '']]}
3431
iconGlyph="Gauge"
35-
tabTheme={getThemeOf(connectionId)}
3632
/>
3733
);
3834
}

packages/compass-shell/src/plugin-tab-title.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import {
33
useConnectionInfo,
44
useConnectionsListRef,
5-
useTabConnectionTheme,
65
} from '@mongodb-js/compass-connections/provider';
76
import {
87
WorkspaceTab,
@@ -16,7 +15,6 @@ type PluginTitleProps = WorkspaceTabCoreProps &
1615
WorkspacePluginProps<typeof WorkspaceName>;
1716

1817
export function ShellPluginTitleComponent(tabProps: PluginTitleProps) {
19-
const { getThemeOf } = useTabConnectionTheme();
2018
const { getConnectionById } = useConnectionsListRef();
2119
const { id: connectionId } = useConnectionInfo();
2220

@@ -29,7 +27,6 @@ export function ShellPluginTitleComponent(tabProps: PluginTitleProps) {
2927
title={connectionName ? `mongosh: ${connectionName}` : 'MongoDB Shell'}
3028
tooltip={connectionName ? [['mongosh', connectionName]] : []}
3129
iconGlyph="Shell"
32-
tabTheme={getThemeOf(connectionId)}
3330
/>
3431
);
3532
}

0 commit comments

Comments
 (0)