2021-04-01 07:15:23 -05:00
|
|
|
import { cx } from '@emotion/css';
|
2022-02-22 08:12:34 -06:00
|
|
|
import { useDialog } from '@react-aria/dialog';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { FocusScope } from '@react-aria/focus';
|
2022-02-22 08:12:34 -06:00
|
|
|
import { OverlayContainer, useOverlay } from '@react-aria/overlays';
|
|
|
|
import React, { PropsWithChildren, useRef } from 'react';
|
2022-01-13 05:55:54 -06:00
|
|
|
|
2021-04-21 07:25:43 -05:00
|
|
|
import { useTheme2 } from '../../themes';
|
2021-01-21 06:59:46 -06:00
|
|
|
import { IconName } from '../../types';
|
2020-04-11 09:07:18 -05:00
|
|
|
import { IconButton } from '../IconButton/IconButton';
|
2021-04-26 11:26:56 -05:00
|
|
|
import { HorizontalGroup } from '../Layout/Layout';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-01-13 05:55:54 -06:00
|
|
|
import { ModalHeader } from './ModalHeader';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { getModalStyles } from './getModalStyles';
|
2019-10-03 07:30:24 -05:00
|
|
|
|
2021-01-21 06:59:46 -06:00
|
|
|
export interface Props {
|
2021-04-26 11:26:56 -05:00
|
|
|
/** @deprecated no longer used */
|
@grafana/ui: Create Icon component and replace part of the icons (#23402)
* Part1: Unicons implementation (#23197)
* Create a new Icon component
* Update icons in main sidebar
* Update icons in Useful links and in react components on main site
* Update icons in Useful links and in main top navigation
* Adjust sizing
* Update panel navigation and timepicker
* Update icons in Panel menu
* NewPanelEditor: Fixed so that test alert rule works in new edit mode (#23179)
* Update icons in add panel widget
* Resolve merge conflict
* Fix part of the test errors and type errors
* Fix storybook errors
* Update getAvailableIcons import in storybook knobs
* Fix import path
* Fix SyntaxError: Cannot use import statement outside a module in test environment error
* Remove dynamic imports
* Remove types as using @ts-ignore
* Update snapshot test
* Add @iconscout/react-unicons to the shouldExclude list as it is blundled with es2015 syntax
* Remove color prop from icon, remove color implemetation in mono icons
* Update navbar styling
* Move toPascalCase to utils/string
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
* Resolve type errors resulted from merge
* Part2: Unicons implementation (#23266)
* Create a new Icon component
* Update icons in main sidebar
* Update icons in Useful links and in react components on main site
* Update icons in Useful links and in main top navigation
* Adjust sizing
* Update panel navigation and timepicker
* Update icons in Panel menu
* Update icons in add panel widget
* Resolve merge conflict
* Fix part of the test errors and type errors
* Fix storybook errors
* Update getAvailableIcons import in storybook knobs
* Fix import path
* Fix SyntaxError: Cannot use import statement outside a module in test environment error
* Remove dynamic imports
* Remove types as using @ts-ignore
* Update snapshot test
* Add @iconscout/react-unicons to the shouldExclude list as it is blundled with es2015 syntax
* Implment icons in Tabs
* Implement icons in search items and empty list
* Update buttons
* Update button-related snapshot tests
* Update icons in modals and page headers
* Create anfular wrapper and update all icons on search screen
* Update sizing, remove colors, update snapshot tests
* Remove color prop from icon, remove color implemetation in mono icons
* Remove color props from monochrome icons
* Complete update of icons for search screen
* Update icons for infor tooltips, playlist, permissions
* Support temporarly font awesome icons used in enterprise grafana
* Part1: Unicons implementation (#23197)
* Create a new Icon component
* Update icons in main sidebar
* Update icons in Useful links and in react components on main site
* Update icons in Useful links and in main top navigation
* Adjust sizing
* Update panel navigation and timepicker
* Update icons in Panel menu
* NewPanelEditor: Fixed so that test alert rule works in new edit mode (#23179)
* Update icons in add panel widget
* Resolve merge conflict
* Fix part of the test errors and type errors
* Fix storybook errors
* Update getAvailableIcons import in storybook knobs
* Fix import path
* Fix SyntaxError: Cannot use import statement outside a module in test environment error
* Remove dynamic imports
* Remove types as using @ts-ignore
* Update snapshot test
* Add @iconscout/react-unicons to the shouldExclude list as it is blundled with es2015 syntax
* Remove color prop from icon, remove color implemetation in mono icons
* Update navbar styling
* Move toPascalCase to utils/string
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
* Icons update
* Add optional chaining to for isFontAwesome variable
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
* Part3: Unicons implementation (#23356)
* Create a new Icon component
* Update icons in main sidebar
* Update icons in Useful links and in react components on main site
* Update icons in Useful links and in main top navigation
* Adjust sizing
* Update panel navigation and timepicker
* Update icons in Panel menu
* Update icons in add panel widget
* Resolve merge conflict
* Fix part of the test errors and type errors
* Fix storybook errors
* Update getAvailableIcons import in storybook knobs
* Fix import path
* Fix SyntaxError: Cannot use import statement outside a module in test environment error
* Remove dynamic imports
* Remove types as using @ts-ignore
* Update snapshot test
* Add @iconscout/react-unicons to the shouldExclude list as it is blundled with es2015 syntax
* Implment icons in Tabs
* Implement icons in search items and empty list
* Update buttons
* Update button-related snapshot tests
* Update icons in modals and page headers
* Create anfular wrapper and update all icons on search screen
* Update sizing, remove colors, update snapshot tests
* Remove color prop from icon, remove color implemetation in mono icons
* Remove color props from monochrome icons
* Complete update of icons for search screen
* Update icons for infor tooltips, playlist, permissions
* Support temporarly font awesome icons used in enterprise grafana
* Part1: Unicons implementation (#23197)
* Create a new Icon component
* Update icons in main sidebar
* Update icons in Useful links and in react components on main site
* Update icons in Useful links and in main top navigation
* Adjust sizing
* Update panel navigation and timepicker
* Update icons in Panel menu
* NewPanelEditor: Fixed so that test alert rule works in new edit mode (#23179)
* Update icons in add panel widget
* Resolve merge conflict
* Fix part of the test errors and type errors
* Fix storybook errors
* Update getAvailableIcons import in storybook knobs
* Fix import path
* Fix SyntaxError: Cannot use import statement outside a module in test environment error
* Remove dynamic imports
* Remove types as using @ts-ignore
* Update snapshot test
* Add @iconscout/react-unicons to the shouldExclude list as it is blundled with es2015 syntax
* Remove color prop from icon, remove color implemetation in mono icons
* Update navbar styling
* Move toPascalCase to utils/string
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
* Update icons in Explore
* Update icons in alerting
* Update + and x buttons
* Update icons in configurations and settings
* Update close icons
* Update icons in rich history
* Update alert messages
* Add optional chaining to for isFontAwesome variable
* Remove icon mock, set up jest.config
* Fix navbar plus icon
* Fir enable-bacground to enableBackgournd
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
* Merge remote branch origin master to icons-unicons
* Revert "Merge remote branch origin master to icons-unicons"
This reverts commit 3f25d50a39a940883fefe73ce51219139c1ed37f.
* Size-up dashnav icons
* Fix alerting icons, panel headers, update tests
* Fix typecheck error
* Adjustments - add panel icon, spacing
* Set TerserPlugin sourceMap to false to prevent running out of memory when publishing storybook
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-04-08 07:33:31 -05:00
|
|
|
icon?: IconName;
|
2021-04-26 11:26:56 -05:00
|
|
|
/** @deprecated no longer used */
|
2020-11-05 02:53:27 -06:00
|
|
|
iconTooltip?: string;
|
2020-05-02 04:52:22 -05:00
|
|
|
/** Title for the modal or custom header element */
|
2019-10-03 07:30:24 -05:00
|
|
|
title: string | JSX.Element;
|
2019-12-10 02:57:34 -06:00
|
|
|
className?: string;
|
2021-01-21 06:59:46 -06:00
|
|
|
contentClassName?: string;
|
2021-02-03 10:01:29 -06:00
|
|
|
closeOnEscape?: boolean;
|
2021-05-14 09:53:20 -05:00
|
|
|
closeOnBackdropClick?: boolean;
|
2022-01-13 05:55:54 -06:00
|
|
|
trapFocus?: boolean;
|
2019-10-25 02:41:58 -05:00
|
|
|
|
2019-10-03 07:30:24 -05:00
|
|
|
isOpen?: boolean;
|
|
|
|
onDismiss?: () => void;
|
2019-10-25 02:41:58 -05:00
|
|
|
|
2020-05-02 04:52:22 -05:00
|
|
|
/** If not set will call onDismiss if that is set. */
|
2019-10-03 07:30:24 -05:00
|
|
|
onClickBackdrop?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-04-26 11:26:56 -05:00
|
|
|
export function Modal(props: PropsWithChildren<Props>) {
|
2021-01-21 06:59:46 -06:00
|
|
|
const {
|
|
|
|
title,
|
|
|
|
children,
|
|
|
|
isOpen = false,
|
2021-02-03 10:01:29 -06:00
|
|
|
closeOnEscape = true,
|
2021-05-14 09:53:20 -05:00
|
|
|
closeOnBackdropClick = true,
|
2021-01-21 06:59:46 -06:00
|
|
|
className,
|
|
|
|
contentClassName,
|
2022-02-22 08:12:34 -06:00
|
|
|
onDismiss,
|
2021-01-21 06:59:46 -06:00
|
|
|
onClickBackdrop,
|
2022-01-13 05:55:54 -06:00
|
|
|
trapFocus = true,
|
2021-01-21 06:59:46 -06:00
|
|
|
} = props;
|
2021-04-21 07:25:43 -05:00
|
|
|
const theme = useTheme2();
|
2021-01-21 06:59:46 -06:00
|
|
|
const styles = getModalStyles(theme);
|
2022-02-22 08:12:34 -06:00
|
|
|
|
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
// Handle interacting outside the dialog and pressing
|
|
|
|
// the Escape key to close the modal.
|
|
|
|
const { overlayProps, underlayProps } = useOverlay(
|
2022-05-24 09:34:47 -05:00
|
|
|
{ isKeyboardDismissDisabled: !closeOnEscape, isOpen, onClose: onDismiss },
|
2022-02-22 08:12:34 -06:00
|
|
|
ref
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get props for the dialog and its title
|
|
|
|
const { dialogProps, titleProps } = useDialog({}, ref);
|
2021-02-03 10:01:29 -06:00
|
|
|
|
2021-01-21 06:59:46 -06:00
|
|
|
if (!isOpen) {
|
|
|
|
return null;
|
2019-12-03 15:47:20 -06:00
|
|
|
}
|
|
|
|
|
2021-04-26 11:26:56 -05:00
|
|
|
const headerClass = cx(styles.modalHeader, typeof title !== 'string' && styles.modalHeaderWithTabs);
|
|
|
|
|
2021-01-21 06:59:46 -06:00
|
|
|
return (
|
2022-01-07 03:00:55 -06:00
|
|
|
<OverlayContainer>
|
2021-07-14 08:04:23 -05:00
|
|
|
<div
|
|
|
|
className={styles.modalBackdrop}
|
|
|
|
onClick={onClickBackdrop || (closeOnBackdropClick ? onDismiss : undefined)}
|
2022-02-22 08:12:34 -06:00
|
|
|
{...underlayProps}
|
2021-07-14 08:04:23 -05:00
|
|
|
/>
|
2022-01-13 05:55:54 -06:00
|
|
|
<FocusScope contain={trapFocus} autoFocus restoreFocus>
|
2022-02-22 08:12:34 -06:00
|
|
|
<div className={cx(styles.modal, className)} ref={ref} {...overlayProps} {...dialogProps}>
|
2021-11-24 11:16:14 -06:00
|
|
|
<div className={headerClass}>
|
2022-02-22 08:12:34 -06:00
|
|
|
{typeof title === 'string' && <DefaultModalHeader {...props} title={title} id={titleProps.id} />}
|
|
|
|
{
|
|
|
|
// FIXME: custom title components won't get an accessible title.
|
|
|
|
// Do we really want to support them or shall we just limit this ModalTabsHeader?
|
|
|
|
typeof title !== 'string' && title
|
|
|
|
}
|
2021-11-24 11:16:14 -06:00
|
|
|
<div className={styles.modalHeaderClose}>
|
2022-05-26 19:00:57 -05:00
|
|
|
<IconButton aria-label="Close dialogue" name="times" size="xl" onClick={onDismiss} />
|
2021-11-24 11:16:14 -06:00
|
|
|
</div>
|
2019-10-03 07:30:24 -05:00
|
|
|
</div>
|
2021-11-24 11:16:14 -06:00
|
|
|
<div className={cx(styles.modalContent, contentClassName)}>{children}</div>
|
2019-10-03 07:30:24 -05:00
|
|
|
</div>
|
2021-11-24 11:16:14 -06:00
|
|
|
</FocusScope>
|
2022-01-07 03:00:55 -06:00
|
|
|
</OverlayContainer>
|
2021-01-21 06:59:46 -06:00
|
|
|
);
|
2019-10-03 07:30:24 -05:00
|
|
|
}
|
2019-10-25 02:41:58 -05:00
|
|
|
|
2021-10-22 11:16:32 -05:00
|
|
|
function ModalButtonRow({ leftItems, children }: { leftItems?: React.ReactNode; children: React.ReactNode }) {
|
2021-04-26 11:26:56 -05:00
|
|
|
const theme = useTheme2();
|
|
|
|
const styles = getModalStyles(theme);
|
|
|
|
|
2021-10-22 11:16:32 -05:00
|
|
|
if (leftItems) {
|
|
|
|
return (
|
|
|
|
<div className={styles.modalButtonRow}>
|
|
|
|
<HorizontalGroup justify="space-between">
|
|
|
|
<HorizontalGroup justify="flex-start" spacing="md">
|
|
|
|
{leftItems}
|
|
|
|
</HorizontalGroup>
|
|
|
|
<HorizontalGroup justify="flex-end" spacing="md">
|
|
|
|
{children}
|
|
|
|
</HorizontalGroup>
|
|
|
|
</HorizontalGroup>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-26 11:26:56 -05:00
|
|
|
return (
|
|
|
|
<div className={styles.modalButtonRow}>
|
|
|
|
<HorizontalGroup justify="flex-end" spacing="md">
|
|
|
|
{children}
|
|
|
|
</HorizontalGroup>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.ButtonRow = ModalButtonRow;
|
|
|
|
|
2021-01-21 06:59:46 -06:00
|
|
|
interface DefaultModalHeaderProps {
|
2022-02-22 08:12:34 -06:00
|
|
|
id?: string;
|
2021-01-21 06:59:46 -06:00
|
|
|
title: string;
|
|
|
|
icon?: IconName;
|
|
|
|
iconTooltip?: string;
|
|
|
|
}
|
|
|
|
|
2022-02-22 08:12:34 -06:00
|
|
|
function DefaultModalHeader({ icon, iconTooltip, title, id }: DefaultModalHeaderProps): JSX.Element {
|
|
|
|
return <ModalHeader icon={icon} iconTooltip={iconTooltip} title={title} id={id} />;
|
2021-01-21 06:59:46 -06:00
|
|
|
}
|