Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SelectInputV2): fix dropdown align on left #4423

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/soft-swans-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ultraviolet/ui": patch
---

Fix `<SelectInputV2 />` to align on the left when dropdown is bigger than the actual input
7 changes: 6 additions & 1 deletion packages/ui/src/components/MenuV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ type MenuProps = {
* If set to `hover`, the menu will open when the user hovers over the disclosure.
*/
triggerMethod?: 'click' | 'hover'
} & Pick<ComponentProps<typeof Popup>, 'placement' | 'dynamicDomRendering'>
} & Pick<
ComponentProps<typeof Popup>,
'placement' | 'dynamicDomRendering' | 'align'
>

const FwdMenu = forwardRef(
(
Expand All @@ -132,6 +135,7 @@ const FwdMenu = forwardRef(
size = 'small',
triggerMethod = 'click',
dynamicDomRendering,
align,
}: MenuProps,
ref: Ref<HTMLButtonElement | null>,
) => {
Expand Down Expand Up @@ -188,6 +192,7 @@ const FwdMenu = forwardRef(
}
portalTarget={portalTarget}
dynamicDomRendering={dynamicDomRendering}
align={align}
>
{finalDisclosure}
</StyledPopup>
Expand Down
7 changes: 6 additions & 1 deletion packages/ui/src/components/Popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ type PopoverProps = {
* behavior by setting a portalTarget prop.
*/
portalTarget?: HTMLElement
} & Pick<ComponentProps<typeof Popup>, 'placement' | 'dynamicDomRendering'>
} & Pick<
ComponentProps<typeof Popup>,
'placement' | 'dynamicDomRendering' | 'align'
>

/**
* Popover component is used to display additional information or actions on top of the main content of the page.
Expand All @@ -140,6 +143,7 @@ export const Popover = forwardRef(
'data-testid': dataTestId,
portalTarget,
dynamicDomRendering,
align,
}: PopoverProps,
ref: Ref<HTMLDivElement>,
) => {
Expand Down Expand Up @@ -184,6 +188,7 @@ export const Popover = forwardRef(
maxHeight={maxHeight}
portalTarget={portalTarget}
dynamicDomRendering={dynamicDomRendering}
align={align}
>
{children}
</StyledPopup>
Expand Down
61 changes: 39 additions & 22 deletions packages/ui/src/components/Popup/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RefObject } from 'react'

export type PopupPlacement = 'top' | 'right' | 'bottom' | 'left' | 'auto'
export type PopupAlign = 'start' | 'center'
export const DEFAULT_ARROW_WIDTH = 8 // in px
const SPACE = 4 // in px
const TOTAL_USED_SPACE = 0 // in px
Expand Down Expand Up @@ -192,6 +193,7 @@
popupRef: RefObject<HTMLDivElement>
popupPortalTarget: HTMLElement
hasArrow: boolean
align: PopupAlign
}

/**
Expand All @@ -203,6 +205,7 @@
popupRef,
popupPortalTarget,
hasArrow,
align,
}: ComputePositionsTypes) => {
const arrowWidth = hasArrow ? DEFAULT_ARROW_WIDTH : 0
const childrenRect = (
Expand Down Expand Up @@ -269,10 +272,13 @@
arrowWidth,
)

const isAligned = align === 'start'

switch (placementBasedOnWindowSize) {
case 'bottom': {
const positionX =
overloadedChildrenLeft + childrenWidth / 2 - popupWidth / 2
const positionX = isAligned
? overloadedChildrenLeft
: overloadedChildrenLeft + childrenWidth / 2 - popupWidth / 2
const positionY =
overloadedChildrenTop +
scrollTopValue +
Expand All @@ -281,31 +287,36 @@
SPACE

return {
arrowLeft: popupWidth / 2 + popupOverflow * -1,
arrowLeft: isAligned
? childrenWidth / 2 - arrowWidth
: popupWidth / 2 + popupOverflow * -1,
arrowTop: -arrowWidth - 5,
arrowTransform: '',
placement: 'bottom',
rotate: 180,
popupInitialPosition: `translate3d(${positionX + popupOverflow}px, ${
popupInitialPosition: `translate3d(${!isAligned ? positionX + popupOverflow : positionX}px, ${
positionY - TOTAL_USED_SPACE
}px, 0)`,
popupPosition: `translate3d(${
positionX + popupOverflow
!isAligned ? positionX + popupOverflow : positionX
}px, ${positionY}px, 0)`,
}
}
case 'left': {
const positionX =
overloadedChildrenLeft - popupWidth - arrowWidth - SPACE * 2
const positionY =
overloadedChildrenTop +
scrollTopValue -
popupHeight / 2 +
childrenHeight / 2
const positionY = isAligned
? overloadedChildrenTop + scrollTopValue

Check warning on line 309 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L309

Added line #L309 was not covered by tests
: overloadedChildrenTop +
scrollTopValue -
popupHeight / 2 +
childrenHeight / 2

return {
arrowLeft: popupWidth + arrowWidth + 5,
arrowTop: popupHeight / 2 + popupOverflow * -1,
arrowTop: isAligned
? childrenHeight / 2 - arrowWidth

Check warning on line 318 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L318

Added line #L318 was not covered by tests
: popupHeight / 2 + popupOverflow * -1,
arrowTransform: 'translate(-50%, -50%)',
placement: 'left',
rotate: -90,
Expand All @@ -319,15 +330,18 @@
}
case 'right': {
const positionX = overloadedChildrenRight + arrowWidth + SPACE * 2
const positionY =
overloadedChildrenTop +
scrollTopValue -
popupHeight / 2 +
childrenHeight / 2
const positionY = isAligned
? overloadedChildrenTop + scrollTopValue

Check warning on line 334 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L334

Added line #L334 was not covered by tests
: overloadedChildrenTop +
scrollTopValue -
popupHeight / 2 +
childrenHeight / 2

return {
arrowLeft: -arrowWidth - 5,
arrowTop: popupHeight / 2 + popupOverflow * -1,
arrowTop: isAligned
? childrenHeight / 2 - arrowWidth

Check warning on line 343 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L343

Added line #L343 was not covered by tests
: popupHeight / 2 + popupOverflow * -1,
arrowTransform: 'translate(50%, -50%)',
placement: 'right',
rotate: 90,
Expand All @@ -341,8 +355,9 @@
}
default: {
// top placement is default value
const positionX =
overloadedChildrenLeft + childrenWidth / 2 - popupWidth / 2
const positionX = isAligned
? overloadedChildrenLeft

Check warning on line 359 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L359

Added line #L359 was not covered by tests
: overloadedChildrenLeft + childrenWidth / 2 - popupWidth / 2
const positionY =
overloadedChildrenTop +
scrollTopValue -
Expand All @@ -351,16 +366,18 @@
SPACE

return {
arrowLeft: popupWidth / 2 + popupOverflow * -1,
arrowLeft: isAligned
? childrenWidth / 2 - arrowWidth

Check warning on line 370 in packages/ui/src/components/Popup/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Popup/helpers.ts#L370

Added line #L370 was not covered by tests
: popupWidth / 2 + popupOverflow * -1,
arrowTop: popupHeight - 1,
arrowTransform: '',
placement: 'top',
rotate: 0,
popupInitialPosition: `translate3d(${positionX + popupOverflow}px, ${
popupInitialPosition: `translate3d(${!isAligned ? positionX + popupOverflow : positionX}px, ${
positionY + TOTAL_USED_SPACE
}px, 0)`,
popupPosition: `translate3d(${
positionX + popupOverflow
!isAligned ? positionX + popupOverflow : positionX
}px, ${positionY}px, 0)`,
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/ui/src/components/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createPortal } from 'react-dom'
import { isClientSide } from '../../helpers/isClientSide'
import type { PositionsType } from './animations'
import { animation, exitAnimation } from './animations'
import type { PopupPlacement } from './helpers'
import type { PopupAlign, PopupPlacement } from './helpers'
import {
DEFAULT_ARROW_WIDTH,
DEFAULT_POSITIONS,
Expand Down Expand Up @@ -133,6 +133,10 @@ type PopupProps = {
* `auto` placement will change the position of the popup if it doesn't fit in the viewport.
*/
placement?: PopupPlacement
/**
* Align the popup to the start or center of the children.
*/
align?: PopupAlign
/**
* Content of the popup, preferably text inside.
*/
Expand Down Expand Up @@ -190,6 +194,7 @@ export const Popup = forwardRef(
children,
text = '',
placement = 'auto',
align = 'center',
id,
className,
containerFullWidth,
Expand Down Expand Up @@ -262,10 +267,11 @@ export const Popup = forwardRef(
popupRef: innerPopupRef,
popupPortalTarget: popupPortalTarget as HTMLElement,
hasArrow,
align,
}),
)
}
}, [hasArrow, placement, popupPortalTarget])
}, [hasArrow, placement, popupPortalTarget, align])

/**
* This function is called when we need to recompute positions of popup due to window scroll or resize.
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/SelectInputV2/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ export const Dropdown = ({
role="dialog"
debounceDelay={0}
containerFullWidth
align="start"
>
{children}
</StyledPopup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const Playground = Template.bind({})
Playground.args = { ...Template.args, options: dataUnGrouped, helper: 'helper' }
Playground.decorators = [
StoryComponent => (
<div style={{ height: '80px', width: '200px' }}>
<div style={{ height: '80px' }}>
<StoryComponent />
</div>
),
Expand Down
Loading