Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

start popover #186

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,9 @@
"React",
"styled-components"
],
"dependencies": {}
"dependencies": {
"@floating-ui/react-dom": "^1.0.0",
"@floating-ui/react-dom-interactions": "^0.10.1",
"react-merge-refs": "^2.0.1"
}
}
124 changes: 85 additions & 39 deletions src/components/PopOver/PopOver.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,109 @@
import React, { cloneElement, useImperativeHandle } from 'react';
import React, {
cloneElement,
// useImperativeHandle,
useMemo,
} from 'react';

import { Props } from './PopOver.types';
import { PopOverStyled } from './PopOver.style';
import { ErrorBoundary } from './PopOver.error';
// import { ErrorBoundary } from './PopOver.error';
import { Minors } from './PopOver.minors';
import { useState } from 'react';
import { mergeRefs } from 'react-merge-refs';

import {
useFloating,
// Placement,
offset,
flip,
shift,
useClick,
useInteractions,
FloatingFocusManager,
useRole,
useDismiss,
autoUpdate,
} from '@floating-ui/react-dom-interactions';

import {
withIris,
usePortal_DEPRECATED,
SimpleAnimation,
// usePortal_DEPRECATED,
// SimpleAnimation,
} from '../../utils';
// import { useFloating } from '@floating-ui/react-dom';

export const PopOver = withIris<HTMLDivElement, Props, Minors>(
PopOverComponent
);

const animation: SimpleAnimation = {
enter: {
opacity: 1,
transform: 'translateY(0) scale(1)',
},
exit: {
opacity: 0,
transform: `translateY(-0.25rem) scale(0.98)`,
},
};
// const animation: SimpleAnimation = {
// enter: {
// opacity: 1,
// transform: 'translateY(0) scale(1)',
// },
// exit: {
// opacity: 0,
// transform: `translateY(-0.25rem) scale(0.98)`,
// },
// };

function PopOverComponent({
active,
// active,
attach = 'bottom',
content,
children,
forwardRef,
onClose,
onOpen,
...props
}: Props) {
const [PopOver, anchor] = usePortal_DEPRECATED(
<PopOverStyled ref={forwardRef} {...props}>
<div>{content}</div>
</PopOverStyled>,
{
attach,
animation,
forceActive: active,
onClose,
onOpen,
onClick: children.props.onClick,
}
);
}: // onClose,
// onOpen,
Props) {
const [open, setOpen] = useState(false);

// It doesn't seem like there's any official guidance on
// preserving overwritten Refs from cloneElement ¯\_(ツ)_/¯
useImperativeHandle(children?.ref, () => anchor.ref.current);
const { x, y, reference, floating, strategy, context } =
useFloating({
open,
onOpenChange: setOpen,
middleware: [offset(10), flip(), shift()],
placement: attach,
whileElementsMounted: autoUpdate,
});

const { getReferenceProps, getFloatingProps } = useInteractions([
useClick(context),
useRole(context),
useDismiss(context),
]);

const ref = useMemo(
() => mergeRefs([reference, (children as any).ref]),
[reference, children]
);

return (
<ErrorBoundary>
{PopOver}
{cloneElement(children, anchor)}
</ErrorBoundary>
<>
{cloneElement(
children,
getReferenceProps({ ref, ...children.props })
)}
{open && (
<FloatingFocusManager
context={context}
modal={false}
order={['reference', 'content']}
returnFocus={false}
>
<div
ref={floating}
style={{
position: strategy,
top: y ?? 0,
left: x ?? 0,
}}
{...getFloatingProps()}
>
<PopOverStyled ref={forwardRef}>{content}</PopOverStyled>
</div>
</FloatingFocusManager>
)}
</>
);
}
2 changes: 1 addition & 1 deletion src/components/PopOver/PopOver.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Props = IrisProps<{
* The `content` defines what will appear inside the portal component,
* whereas the `children` defines what the portal component is anchored to.
*/
content?: ReactNode;
content?: JSX.Element;
children: ReactElement & { ref?: Ref<any> };
onClose?: EventHandler<any>;
onOpen?: EventHandler<any>;
Expand Down
39 changes: 39 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,33 @@
minimatch "^3.1.2"
strip-json-comments "^3.1.1"

"@floating-ui/core@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-1.0.1.tgz#00e64d74e911602c8533957af0cce5af6b2e93c8"
integrity sha512-bO37brCPfteXQfFY0DyNDGB3+IMe4j150KFQcgJ5aBP295p9nBGeHEs/p0czrRbtlHq4Px/yoPXO/+dOCcF4uA==

"@floating-ui/dom@^1.0.0":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@floating-ui/dom/-/dom-1.0.2.tgz#c5184c52c6f50abd11052d71204f4be2d9245237"
integrity sha512-5X9WSvZ8/fjy3gDu8yx9HAA4KG1lazUN2P4/VnaXLxTO9Dz53HI1oYoh1OlhqFNlHgGDiwFX5WhFCc2ljbW3yA==
dependencies:
"@floating-ui/core" "^1.0.1"

"@floating-ui/react-dom-interactions@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@floating-ui/react-dom-interactions/-/react-dom-interactions-0.10.1.tgz#45fc7c3d9a2ae58f0ef39078660e97594f484af8"
integrity sha512-mb9Sn/cnPjVlEucSZTSt4Iu7NAvqnXTvmzeE5EtfdRhVQO6L94dqqT+DPTmJmbiw4XqzoyGP+Q6J+I5iK2p6bw==
dependencies:
"@floating-ui/react-dom" "^1.0.0"
aria-hidden "^1.1.3"

"@floating-ui/react-dom@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@floating-ui/react-dom/-/react-dom-1.0.0.tgz#e0975966694433f1f0abffeee5d8e6bb69b7d16e"
integrity sha512-uiOalFKPG937UCLm42RxjESTWUVpbbatvlphQAU6bsv+ence6IoVG8JOUZcy8eW81NkU+Idiwvx10WFLmR4MIg==
dependencies:
"@floating-ui/dom" "^1.0.0"

"@humanwhocodes/config-array@^0.10.4":
version "0.10.4"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.4.tgz#01e7366e57d2ad104feea63e72248f22015c520c"
Expand Down Expand Up @@ -5384,6 +5411,13 @@ argparse@^2.0.1:
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

aria-hidden@^1.1.3:
version "1.2.1"
resolved "https://registry.yarnpkg.com/aria-hidden/-/aria-hidden-1.2.1.tgz#ad8c1edbde360b454eb2bf717ea02da00bfee0f8"
integrity sha512-PN344VAf9j1EAi+jyVHOJ8XidQdPVssGco39eNcsGdM4wcsILtxrKLkbuiMfLWYROK1FjRQasMWCBttrhjnr6A==
dependencies:
tslib "^2.0.0"

aria-query@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-4.2.2.tgz#0d2ca6c9aceb56b8977e9fed6aed7e15bbd2f83b"
Expand Down Expand Up @@ -12742,6 +12776,11 @@ react-is@^18.0.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b"
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==

react-merge-refs@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/react-merge-refs/-/react-merge-refs-2.0.1.tgz#a1f8c2dadefa635333e9b91ec59f30b65228b006"
integrity sha512-pywF6oouJWuqL26xV3OruRSIqai31R9SdJX/I3gP2q8jLxUnA1IwXcLW8werUHLZOrp4N7YOeQNZrh/BKrHI4A==

react-refresh@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.11.0.tgz#77198b944733f0f1f1a90e791de4541f9f074046"
Expand Down