Skip to content
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

[Theme] configure appearance color mode #203406

Merged
merged 43 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7f7c024
Use Appearance selector instead of ThemeDarkModeToggle
sebelga Dec 6, 2024
290d9d5
Move to folder
sebelga Dec 6, 2024
496af3c
WIP design
sebelga Dec 6, 2024
fa32871
Use EuiKeyPadMenuItem
sebelga Dec 6, 2024
6dad327
Add deprecation callout
sebelga Dec 6, 2024
65214ef
Update user profile with system option
sebelga Dec 9, 2024
9a992da
Use EuiKeyPadMenu instead of flex groups
sebelga Dec 9, 2024
859f732
Update UserSettingsService to return "system" value
sebelga Dec 9, 2024
47355a5
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Dec 9, 2024
97f4001
Fix i18n issues
sebelga Dec 9, 2024
c6e784f
Cleanup
sebelga Dec 9, 2024
2dfbf84
Update user profile from appearance modal
sebelga Dec 11, 2024
b005761
Update jest test
sebelga Dec 11, 2024
447b229
Fix i18n issue
sebelga Dec 11, 2024
8a4468b
Update functional tests
sebelga Dec 12, 2024
30a86ea
Fix functional test
sebelga Dec 12, 2024
4f4ce28
Add functional tests
sebelga Dec 13, 2024
e4c09cb
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 15, 2024
3934e1e
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 16, 2024
e3b64b4
Fix default value in modal
sebelga Dec 16, 2024
04a200e
Don't render space default on serverless
sebelga Dec 16, 2024
6e47aad
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Dec 16, 2024
3ca836b
Fix modal width in serverless
sebelga Dec 16, 2024
c4bb4ae
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 16, 2024
b79020f
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 17, 2024
c3144dc
Fix default value in serverless
sebelga Dec 18, 2024
82d06bd
Update warning message
sebelga Dec 18, 2024
09a6c68
Address CR changes
sebelga Dec 18, 2024
1e5fff2
Merge remote-tracking branch 'upstream/main' into dark-mode/appearanc…
sebelga Dec 18, 2024
e9581b9
Merge branch 'dark-mode/appearance-selector' of github.com:sebelga/ki…
sebelga Dec 18, 2024
6799d71
Update i18n id
sebelga Dec 18, 2024
dc6273e
Reduce width
sebelga Dec 19, 2024
9bd3876
Show toast message whenever the system color mode changes
sebelga Dec 19, 2024
6b43d4e
Add jest tests
sebelga Dec 20, 2024
dc1aba4
Don't update the profile if colorMode value did not change
sebelga Dec 20, 2024
9cc80a0
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 20, 2024
5b8cde6
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Dec 20, 2024
39aba89
Fix TS circular dependency
sebelga Dec 21, 2024
583b069
Update ChromeService jest teset
sebelga Dec 22, 2024
68c1b39
Merge remote-tracking branch 'upstream/main' into dark-mode/appearanc…
sebelga Dec 22, 2024
a23eedd
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 23, 2024
99c94b0
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 26, 2024
8a369ef
Merge branch 'main' into dark-mode/appearance-selector
sebelga Dec 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ describe('#setup', () => {
});
});

it('fetches userSettings when client is set and returns `system` when `darkMode` is set to `system`', async () => {
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile('system'));

const { getUserSettingDarkMode } = service.setup();
service.start(startDeps);

const kibanaRequest = httpServerMock.createKibanaRequest();
const darkMode = await getUserSettingDarkMode(kibanaRequest);

expect(darkMode).toEqual('system');
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledTimes(1);
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledWith({
request: kibanaRequest,
dataPath: 'userSettings',
});
});

it('fetches userSettings when client is set and returns `undefined` when `darkMode` is set to `` (the default value)', async () => {
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile(''));

Expand All @@ -87,6 +104,23 @@ describe('#setup', () => {
});
});

it('fetches userSettings when client is set and returns `undefined` when `darkMode` is set to `space_default`', async () => {
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile('space_default'));

const { getUserSettingDarkMode } = service.setup();
service.start(startDeps);

const kibanaRequest = httpServerMock.createKibanaRequest();
const darkMode = await getUserSettingDarkMode(kibanaRequest);

expect(darkMode).toEqual(undefined);
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledTimes(1);
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledWith({
request: kibanaRequest,
dataPath: 'userSettings',
});
});

