-
Notifications
You must be signed in to change notification settings - Fork 128
Add Portal component #3107
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
Merged
Merged
Add Portal component #3107
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5798ec8
Add Portal component to Holocene
laurakwhit 577638d
Add usePortal support to Menu component
laurakwhit 7e0a1d2
Fix transition in Portal
laurakwhit a603e5e
Update Portal prop to hideWhenAnchorHidden
laurakwhit 9ac6b3c
Fix updatePosition triggering re-runs
laurakwhit 3e31c55
Fix strict errors
laurakwhit fb763ee
Check all scrollable ancestors
laurakwhit 3045b70
CR feedback
laurakwhit fa9cf7a
Use a named function in effect
laurakwhit 3048fe3
Merge branch 'main' into portal-component
laurakwhit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| <script lang="ts" module> | ||
| import { cva } from 'class-variance-authority'; | ||
|
|
||
| const sharedMenuStyles = [ | ||
| 'surface-primary', | ||
| 'min-w-fit', | ||
| 'list-none', | ||
| 'overflow-auto', | ||
| 'border', | ||
| 'border-subtle', | ||
| 'text-primary', | ||
| 'shadow', | ||
| 'w-full', | ||
| ]; | ||
|
|
||
| const menuStyles = cva( | ||
| [ | ||
| 'surface-primary', | ||
| ...sharedMenuStyles, | ||
| 'absolute', | ||
| 'z-20', | ||
| 'mt-1', | ||
| 'min-w-fit', | ||
| 'list-none', | ||
| 'overflow-auto', | ||
| 'border', | ||
| 'border-subtle', | ||
| 'text-primary', | ||
| 'shadow', | ||
| 'w-full', | ||
| 'transition-all', | ||
| 'duration-100', | ||
| 'ease-out', | ||
|
|
@@ -42,6 +46,8 @@ | |
| import { getContext } from 'svelte'; | ||
| import { type ClassNameValue, twMerge as merge } from 'tailwind-merge'; | ||
|
|
||
| import Portal from '$lib/holocene/portal/portal.svelte'; | ||
| import type { PortalPosition } from '$lib/holocene/portal/types'; | ||
| import { getFocusableElements } from '$lib/utilities/focus-trap'; | ||
|
|
||
| import { MENU_CONTEXT, type MenuContext } from './menu-container.svelte'; | ||
|
|
@@ -51,9 +57,13 @@ | |
| id: string; | ||
| keepOpen?: boolean; | ||
| position?: 'left' | 'right' | 'top-left' | 'top-right'; | ||
| menuElement?: HTMLUListElement; | ||
| menuElement?: HTMLUListElement | null; | ||
| maxHeight?: string; | ||
| class?: ClassNameValue; | ||
| usePortal?: boolean; | ||
| scrollContainer?: string; | ||
| flipOnCollision?: boolean; | ||
| hideWhenAnchorHidden?: boolean; | ||
| } | ||
|
|
||
| let { | ||
|
|
@@ -63,11 +73,16 @@ | |
| position = 'left', | ||
| menuElement = $bindable(null), | ||
| maxHeight = 'max-h-[20rem]', | ||
| usePortal = false, | ||
| scrollContainer, | ||
| flipOnCollision = undefined, | ||
| hideWhenAnchorHidden = undefined, | ||
| children, | ||
| ...rest | ||
| }: Props = $props(); | ||
|
|
||
| let height = $state(0); | ||
| let anchorElement = $state<HTMLElement | null>(null); | ||
|
|
||
| const { | ||
| keepOpen: keepOpenCtx, | ||
|
|
@@ -83,6 +98,21 @@ | |
| $menuElementCtx = menuElement; | ||
| }); | ||
|
|
||
| $effect(() => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Named function please |
||
| if (usePortal && id) { | ||
| anchorElement = document.querySelector( | ||
| `[aria-controls="${id}"]`, | ||
| ) as HTMLElement | null; | ||
| } | ||
| }); | ||
|
|
||
| const portalPosition: PortalPosition | undefined = $derived.by(() => { | ||
| if (!usePortal) return undefined; | ||
| if (position.includes('top')) return position; | ||
|
|
||
| return position === 'left' ? 'bottom-left' : 'bottom-right'; | ||
| }); | ||
|
|
||
| const menuItems = $derived( | ||
| menuElement ? getFocusableElements(menuElement) : [], | ||
| ); | ||
|
|
@@ -101,20 +131,46 @@ | |
| ); | ||
| </script> | ||
|
|
||
| <ul | ||
| role="menu" | ||
| class={merge(styles, maxHeight, className)} | ||
| aria-labelledby={id} | ||
| tabindex={-1} | ||
| style={position === 'top-right' || position === 'top-left' | ||
| ? `top: -${height + 16}px;` | ||
| : ''} | ||
| {id} | ||
| bind:this={menuElement} | ||
| bind:clientHeight={height} | ||
| onfocusout={handleFocusOut} | ||
| onclick={handleClick} | ||
| {...rest} | ||
| > | ||
| {@render children?.()} | ||
| </ul> | ||
| {#if usePortal && anchorElement} | ||
| <Portal | ||
| anchor={anchorElement} | ||
| open={$open} | ||
| position={portalPosition} | ||
| {flipOnCollision} | ||
| {hideWhenAnchorHidden} | ||
| {scrollContainer} | ||
| > | ||
| <ul | ||
| role="menu" | ||
| class={merge(sharedMenuStyles, maxHeight, className)} | ||
| aria-labelledby={id} | ||
| tabindex={-1} | ||
| {id} | ||
| bind:this={menuElement} | ||
| bind:clientHeight={height} | ||
| onfocusout={handleFocusOut} | ||
| onclick={handleClick} | ||
| {...rest} | ||
| > | ||
| {@render children?.()} | ||
| </ul> | ||
| </Portal> | ||
| {:else} | ||
| <ul | ||
andrewzamojc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| role="menu" | ||
| class={merge(styles, maxHeight, className)} | ||
| aria-labelledby={id} | ||
| tabindex={-1} | ||
| style={position === 'top-right' || position === 'top-left' | ||
| ? `top: -${height + 16}px;` | ||
| : ''} | ||
| {id} | ||
| bind:this={menuElement} | ||
| bind:clientHeight={height} | ||
| onfocusout={handleFocusOut} | ||
| onclick={handleClick} | ||
| {...rest} | ||
| > | ||
| {@render children?.()} | ||
| </ul> | ||
| {/if} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| export function portal( | ||
| node: HTMLElement, | ||
| target: HTMLElement | string = document.body, | ||
| ) { | ||
| const targetEl = | ||
| typeof target === 'string' ? document.querySelector(target) : target; | ||
|
|
||
| if (!targetEl) { | ||
| console.warn('Portal target not found'); | ||
| return { | ||
| destroy() {}, | ||
| }; | ||
| } | ||
|
|
||
| targetEl.appendChild(node); | ||
|
|
||
| return { | ||
| destroy() { | ||
| if (node.parentNode) { | ||
| node.parentNode.removeChild(node); | ||
| } | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.