mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
feat(sidebar): update menu structure [EE-5666] (#10418)
This commit is contained in:
parent
b468070945
commit
a0dbabcc5f
13 changed files with 519 additions and 146 deletions
51
app/react/sidebar/SidebarItem/useSidebarSrefActive.tsx
Normal file
51
app/react/sidebar/SidebarItem/useSidebarSrefActive.tsx
Normal file
|
@ -0,0 +1,51 @@
|
|||
import {
|
||||
TransitionOptions,
|
||||
useCurrentStateAndParams,
|
||||
useSrefActive,
|
||||
} from '@uirouter/react';
|
||||
|
||||
export type PathOptions = {
|
||||
ignorePaths?: string[];
|
||||
includePaths?: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Extends useSrefActive by ignoring or including paths and updating the classNames field returned when a child route is active.
|
||||
* @param to The route to match
|
||||
* @param activeClassName The active class names to return
|
||||
* @param params The route params
|
||||
* @param options The transition options
|
||||
* @param pathOptions The paths to ignore/include
|
||||
*/
|
||||
export function useSidebarSrefActive(
|
||||
to: string,
|
||||
// default values are the classes used in the sidebar for an active item
|
||||
activeClassName: string = 'bg-blue-5/25 be:bg-gray-5/25 th-dark:bg-gray-true-5/25',
|
||||
params: Partial<Record<string, string>> = {},
|
||||
options: TransitionOptions = {},
|
||||
pathOptions: PathOptions = {
|
||||
ignorePaths: [],
|
||||
includePaths: [],
|
||||
}
|
||||
) {
|
||||
const { state: { name: stateName = '' } = {} } = useCurrentStateAndParams();
|
||||
const anchorProps = useSrefActive(to, params || {}, activeClassName, options);
|
||||
|
||||
// overwrite the className to '' if the the current route is in ignorePaths
|
||||
const isIgnorePathInRoute = pathOptions.ignorePaths?.some((path) =>
|
||||
stateName.includes(path)
|
||||
);
|
||||
if (isIgnorePathInRoute) {
|
||||
return { ...anchorProps, className: '' };
|
||||
}
|
||||
|
||||
// overwrite the className to activeClassName if the the current route is in includePaths
|
||||
const isIncludePathInRoute = pathOptions.includePaths?.some((path) =>
|
||||
stateName.includes(path)
|
||||
);
|
||||
if (isIncludePathInRoute) {
|
||||
return { ...anchorProps, className: activeClassName };
|
||||
}
|
||||
|
||||
return anchorProps;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue