Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/grid-ui/src/components/ThemeToggle/ThemeToggle.tsx
1989 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
import React from 'react'
19
import { IconButton, Tooltip } from '@mui/material'
20
import { LightMode, DarkMode, AutoMode } from '@mui/icons-material'
21
import { useTheme } from '../../contexts/ThemeContext'
22
23
export const ThemeToggle: React.FC = () => {
24
const { themeMode, setThemeMode } = useTheme()
25
26
const handleClick = () => {
27
const nextMode = themeMode === 'light' ? 'dark' : themeMode === 'dark' ? 'system' : 'light'
28
setThemeMode(nextMode)
29
}
30
31
const getIcon = () => {
32
if (themeMode === 'light') return <LightMode />
33
if (themeMode === 'dark') return <DarkMode />
34
return <AutoMode />
35
}
36
37
const getTooltip = () => {
38
if (themeMode === 'light') return 'Switch to dark mode'
39
if (themeMode === 'dark') return 'Switch to system mode'
40
return 'Switch to light mode'
41
}
42
43
return (
44
<Tooltip title={getTooltip()}>
45
<IconButton
46
color="inherit"
47
onClick={handleClick}
48
aria-label="Toggle theme"
49
>
50
{getIcon()}
51
</IconButton>
52
</Tooltip>
53
)
54
}
55