-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
178 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/docusaurus-theme/src/components/theme_switcher/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { css } from '@emotion/react'; | ||
import { | ||
EuiAvatar, | ||
EuiButtonEmpty, | ||
euiFocusRing, | ||
EuiListGroup, | ||
EuiListGroupItem, | ||
EuiPopover, | ||
useEuiMemoizedStyles, | ||
useEuiTheme, | ||
UseEuiTheme, | ||
} from '@elastic/eui'; | ||
|
||
import { AppThemeContext, AVAILABLE_THEMES } from '../theme_context'; | ||
|
||
const getStyles = (euiThemeContext: UseEuiTheme) => { | ||
const { euiTheme } = euiThemeContext; | ||
|
||
return { | ||
button: css` | ||
padding: 0; | ||
`, | ||
listItem: css` | ||
.euiListGroupItem__button:focus-visible { | ||
// overriding the global "outset" style to ensure the focus style is not cut off | ||
${euiFocusRing(euiThemeContext, 'inset', { | ||
color: euiTheme.colors.primary, | ||
})}; | ||
} | ||
`, | ||
}; | ||
}; | ||
|
||
export const ThemeSwitcher = () => { | ||
const { euiTheme } = useEuiTheme(); | ||
const [currentTheme, setCurrentTheme] = useState( | ||
AVAILABLE_THEMES[0]?.value ?? '' | ||
); | ||
const [isPopoverOpen, setPopoverOpen] = useState(false); | ||
const { theme, changeTheme } = useContext(AppThemeContext); | ||
|
||
useEffect(() => { | ||
changeTheme(currentTheme); | ||
}, [currentTheme]); | ||
|
||
const styles = useEuiMemoizedStyles(getStyles); | ||
|
||
const button = ( | ||
<EuiButtonEmpty | ||
size="s" | ||
color="text" | ||
css={styles.button} | ||
onClick={() => setPopoverOpen((isOpen) => !isOpen)} | ||
aria-label={`${theme.text} theme`} | ||
> | ||
<EuiAvatar name={theme.text} size="s" color={euiTheme.colors.primary} /> | ||
</EuiButtonEmpty> | ||
); | ||
|
||
return ( | ||
<EuiPopover | ||
isOpen={isPopoverOpen} | ||
closePopover={() => setPopoverOpen(false)} | ||
button={button} | ||
panelPaddingSize="xs" | ||
repositionOnScroll | ||
aria-label="EUI theme list" | ||
> | ||
<EuiListGroup> | ||
{AVAILABLE_THEMES && | ||
AVAILABLE_THEMES.map((theme) => { | ||
const isCurrentTheme = currentTheme === theme.value; | ||
|
||
const handleOnClick = () => { | ||
setCurrentTheme(theme.value); | ||
}; | ||
|
||
return ( | ||
<EuiListGroupItem | ||
key={theme.value} | ||
css={styles.listItem} | ||
label={theme.text} | ||
size="xs" | ||
isActive={isCurrentTheme} | ||
color={isCurrentTheme ? 'primary' : 'text'} | ||
onClick={handleOnClick} | ||
/> | ||
); | ||
})} | ||
</EuiListGroup> | ||
</EuiPopover> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters