-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Dark Mode Toggle button without css #33
- Loading branch information
1 parent
904ca8a
commit 296969f
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
public/app/core/components/AppChrome/TopBar/DarkModeToggle.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,82 @@ | ||
import { css } from '@emotion/css'; | ||
import React, { useState } from 'react'; | ||
|
||
import { GrafanaTheme2 } from '@grafana/data'; | ||
import { useStyles2 } from '@grafana/ui'; | ||
import { changeTheme } from 'app/core/services/theme'; | ||
|
||
import { config } from '../../../../core/config'; | ||
|
||
const DarkModeToggle = () => { | ||
const styles = useStyles2(getStyles); | ||
const [isDark, setIsDark] = useState(config.theme2.isDark); | ||
|
||
return ( | ||
<div className={styles.wrapper}> | ||
<input | ||
type="checkbox" | ||
id="switch" | ||
className={styles.switch} | ||
checked={isDark} | ||
onChange={() => { | ||
changeTheme(isDark ? 'light' : 'dark'); | ||
setIsDark(!isDark); | ||
}} | ||
/> | ||
<label htmlFor="switch" className={styles.switchLabel}> | ||
<span className={styles.button}>{}</span> | ||
</label> | ||
</div> | ||
); | ||
}; | ||
|
||
const getStyles = (theme: GrafanaTheme2) => ({ | ||
wrapper: css({ position: 'relative', display: 'flex' }), | ||
switchLabel: css({ | ||
position: 'relative', | ||
cursor: 'pointer', | ||
display: 'inline-block', | ||
width: '58px', | ||
height: '28px', | ||
background: '#fff', | ||
border: '2px solid #daa', | ||
borderRadius: '20px', | ||
transition: '0.2s', | ||
|
||
'&:hover': { | ||
background: '#efefef', | ||
}, | ||
}), | ||
switch: css({ | ||
position: 'absolute', | ||
appearance: 'none', | ||
|
||
'&:checked + label': { | ||
background: '#c44', | ||
border: '2px solid #c44', | ||
}, | ||
|
||
'&:checked + label:hover': { | ||
background: '#e55', | ||
}, | ||
|
||
'&:checked + label span': { | ||
left: '33px', | ||
background: '#fff', | ||
boxShadow: '1px 2px 3px #00000020', | ||
}, | ||
}), | ||
button: css({ | ||
position: 'absolute', | ||
top: '3px', | ||
left: '5px', | ||
display: 'inline-block', | ||
width: '19px', | ||
height: '19px', | ||
borderRadius: '20px', | ||
background: '#daa', | ||
transition: '0.2s', | ||
}), | ||
}); | ||
|
||
export default DarkModeToggle; |