|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import React from 'react'; |
| 10 | +import { Icons, IconButton, TooltipLinkList, WithTooltip } from '@storybook/components'; |
| 11 | +import { useGlobals } from '@storybook/api'; |
| 12 | +import { Link } from '@storybook/components/dist/tooltip/TooltipLinkList'; |
| 13 | + |
| 14 | +const defaultTheme = 'v8.light'; |
| 15 | + |
| 16 | +export function ThemeSwitcher() { |
| 17 | + const [globals, updateGlobals] = useGlobals(); |
| 18 | + const selectedTheme = globals.euiTheme; |
| 19 | + |
| 20 | + if (!selectedTheme || selectedTheme === defaultTheme) { |
| 21 | + updateGlobals({ euiTheme: defaultTheme }); |
| 22 | + } |
| 23 | + |
| 24 | + function Menu({ onHide }: { onHide: () => void }) { |
| 25 | + const links: Link[] = [ |
| 26 | + { |
| 27 | + id: 'v8.light', |
| 28 | + title: 'Amsterdam: Light', |
| 29 | + }, |
| 30 | + { |
| 31 | + id: 'v8.dark', |
| 32 | + title: 'Amsterdam: Dark', |
| 33 | + }, |
| 34 | + { id: 'v7.light', title: 'Light' }, |
| 35 | + { id: 'v7.dark', title: 'Dark' }, |
| 36 | + ].map((link) => ({ |
| 37 | + ...link, |
| 38 | + onClick: (_event, item) => { |
| 39 | + if (item.id !== selectedTheme) { |
| 40 | + updateGlobals({ euiTheme: item.id }); |
| 41 | + } |
| 42 | + onHide(); |
| 43 | + }, |
| 44 | + active: selectedTheme === link.id, |
| 45 | + })); |
| 46 | + |
| 47 | + return <TooltipLinkList links={links} />; |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <WithTooltip |
| 52 | + placement="top" |
| 53 | + trigger="click" |
| 54 | + closeOnClick |
| 55 | + tooltip={({ onHide }) => <Menu onHide={onHide} />} |
| 56 | + > |
| 57 | + <IconButton key="eui-theme" title="Change the EUI theme"> |
| 58 | + <Icons icon={selectedTheme?.includes('dark') ? 'heart' : 'hearthollow'} /> |
| 59 | + </IconButton> |
| 60 | + </WithTooltip> |
| 61 | + ); |
| 62 | +} |
0 commit comments