it('does not fetch userSettings when client is not set, returns `undefined`, and logs a debug statement', async () => {
const { getUserSettingDarkMode } = service.setup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ export class UserSettingsService {
}
}

/**
* Extracts the dark mode setting from the user settings.
* Returning "undefined" means that we will use the space default settings.
*/
const getUserSettingDarkMode = (
userSettings: Record<string, string>
): DarkModeValue | undefined => {
if (userSettings?.darkMode) {
return userSettings.darkMode.toUpperCase() === 'DARK';
if (userSettings.darkMode) {
const { darkMode } = userSettings;
if (darkMode === 'space_default') return undefined;
sebelga marked this conversation as resolved.
Show resolved Hide resolved

return darkMode.toUpperCase() === 'SYSTEM' ? 'system' : darkMode.toUpperCase() === 'DARK';
}
return undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ export const useUpdateUserProfile = ({
<D extends Partial<UserProfileData>>(updatedData: D) => {
userProfileSnapshot.current = merge({}, userProfileData);
setIsLoading(true);
return userProfileApiClient
.partialUpdate(updatedData)
.then(() => onUserProfileUpdate(updatedData));
return userProfileApiClient.partialUpdate(updatedData).then(() => {
onUserProfileUpdate(updatedData);
return updatedData;
});
},
[userProfileApiClient, onUserProfileUpdate, userProfileData]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-user-profile-components/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface UserProfileAvatarData {
imageUrl?: string | null;
}

export type DarkModeValue = '' | 'dark' | 'light';
export type DarkModeValue = 'system' | 'dark' | 'light' | 'space_default';

/**
* User settings stored in the data object of the User Profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface UserMenuLink {
order?: number;
setAsProfile?: boolean;
/** Render a custom ReactNode instead of the default <EuiContextMenuItem /> */
content?: ReactNode;
content?: ReactNode | ((args: { closePopover: () => void }) => ReactNode);
}

export interface SecurityNavControlServiceStart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14179,11 +14179,6 @@
"xpack.cloudLinks.helpMenuLinks.support": "Support technique",
"xpack.cloudLinks.setupGuide": "Guides de configuration",
"xpack.cloudLinks.userMenuLinks.billingLinkText": "Facturation",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationText": "Recharger la page pour afficher les modifications",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationTitle": "Thème de couleurs actualisé",
"xpack.cloudLinks.userMenuLinks.darkModeOffLabel": "désactivé",
"xpack.cloudLinks.userMenuLinks.darkModeOnLabel": "le",
"xpack.cloudLinks.userMenuLinks.darkModeToggle": "Mode sombre",
"xpack.cloudLinks.userMenuLinks.organizationLinkText": "Organisation",
"xpack.cloudLinks.userMenuLinks.profileLinkText": "Profil",
"xpack.crossClusterReplication.addAutoFollowPatternButtonLabel": "Créer un modèle de suivi automatique",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14049,11 +14049,6 @@
"xpack.cloudLinks.helpMenuLinks.support": "サポート",
"xpack.cloudLinks.setupGuide": "セットアップガイド",
"xpack.cloudLinks.userMenuLinks.billingLinkText": "請求",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationText": "変更を確認するには、ページを再読み込みしてください",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationTitle": "カラーテーマが更新されました",
"xpack.cloudLinks.userMenuLinks.darkModeOffLabel": "オフ",
"xpack.cloudLinks.userMenuLinks.darkModeOnLabel": "日付",
"xpack.cloudLinks.userMenuLinks.darkModeToggle": "ダークモード",
"xpack.cloudLinks.userMenuLinks.organizationLinkText": "組織別",
"xpack.cloudLinks.userMenuLinks.profileLinkText": "プロフィール",
"xpack.crossClusterReplication.addAutoFollowPatternButtonLabel": "自動フォローパターンを作成",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13804,11 +13804,6 @@
"xpack.cloudLinks.helpMenuLinks.support": "支持",
"xpack.cloudLinks.setupGuide": "设置指南",
"xpack.cloudLinks.userMenuLinks.billingLinkText": "帐单",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationText": "重新加载页面以查看更改",
"xpack.cloudLinks.userMenuLinks.darkMode.successNotificationTitle": "已更新颜色主题",
"xpack.cloudLinks.userMenuLinks.darkModeOffLabel": "关闭",
"xpack.cloudLinks.userMenuLinks.darkModeOnLabel": "在",
"xpack.cloudLinks.userMenuLinks.darkModeToggle": "深色模式",
"xpack.cloudLinks.userMenuLinks.organizationLinkText": "组织",
"xpack.cloudLinks.userMenuLinks.profileLinkText": "配置文件",
"xpack.crossClusterReplication.addAutoFollowPatternButtonLabel": "创建自动跟随模式",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/

import type { PluginInitializerContext } from '@kbn/core/public';
import { CloudLinksPlugin } from './plugin';

export function plugin() {
return new CloudLinksPlugin();
export function plugin(context: PluginInitializerContext) {
return new CloudLinksPlugin(context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { type FC } from 'react';
import {
EuiButton,
EuiModal,
EuiModalBody,
EuiModalFooter,
EuiModalHeader,
EuiModalHeaderTitle,
EuiSpacer,
useGeneratedHtmlId,
EuiButtonEmpty,
EuiCallOut,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
import type { DarkModeValue as ColorMode } from '@kbn/user-profile-components';
import { type Value, ValuesGroup } from './values_group';
import { useAppearance } from './use_appearance_hook';

const systemLabel = i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalSystemLabel', {
defaultMessage: 'System',
});

const colorModeOptions: Array<Value<ColorMode>> = [
{
id: 'system',
label: systemLabel,
icon: 'desktop',
},
{
id: 'light',
label: i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalLightLabel', {
defaultMessage: 'Light',
}),
icon: 'sun',
},
{
id: 'dark',
label: i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalDarkLabel', {
defaultMessage: 'Dark',
}),
icon: 'moon',
},
{
id: 'space_default',
label: i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalSpaceDefaultLabel', {
defaultMessage: 'Space default',
}),
icon: 'spaces',
betaBadgeLabel: i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalBetaBadgeLabel', {
defaultMessage: 'Deprecated',
}),
betaBadgeTooltipContent: i18n.translate(
'xpack.cloudLinks.userMenuLinks.appearanceModalBetaBadgeTooltip',
{
defaultMessage: 'Space default settings will be deprecated in 10.0.',
sebelga marked this conversation as resolved.
Show resolved Hide resolved
}
),
betaBadgeIconType: 'warning',
},
];

