diff --git a/src/index.tsx b/src/index.tsx index 531a573..85f470c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -7,6 +7,7 @@ const ELEMENT_TYPE_SVG = 'svg'; type BaseOptions = { attributes?: { [key: string]: string }; + fallbackMountNode?: React.MutableRefObject; }; type HtmlOptions = BaseOptions & { @@ -144,6 +145,16 @@ const createPortalNode = >( parent = undefined; lastPlaceholder = undefined; } + + if (parent === undefined && options?.fallbackMountNode?.current) { + const newParent = options?.fallbackMountNode.current; + + newParent.appendChild( + portalNode.element + ); + + parent = newParent; + } } } as AnyPortalNode; diff --git a/stories/html.stories.tsx b/stories/html.stories.tsx index 54e1b57..d4cabb2 100644 --- a/stories/html.stories.tsx +++ b/stories/html.stories.tsx @@ -426,3 +426,108 @@ export const ExampleFromREADME = () => { return }; + +export const DefaultToFallbackElementWhenNotInUse = () => { + const fallbackRef = React.useRef(null); + const portalNode = React.useMemo(() => createHtmlPortalNode({ fallbackMountNode: fallbackRef }), []); + const [showPortal, setShowPortal] = React.useState(true); + + return
+
+ + + +

+ The video below should continue playing in the background toggled. +

+ + + +
+ +

Regular OutPortal

+ + { showPortal === true && } + + +

Fallback:

+ +
+
+
+
; +}; + +export const PersistPlaybackWhilstDisplayedInAHiddenElement = () => { + const fallbackRef = React.useRef(null); + const portalNode = React.useMemo(() => createHtmlPortalNode({ fallbackMountNode: fallbackRef }), []); + const [showPortal, setShowPortal] = React.useState(true); + + return
+
+
+ + + +

+ The video below should continue playing in the background toggled. +

+ + + +
+ +
+ { showPortal === true && } +
+
+
; +}; + +export const CombineFallbackContainerAndSwitchingBetweenTwoPlaces = () => { + const fallbackRef = React.useRef(null); + const portalNode = React.useMemo(() => createHtmlPortalNode({ fallbackMountNode: fallbackRef }), []); + const [showPortal, setShowPortal] = React.useState(true); + const [secondPortalSelected, setSecondPortalSelected] = React.useState(true); + + return
+
+
+ + + +

+ The video below should continue playing in the background toggled. +

+ + + + + +
+ + { showPortal === true &&
+ +

Portal 1:

+ { !secondPortalSelected && } +
+ +

Portal 2:

+ { secondPortalSelected && } +
+
} +
+
; +}; +