Download spa-github-pages/src/ui/DarkModeButton.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spa-github-pages/src/ui/DarkModeButton.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spa-github-pages/src/ui/DarkModeButton.tsx
-
curl -L -o DarkModeButton.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spa-github-pages/src/ui/DarkModeButton.tsx
1.79 kB
| import * as React from 'react'; | |
| import { SunIcon } from '@radix-ui/react-icons'; | |
| import useDarkMode from 'use-dark-mode'; | |
| import { Button } from './Button'; | |
| import { darkThemeClass } from '../stitches.config'; | |
| interface DarkModeButtonProps { | |
| css?: React.ComponentProps<typeof Button>['css']; | |
| } | |
| export const DarkModeButton: React.VFC<DarkModeButtonProps> = ({ | |
| css, | |
| ...props | |
| }) => { | |
| // put a try catch around localStorage so this app will work in codesandbox | |
| // when the user blocks third party cookies in chrome, | |
| // which results in a security error when useDarkMode tries to access localStorage | |
| // see https://github.com/codesandbox/codesandbox-client/issues/5397 | |
| let storageProvider: any = null; | |
| try { | |
| storageProvider = localStorage; | |
| } catch {} | |
| const darkMode = useDarkMode(undefined, { | |
| classNameDark: darkThemeClass, | |
| storageProvider, | |
| }); | |
| // add color-scheme style to <html> element | |
| // so document scroll bars will have native dark mode styling | |
| React.useEffect(() => { | |
| if (darkMode.value === true) { | |
| // @ts-ignore because colorScheme type not added yet | |
| document.documentElement.style.colorScheme = 'dark'; | |
| } else { | |
| // @ts-ignore | |
| document.documentElement.style.colorScheme = 'light'; | |
| } | |
| }, [darkMode.value]); | |
| return ( | |
| <Button | |
| {...props} | |
| onClick={darkMode.toggle} | |
| focus="boxShadow" | |
| css={{ | |
| width: '36px', | |
| height: '36px', | |
| padding: '3px', | |
| margin: '-3px', | |
| borderRadius: '50%', | |
| // cast as any b/c of Stitches bug: https://github.com/modulz/stitches/issues/407 | |
| ...(css as any), | |
| }} | |
| title="Toggle dark mode" | |
| aria-label="Toggle dark mode" | |
| > | |
| <SunIcon width="30" height="30" /> | |
| </Button> | |
| ); | |
| }; | |