Skip to content

Commit

Permalink
Fix: mounting each floating element into their own portal by default (#…
Browse files Browse the repository at this point in the history
…1103)

Co-authored-by: Hunter Johnston <64506580 [email protected]>
  • Loading branch information
anatolzak and huntabyte committed May 31, 2024
1 parent 463ab77 commit 98a56e0
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-scissors-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
'@melt-ui/svelte': minor
---

Mount each floating element into their own portal by default now that stack handling has been improved
2 changes: 1 addition & 1 deletion src/docs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 41,7 @@ export const DESCRIPTIONS = {
`Whether or not to close the ${name} when the user clicks outside of it.`,
BUILDER: (name: string) => `The builder function used to create the ${name} component.`,
PORTAL: (name = 'floating element') =>
`The element or selector to render the ${name} into. Nested floating elements are automatically rendered into their parent if not specified. Pass in \`null\` to disable portalling.`,
`The element or selector to render the ${name} into. Nested floating elements are automatically rendered into their own portal if not specified. Pass in \`null\` to disable portalling. Pass in \`undefined\` to use parent portal.`,
FORCE_VISIBLE: (name = 'element') =>
`Whether or not to force the ${name} to always be visible. This is useful for custom transitions and animations using conditional blocks.`,
ON_CHANGE: (store: string) =>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/context-menu/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ const defaults = {
preventScroll: true,
escapeBehavior: 'close',
closeOnOutsideClick: true,
portal: undefined,
portal: 'body',
loop: false,
dir: 'ltr',
defaultOpen: false,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/dialog/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 43,7 @@ const defaults = {
closeOnOutsideClick: true,
role: 'dialog',
defaultOpen: false,
portal: undefined,
portal: 'body',
forceVisible: false,
openFocus: undefined,
closeFocus: undefined,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/builders/dialog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import type { ChangeFn, FocusProp, IdObj } from '$lib/internal/helpers/index.js'
import type { BuilderReturn } from '$lib/internal/types.js';
import type { Writable } from 'svelte/store';
import type { DialogIdParts, createDialog } from './create.js';
import type { PortalConfig } from '$lib/internal/actions/portal.js';
import type { EscapeBehaviorType } from '$lib/internal/actions/index.js';
export type { DialogComponentEvents } from './events.js';
export type CreateDialogProps = {
Expand Down Expand Up @@ -70,7 71,7 @@ export type CreateDialogProps = {
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* If true, the dialog will be visible regardless of the open state.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/dropdown-menu/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ const defaults = {
preventScroll: true,
escapeBehavior: 'close',
closeOnOutsideClick: true,
portal: undefined,
portal: 'body',
loop: false,
dir: 'ltr',
defaultOpen: false,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/link-preview/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 42,7 @@ const defaults = {
arrowSize: 8,
closeOnOutsideClick: true,
forceVisible: false,
portal: undefined,
portal: 'body',
escapeBehavior: 'close',
onOutsideClick: undefined,
preventTextSelectionOverflow: true,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/builders/link-preview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import type {
EscapeBehaviorType,
FloatingConfig,
InteractOutsideEvent,
PortalConfig,
} from '$lib/internal/actions/index.js';
import type { ChangeFn, IdObj } from '$lib/internal/helpers/index.js';
import type { BuilderReturn } from '$lib/internal/types.js';
Expand Down Expand Up @@ -109,7 110,7 @@ export type CreateLinkPreviewProps = {
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* Optionally override the default ids we assign to the elements
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/listbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 68,7 @@ const defaults = {
preventScroll: true,
escapeBehavior: 'close',
forceVisible: false,
portal: undefined,
portal: 'body',
builder: 'listbox',
disabled: false,
required: false,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/builders/listbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import type {
EscapeBehaviorType,
FloatingConfig,
InteractOutsideEvent,
PortalConfig,
} from '$lib/internal/actions/index.js';
import type { ChangeFn, IdObj } from '$lib/internal/helpers/index.js';
import type { BuilderReturn, WhenTrue } from '$lib/internal/types.js';
Expand Down Expand Up @@ -146,7 147,7 @@ export type CreateListboxProps<
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* Whether the menu content should be displayed even if it is not open.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/menu/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 73,7 @@ const defaults = {
preventScroll: true,
escapeBehavior: 'close',
closeOnOutsideClick: true,
portal: undefined,
portal: 'body',
loop: false,
dir: 'ltr',
defaultOpen: false,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/builders/menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import type {
EscapeBehaviorType,
FloatingConfig,
InteractOutsideEvent,
PortalConfig,
} from '$lib/internal/actions/index.js';
import type { TextDirection } from '$lib/internal/types.js';
import type { ChangeFn, FocusProp, IdObj } from '$lib/internal/helpers/index.js';
Expand Down Expand Up @@ -60,7 61,7 @@ export type _CreateMenuProps = {
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* Whether or not to close the menu when a click occurs outside of it.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/menubar/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 100,7 @@ export function createMenubar(props?: CreateMenubarProps) {
loop: false,
escapeBehavior: 'close',
closeOnOutsideClick: true,
portal: undefined,
portal: 'body',
forceVisible: false,
defaultOpen: false,
typeahead: true,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/popover/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 40,7 @@ const defaults = {
preventScroll: false,
onOpenChange: undefined,
closeOnOutsideClick: true,
portal: undefined,
portal: 'body',
forceVisible: false,
openFocus: undefined,
closeFocus: undefined,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/builders/popover/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import type {
EscapeBehaviorType,
FloatingConfig,
InteractOutsideEvent,
PortalConfig,
} from '$lib/internal/actions/index.js';
import type { ChangeFn, FocusProp, IdObj } from '$lib/internal/helpers/index.js';
import type { BuilderReturn } from '$lib/internal/types.js';
Expand Down Expand Up @@ -95,7 96,7 @@ export type CreatePopoverProps = {
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* Whether the menu content should be displayed even if it is not open.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builders/tooltip/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 43,7 @@ const defaults = {
openDelay: 1000,
closeDelay: 0,
forceVisible: false,
portal: undefined,
portal: 'body',
escapeBehavior: 'close',
disableHoverableContent: false,
group: undefined,
Expand Down
8 changes: 6 additions & 2 deletions src/lib/builders/tooltip/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 1,8 @@
import type { EscapeBehaviorType, FloatingConfig } from '$lib/internal/actions/index.js';
import type {
EscapeBehaviorType,
FloatingConfig,
PortalConfig,
} from '$lib/internal/actions/index.js';
import type { ChangeFn, IdObj } from '$lib/internal/helpers/index.js';
import type { BuilderReturn } from '$lib/internal/types.js';
import type { Writable } from 'svelte/store';
Expand Down Expand Up @@ -37,7 41,7 @@ export type CreateTooltipProps = {
*
* @default 'body'
*/
portal?: HTMLElement | string | null;
portal?: PortalConfig | null;

/**
* Optionally override the default ids we assign to the elements
Expand Down
2 changes: 1 addition & 1 deletion src/lib/internal/actions/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ export const usePortal = ((el, target = 'body') => {
};
}

async function update(newTarget: HTMLElement | string | undefined) {
async function update(newTarget: PortalConfig) {
target = newTarget;
if (typeof target === 'string') {
targetEl = document.querySelector(target);
Expand Down

0 comments on commit 98a56e0

Please sign in to comment.