Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,4 @@



window['STORIES'] = [{"titlePrefix":"","directory":"./src","files":"**/*.story.tsx","importPathMatcher":"^\\.[\\\\/](?:src(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.story\\.tsx)$"}];</script><script src="runtime~main.18d1c39a.iframe.bundle.js"></script><script src="4.683ff1ef.iframe.bundle.js"></script><script src="main.5f3505d3.iframe.bundle.js"></script></body></html>
window['STORIES'] = [{"titlePrefix":"","directory":"./src","files":"**/*.story.tsx","importPathMatcher":"^\\.[\\\\/](?:src(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.story\\.tsx)$"}];</script><script src="runtime~main.18d1c39a.iframe.bundle.js"></script><script src="4.683ff1ef.iframe.bundle.js"></script><script src="main.c53c429d.iframe.bundle.js"></script></body></html>
1 change: 0 additions & 1 deletion docs/main.5f3505d3.iframe.bundle.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/project.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"generatedAt":1688568222358,"builder":{"name":"webpack5"},"hasCustomBabel":false,"hasCustomWebpack":false,"hasStaticDirs":false,"hasStorybookEslint":false,"refCount":0,"packageManager":{"type":"npm","version":"8.19.3"},"storybookVersion":"6.5.14","language":"typescript","storybookPackages":{"@storybook/builder-webpack5":{"version":"6.5.14"},"@storybook/manager-webpack5":{"version":"6.5.14"},"@storybook/react":{"version":"6.5.14"}},"framework":{"name":"react"},"addons":{"@storybook/addon-essentials":{"version":"6.5.14"},"@storybook/addon-storysource":{"options":{"rule":{"test":{},"include":["/Users/mvolke/Documents/work/codastic/react-positioning-portal/src"]},"loaderOptions":{"parser":"typescript"}},"version":"6.5.14"}}}
{"generatedAt":1763140618837,"builder":{"name":"webpack5"},"hasCustomBabel":false,"hasCustomWebpack":false,"hasStaticDirs":false,"hasStorybookEslint":false,"refCount":0,"packageManager":{"type":"npm","version":"10.9.2"},"storybookVersion":"6.5.14","language":"typescript","storybookPackages":{"@storybook/builder-webpack5":{"version":"6.5.14"},"@storybook/manager-webpack5":{"version":"6.5.14"},"@storybook/react":{"version":"6.5.14"}},"framework":{"name":"react"},"addons":{"@storybook/addon-essentials":{"version":"6.5.14"},"@storybook/addon-storysource":{"options":{"rule":{"test":{},"include":["/Users/jacopobonta/workspace/react-positioning-portal/src"]},"loaderOptions":{"parser":"typescript"}},"version":"6.5.14"}}}
1 change: 1 addition & 0 deletions lib/components/PositioningPortal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare class PositioningPortal<Strategy = Position> extends React.Component<Pro
};
state: State<Strategy>;
private portalRef;
private parentRef;
componentDidMount(): void;
componentDidUpdate(prevProps: Props<Strategy>): void;
componentWillUnmount(): void;
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var PositioningPortal = /** @class */ (function (_super) {
strategy: null,
};
_this.portalRef = React.createRef();
_this.parentRef = React.createRef();
_this.close = function () {
if (_this.props.onShouldClose) {
_this.props.onShouldClose();
Expand All @@ -181,7 +182,7 @@ var PositioningPortal = /** @class */ (function (_super) {
if ((event[EVENT_CONTEXT_KEY] || []).includes(_this)) {
return;
}
var parentDom = ReactDOM.findDOMNode(_this);
var parentDom = _this.parentRef.current;
if (parentDom && parentDom.contains(event.target)) {
return;
}
Expand Down Expand Up @@ -229,7 +230,7 @@ var PositioningPortal = /** @class */ (function (_super) {
return new Promise(function (resolve) {
// A tricky way to get the first child DOM element of the fragment of this component.
// Unfortunately there seems to be no way to achieve this with refs.
var parentDom = ReactDOM.findDOMNode(_this);
var parentDom = _this.parentRef.current;
if (parentDom && parentDom.nodeType === Node.ELEMENT_NODE) {
var parentRect = parentDom.getBoundingClientRect();
var scrollParents = [];
Expand Down Expand Up @@ -340,7 +341,7 @@ var PositioningPortal = /** @class */ (function (_super) {
})), rootNode || window.document.body);
};
var shouldRenderPortal = shouldRender && (isOpen || transitionActive);
return (React.createElement(React.Fragment, null,
return (React.createElement("div", { ref: this.parentRef },
children,
shouldRenderPortal && renderPortal()));
};
Expand Down
1 change: 1 addition & 0 deletions lib/legacy/index.js/components/PositioningPortal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare class PositioningPortal<Strategy = Position> extends React.Component<Pro
};
state: State<Strategy>;
private portalRef;
private parentRef;
componentDidMount(): void;
componentDidUpdate(prevProps: Props<Strategy>): void;
componentWillUnmount(): void;
Expand Down
7 changes: 4 additions & 3 deletions lib/legacy/index.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ var PositioningPortal = /** @class */ (function (_super) {
strategy: null,
};
_this.portalRef = React__namespace.createRef();
_this.parentRef = React__namespace.createRef();
_this.close = function () {
if (_this.props.onShouldClose) {
_this.props.onShouldClose();
Expand All @@ -203,7 +204,7 @@ var PositioningPortal = /** @class */ (function (_super) {
if ((event[EVENT_CONTEXT_KEY] || []).includes(_this)) {
return;
}
var parentDom = ReactDOM__namespace.findDOMNode(_this);
var parentDom = _this.parentRef.current;
if (parentDom && parentDom.contains(event.target)) {
return;
}
Expand Down Expand Up @@ -251,7 +252,7 @@ var PositioningPortal = /** @class */ (function (_super) {
return new Promise(function (resolve) {
// A tricky way to get the first child DOM element of the fragment of this component.
// Unfortunately there seems to be no way to achieve this with refs.
var parentDom = ReactDOM__namespace.findDOMNode(_this);
var parentDom = _this.parentRef.current;
if (parentDom && parentDom.nodeType === Node.ELEMENT_NODE) {
var parentRect = parentDom.getBoundingClientRect();
var scrollParents = [];
Expand Down Expand Up @@ -362,7 +363,7 @@ var PositioningPortal = /** @class */ (function (_super) {
})), rootNode || window.document.body);
};
var shouldRenderPortal = shouldRender && (isOpen || transitionActive);
return (React__namespace.createElement(React__namespace.Fragment, null,
return (React__namespace.createElement("div", { ref: this.parentRef },
children,
shouldRenderPortal && renderPortal()));
};
Expand Down
19 changes: 10 additions & 9 deletions src/components/PositioningPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface Props<Strategy> {
children: React.ReactNode;
portalElement?: React.ReactElement;
portalContent:
| React.ReactNode
| ((params: PortalContentRenderProps<Strategy>) => React.ReactNode);
| React.ReactNode
| ((params: PortalContentRenderProps<Strategy>) => React.ReactNode);
onOpen?: () => void;
onClose?: () => void;
onShouldClose?: () => void;
Expand Down Expand Up @@ -53,7 +53,7 @@ const renderProps: <Strategy>(
element: Props<Strategy>['portalContent'],
props: PortalContentRenderProps<Strategy>
) => React.ReactNode = (element, props) =>
typeof element === 'function' ? element(props) : element;
typeof element === 'function' ? element(props) : element;

export const defaultPositionStrategy: PositioningStrategy<Position> = (
parentRect: ClientRect,
Expand All @@ -64,7 +64,7 @@ export const defaultPositionStrategy: PositioningStrategy<Position> = (
// but if there also isn't enough space at the top, open to the bottom.
const openAbove =
parentRect.top + parentRect.height + portalRect.height >
(window.document.documentElement || window.document.body).clientHeight &&
(window.document.documentElement || window.document.body).clientHeight &&
parentRect.top - portalRect.height > 0;

const top = openAbove
Expand All @@ -75,7 +75,7 @@ export const defaultPositionStrategy: PositioningStrategy<Position> = (
// but if there also isn't enough space at the right, open to the left.
const alignRight =
parentRect.left + portalRect.width >
(window.document.documentElement || window.document.body).clientWidth &&
(window.document.documentElement || window.document.body).clientWidth &&
parentRect.left - portalRect.width > 0;

const left = !alignRight
Expand Down Expand Up @@ -151,6 +151,7 @@ class PositioningPortal<Strategy = Position> extends React.Component<
};

private portalRef = React.createRef<HTMLDivElement>();
private parentRef = React.createRef<HTMLDivElement>();

public componentDidMount() {
window.document.addEventListener('keydown', this.handleKeydown, false);
Expand Down Expand Up @@ -232,7 +233,7 @@ class PositioningPortal<Strategy = Position> extends React.Component<
return;
}

const parentDom = ReactDOM.findDOMNode(this);
const parentDom = this.parentRef.current;
if (parentDom && parentDom.contains(event.target as Node)) {
return;
}
Expand Down Expand Up @@ -299,7 +300,7 @@ class PositioningPortal<Strategy = Position> extends React.Component<
new Promise<void>((resolve) => {
// A tricky way to get the first child DOM element of the fragment of this component.
// Unfortunately there seems to be no way to achieve this with refs.
const parentDom = ReactDOM.findDOMNode(this);
const parentDom = this.parentRef.current;

if (parentDom && parentDom.nodeType === Node.ELEMENT_NODE) {
const parentRect = (parentDom as Element).getBoundingClientRect();
Expand Down Expand Up @@ -417,10 +418,10 @@ class PositioningPortal<Strategy = Position> extends React.Component<
const shouldRenderPortal = shouldRender && (isOpen || transitionActive);

return (
<>
<div ref={this.parentRef}>
{children}
{shouldRenderPortal && renderPortal()}
</>
</div>
);
}
}
Expand Down