interface Props {
closeModal: () => void;
uiSettingsClient: IUiSettingsClient;
isServerless: boolean;
}

export const AppearanceModal: FC<Props> = ({ closeModal, uiSettingsClient, isServerless }) => {
const modalTitleId = useGeneratedHtmlId();

const { onChange, colorMode, isLoading } = useAppearance({
uiSettingsClient,
defaultColorMode: isServerless ? 'system' : 'space_default',
});

return (
<EuiModal
aria-labelledby={modalTitleId}
onClose={closeModal}
style={
isServerless
? undefined
: // When not in serverless, we have the "Space default" as an option.
// which renders a warning callout. We don't want the modal to scale up when
// the callout is rendered, so we set a fixed width.
{ width: 600 }
sebelga marked this conversation as resolved.
Show resolved Hide resolved
}
>
<EuiModalHeader>
<EuiModalHeaderTitle size="m" id={modalTitleId}>
{i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalTitle', {
defaultMessage: 'Appearance',
})}
</EuiModalHeaderTitle>
</EuiModalHeader>

<EuiModalBody>
<ValuesGroup<ColorMode>
title={i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalColorModeTitle', {
defaultMessage: 'Color mode',
})}
values={
isServerless
? colorModeOptions.filter(({ id }) => id !== 'space_default')
: colorModeOptions
}
selectedValue={colorMode}
onChange={(id) => {
onChange({ colorMode: id }, false);
}}
ariaLabel={i18n.translate(
'xpack.cloudLinks.userMenuLinks.appearanceModalColorModeAriaLabel',
{
defaultMessage: 'Appearance color mode',
}
)}
/>

{colorMode === 'space_default' && (
<>
<EuiSpacer />
<EuiCallOut
title={i18n.translate(
'xpack.cloudLinks.userMenuLinks.appearanceModalDeprecatedSpaceDefaultTitle',
{
defaultMessage: 'Space default settings will be removed in a future version',
}
)}
color="warning"
iconType="warning"
>
<p>
{i18n.translate(
'xpack.cloudLinks.userMenuLinks.appearanceModalDeprecatedSpaceDefaultDescr',
{
defaultMessage:
'All users with the Space default color mode enabled will be automatically transitioned to the System color mode.',
}
)}
</p>
</EuiCallOut>
<EuiSpacer />
</>
)}
</EuiModalBody>

<EuiModalFooter>
<EuiButtonEmpty data-test-subj="appearanceModalDiscardButton" onClick={closeModal}>
{i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalDiscardBtnLabel', {
defaultMessage: 'Discard',
})}
</EuiButtonEmpty>

<EuiButton
data-test-subj="appearanceModalSaveButton"
onClick={async () => {
await onChange({ colorMode }, true);
closeModal();
}}
fill
isLoading={isLoading}
>
{i18n.translate('xpack.cloudLinks.userMenuLinks.appearanceModalSaveBtnLabel', {
defaultMessage: 'Save changes',
})}
</EuiButton>
</EuiModalFooter>
</EuiModal>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { coreMock } from '@kbn/core/public/mocks';
import { securityMock } from '@kbn/security-plugin/public/mocks';

import { AppearanceSelector } from './appearance_selector';

describe('AppearanceSelector', () => {
sebelga marked this conversation as resolved.
Show resolved Hide resolved
const closePopover = jest.fn();

it('renders correctly and toggles dark mode', () => {
const security = securityMock.createStart();
const core = coreMock.createStart();

const { getByTestId } = render(
<AppearanceSelector
core={core}
security={security}
closePopover={closePopover}
isServerless={false}
/>
);

const appearanceSelector = getByTestId('appearanceSelector');
fireEvent.click(appearanceSelector);

expect(core.overlays.openModal).toHaveBeenCalled();
});
});
Loading