-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6603 from marmelab/extract-sidebar-toggle-button
Extract sidebar toggle button
- Loading branch information
Showing
5 changed files
with
140 additions
and
66 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.md |
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
71 changes: 71 additions & 0 deletions
71
packages/ra-ui-materialui/src/layout/SidebarToggleButton.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,71 @@ | ||
import * as React from 'react'; | ||
import { IconButton, Tooltip } from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import MenuIcon from '@material-ui/icons/Menu'; | ||
import { useTranslate } from 'ra-core'; | ||
import { ClassesOverride } from '../types'; | ||
import { useToggleSidebar } from './useToggleSidebar'; | ||
|
||
/** | ||
* A button that toggles the sidebar. Used by default in the <AppBar>. | ||
* @param props The component props | ||
* @param {String} props.className An optional class name to apply to the button | ||
* @param {ClassesOverride<typeof useStyles>} props.classes An object containing styles. | ||
*/ | ||
export const SidebarToggleButton = (props: SidebarToggleButtonProps) => { | ||
const translate = useTranslate(); | ||
const classes = useStyles(props); | ||
const { className } = props; | ||
const [open, toggleSidebar] = useToggleSidebar(); | ||
|
||
return ( | ||
<Tooltip | ||
title={translate( | ||
open ? 'ra.action.close_menu' : 'ra.action.open_menu', | ||
{ | ||
_: 'Open/Close menu', | ||
} | ||
)} | ||
enterDelay={500} | ||
> | ||
<IconButton | ||
color="inherit" | ||
onClick={() => toggleSidebar()} | ||
className={className} | ||
> | ||
<MenuIcon | ||
classes={{ | ||
root: open | ||
? classes.menuButtonIconOpen | ||
: classes.menuButtonIconClosed, | ||
}} | ||
/> | ||
</IconButton> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
const useStyles = makeStyles( | ||
theme => ({ | ||
menuButtonIconClosed: { | ||
transition: theme.transitions.create(['transform'], { | ||
easing: theme.transitions.easing.sharp, | ||
duration: theme.transitions.duration.leavingScreen, | ||
}), | ||
transform: 'rotate(0deg)', | ||
}, | ||
menuButtonIconOpen: { | ||
transition: theme.transitions.create(['transform'], { | ||
easing: theme.transitions.easing.sharp, | ||
duration: theme.transitions.duration.leavingScreen, | ||
}), | ||
transform: 'rotate(180deg)', | ||
}, | ||
}), | ||
{ name: 'RaSidebarToggleButton' } | ||
); | ||
|
||
export type SidebarToggleButtonProps = { | ||
className?: string; | ||
classes?: ClassesOverride<typeof useStyles>; | ||
}; |
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,29 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { ReduxState, toggleSidebar } from 'ra-core'; | ||
|
||
/** | ||
* A hook that returns the sidebar open state and a function to toggle it. | ||
* @returns A tuple containing a boolean indicating whether the sidebar is open or not and a function to toggle the sidebar. | ||
* @example | ||
* const MyButton = () => { | ||
* const [sidebarOpen, toggleSidebar] = useToggleSidebar(); | ||
* return ( | ||
* <Button | ||
* color="inherit" | ||
* onClick={() => toggleSidebar()} | ||
* > | ||
* {open ? 'Open' : 'Close'} | ||
* </Button> | ||
* ); | ||
*/ | ||
export const useToggleSidebar = (): UseToggleSidebarResult => { | ||
const open = useSelector<ReduxState, boolean>( | ||
state => state.admin.ui.sidebarOpen | ||
); | ||
const dispatch = useDispatch(); | ||
|
||
const toggle = () => dispatch(toggleSidebar()); | ||
return [open, toggle]; | ||
}; | ||
|
||
export type UseToggleSidebarResult = [boolean, () => void]; |