The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/packages/grafana-ui/src/components/Menu/Menu.tsx

60 lines
1.8 KiB

import React, { useImperativeHandle, useRef } from 'react';
import { css } from '@emotion/css';
import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2 } from '../../themes';
import { useMenuFocus } from './hooks';
/** @internal */
export interface MenuProps extends React.HTMLAttributes<HTMLDivElement> {
/** React element rendered at the top of the menu */
header?: React.ReactNode;
children: React.ReactNode;
ariaLabel?: string;
onOpen?: (focusOnItem: (itemId: number) => void) => void;
onClose?: () => void;
onKeyDown?: React.KeyboardEventHandler;
}
/** @internal */
export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
({ header, children, ariaLabel, onOpen, onClose, onKeyDown, ...otherProps }, forwardedRef) => {
const styles = useStyles2(getStyles);
const localRef = useRef<HTMLDivElement>(null);
useImperativeHandle(forwardedRef, () => localRef.current!);
const [handleKeys, handleFocus] = useMenuFocus({ localRef, onOpen, onClose, onKeyDown });
return (
<div
{...otherProps}
ref={localRef}
className={styles.wrapper}
role="menu"
aria-label={ariaLabel}
onKeyDown={handleKeys}
onFocus={handleFocus}
>
{header && <div className={styles.header}>{header}</div>}
{children}
</div>
);
}
);
Menu.displayName = 'Menu';
/** @internal */
const getStyles = (theme: GrafanaTheme2) => {
return {
header: css`
padding: ${theme.spacing(0.5, 0.5, 1, 0.5)};
border-bottom: 1px solid ${theme.colors.border.weak};
`,
wrapper: css`
background: ${theme.colors.background.primary};
box-shadow: ${theme.shadows.z3};
display: inline-block;
border-radius: ${theme.shape.borderRadius()};
`,
};
};