-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CConditionalPortal, CDropdown, CPopover, CTooltip): allow to app…
…end component to the specific element
- Loading branch information
Showing
10 changed files
with
140 additions
and
96 deletions.
There are no files selected for viewing
37 changes: 31 additions & 6 deletions
37
packages/coreui-react/src/components/conditional-portal/CConditionalPortal.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 |
---|---|---|
@@ -1,29 +1,54 @@ | ||
import React, { FC, ReactNode } from 'react' | ||
import React, { FC, ReactNode, useEffect, useState } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import PropTypes from 'prop-types' | ||
|
||
const getContainer = (container?: Element | (() => Element | null) | null) => { | ||
if (container) { | ||
return typeof container === 'function' ? container() : container | ||
} | ||
|
||
return document.body | ||
} | ||
|
||
export interface CConditionalPortalProps { | ||
/** | ||
* @ignore | ||
*/ | ||
children: ReactNode | ||
/** | ||
* An HTML element or function that returns a single element, with `document.body` as the default. | ||
* | ||
* @since v4.11.0 | ||
*/ | ||
container?: Element | (() => Element | null) | null | ||
/** | ||
* Render some children into a different part of the DOM | ||
*/ | ||
portal: boolean | ||
portal: boolean | any | ||
} | ||
|
||
export const CConditionalPortal: FC<CConditionalPortalProps> = ({ children, portal }) => { | ||
return typeof window !== 'undefined' && portal ? ( | ||
createPortal(children, document.body) | ||
export const CConditionalPortal: FC<CConditionalPortalProps> = ({ | ||
children, | ||
container, | ||
portal, | ||
}) => { | ||
const [_container, setContainer] = useState<ReturnType<typeof getContainer>>(null) | ||
|
||
useEffect(() => { | ||
portal && setContainer(getContainer(container) || document.body) | ||
}, [container, portal]) | ||
|
||
return typeof window !== 'undefined' && portal && _container ? ( | ||
createPortal(children, _container) | ||
) : ( | ||
<>{children}</> | ||
) | ||
} | ||
|
||
CConditionalPortal.propTypes = { | ||
children: PropTypes.node, | ||
portal: PropTypes.bool.isRequired, | ||
container: PropTypes.any, // HTMLElement | ||
portal: PropTypes.bool, | ||
} | ||
|
||
CConditionalPortal.displayName = 'CConditionalPortal' |
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
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
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
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
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
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
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
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
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