-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Modal next): Introduce a next composable Modal #9852
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed0b429
feat(Modal next): Introduce a next composble Modal
tlabaj dac87ea
upodate for failing tests
tlabaj b071537
add integration test
tlabaj aefc469
updates from review
tlabaj 99f4814
updates from Erin's comments
tlabaj 924534f
Updates from comments
tlabaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
packages/react-core/src/next/components/Modal/Modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import { canUseDOM, KeyTypes, PickOptional } from '../../../helpers'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/react-styles/css/components/Backdrop/backdrop'; | ||
import { ModalContent } from './ModalContent'; | ||
import { OUIAProps, getDefaultOUIAId } from '../../../helpers'; | ||
|
||
export interface ModalProps extends React.HTMLProps<HTMLDivElement>, OUIAProps { | ||
/** The parent container to append the modal to. Defaults to "document.body". */ | ||
appendTo?: HTMLElement | (() => HTMLElement); | ||
/** Id to use for the modal box description. This should match the ModalHeader labelId or descriptorId. */ | ||
'aria-describedby'?: string; | ||
/** Adds an accessible name to the modal when there is no title in the ModalHeader. */ | ||
'aria-label'?: string; | ||
/** Id to use for the modal box label. This should include the ModalHeader labelId. */ | ||
'aria-labelledby'?: string; | ||
/** Content rendered inside the modal. */ | ||
children: React.ReactNode; | ||
/** Additional classes added to the modal. */ | ||
className?: string; | ||
/** Flag to disable focus trap. */ | ||
disableFocusTrap?: boolean; | ||
/** The element to focus when the modal opens. By default the first | ||
* focusable element will receive focus. | ||
*/ | ||
elementToFocus?: HTMLElement | SVGElement | string; | ||
/** An id to use for the modal box container. */ | ||
id?: string; | ||
/** Flag to show the modal. */ | ||
isOpen?: boolean; | ||
/** Add callback for when the close button is clicked. This prop needs to be passed to render the close button */ | ||
onClose?: (event: KeyboardEvent | React.MouseEvent) => void; | ||
/** Modal handles pressing of the escape key and closes the modal. If you want to handle | ||
* this yourself you can use this callback function. */ | ||
onEscapePress?: (event: KeyboardEvent) => void; | ||
/** Position of the modal. By default a modal will be positioned vertically and horizontally centered. */ | ||
position?: 'default' | 'top'; | ||
/** Offset from alternate position. Can be any valid CSS length/percentage. */ | ||
positionOffset?: string; | ||
/** Variant of the modal. */ | ||
variant?: 'small' | 'medium' | 'large' | 'default'; | ||
/** Default width of the modal. */ | ||
width?: number | string; | ||
/** Maximum width of the modal. */ | ||
maxWidth?: number | string; | ||
/** Value to overwrite the randomly generated data-ouia-component-id.*/ | ||
ouiaId?: number | string; | ||
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ | ||
ouiaSafe?: boolean; | ||
} | ||
|
||
export enum ModalVariant { | ||
small = 'small', | ||
medium = 'medium', | ||
large = 'large', | ||
default = 'default' | ||
} | ||
|
||
interface ModalState { | ||
container: HTMLElement; | ||
ouiaStateId: string; | ||
} | ||
|
||
class Modal extends React.Component<ModalProps, ModalState> { | ||
static displayName = 'Modal'; | ||
static currentId = 0; | ||
boxId = ''; | ||
|
||
static defaultProps: PickOptional<ModalProps> = { | ||
isOpen: false, | ||
variant: 'default', | ||
appendTo: () => document.body, | ||
ouiaSafe: true, | ||
position: 'default' | ||
}; | ||
|
||
constructor(props: ModalProps) { | ||
super(props); | ||
const boxIdNum = Modal.currentId++; | ||
this.boxId = props.id || `pf-modal-part-${boxIdNum}`; | ||
|
||
this.state = { | ||
container: undefined, | ||
ouiaStateId: getDefaultOUIAId(Modal.displayName, props.variant) | ||
}; | ||
} | ||
|
||
handleEscKeyClick = (event: KeyboardEvent): void => { | ||
const { onEscapePress } = this.props; | ||
if (event.key === KeyTypes.Escape && this.props.isOpen) { | ||
onEscapePress ? onEscapePress(event) : this.props.onClose?.(event); | ||
} | ||
}; | ||
|
||
getElement = (appendTo: HTMLElement | (() => HTMLElement)) => { | ||
if (typeof appendTo === 'function') { | ||
return appendTo(); | ||
} | ||
return appendTo || document.body; | ||
}; | ||
|
||
toggleSiblingsFromScreenReaders = (hide: boolean) => { | ||
const { appendTo } = this.props; | ||
const target: HTMLElement = this.getElement(appendTo); | ||
const bodyChildren = target.children; | ||
for (const child of Array.from(bodyChildren)) { | ||
if (child !== this.state.container) { | ||
hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden'); | ||
} | ||
} | ||
}; | ||
|
||
isEmpty = (value: string | null | undefined) => value === null || value === undefined || value === ''; | ||
|
||
componentDidMount() { | ||
const { | ||
appendTo, | ||
'aria-describedby': ariaDescribedby, | ||
'aria-label': ariaLabel, | ||
'aria-labelledby': ariaLabelledby | ||
} = this.props; | ||
const target: HTMLElement = this.getElement(appendTo); | ||
const container = document.createElement('div'); | ||
this.setState({ container }); | ||
target.appendChild(container); | ||
target.addEventListener('keydown', this.handleEscKeyClick, false); | ||
|
||
if (this.props.isOpen) { | ||
target.classList.add(css(styles.backdropOpen)); | ||
} else { | ||
target.classList.remove(css(styles.backdropOpen)); | ||
} | ||
|
||
if (!ariaDescribedby && !ariaLabel && !ariaLabelledby) { | ||
// eslint-disable-next-line no-console | ||
console.error('Modal: Specify at least one of: aria-describedby, aria-label, aria-labelledby.'); | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
const { appendTo } = this.props; | ||
const target: HTMLElement = this.getElement(appendTo); | ||
if (this.props.isOpen) { | ||
target.classList.add(css(styles.backdropOpen)); | ||
this.toggleSiblingsFromScreenReaders(true); | ||
} else { | ||
target.classList.remove(css(styles.backdropOpen)); | ||
this.toggleSiblingsFromScreenReaders(false); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
const { appendTo } = this.props; | ||
const target: HTMLElement = this.getElement(appendTo); | ||
if (this.state.container) { | ||
target.removeChild(this.state.container); | ||
} | ||
target.removeEventListener('keydown', this.handleEscKeyClick, false); | ||
target.classList.remove(css(styles.backdropOpen)); | ||
this.toggleSiblingsFromScreenReaders(false); | ||
} | ||
|
||
render() { | ||
const { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
appendTo, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
onEscapePress, | ||
'aria-labelledby': ariaLabelledby, | ||
'aria-label': ariaLabel, | ||
'aria-describedby': ariaDescribedby, | ||
ouiaId, | ||
ouiaSafe, | ||
position, | ||
elementToFocus, | ||
...props | ||
} = this.props; | ||
const { container } = this.state; | ||
|
||
if (!canUseDOM || !container) { | ||
return null; | ||
} | ||
|
||
return ReactDOM.createPortal( | ||
<ModalContent | ||
boxId={this.boxId} | ||
aria-label={ariaLabel} | ||
aria-describedby={ariaDescribedby} | ||
aria-labelledby={ariaLabelledby} | ||
ouiaId={ouiaId !== undefined ? ouiaId : this.state.ouiaStateId} | ||
ouiaSafe={ouiaSafe} | ||
position={position} | ||
elementToFocus={elementToFocus} | ||
{...props} | ||
/>, | ||
container | ||
) as React.ReactElement; | ||
} | ||
} | ||
|
||
export { Modal }; |
42 changes: 42 additions & 0 deletions
42
packages/react-core/src/next/components/Modal/ModalBody.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/react-styles/css/components/ModalBox/modal-box'; | ||
|
||
/** Renders content in the body of the modal */ | ||
|
||
export interface ModalBodyProps extends React.HTMLProps<HTMLDivElement> { | ||
/** Content rendered inside the modal body. */ | ||
children?: React.ReactNode; | ||
/** Additional classes added to the modal body. */ | ||
className?: string; | ||
/** Accessible label applied to the modal body. This should be used to communicate | ||
* important information about the modal body div element if needed, such as when it is scrollable. | ||
*/ | ||
'aria-label'?: string; | ||
/** Accessible role applied to the modal body. This will default to "region" if the | ||
* aria-label property is passed in. Set to a more appropriate role as applicable | ||
* based on the modal content and context. | ||
*/ | ||
role?: string; | ||
} | ||
|
||
export const ModalBody: React.FunctionComponent<ModalBodyProps> = ({ | ||
children, | ||
className, | ||
'aria-label': ariaLabel, | ||
role, | ||
...props | ||
}: ModalBodyProps) => { | ||
const defaultModalBodyRole = ariaLabel ? 'region' : undefined; | ||
return ( | ||
<div | ||
aria-label={ariaLabel} | ||
role={role || defaultModalBodyRole} | ||
className={css(styles.modalBoxBody, className)} | ||
{...props} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
ModalBody.displayName = 'ModalBody'; |
63 changes: 63 additions & 0 deletions
63
packages/react-core/src/next/components/Modal/ModalBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/react-styles/css/components/ModalBox/modal-box'; | ||
import topSpacer from '@patternfly/react-tokens/dist/esm/c_modal_box_m_align_top_spacer'; | ||
|
||
export interface ModalBoxProps extends React.HTMLProps<HTMLDivElement> { | ||
/** Id to use for the modal box description. This should match the ModalHeader labelId or descriptorId */ | ||
'aria-describedby'?: string; | ||
/** Adds an accessible name to the modal when there is no title in the ModalHeader. */ | ||
'aria-label'?: string; | ||
/** Id to use for the modal box label. */ | ||
'aria-labelledby'?: string; | ||
/** Content rendered inside the modal box. */ | ||
children: React.ReactNode; | ||
/** Additional classes added to the modal box. */ | ||
className?: string; | ||
/** Position of the modal. By default a modal will be positioned vertically and horizontally centered. */ | ||
position?: 'default' | 'top'; | ||
/** Offset from alternate position. Can be any valid CSS length/percentage. */ | ||
positionOffset?: string; | ||
/** Variant of the modal. */ | ||
variant?: 'small' | 'medium' | 'large' | 'default'; | ||
} | ||
|
||
export const ModalBox: React.FunctionComponent<ModalBoxProps> = ({ | ||
children, | ||
className, | ||
variant = 'default', | ||
position, | ||
positionOffset, | ||
'aria-labelledby': ariaLabelledby, | ||
'aria-label': ariaLabel, | ||
'aria-describedby': ariaDescribedby, | ||
style, | ||
...props | ||
}: ModalBoxProps) => { | ||
if (positionOffset) { | ||
style = style || {}; | ||
(style as any)[topSpacer.name] = positionOffset; | ||
} | ||
return ( | ||
<div | ||
role="dialog" | ||
aria-label={ariaLabel || null} | ||
aria-labelledby={ariaLabelledby || null} | ||
aria-describedby={ariaDescribedby} | ||
aria-modal="true" | ||
className={css( | ||
styles.modalBox, | ||
className, | ||
position === 'top' && styles.modifiers.alignTop, | ||
variant === 'large' && styles.modifiers.lg, | ||
variant === 'small' && styles.modifiers.sm, | ||
variant === 'medium' && styles.modifiers.md | ||
)} | ||
style={style} | ||
{...props} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
ModalBox.displayName = 'ModalBox'; |
38 changes: 38 additions & 0 deletions
38
packages/react-core/src/next/components/Modal/ModalBoxCloseButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/react-styles/css/components/ModalBox/modal-box'; | ||
import { Button } from '../../../components/Button'; | ||
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon'; | ||
import { OUIAProps } from '../../../helpers'; | ||
|
||
export interface ModalBoxCloseButtonProps extends OUIAProps { | ||
/** Additional classes added to the close button. */ | ||
className?: string; | ||
/** A callback for when the close button is clicked. */ | ||
onClose?: (event: KeyboardEvent | React.MouseEvent) => void; | ||
/** Accessible descriptor of the close button. */ | ||
'aria-label'?: string; | ||
/** Value to set the data-ouia-component-id.*/ | ||
ouiaId?: number | string; | ||
} | ||
|
||
export const ModalBoxCloseButton: React.FunctionComponent<ModalBoxCloseButtonProps> = ({ | ||
className, | ||
onClose, | ||
'aria-label': ariaLabel = 'Close', | ||
ouiaId, | ||
...props | ||
}: ModalBoxCloseButtonProps) => ( | ||
<div className={css(styles.modalBoxClose, className)}> | ||
<Button | ||
variant="plain" | ||
onClick={(event) => onClose(event)} | ||
aria-label={ariaLabel} | ||
{...(ouiaId && { ouiaId: `${ouiaId}-${ModalBoxCloseButton.displayName}` })} | ||
{...props} | ||
> | ||
<TimesIcon /> | ||
</Button> | ||
</div> | ||
); | ||
ModalBoxCloseButton.displayName = 'ModalBoxCloseButton'; |
24 changes: 24 additions & 0 deletions
24
packages/react-core/src/next/components/Modal/ModalBoxDescription.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/react-styles/css/components/ModalBox/modal-box'; | ||
|
||
export interface ModalBoxDescriptionProps { | ||
/** Content rendered inside the description. */ | ||
children?: React.ReactNode; | ||
/** Additional classes added to the description. */ | ||
className?: string; | ||
/** Id of the description. */ | ||
id?: string; | ||
} | ||
|
||
export const ModalBoxDescription: React.FunctionComponent<ModalBoxDescriptionProps> = ({ | ||
children = null, | ||
className = '', | ||
id = '', | ||
...props | ||
}: ModalBoxDescriptionProps) => ( | ||
<div {...props} id={id} className={css(styles.modalBoxDescription, className)}> | ||
{children} | ||
</div> | ||
); | ||
ModalBoxDescription.displayName = 'ModalBoxDescription'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update components to functional components