2022-01-25 02:04:44 -06:00
|
|
|
import { css, cx } from '@emotion/css';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React, { ReactNode } from 'react';
|
|
|
|
|
2022-01-25 02:04:44 -06:00
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
|
|
|
import { Link, useTheme2 } from '@grafana/ui';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-01-25 02:04:44 -06:00
|
|
|
import { NavFeatureHighlight } from './NavFeatureHighlight';
|
2021-12-05 23:58:17 -06:00
|
|
|
|
|
|
|
export interface NavBarItemWithoutMenuProps {
|
|
|
|
label: string;
|
|
|
|
children: ReactNode;
|
|
|
|
className?: string;
|
2022-06-16 04:48:38 -05:00
|
|
|
elClassName?: string;
|
2021-12-05 23:58:17 -06:00
|
|
|
url?: string;
|
|
|
|
target?: string;
|
|
|
|
isActive?: boolean;
|
|
|
|
onClick?: () => void;
|
2022-01-25 02:04:44 -06:00
|
|
|
highlightText?: string;
|
2021-12-05 23:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function NavBarItemWithoutMenu({
|
|
|
|
label,
|
|
|
|
children,
|
|
|
|
url,
|
|
|
|
target,
|
|
|
|
isActive = false,
|
|
|
|
onClick,
|
2022-01-25 02:04:44 -06:00
|
|
|
highlightText,
|
2022-06-16 04:48:38 -05:00
|
|
|
className,
|
|
|
|
elClassName,
|
2021-12-05 23:58:17 -06:00
|
|
|
}: NavBarItemWithoutMenuProps) {
|
|
|
|
const theme = useTheme2();
|
|
|
|
const styles = getNavBarItemWithoutMenuStyles(theme, isActive);
|
|
|
|
|
2022-01-25 02:04:44 -06:00
|
|
|
const content = highlightText ? (
|
|
|
|
<NavFeatureHighlight>
|
2022-06-16 04:48:38 -05:00
|
|
|
<div className={styles.icon}>{children}</div>
|
2022-01-25 02:04:44 -06:00
|
|
|
</NavFeatureHighlight>
|
|
|
|
) : (
|
2022-06-16 04:48:38 -05:00
|
|
|
<div className={styles.icon}>{children}</div>
|
2022-01-25 02:04:44 -06:00
|
|
|
);
|
|
|
|
|
2022-06-16 04:48:38 -05:00
|
|
|
const elStyle = cx(styles.element, elClassName);
|
|
|
|
|
|
|
|
const renderContents = () => {
|
|
|
|
if (!url) {
|
|
|
|
return (
|
|
|
|
<button className={elStyle} onClick={onClick} aria-label={label}>
|
2022-01-25 02:04:44 -06:00
|
|
|
{content}
|
2021-12-05 23:58:17 -06:00
|
|
|
</button>
|
2022-06-16 04:48:38 -05:00
|
|
|
);
|
|
|
|
} else if (!target && url.startsWith('/')) {
|
|
|
|
return (
|
|
|
|
<Link className={elStyle} href={url} target={target} aria-label={label} onClick={onClick} aria-haspopup="true">
|
|
|
|
{content}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<a href={url} target={target} className={elStyle} onClick={onClick} aria-label={label}>
|
|
|
|
{content}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return <div className={cx(styles.container, className)}>{renderContents()}</div>;
|
2021-12-05 23:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getNavBarItemWithoutMenuStyles(theme: GrafanaTheme2, isActive?: boolean) {
|
|
|
|
return {
|
2022-06-16 04:48:38 -05:00
|
|
|
container: css({
|
|
|
|
position: 'relative',
|
|
|
|
color: isActive ? theme.colors.text.primary : theme.colors.text.secondary,
|
|
|
|
display: 'grid',
|
2021-12-05 23:58:17 -06:00
|
|
|
|
2022-06-16 04:48:38 -05:00
|
|
|
'&:hover': {
|
|
|
|
backgroundColor: theme.colors.action.hover,
|
|
|
|
color: theme.colors.text.primary,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
element: css({
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
border: 'none',
|
|
|
|
color: 'inherit',
|
|
|
|
display: 'block',
|
|
|
|
padding: 0,
|
|
|
|
overflowWrap: 'anywhere',
|
2021-12-05 23:58:17 -06:00
|
|
|
|
2022-06-16 04:48:38 -05:00
|
|
|
'&::before': {
|
|
|
|
display: isActive ? 'block' : 'none',
|
|
|
|
content: "' '",
|
|
|
|
position: 'absolute',
|
|
|
|
left: theme.spacing(1),
|
|
|
|
top: theme.spacing(1.5),
|
|
|
|
bottom: theme.spacing(1.5),
|
|
|
|
width: theme.spacing(0.5),
|
|
|
|
borderRadius: theme.shape.borderRadius(1),
|
|
|
|
backgroundImage: theme.colors.gradients.brandVertical,
|
|
|
|
},
|
2021-12-05 23:58:17 -06:00
|
|
|
|
2022-06-16 04:48:38 -05:00
|
|
|
'&:focus-visible': {
|
|
|
|
backgroundColor: theme.colors.action.hover,
|
|
|
|
boxShadow: 'none',
|
|
|
|
color: theme.colors.text.primary,
|
|
|
|
outline: `${theme.shape.borderRadius(1)} solid ${theme.colors.primary.main}`,
|
|
|
|
outlineOffset: `-${theme.shape.borderRadius(1)}`,
|
|
|
|
transition: 'none',
|
|
|
|
},
|
|
|
|
}),
|
2022-01-25 02:04:44 -06:00
|
|
|
|
2022-06-16 04:48:38 -05:00
|
|
|
icon: css({
|
|
|
|
height: '100%',
|
|
|
|
width: '100%',
|
|
|
|
}),
|
2021-12-05 23:58:17 -06:00
|
|
|
};
|
|
|
|
}
|