import React, { useState } from 'react';
import {
Divider,
Dropdown,
DropdownGroup,
DropdownItem,
DropdownList,
Flex,
FlexItem,
MenuToggle,
MenuToggleElement,
} from '@patternfly/react-core';
import { AdjustIcon } from '@patternfly/react-icons/dist/esm/icons/adjust-icon';
import { CogIcon } from '@patternfly/react-icons/dist/esm/icons/cog-icon';
import { EyeIcon } from '@patternfly/react-icons/dist/esm/icons/eye-icon';
import { MoonIcon } from '@patternfly/react-icons/dist/esm/icons/moon-icon';
import { SunIcon } from '@patternfly/react-icons/dist/esm/icons/sun-icon';
import {
THEME_SYSTEM,
THEME_DARK,
THEME_LIGHT,
CONTRAST_SYSTEM,
CONTRAST_DEFAULT,
CONTRAST_HIGH,
THEME_PREFERENCE_LABELS,
CONTRAST_PREFERENCE_LABELS,
} from './const';
import { ContrastMode, ContrastPreference, Theme, ThemePreference } from './types';
import { useTheme } from './useTheme';
interface ThemeDropdownProps {
/** Custom class for the dropdown toggle */
className?: string;
/** Whether to show text alongside the icon */
showText?: boolean;
}
const getColorSchemeIcon = (preference: ThemePreference, effectiveTheme: Theme) => {
switch (preference) {
case THEME_DARK:
return ;
case THEME_LIGHT:
return ;
case THEME_SYSTEM:
return effectiveTheme === THEME_DARK ? : ;
default:
return ;
}
};
const getContrastIcon = (preference: ContrastPreference, effectiveContrast: ContrastMode) => {
switch (preference) {
case CONTRAST_HIGH:
return ;
case CONTRAST_DEFAULT:
return ;
case CONTRAST_SYSTEM:
return effectiveContrast === CONTRAST_HIGH ? : ;
default:
return ;
}
};
export const ThemeDropdown: React.FC = ({ className, showText = false }) => {
const {
preference,
contrastPreference,
effectiveTheme,
effectiveContrast,
setThemePreference,
setContrastPreference,
} = useTheme();
const [isOpen, setIsOpen] = useState(false);
const onToggleClick = () => {
setIsOpen(!isOpen);
};
const onSelect = (
_event: React.MouseEvent | undefined,
value: string | number | undefined,
) => {
if (!value || typeof value !== 'string') {
return;
}
// Values are prefixed to distinguish groups
if (value === 'brand-theme') {
return; // Handled by the Switch onChange directly
} else if (value.startsWith('theme:')) {
setThemePreference(value.replace('theme:', '') as ThemePreference);
} else if (value.startsWith('contrast:')) {
setContrastPreference(value.replace('contrast:', '') as ContrastPreference);
}
// Don't close on select -- user may want to change both groups
};
const toggle = (toggleRef: React.Ref) => (
{getColorSchemeIcon(preference, effectiveTheme)}{' '}
{showText && {THEME_PREFERENCE_LABELS[preference]}}
|
{getContrastIcon(contrastPreference, effectiveContrast)}{' '}
{showText && {CONTRAST_PREFERENCE_LABELS[contrastPreference]}}
);
return (
setIsOpen(isDropdownOpen)}
toggle={toggle}
data-test="theme-dropdown"
>
}
description="Follow system preference"
>
{THEME_PREFERENCE_LABELS[THEME_SYSTEM]}
}
description="Always use light mode"
>
{THEME_PREFERENCE_LABELS[THEME_LIGHT]}
}
description="Always use dark mode"
>
{THEME_PREFERENCE_LABELS[THEME_DARK]}
}
description="Follow system preference"
>
{CONTRAST_PREFERENCE_LABELS[CONTRAST_SYSTEM]}
}
description="Standard contrast for most users"
>
{CONTRAST_PREFERENCE_LABELS[CONTRAST_DEFAULT]}
}
description="Increased contrast with stronger borders"
>
{CONTRAST_PREFERENCE_LABELS[CONTRAST_HIGH]}
);
};