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

feat(action-menu, combobox, dropdown, input-date-picker, popover): allow logical placements for flipPlacements property. #8825 #9490

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
122 changes: 110 additions & 12 deletions packages/calcite-components/src/components.d.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Fragment, VNode } from "@stencil/core/internal";
import { getRoundRobinIndex } from "../../utils/array";
import { focusElement, toAriaBoolean } from "../../utils/dom";
import { EffectivePlacement, LogicalPlacement, OverlayPositioning } from "../../utils/floating-ui";
import { FlipPlacement, LogicalPlacement, OverlayPositioning } from "../../utils/floating-ui";
import { guid } from "../../utils/guid";
import { isActivationKey } from "../../utils/key";
import {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ActionMenu implements LoadableComponent {
/**
* Defines the available placements that can be used when a flip occurs.
*/
@Prop() flipPlacements: EffectivePlacement[];
@Prop() flipPlacements: FlipPlacement[];

/**
* Specifies the text string for the component.
Expand Down
10 changes: 5 additions & 5 deletions packages/calcite-components/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
connectFloatingUI,
defaultMenuPlacement,
disconnectFloatingUI,
EffectivePlacement,
filterComputedPlacements,
filterValidFlipPlacements,
FlipPlacement,
FloatingCSS,
FloatingUIComponent,
LogicalPlacement,
Expand Down Expand Up @@ -283,7 +283,7 @@ export class Combobox
/**
* Defines the available placements that can be used when a flip occurs.
*/
@Prop() flipPlacements: EffectivePlacement[];
@Prop() flipPlacements: FlipPlacement[];

/**
* Made into a prop for testing purposes only
Expand Down Expand Up @@ -523,7 +523,7 @@ export class Combobox

placement: LogicalPlacement = defaultMenuPlacement;

filteredFlipPlacements: EffectivePlacement[];
filteredFlipPlacements: FlipPlacement[];

internalValueChangeFlag = false;

Expand Down Expand Up @@ -629,7 +629,7 @@ export class Combobox
const { el, flipPlacements } = this;

this.filteredFlipPlacements = flipPlacements
? filterComputedPlacements(flipPlacements, el)
? filterValidFlipPlacements(flipPlacements, el)
: null;
};

Expand Down
10 changes: 5 additions & 5 deletions packages/calcite-components/src/components/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
connectFloatingUI,
defaultMenuPlacement,
disconnectFloatingUI,
EffectivePlacement,
filterComputedPlacements,
filterValidFlipPlacements,
FlipPlacement,
FloatingCSS,
FloatingUIComponent,
MenuPlacement,
Expand Down Expand Up @@ -107,7 +107,7 @@ export class Dropdown
/**
* Defines the available placements that can be used when a flip occurs.
*/
@Prop() flipPlacements: EffectivePlacement[];
@Prop() flipPlacements: FlipPlacement[];

@Watch("flipPlacements")
flipPlacementsHandler(): void {
Expand Down Expand Up @@ -421,7 +421,7 @@ export class Dropdown

@Element() el: HTMLCalciteDropdownElement;

filteredFlipPlacements: EffectivePlacement[];
filteredFlipPlacements: FlipPlacement[];

private items: HTMLCalciteDropdownItemElement[] = [];

Expand Down Expand Up @@ -466,7 +466,7 @@ export class Dropdown
const { el, flipPlacements } = this;

this.filteredFlipPlacements = flipPlacements
? filterComputedPlacements(flipPlacements, el)
? filterValidFlipPlacements(flipPlacements, el)
: null;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import {
connectFloatingUI,
defaultMenuPlacement,
disconnectFloatingUI,
EffectivePlacement,
filterComputedPlacements,
filterValidFlipPlacements,
FloatingCSS,
FloatingUIComponent,
FlipPlacement,
MenuPlacement,
OverlayPositioning,
reposition,
Expand Down Expand Up @@ -201,7 +201,7 @@ export class InputDatePicker
/**
* Defines the available placements that can be used when a flip occurs.
*/
@Prop() flipPlacements: EffectivePlacement[];
@Prop() flipPlacements: FlipPlacement[];

@Watch("flipPlacements")
flipPlacementsHandler(): void {
Expand Down Expand Up @@ -720,7 +720,7 @@ export class InputDatePicker

private dialogId = `date-picker-dialog--${guid()}`;

filteredFlipPlacements: EffectivePlacement[];
filteredFlipPlacements: FlipPlacement[];

private focusOnOpen = false;

Expand Down Expand Up @@ -814,7 +814,7 @@ export class InputDatePicker
const { el, flipPlacements } = this;

this.filteredFlipPlacements = flipPlacements
? filterComputedPlacements(flipPlacements, el)
? filterValidFlipPlacements(flipPlacements, el)
: null;
};

Expand Down
10 changes: 5 additions & 5 deletions packages/calcite-components/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
connectFloatingUI,
defaultOffsetDistance,
disconnectFloatingUI,
EffectivePlacement,
filterComputedPlacements,
filterValidFlipPlacements,
FlipPlacement,
FloatingCSS,
FloatingLayout,
FloatingUIComponent,
Expand Down Expand Up @@ -121,7 +121,7 @@ export class Popover
/**
* Defines the available placements that can be used when a flip occurs.
*/
@Prop() flipPlacements: EffectivePlacement[];
@Prop() flipPlacements: FlipPlacement[];

@Watch("flipPlacements")
flipPlacementsHandler(): void {
Expand Down Expand Up @@ -253,7 +253,7 @@ export class Popover
this.updateFocusTrapElements(),
);

filteredFlipPlacements: EffectivePlacement[];
filteredFlipPlacements: FlipPlacement[];

@State() effectiveLocale = "";

Expand Down Expand Up @@ -409,7 +409,7 @@ export class Popover
const { el, flipPlacements } = this;

this.filteredFlipPlacements = flipPlacements
? filterComputedPlacements(flipPlacements, el)
? filterValidFlipPlacements(flipPlacements, el)
: null;
};

Expand Down
10 changes: 5 additions & 5 deletions packages/calcite-components/src/utils/floating-ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const {
connectFloatingUI,
defaultOffsetDistance,
disconnectFloatingUI,
effectivePlacements,
filterComputedPlacements,
flipPlacements,
filterValidFlipPlacements,
getEffectivePlacement,
placements,
positionFloatingUI,
Expand Down Expand Up @@ -197,8 +197,8 @@ it("should have correct value for defaultOffsetDistance", () => {
expect(defaultOffsetDistance).toBe(6);
});

it("should filter computed placements", () => {
expect(new Set(filterComputedPlacements([...placements], document.createElement("div")))).toEqual(
new Set(effectivePlacements),
it("should filter valid placements", () => {
expect(new Set(filterValidFlipPlacements([...placements], document.createElement("div")))).toEqual(
new Set(flipPlacements),
);
});
20 changes: 14 additions & 6 deletions packages/calcite-components/src/utils/floating-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const positionFloatingUI =
overlayPositioning: Strategy;
placement: LogicalPlacement;
flipDisabled?: boolean;
flipPlacements?: EffectivePlacement[];
flipPlacements?: FlipPlacement[];
offsetDistance?: number;
offsetSkidding?: number;
arrowEl?: SVGElement;
Expand All @@ -117,7 +117,7 @@ export const positionFloatingUI =
middleware: getMiddleware({
placement,
flipDisabled,
flipPlacements,
flipPlacements: flipPlacements?.map((placement) => getEffectivePlacement(floatingEl, placement)),
offsetDistance,
offsetSkidding,
arrowEl,
Expand Down Expand Up @@ -244,7 +244,9 @@ export const menuEffectivePlacements: EffectivePlacement[] = [
"bottom-end",
];

export const flipPlacements: EffectivePlacement[] = [
export type FlipPlacement = Exclude<LogicalPlacement, "auto" | "auto-start" | "auto-end">;

export const flipPlacements: FlipPlacement[] = [
"top",
"bottom",
"right",
Expand All @@ -257,6 +259,12 @@ export const flipPlacements: EffectivePlacement[] = [
"right-end",
"left-start",
"left-end",
"leading",
"trailing",
"leading-start",
"leading-end",
"trailing-start",
"trailing-end",
];

export type MenuPlacement = Extract<
Expand Down Expand Up @@ -374,14 +382,14 @@ function getMiddleware({
return [];
}

export function filterComputedPlacements(placements: string[], el: HTMLElement): EffectivePlacement[] {
export function filterValidFlipPlacements(placements: string[], el: HTMLElement): EffectivePlacement[] {
const filteredPlacements = placements.filter((placement: EffectivePlacement) =>
effectivePlacements.includes(placement),
flipPlacements.includes(placement),
) as EffectivePlacement[];

if (filteredPlacements.length !== placements.length) {
console.warn(
`${el.tagName}: Invalid value found in: flipPlacements. Try any of these: ${effectivePlacements
`${el.tagName}: Invalid value found in: flipPlacements. Try any of these: ${flipPlacements
.map((placement) => `"${placement}"`)
.join(", ")
.trim()}`,
Expand Down
Loading