From 5dce100e073122e1c066eaeae0462a9357b84171 Mon Sep 17 00:00:00 2001 From: Yash Raj Chhabra Date: Wed, 29 Jun 2022 14:59:29 +0530 Subject: [PATCH] feat: dropdown: add ability to portal --- src/components/Dropdown/Dropdown.tsx | 17 ++++++- src/components/Dropdown/Dropdown.types.ts | 5 ++ src/components/Dropdown/dropdown.module.scss | 48 ++++++++++---------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/components/Dropdown/Dropdown.tsx b/src/components/Dropdown/Dropdown.tsx index 5def8356f..c83130075 100644 --- a/src/components/Dropdown/Dropdown.tsx +++ b/src/components/Dropdown/Dropdown.tsx @@ -8,10 +8,15 @@ import React, { import { DropdownProps } from './Dropdown.types'; import { autoUpdate, shift, useFloating } from '@floating-ui/react-dom'; import { offset as fOffset } from '@floating-ui/core'; -import { mergeClasses, uniqueId } from '../../shared/utilities'; +import { + ConditionalWrapper, + mergeClasses, + uniqueId, +} from '../../shared/utilities'; import { useOnClickOutside } from '../../hooks/useOnClickOutside'; import { useAccessibility } from '../../hooks/useAccessibility'; import styles from './dropdown.module.scss'; +import { FloatingPortal } from '@floating-ui/react-dom-interactions'; const TRIGGER_TO_HANDLER_MAP_ON_ENTER = { click: 'onClick', @@ -37,6 +42,7 @@ export const Dropdown: FC = ({ dropdownStyle, children, placement = 'bottom-start', + portal = false, overlay, offset = 4, positionStrategy = 'absolute', @@ -163,7 +169,14 @@ export const Dropdown: FC = ({ : {})} > {getReference()} - {getDropdown()} + ( + {children} + )} + > + {getDropdown()} + ); }; diff --git a/src/components/Dropdown/Dropdown.types.ts b/src/components/Dropdown/Dropdown.types.ts index 0beebb74a..cb20c37ff 100644 --- a/src/components/Dropdown/Dropdown.types.ts +++ b/src/components/Dropdown/Dropdown.types.ts @@ -39,6 +39,11 @@ export interface DropdownProps { * @default bottom-start */ placement?: Placement; + /** + * If the dropdown is portaled + * @default false + */ + portal?: boolean; /** * Positioning strategy for the tooltip * @default absolute diff --git a/src/components/Dropdown/dropdown.module.scss b/src/components/Dropdown/dropdown.module.scss index c385c1205..eb49fb8da 100644 --- a/src/components/Dropdown/dropdown.module.scss +++ b/src/components/Dropdown/dropdown.module.scss @@ -2,30 +2,6 @@ display: inline-block; white-space: nowrap; - .dropdown-wrapper { - background: var(--background-color); - padding: $space-xs; - box-shadow: $shadow-object-m; - border-radius: $space-xxs; - transition: all $motion-duration-fast $motion-easing-easeout; - min-width: 280px; - opacity: 0; - white-space: normal; - z-index: $z-index-400; - max-height: 34vh; - overflow-y: auto; - - &.open { - animation: slideUpIn $motion-duration-extra-fast - $motion-easing-easeinout 0s forwards; - } - - &.close { - animation: slideUpOut $motion-duration-extra-fast - $motion-easing-easeinout 0s forwards; - } - } - .reference-wrapper { display: flex; cursor: pointer; @@ -41,3 +17,27 @@ } } } + +.dropdown-wrapper { + background: var(--background-color); + padding: $space-xs; + box-shadow: $shadow-object-m; + border-radius: $space-xxs; + transition: all $motion-duration-fast $motion-easing-easeout; + min-width: 280px; + opacity: 0; + white-space: normal; + z-index: $z-index-400; + max-height: 34vh; + overflow-y: auto; + + &.open { + animation: slideUpIn $motion-duration-extra-fast + $motion-easing-easeinout 0s forwards; + } + + &.close { + animation: slideUpOut $motion-duration-extra-fast + $motion-easing-easeinout 0s forwards; + } +}