Skip to content

Commit

Permalink
chore(): reuse teleport reference from core delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-perkins committed Oct 12, 2022
1 parent 172bf6b commit 6ddb0f6
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions packages/react/src/components/createInlineOverlayComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrameworkDelegate, OverlayEventDetail } from '@ionic/core/components';
import { OverlayEventDetail } from '@ionic/core/components';
import React, { createElement } from 'react';
import { createPortal } from 'react-dom';

Expand All @@ -23,7 +23,6 @@ interface IonicReactInternalProps<ElementType> extends React.HTMLAttributes<Elem
onWillDismiss?: (event: CustomEvent<OverlayEventDetail>) => void;
onWillPresent?: (event: CustomEvent<OverlayEventDetail>) => void;
keepContentsMounted?: boolean;
delegate?: FrameworkDelegate;
}

export const createInlineOverlayComponent = <PropType, ElementType>(
Expand All @@ -41,10 +40,6 @@ export const createInlineOverlayComponent = <PropType, ElementType>(
ref: React.RefObject<HTMLElement>;
wrapperRef: React.RefObject<HTMLElement>;
stableMergedRefs: React.RefCallback<HTMLElement>;
delegate: FrameworkDelegate = {
attachViewToDom: () => Promise.resolve(this.ref.current!),
removeViewFromDom: () => Promise.resolve(),
};
// The document comment to track the original position of the element in the DOM.
teleportReference?: Comment;

Expand All @@ -61,11 +56,6 @@ export const createInlineOverlayComponent = <PropType, ElementType>(
}

componentDidMount() {
this.teleportReference = this.ref.current?.parentNode?.insertBefore(
document.createComment('ionic teleport'),
this.ref.current
);

this.componentDidUpdate(this.props);

/**
Expand All @@ -77,6 +67,8 @@ export const createInlineOverlayComponent = <PropType, ElementType>(
* destroyed and re-created.
*/
this.ref.current?.addEventListener('willPresent', (evt: any) => {
this.teleportReference = this.ref.current?.previousSibling as Comment;

this.setState({ isOpen: true });

this.props.onWillPresent && this.props.onWillPresent(evt);
Expand Down Expand Up @@ -144,7 +136,6 @@ export const createInlineOverlayComponent = <PropType, ElementType>(

const newProps: IonicReactInternalProps<PropType> = {
...propsToPass,
delegate: this.delegate,
ref: this.stableMergedRefs,
style,
};
Expand Down

0 comments on commit 6ddb0f6

Please sign in to comment.