-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
next: Navigation Menu Improvements (#1001)
- Loading branch information
Showing
20 changed files
with
1,481 additions
and
869 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"bits-ui": patch | ||
--- | ||
|
||
feat: Navigation Menu Submenu support |
90 changes: 90 additions & 0 deletions
90
packages/bits-ui/src/lib/bits/navigation-menu/components/navigation-menu-content-impl.svelte
This file contains 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,90 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import { untrack, type Snippet } from "svelte"; | ||
import type { NavigationMenuContentProps } from "../types.js"; | ||
import { | ||
NavigationMenuItemContext, | ||
NavigationMenuItemState, | ||
useNavigationMenuContentImpl, | ||
} from "../navigation-menu.svelte.js"; | ||
import { noop } from "$lib/internal/noop.js"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import DismissibleLayer from "$lib/bits/utilities/dismissible-layer/dismissible-layer.svelte"; | ||
import EscapeLayer from "$lib/bits/utilities/escape-layer/escape-layer.svelte"; | ||
let { | ||
ref = $bindable(null), | ||
id = useId(), | ||
child: childProp, | ||
children: childrenProp, | ||
onInteractOutside = noop, | ||
onFocusOutside = noop, | ||
onEscapeKeydown = noop, | ||
escapeKeydownBehavior = "close", | ||
interactOutsideBehavior = "close", | ||
itemState, | ||
onRefChange, | ||
...restProps | ||
}: Omit<NavigationMenuContentProps, "child"> & { | ||
itemState?: NavigationMenuItemState; | ||
onRefChange?: (ref: HTMLElement | null) => void; | ||
child?: Snippet<[{ props: Record<string, unknown> }]>; | ||
} = $props(); | ||
const contentImplState = useNavigationMenuContentImpl( | ||
{ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => { | ||
ref = v; | ||
untrack(() => onRefChange?.(v)); | ||
} | ||
), | ||
}, | ||
itemState | ||
); | ||
if (itemState) { | ||
NavigationMenuItemContext.set(itemState); | ||
} | ||
const mergedProps = $derived(mergeProps(restProps, contentImplState.props)); | ||
</script> | ||
|
||
<DismissibleLayer | ||
{id} | ||
enabled={true} | ||
onInteractOutside={(e) => { | ||
onInteractOutside(e); | ||
if (e.defaultPrevented) return; | ||
contentImplState.onInteractOutside(e); | ||
}} | ||
onFocusOutside={(e) => { | ||
onFocusOutside(e); | ||
if (e.defaultPrevented) return; | ||
contentImplState.onFocusOutside(e); | ||
}} | ||
{interactOutsideBehavior} | ||
> | ||
{#snippet children({ props: dismissibleProps })} | ||
<EscapeLayer | ||
enabled={true} | ||
onEscapeKeydown={(e) => { | ||
onEscapeKeydown(e); | ||
if (e.defaultPrevented) return; | ||
contentImplState.onEscapeKeydown(e); | ||
}} | ||
{escapeKeydownBehavior} | ||
> | ||
{@const finalProps = mergeProps(mergedProps, dismissibleProps)} | ||
{#if childProp} | ||
{@render childProp({ props: finalProps })} | ||
{:else} | ||
<div {...finalProps}> | ||
{@render childrenProp?.()} | ||
</div> | ||
{/if} | ||
</EscapeLayer> | ||
{/snippet} | ||
</DismissibleLayer> |
75 changes: 18 additions & 57 deletions
75
packages/bits-ui/src/lib/bits/navigation-menu/components/navigation-menu-content.svelte
This file contains 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,82 +1,43 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuContentProps } from "../types.js"; | ||
import { useNavigationMenuContent } from "../navigation-menu.svelte.js"; | ||
import NavigationMenuContentImpl from "./navigation-menu-content-impl.svelte"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import type { NavigationMenuContentProps } from "$lib/types.js"; | ||
import Portal from "$lib/bits/utilities/portal/portal.svelte"; | ||
import PresenceLayer from "$lib/bits/utilities/presence-layer/presence-layer.svelte"; | ||
import DismissibleLayer from "$lib/bits/utilities/dismissible-layer/dismissible-layer.svelte"; | ||
import EscapeLayer from "$lib/bits/utilities/escape-layer/escape-layer.svelte"; | ||
import Mounted from "$lib/bits/utilities/mounted.svelte"; | ||
let { | ||
children: contentChildren, | ||
child, | ||
ref = $bindable(null), | ||
id = useId(), | ||
children, | ||
child, | ||
forceMount = false, | ||
onEscapeKeydown, | ||
onInteractOutside, | ||
onFocusOutside, | ||
...restProps | ||
}: NavigationMenuContentProps = $props(); | ||
let isMounted = $state(false); | ||
const contentState = useNavigationMenuContent({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => { | ||
ref = v; | ||
} | ||
(v) => (ref = v) | ||
), | ||
forceMount: box.with(() => forceMount), | ||
isMounted: box.with(() => isMounted), | ||
}); | ||
const mergedProps = $derived(mergeProps(restProps, contentState.props)); | ||
const portalDisabled = $derived(!contentState.menu.viewportNode); | ||
</script> | ||
|
||
<Portal to={contentState.menu.viewportNode ?? undefined} disabled={portalDisabled}> | ||
<PresenceLayer {id} present={contentState.isPresent}> | ||
{#snippet presence()} | ||
<EscapeLayer | ||
enabled={contentState.isPresent} | ||
onEscapeKeydown={(e) => { | ||
onEscapeKeydown?.(e); | ||
if (e.defaultPrevented) return; | ||
contentState.onEscapeKeydown(e); | ||
}} | ||
> | ||
<DismissibleLayer | ||
enabled={contentState.isPresent} | ||
{id} | ||
onInteractOutside={(e) => { | ||
onInteractOutside?.(e); | ||
if (e.defaultPrevented) return; | ||
contentState.onInteractOutside(e); | ||
}} | ||
onFocusOutside={(e) => { | ||
onFocusOutside?.(e); | ||
if (e.defaultPrevented) return; | ||
contentState.onFocusOutside(e); | ||
}} | ||
> | ||
{#snippet children({ props: dismissibleProps })} | ||
{#if child} | ||
<Mounted bind:mounted={isMounted} /> | ||
{@render child({ props: mergeProps(dismissibleProps, mergedProps) })} | ||
{:else} | ||
<Mounted bind:mounted={isMounted} /> | ||
<div {...mergeProps(dismissibleProps, mergedProps)}> | ||
{@render contentChildren?.()} | ||
</div> | ||
{/if} | ||
{/snippet} | ||
</DismissibleLayer> | ||
</EscapeLayer> | ||
{/snippet} | ||
</PresenceLayer> | ||
</Portal> | ||
{#if contentState.context.viewportRef.current} | ||
<Portal to={contentState.context.viewportRef.current}> | ||
<PresenceLayer | ||
{id} | ||
present={forceMount || contentState.open || contentState.isLastActiveValue} | ||
> | ||
{#snippet presence()} | ||
<NavigationMenuContentImpl {...mergedProps} {children} {child} /> | ||
<Mounted bind:mounted={contentState.mounted} /> | ||
{/snippet} | ||
</PresenceLayer> | ||
</Portal> | ||
{/if} |
32 changes: 32 additions & 0 deletions
32
...ges/bits-ui/src/lib/bits/navigation-menu/components/navigation-menu-indicator-impl.svelte
This file contains 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,32 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuIndicatorProps } from "../types.js"; | ||
import { useNavigationMenuIndicatorImpl } from "../navigation-menu.svelte.js"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
let { | ||
id = useId(), | ||
ref = $bindable(null), | ||
children, | ||
child, | ||
...restProps | ||
}: NavigationMenuIndicatorProps = $props(); | ||
const indicatorState = useNavigationMenuIndicatorImpl({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
const mergedProps = $derived(mergeProps(restProps, indicatorState.props)); | ||
</script> | ||
|
||
{#if child} | ||
{@render child({ props: mergedProps })} | ||
{:else} | ||
<div {...mergedProps}> | ||
{@render children?.()} | ||
</div> | ||
{/if} |
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
packages/bits-ui/src/lib/bits/navigation-menu/components/navigation-menu-sub.svelte
This file contains 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,44 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuSubProps } from "../types.js"; | ||
import { useNavigationMenuSub } from "../navigation-menu.svelte.js"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import { noop } from "$lib/internal/noop.js"; | ||
let { | ||
child, | ||
children, | ||
id = useId(), | ||
ref = $bindable(null), | ||
value = $bindable(""), | ||
onValueChange = noop, | ||
orientation = "horizontal", | ||
...restProps | ||
}: NavigationMenuSubProps = $props(); | ||
const rootState = useNavigationMenuSub({ | ||
id: box.with(() => id), | ||
value: box.with( | ||
() => value, | ||
(v) => { | ||
value = v; | ||
onValueChange(v); | ||
} | ||
), | ||
orientation: box.with(() => orientation), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
const mergedProps = $derived(mergeProps(restProps, rootState.props)); | ||
</script> | ||
|
||
{#if child} | ||
{@render child({ props: mergedProps })} | ||
{:else} | ||
<div {...mergedProps}> | ||
{@render children?.()} | ||
</div> | ||
{/if} |
This file contains 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
6 changes: 3 additions & 3 deletions
6
packages/bits-ui/src/lib/bits/navigation-menu/components/navigation-menu-viewport.svelte
This file contains 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
Oops, something went wrong.