-
-
Notifications
You must be signed in to change notification settings - Fork 310
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 prop sidebarCollapsed and onToggleSidebar #4516
base: master
Are you sure you want to change the base?
Conversation
@Janpot @bharatkashyap from previous convo here
|
Yes, I'm suggesting to make this similar to how we do this in other products of the MUI ecosystem. for example So something like please wait a moment with implementing this to allow us get to a consensus. |
I've been calling it About referring to the property as "open", it does make more sense than referring to it as "collapsed" now that we're making it a controlled prop... So I agree with your suggestions. |
@Janpot @apedroferreira I have made changes as per the above suggestions, please review them when possible |
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.
Thanks for the effort so far, it's looking good but I have many important suggestions!
@@ -89,6 +89,18 @@ export interface DashboardLayoutProps { | |||
* @default false | |||
*/ | |||
hideNavigation?: boolean; | |||
/** A prop that controls the collapsed state of the sidebar. |
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.
/** A prop that controls the collapsed state of the sidebar. | |
/** If `true`, the navigation menu is expanded. | |
-- | |
<br class="Apple-interchange-newline"> |
*/ | ||
navigationMenuOpen?: boolean; | ||
/** | ||
* Callback function to be executed on navigation menu state changes to open |
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.
* Callback function to be executed on navigation menu state changes to open | |
* Callback fired when navigation menu is expanded. |
*/ | ||
onNavigationMenuOpen?: () => void; | ||
/** | ||
* Callback function to be executed on navigation menu state changes to closed |
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.
* Callback function to be executed on navigation menu state changes to closed | |
* Callback fired when navigation menu is collapsed. |
React.useEffect(() => { | ||
if (typeof navigationMenuOpen === 'boolean') { | ||
setIsNavigationExpanded(navigationMenuOpen); | ||
if (navigationMenuOpen) { |
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.
When navigationMenuOpen
is a boolean, we cannot use the current isDesktopNavigationExpanded
or isMobileNavigationExpanded
states the way they're currently being used in this component.
So besides this effect, we need to make it so that all checks that involve isDesktopNavigationExpanded
or isMobileNavigationExpanded
check navigationMenuOpen
in those cases instead.
And every current call to setIsDesktopNavigationExpanded
or setIsMobileNavigationExpanded
can be ignored when navigationMenuOpen
is a boolean, but the appropriate onNavigationMenuOpen
or onNavigationMenuClose
should be called instead.
onNavigationMenuOpen
and onNavigationMenuClose
should also still be called when navigationMenuOpen
is undefined
, however, so that they can be used for other cases.
Your current approach requires an extra render for the effect to run, which should not be necessary, and the navigationMenuOpen
value can become out of sync with the UI when the internal state is changed.
Hopefully this makes sense and points you in the right direction!
@@ -410,4 +410,62 @@ describe('DashboardLayout', () => { | |||
// Ensure that main content is still rendered | |||
expect(screen.getByText('Hello world')).toBeTruthy(); | |||
}); | |||
|
|||
test('renders the sidebar in collapsed state when navigationMenuOpen is false', () => { |
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.
Thanks a lot for adding tests, they look good!
@@ -126,6 +126,12 @@ The layout sidebar can be hidden if needed with the `hideNavigation` prop. | |||
|
|||
{{"demo": "DashboardLayoutSidebarHidden.js", "height": 400, "iframe": true}} | |||
|
|||
### Toggle sidebar |
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.
This section needs to be a bit more complete as in referring to the whole concept of using "controlled state" for the navigation menu, as well as what kind of use case that could serve.
We also need to document the new callbacks, and how they could be used together with navigationMenuOpen
.
Anyway, I can take care of the documentation part when the functionality is done unless you really want to tackle it!
<DashboardLayout navigationMenuOpen={navigationMenuOpen}> | ||
<DemoPageContent | ||
pathname={pathname} | ||
toggleSidebar={() => toggleSidebar(!navigationMenuOpen)} |
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.
We should also show the typical usage for the new callbacks in this example, which would change the navigationMenuOpen
state accordingly.
const { window } = props; | ||
|
||
const [pathname, setPathname] = React.useState('/dashboard'); | ||
const [navigationMenuOpen, toggleSidebar] = React.useState<boolean>(true); |
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.
const [navigationMenuOpen, toggleSidebar] = React.useState<boolean>(true); | |
const [navigationMenuOpen, setNavigationMenuOpen] = React.useState<boolean>(true); |
Closes #4406
Screen.Recording.2024-12-06.at.3.00.24.PM.mov