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: fix jarring positioning when a closed component is first opened #5484

Merged
Merged
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
1 change: 0 additions & 1 deletion src/assets/styles/_floating-ui.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ $floating-ui-default-z-index: 900;
display: block;
position: absolute;
z-index: $zIndex;
transform: scale(0);
}

@mixin floatingUIWrapper {
Expand Down
53 changes: 32 additions & 21 deletions src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { filter } from "../../utils/filter";
import { debounce } from "lodash-es";

import {
positionFloatingUI,
FloatingCSS,
OverlayPositioning,
FloatingUIComponent,
Expand All @@ -26,7 +25,8 @@ import {
EffectivePlacement,
defaultMenuPlacement,
filterComputedPlacements,
repositionDebounceTimeout
reposition,
updateAfterClose
} from "../../utils/floating-ui";
import { guid } from "../../utils/guid";
import { DeprecatedEventPayload, Scale } from "../interfaces";
Expand Down Expand Up @@ -115,11 +115,16 @@ export class Combobox

@Watch("open")
openHandler(value: boolean): void {
if (!value) {
updateAfterClose(this.floatingEl);
}

if (this.disabled) {
this.active = false;
this.open = false;
return;
}

this.active = value;
this.setMaxScrollerHeight();
}
Expand Down Expand Up @@ -170,7 +175,7 @@ export class Combobox

@Watch("overlayPositioning")
overlayPositioningHandler(): void {
this.debouncedReposition();
this.reposition(true);
}

/**
Expand Down Expand Up @@ -224,7 +229,7 @@ export class Combobox
@Watch("flipPlacements")
flipPlacementsHandler(): void {
this.setFilteredPlacements();
this.debouncedReposition();
this.reposition(true);
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -260,19 +265,27 @@ export class Combobox
//
//--------------------------------------------------------------------------

/** Updates the position of the component. */
/**
* Updates the position of the component.
*
* @param delayed
*/
@Method()
async reposition(): Promise<void> {
async reposition(delayed = false): Promise<void> {
const { floatingEl, referenceEl, placement, overlayPositioning, filteredFlipPlacements } = this;

return positionFloatingUI({
floatingEl,
referenceEl,
overlayPositioning,
placement,
flipPlacements: filteredFlipPlacements,
type: "menu"
});
return reposition(
this,
{
floatingEl,
referenceEl,
overlayPositioning,
placement,
flipPlacements: filteredFlipPlacements,
type: "menu"
},
delayed
);
}

/** Sets focus on the component. */
Expand Down Expand Up @@ -343,7 +356,7 @@ export class Combobox
connectForm(this);
connectOpenCloseComponent(this);
this.setFilteredPlacements();
this.debouncedReposition();
this.reposition(true);
if (this.active) {
this.activeHandler(this.active);
}
Expand All @@ -358,12 +371,12 @@ export class Combobox

componentDidLoad(): void {
afterConnectDefaultValueSet(this, this.getValue());
this.debouncedReposition();
this.reposition(true);
}

componentDidRender(): void {
if (this.el.offsetHeight !== this.inputHeight) {
this.debouncedReposition();
this.reposition(true);
this.inputHeight = this.el.offsetHeight;
}

Expand Down Expand Up @@ -458,8 +471,6 @@ export class Combobox
//
// --------------------------------------------------------------------------

private debouncedReposition = debounce(() => this.reposition(), repositionDebounceTimeout);

setFilteredPlacements = (): void => {
const { el, flipPlacements } = this;

Expand Down Expand Up @@ -622,11 +633,11 @@ export class Combobox
return;
}

await this.debouncedReposition();
await this.reposition(true);
const maxScrollerHeight = this.getMaxScrollerHeight();
listContainerEl.style.maxHeight = maxScrollerHeight > 0 ? `${maxScrollerHeight}px` : "";
listContainerEl.style.minWidth = `${referenceEl.clientWidth}px`;
await this.debouncedReposition();
await this.reposition(true);
};

calciteChipDismissHandler = (
Expand Down
63 changes: 38 additions & 25 deletions src/components/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { ItemKeyboardEvent, Selection } from "./interfaces";

import { focusElement, isPrimaryPointerButton, toAriaBoolean } from "../../utils/dom";
import {
positionFloatingUI,
FloatingCSS,
OverlayPositioning,
FloatingUIComponent,
Expand All @@ -25,7 +24,8 @@ import {
MenuPlacement,
defaultMenuPlacement,
filterComputedPlacements,
repositionDebounceTimeout
reposition,
updateAfterClose
} from "../../utils/floating-ui";
import { Scale } from "../interfaces";
import { SLOTS } from "./resources";
Expand All @@ -39,7 +39,6 @@ import {
import { guid } from "../../utils/guid";
import { RequestedItem } from "../dropdown-group/interfaces";
import { isActivationKey } from "../../utils/key";
import { debounce } from "lodash-es";

/**
* @slot - A slot for adding `calcite-dropdown-group` components. Every `calcite-dropdown-item` must have a parent `calcite-dropdown-group`, even if the `groupTitle` property is not set.
Expand Down Expand Up @@ -83,11 +82,19 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
@Watch("open")
openHandler(value: boolean): void {
if (!this.disabled) {
this.debouncedReposition();
if (value) {
this.reposition(true);
} else {
updateAfterClose(this.floatingEl);
}
this.active = value;
return;
}

if (!value) {
updateAfterClose(this.floatingEl);
}

this.open = false;
}

Expand Down Expand Up @@ -115,7 +122,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
@Watch("flipPlacements")
flipPlacementsHandler(): void {
this.setFilteredPlacements();
this.debouncedReposition();
this.reposition(true);
}

/**
Expand All @@ -141,7 +148,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float

@Watch("overlayPositioning")
overlayPositioningHandler(): void {
this.debouncedReposition();
this.reposition(true);
}

/**
Expand All @@ -153,7 +160,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float

@Watch("placement")
placementHandler(): void {
this.debouncedReposition();
this.reposition(true);
}

/** specify the scale of dropdown, defaults to m */
Expand Down Expand Up @@ -181,7 +188,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
connectedCallback(): void {
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
this.setFilteredPlacements();
this.debouncedReposition();
this.reposition(true);
if (this.open) {
this.openHandler(this.open);
}
Expand All @@ -192,7 +199,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
}

componentDidLoad(): void {
this.debouncedReposition();
this.reposition(true);
}

componentDidRender(): void {
Expand Down Expand Up @@ -254,19 +261,27 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
//
//--------------------------------------------------------------------------

/** Updates the position of the component. */
/**
* Updates the position of the component.
*
* @param delayed
*/
@Method()
async reposition(): Promise<void> {
async reposition(delayed = false): Promise<void> {
const { floatingEl, referenceEl, placement, overlayPositioning, filteredFlipPlacements } = this;

return positionFloatingUI({
floatingEl,
referenceEl,
overlayPositioning,
placement,
flipPlacements: filteredFlipPlacements,
type: "menu"
});
return reposition(
this,
{
floatingEl,
referenceEl,
overlayPositioning,
placement,
flipPlacements: filteredFlipPlacements,
type: "menu"
},
delayed
);
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -420,8 +435,6 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
//
//--------------------------------------------------------------------------

private debouncedReposition = debounce(() => this.reposition(), repositionDebounceTimeout);

slotChangeHandler = (event: Event): void => {
this.defaultAssignedElements = (event.target as HTMLSlotElement).assignedElements({
flatten: true
Expand All @@ -443,7 +456,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
flatten: true
}) as HTMLElement[];

this.debouncedReposition();
this.reposition(true);
};

updateItems = (): void => {
Expand All @@ -453,7 +466,7 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float

this.updateSelectedItems();

this.debouncedReposition();
this.reposition(true);
};

updateGroups = (event: Event): void => {
Expand Down Expand Up @@ -494,10 +507,10 @@ export class Dropdown implements InteractiveComponent, OpenCloseComponent, Float
return;
}

this.debouncedReposition();
this.reposition(true);
const maxScrollerHeight = this.getMaxScrollerHeight();
scrollerEl.style.maxHeight = maxScrollerHeight > 0 ? `${maxScrollerHeight}px` : "";
this.debouncedReposition();
this.reposition(true);
};

setScrollerAndTransitionEl = (el: HTMLDivElement): void => {
Expand Down
Loading