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: dragging floating ui components #8230

Merged
merged 6 commits into from
Nov 21, 2023
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
5 changes: 5 additions & 0 deletions packages/calcite-components/src/assets/styles/_sortable.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
@mixin sortable-helper-classes() {
.calcite-sortable--ghost,
.calcite-sortable--drag {
overflow: hidden;
}

.calcite-sortable--ghost::before {
content: "";
@apply box-border
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ describe("calcite-dropdown", () => {
filterInput.value = "numbers";
});

expect(dropdownContentHeight.height).toBe("72px");
expect(dropdownContentHeight.height).toBe("88px");
});

describe("owns a floating-ui", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ export class Dropdown
@Prop({ reflect: true, mutable: true }) open = false;

@Watch("open")
openHandler(value: boolean): void {
openHandler(): void {
if (!this.disabled) {
if (value) {
this.reposition(true);
}
onToggleOpenCloseComponent(this);
return;
}
Expand Down Expand Up @@ -207,7 +204,7 @@ export class Dropdown
this.setFilteredPlacements();
this.reposition(true);
if (this.open) {
this.openHandler(this.open);
this.openHandler();
onToggleOpenCloseComponent(this);
}
connectInteractive(this);
Expand Down Expand Up @@ -546,6 +543,7 @@ export class Dropdown
};

onBeforeOpen(): void {
this.reposition(true);
this.calciteDropdownBeforeOpen.emit();
}

Expand All @@ -559,6 +557,7 @@ export class Dropdown

onClose(): void {
this.calciteDropdownClose.emit();
this.reposition(true);
}

setReferenceEl = (el: HTMLDivElement): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,13 @@ export class InputDatePicker
@Prop({ mutable: true, reflect: true }) open = false;

@Watch("open")
openHandler(value: boolean): void {
openHandler(): void {
onToggleOpenCloseComponent(this);

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

if (value) {
this.reposition(true);
}
}

/**
Expand Down Expand Up @@ -441,7 +437,7 @@ export class InputDatePicker
connectLocalized(this);

const { open } = this;
open && this.openHandler(open);
open && this.openHandler();
if (Array.isArray(this.value)) {
this.valueAsDate = getValueAsDateRange(this.value);
} else if (this.value) {
Expand Down Expand Up @@ -766,6 +762,7 @@ export class InputDatePicker
}

onBeforeOpen(): void {
this.reposition(true);
this.calciteInputDatePickerBeforeOpen.emit();
}

Expand All @@ -791,6 +788,7 @@ export class InputDatePicker
this.restoreInputFocus();
this.focusOnOpen = false;
this.datePickerEl.reset();
this.reposition(true);
}

setStartInput = (el: HTMLCalciteInputElement): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,9 @@ export class Popover
@Prop({ reflect: true, mutable: true }) open = false;

@Watch("open")
openHandler(value: boolean): void {
openHandler(): void {
onToggleOpenCloseComponent(this);

if (value) {
this.reposition(true);
}

this.setExpandedAttr();
}

Expand Down Expand Up @@ -502,6 +498,7 @@ export class Popover
};

onBeforeOpen(): void {
this.reposition(true);
this.calcitePopoverBeforeOpen.emit();
}

Expand All @@ -517,6 +514,7 @@ export class Popover
onClose(): void {
this.calcitePopoverClose.emit();
deactivateFocusTrap(this);
this.reposition(true);
}

storeArrowEl = (el: SVGElement): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ export class Tooltip implements FloatingUIComponent, OpenCloseComponent {
@Prop({ reflect: true }) open = false;

@Watch("open")
openHandler(value: boolean): void {
openHandler(): void {
onToggleOpenCloseComponent(this);
if (value) {
this.reposition(true);
}
}

/**
Expand Down Expand Up @@ -251,6 +248,7 @@ export class Tooltip implements FloatingUIComponent, OpenCloseComponent {
// --------------------------------------------------------------------------

onBeforeOpen(): void {
this.reposition(true);
this.calciteTooltipBeforeOpen.emit();
}

Expand All @@ -264,6 +262,7 @@ export class Tooltip implements FloatingUIComponent, OpenCloseComponent {

onClose(): void {
this.calciteTooltipClose.emit();
this.reposition(true);
}

private setTransitionEl = (el): void => {
Expand Down
10 changes: 0 additions & 10 deletions packages/calcite-components/src/utils/floating-ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ describe("repositioning", () => {
expect(floatingEl.style.left).toBe("0");
}

it("repositions for unopened components", async () => {
await reposition(fakeFloatingUiComponent, positionOptions);
assertOpenPositioning(floatingEl);

fakeFloatingUiComponent.open = true;

await reposition(fakeFloatingUiComponent, positionOptions);
assertOpenPositioning(floatingEl);
});

it("repositions immediately by default", async () => {
assertPreOpenPositioning(floatingEl);

Expand Down
13 changes: 9 additions & 4 deletions packages/calcite-components/src/utils/floating-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { offsetParent } from "composed-offset-position";
}
})();

function roundByDPR(value: number): number {
const dpr = window.devicePixelRatio || 1;
return Math.round(value * dpr) / dpr;
}

/**
* Positions the floating element relative to the reference element.
*
Expand Down Expand Up @@ -145,15 +150,15 @@ export const positionFloatingUI =

floatingEl.setAttribute(placementDataAttribute, effectivePlacement);

const transform = `translate(${Math.round(x)}px,${Math.round(y)}px)`;
const { open } = component;

Object.assign(floatingEl.style, {
visibility,
pointerEvents,
position,
top: "0",
left: "0",
transform,
transform: open ? `translate(${roundByDPR(x)}px,${roundByDPR(y)}px)` : "",
left: open ? "0" : "",
top: open ? "0" : "",
});
};

Expand Down
Loading