1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/layout/sidebar-item.tsx
Alex Andreev 37318cab4e
Fix: align sidebar menu items (#6138)
* Introduce sitebar-items.module.scss

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adjust sidebar item styles

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Use classnames from a scss module

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Set sidebar min width to 150px to keep readability

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Break words in long links

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adjust navigation paddings

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Remove unused scss file

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Updating bunch of snapshots

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
2022-09-01 12:52:55 +03:00

126 lines
3.3 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./sidebar-items.module.scss";
import React from "react";
import { computed, makeObservable } from "mobx";
import type { StorageLayer } from "../../utils";
import { cssNames } from "../../utils";
import { observer } from "mobx-react";
import { NavLink } from "react-router-dom";
import { Icon } from "../icon";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { SidebarStorageState } from "./sidebar-storage/sidebar-storage.injectable";
import sidebarStorageInjectable from "./sidebar-storage/sidebar-storage.injectable";
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
interface Dependencies {
sidebarStorage: StorageLayer<SidebarStorageState>;
}
export interface SidebarItemProps {
item: HierarchicalSidebarItem;
}
@observer
class NonInjectedSidebarItem extends React.Component<
SidebarItemProps & Dependencies
> {
static displayName = "SidebarItem";
constructor(props: SidebarItemProps & Dependencies) {
super(props);
makeObservable(this);
}
get id(): string {
return this.registration.id;
}
@computed get expanded(): boolean {
return Boolean(this.props.sidebarStorage.get().expanded[this.id]);
}
@computed get isExpandable(): boolean {
return this.props.item.children.length > 0;
}
@computed get isActive(): boolean {
return this.props.item.isActive.get();
}
get registration() {
return this.props.item.registration;
}
toggleExpand = () => {
this.props.sidebarStorage.merge((draft) => {
draft.expanded[this.id] = !draft.expanded[this.id];
});
};
renderSubMenu() {
const { isExpandable, expanded } = this;
if (!isExpandable || !expanded) {
return null;
}
return (
<ul className={cssNames(styles.subMenu, { [styles.active]: this.isActive })}>
{this.props.item.children.map(item => <SidebarItem key={item.registration.id} item={item} />)}
</ul>
);
}
render() {
return (
<div
className={styles.SidebarItem}
data-testid={`sidebar-item-${this.id}`}
data-is-active-test={this.isActive}
data-parent-id-test={this.registration.parentId}
>
<NavLink
to={""}
isActive={() => this.isActive}
className={styles.navItem}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
if (this.isExpandable) {
this.toggleExpand();
} else {
this.registration.onClick();
}
}}
data-testid={`sidebar-item-link-for-${this.id}`}
>
{this.registration.getIcon?.()}
<span>{this.registration.title}</span>
{this.isExpandable && (
<Icon
className={styles.expandIcon}
material={
this.expanded ? "keyboard_arrow_up" : "keyboard_arrow_down"
}
/>
)}
</NavLink>
{this.renderSubMenu()}
</div>
);
}
}
export const SidebarItem = withInjectables<Dependencies, SidebarItemProps>(NonInjectedSidebarItem, {
getProps: (di, props) => ({
...props,
sidebarStorage: di.inject(sidebarStorageInjectable),
}),
});