-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ToggleThemeButton #7340
Merged
Merged
Add ToggleThemeButton #7340
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,64 @@ | ||
--- | ||
layout: default | ||
title: "ToggleThemeButton" | ||
--- | ||
|
||
# `<ToggleThemeButton>` | ||
|
||
The `<ToggleThemeButton>` component lets users switch from light to dark mode, and persists that choice by leveraging the [store](./Store.md). | ||
|
||
![Dark Mode support](./img/ToggleThemeButton.gif) | ||
|
||
## Usage | ||
|
||
You can add the `<ToggleThemeButton>` to a custom App Bar: | ||
|
||
```tsx | ||
import React from 'react'; | ||
import { | ||
Admin, | ||
Resource, | ||
ToggleThemeButton, | ||
List, | ||
SimpleList, | ||
Layout, | ||
AppBar, | ||
} from 'react-admin'; | ||
import { createTheme, Box, Typography } from '@mui/material'; | ||
|
||
const darkTheme = createTheme({ | ||
palette: { mode: 'dark' }, | ||
}); | ||
|
||
const MyAppBar = (props) => ( | ||
<AppBar {...props}> | ||
<Box flex="1"> | ||
<Typography variant="h6" id="react-admin-title"></Typography> | ||
</Box> | ||
<ToggleThemeButton | ||
lightTheme={defaultTheme} | ||
darkTheme={darkTheme} | ||
/> | ||
</AppBar> | ||
); | ||
|
||
const MyLayout = (props) => ( | ||
<Layout {...props} appBar={MyAppBar} /> | ||
); | ||
|
||
const App = () => ( | ||
<Admin | ||
dataProvider={dataProvider} | ||
layout={MyLayout} | ||
> | ||
<Resource name="posts" list={PostList} /> | ||
</Admin> | ||
); | ||
``` | ||
|
||
## API | ||
|
||
* [`ToggleThemeButton`] | ||
|
||
[`ToggleThemeButton`]: https://github.com/marmelab/react-admin/blob/master/packages/ra-ui-materialui/src/button/ToggleThemeButton.tsx | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions
122
packages/ra-ui-materialui/src/button/ToggleThemeButton.stories.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,122 @@ | ||
import * as React from 'react'; | ||
import { Admin } from 'react-admin'; | ||
import { Resource } from 'ra-core'; | ||
import fakeRestDataProvider from 'ra-data-fakerest'; | ||
import { createMemoryHistory } from 'history'; | ||
import { Box, Typography } from '@mui/material'; | ||
|
||
import { List, Datagrid } from '../list'; | ||
import { TextField } from '../field'; | ||
import { AppBar, Layout } from '../layout'; | ||
import { ToggleThemeButton } from './ToggleThemeButton'; | ||
import { defaultTheme } from '..'; | ||
|
||
export default { title: 'ra-ui-materialui/button/ToggleThemeButton' }; | ||
|
||
const dataProvider = fakeRestDataProvider({ | ||
books: [ | ||
{ | ||
id: 1, | ||
title: 'War and Peace', | ||
author: 'Leo Tolstoy', | ||
year: 1869, | ||
}, | ||
{ | ||
id: 2, | ||
title: 'Pride and Predjudice', | ||
author: 'Jane Austen', | ||
year: 1813, | ||
}, | ||
{ | ||
id: 3, | ||
title: 'The Picture of Dorian Gray', | ||
author: 'Oscar Wilde', | ||
year: 1890, | ||
}, | ||
{ | ||
id: 4, | ||
title: 'Le Petit Prince', | ||
author: 'Antoine de Saint-Exupéry', | ||
year: 1943, | ||
}, | ||
{ | ||
id: 5, | ||
title: "Alice's Adventures in Wonderland", | ||
author: 'Lewis Carroll', | ||
year: 1865, | ||
}, | ||
{ | ||
id: 6, | ||
title: 'Madame Bovary', | ||
author: 'Gustave Flaubert', | ||
year: 1856, | ||
}, | ||
{ | ||
id: 7, | ||
title: 'The Lord of the Rings', | ||
author: 'J. R. R. Tolkien', | ||
year: 1954, | ||
}, | ||
{ | ||
id: 8, | ||
title: "Harry Potter and the Philosopher's Stone", | ||
author: 'J. K. Rowling', | ||
year: 1997, | ||
}, | ||
{ | ||
id: 9, | ||
title: 'The Alchemist', | ||
author: 'Paulo Coelho', | ||
year: 1988, | ||
}, | ||
{ | ||
id: 10, | ||
title: 'A Catcher in the Rye', | ||
author: 'J. D. Salinger', | ||
year: 1951, | ||
}, | ||
{ | ||
id: 11, | ||
title: 'Ulysses', | ||
author: 'James Joyce', | ||
year: 1922, | ||
}, | ||
], | ||
authors: [], | ||
}); | ||
|
||
const history = createMemoryHistory({ initialEntries: ['/books'] }); | ||
|
||
const BookList = () => { | ||
return ( | ||
<List> | ||
<Datagrid> | ||
<TextField source="id" /> | ||
<TextField source="title" /> | ||
<TextField source="author" /> | ||
<TextField source="year" /> | ||
</Datagrid> | ||
</List> | ||
); | ||
}; | ||
|
||
const MyAppBar = props => ( | ||
<AppBar {...props}> | ||
<Box flex="1"> | ||
<Typography variant="h6" id="react-admin-title"></Typography> | ||
</Box> | ||
<ToggleThemeButton | ||
lightTheme={defaultTheme} | ||
darkTheme={{ | ||
palette: { mode: 'dark' }, | ||
}} | ||
/> | ||
</AppBar> | ||
); | ||
const MyLayout = props => <Layout {...props} appBar={MyAppBar} />; | ||
|
||
export const Basic = () => ( | ||
<Admin dataProvider={dataProvider} history={history} layout={MyLayout}> | ||
<Resource name="books" list={BookList} /> | ||
</Admin> | ||
); |
57 changes: 57 additions & 0 deletions
57
packages/ra-ui-materialui/src/button/ToggleThemeButton.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,57 @@ | ||
import React from 'react'; | ||
import { Tooltip, IconButton } from '@mui/material'; | ||
import Brightness4Icon from '@mui/icons-material/Brightness4'; | ||
import Brightness7Icon from '@mui/icons-material/Brightness7'; | ||
import { useTranslate } from 'ra-core'; | ||
import { useTheme } from '../layout'; | ||
import { RaThemeOptions } from '..'; | ||
|
||
/** | ||
* Button toggling the theme (light or dark). | ||
* | ||
* @example | ||
* | ||
* const MyAppBar = props => ( | ||
* <AppBar {...props}> | ||
* <Box flex="1"> | ||
* <Typography variant="h6" id="react-admin-title"></Typography> | ||
* </Box> | ||
* <ToggleThemeButton /> | ||
* </AppBar> | ||
* ); | ||
* | ||
* const MyLayout = props => <Layout {...props} appBar={MyAppBar} />; | ||
*/ | ||
export const ToggleThemeButton = (props: ToggleThemeButtonProps) => { | ||
const translate = useTranslate(); | ||
const { darkTheme, lightTheme } = props; | ||
const [theme, setTheme] = useTheme(lightTheme); | ||
|
||
const handleTogglePaletteType = (): void => { | ||
setTheme(theme?.palette.mode === 'dark' ? lightTheme : darkTheme); | ||
}; | ||
const toggleThemeTitle = translate('ra-preferences.action.toggle_theme', { | ||
_: 'Toggle Theme', | ||
}); | ||
|
||
return ( | ||
<Tooltip title={toggleThemeTitle} enterDelay={300}> | ||
<IconButton | ||
color="inherit" | ||
onClick={handleTogglePaletteType} | ||
aria-label={toggleThemeTitle} | ||
> | ||
{theme?.palette.mode === 'dark' ? ( | ||
<Brightness7Icon /> | ||
) : ( | ||
<Brightness4Icon /> | ||
)} | ||
</IconButton> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export interface ToggleThemeButtonProps { | ||
darkTheme?: RaThemeOptions; | ||
lightTheme?: RaThemeOptions; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You put the image file in the
assets
folder instead of theimg
folder