-
Notifications
You must be signed in to change notification settings - Fork 631
OCPBUGS-49709: Add useOverlay hook to dynamic plugin SDK and deprecate useModal hook #14707
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
Open
kyoto
wants to merge
1
commit into
openshift:main
Choose a base branch
from
kyoto:ModalProvider-multiple-modals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−19
Open
Changes from all commits
Commits
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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
55 changes: 55 additions & 0 deletions
55
frontend/packages/console-dynamic-plugin-sdk/src/app/modal-support/OverlayProvider.tsx
This file contains hidden or 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,55 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
|
||
type CloseOverlay = () => void; | ||
type CloseOverlayContextValue = (id: string) => void; | ||
type UnknownProps = { [key: string]: unknown }; | ||
|
||
export type OverlayComponent<P = UnknownProps> = React.FC<P & { closeOverlay: CloseOverlay }>; | ||
|
||
export type LaunchOverlay = <P = UnknownProps>( | ||
component: OverlayComponent<P>, | ||
extraProps: P, | ||
) => void; | ||
|
||
type OverlayContextValue = { | ||
launchOverlay: LaunchOverlay; | ||
closeOverlay: CloseOverlayContextValue; | ||
}; | ||
|
||
export const OverlayContext = React.createContext<OverlayContextValue>({ | ||
launchOverlay: () => {}, | ||
closeOverlay: () => {}, | ||
}); | ||
|
||
type ComponentMap = { | ||
[id: string]: { | ||
Component: OverlayComponent; | ||
props: { [key: string]: any }; | ||
}; | ||
}; | ||
|
||
export const OverlayProvider: React.FC = ({ children }) => { | ||
const [componentsMap, setComponentsMap] = React.useState<ComponentMap>({}); | ||
|
||
const launchOverlay = React.useCallback<LaunchOverlay>((component, componentProps) => { | ||
const id = _.uniqueId('plugin-overlay-'); | ||
setComponentsMap((components) => ({ | ||
...components, | ||
[id]: { Component: component, props: componentProps }, | ||
})); | ||
}, []); | ||
|
||
const closeOverlay = React.useCallback<CloseOverlayContextValue>((id) => { | ||
setComponentsMap((components) => _.omit(components, id)); | ||
}, []); | ||
|
||
return ( | ||
<OverlayContext.Provider value={{ launchOverlay, closeOverlay }}> | ||
{_.map(componentsMap, (c, id) => ( | ||
<c.Component {...c.props} key={id} closeOverlay={() => closeOverlay(id)} /> | ||
))} | ||
{children} | ||
</OverlayContext.Provider> | ||
); | ||
}; |
This file contains hidden or 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
50 changes: 50 additions & 0 deletions
50
frontend/packages/console-dynamic-plugin-sdk/src/app/modal-support/useOverlay.ts
This file contains hidden or 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,50 @@ | ||
import * as React from 'react'; | ||
import { LaunchOverlay, OverlayContext } from './OverlayProvider'; | ||
|
||
type UseOverlayLauncher = () => LaunchOverlay; | ||
|
||
/** | ||
* The `useOverlay` hook inserts a component directly to the DOM, outside the web console's page structure. This allows the component to be freely styled and positioning via CSS. For example, to float the overlay in the top right corner of the UI: `style={{ position: 'absolute', right: '2rem', top: '2rem', zIndex: 999 }}`. | ||
* | ||
* It is possible to add multiple overlays by calling `useOverlay` multiple times. | ||
* | ||
* A `closeOverlay` function is passed to the overlay component. Calling it removes the component from the DOM without affecting any other overlays that may have been added via useOverlay. | ||
* | ||
* Additional props can be passed to `useOverlay` and they will be passed through to the overlay component. | ||
* @example | ||
*```tsx | ||
* const OverlayComponent = ({ closeOverlay, heading }) => { | ||
* return ( | ||
* <div style={{ position: 'absolute', right: '2rem', top: '2rem', zIndex: 999 }}> | ||
* <h2>{heading}</h2> | ||
* <Button onClick={closeOverlay}>Close</Button> | ||
* </div> | ||
* ); | ||
* }; | ||
* | ||
* const ModalComponent = ({ body, closeOverlay, title }) => ( | ||
* <Modal isOpen onClose={closeOverlay}> | ||
* <ModalHeader title={title} /> | ||
* <ModalBody>{body}</ModalBody> | ||
* </Modal> | ||
* ); | ||
* | ||
* const AppPage: React.FC = () => { | ||
* const launchOverlay = useOverlay(); | ||
* const onClickOverlay = () => { | ||
* launchOverlay(OverlayComponent, { heading: 'Test overlay' }); | ||
* }; | ||
* const onClickModal = () => { | ||
* launchOverlay(ModalComponent, { body: 'Test modal', title: 'Overlay modal' }); | ||
* }; | ||
* return ( | ||
* <Button onClick={onClickOverlay}>Launch an Overlay</Button> | ||
* <Button onClick={onClickModal}>Launch a Modal</Button> | ||
* ) | ||
* } | ||
* ``` | ||
*/ | ||
export const useOverlay: UseOverlayLauncher = () => { | ||
const { launchOverlay } = React.useContext(OverlayContext); | ||
return launchOverlay; | ||
}; |
This file contains hidden or 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
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.
@opayne1 FYI, we're deprecating a hook from the console plugin API. We should look at release noting this. Let us know what we need to do.