Skip to content
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

[RFR] Add ability to customize icon in Logout button #4229

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ title: "Reference"
* `<Labeled>`
* [`<Layout>`](./Theming.md#using-a-custom-layout)
* [`<Loading>`](./Theming.md#Loading)
* [`<Logout>`](./Theming.md#using-a-custom-logout-button)
* [`<List>`](./List.md#the-list-component)
* [`<ListGuesser>`](./List.md#the-listguesser-component)
* `<ListButton>`
Expand Down
19 changes: 19 additions & 0 deletions docs/Theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,25 @@ const App = () => (
);
```

## Using a Custom Logout Button

### Changing the Icon

It is possible to use a completely [custom logout button](./Authentication.md#the-datagrid-component) or you can simply override some properties of the default button. If you want to change the icon, you can use the default `<Logout>` component and pass a different icon as the `icon` prop.

```jsx
import { Admin, Logout } from 'react-admin';
import ExitToAppIcon from '@material-ui/icons/ExitToApp'

const MyLogoutButton = props => <Logout {...props} icon={<ExitToAppIcon/>} />;

const App = () => (
<Admin logoutButton={MyLogoutButton}>
// ...
</Admin>
);
```

## Notifications

You can override the notification component, for instance to change the notification duration. It defaults to 4000, i.e. 4 seconds, and you can override it using the `autoHideDuration` prop. For instance, to create a custom Notification component with a 5 seconds default:
Expand Down
8 changes: 5 additions & 3 deletions packages/ra-ui-materialui/src/auth/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, FunctionComponent } from 'react';
import React, { useCallback, FunctionComponent, ReactElement } from 'react';
import PropTypes from 'prop-types';
import { ListItemIcon, MenuItem, makeStyles } from '@material-ui/core';
import { MenuItemProps } from '@material-ui/core/MenuItem';
Expand All @@ -11,6 +11,7 @@ import { useTranslate, useLogout } from 'ra-core';
interface Props {
className?: string;
redirectTo?: string;
icon?: ReactElement;
}

const useStyles = makeStyles(
Expand All @@ -31,7 +32,7 @@ const useStyles = makeStyles(
const LogoutWithRef: FunctionComponent<
Props & MenuItemProps<'li', { button: true }> // HACK: https://github.com/mui-org/material-ui/issues/16245
> = React.forwardRef(function Logout(props, ref) {
const { className, redirectTo, ...rest } = props;
const { className, redirectTo, icon, ...rest } = props;
const classes = useStyles({}); // the empty {} is a temp fix for https://github.com/mui-org/material-ui/issues/15942
const translate = useTranslate();
const logout = useLogout();
Expand All @@ -48,7 +49,7 @@ const LogoutWithRef: FunctionComponent<
{...rest}
>
<ListItemIcon className={classes.icon}>
<ExitIcon />
{icon ? icon : <ExitIcon />}
</ListItemIcon>
{translate('ra.auth.logout')}
</MenuItem>
Expand All @@ -58,6 +59,7 @@ const LogoutWithRef: FunctionComponent<
LogoutWithRef.propTypes = {
className: PropTypes.string,
redirectTo: PropTypes.string,
icon: PropTypes.element,
};

export default LogoutWithRef;