-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
260 additions
and
135 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...ges/bits-ui/src/lib/bits/navigation-menu-2/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 |
---|---|---|
@@ -1,2 +1,69 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuContentProps } from "../types.js"; | ||
import { 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, | ||
children: childrenProp, | ||
onInteractOutside = noop, | ||
onFocusOutside = noop, | ||
onEscapeKeydown = noop, | ||
escapeKeydownBehavior = "close", | ||
interactOutsideBehavior = "close", | ||
...restProps | ||
}: NavigationMenuContentProps = $props(); | ||
const contentImplState = useNavigationMenuContentImpl({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
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 child} | ||
{@render child({ props: finalProps })} | ||
{:else} | ||
<div {...finalProps}> | ||
{@render childrenProp?.()} | ||
</div> | ||
{/if} | ||
</EscapeLayer> | ||
{/snippet} | ||
</DismissibleLayer> |
70 changes: 13 additions & 57 deletions
70
packages/bits-ui/src/lib/bits/navigation-menu-2/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,38 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuContentProps } from "../types.js"; | ||
import { useNavigationMenuContent } from "../navigation-menu.svelte.js"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import Portal from "$lib/bits/utilities/portal/portal.svelte"; | ||
import NavigationMenuContentImpl from "./navigation-menu-content-impl.svelte"; | ||
import NavigationMenuViewportContentMounter from "./navigation-menu-viewport-content-mounter.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"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import type { NavigationMenuContentProps } from "$lib/types.js"; | ||
let { | ||
children: contentChildren, | ||
child, | ||
ref = $bindable(null), | ||
id = useId(), | ||
forceMount = false, | ||
onEscapeKeydown, | ||
onInteractOutside, | ||
onFocusOutside, | ||
children, | ||
child, | ||
...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}> | ||
{#if !contentState.context.viewportRef.current} | ||
<PresenceLayer {id} present={forceMount || contentState.open}> | ||
{#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:isMounted /> | ||
{@render child({ props: mergeProps(dismissibleProps, mergedProps) })} | ||
{:else} | ||
<Mounted bind:isMounted /> | ||
<div {...mergeProps(dismissibleProps, mergedProps)}> | ||
{@render contentChildren?.()} | ||
</div> | ||
{/if} | ||
{/snippet} | ||
</DismissibleLayer> | ||
</EscapeLayer> | ||
<NavigationMenuContentImpl {...mergedProps} {children} {child} /> | ||
{/snippet} | ||
</PresenceLayer> | ||
</Portal> | ||
{:else} | ||
<NavigationMenuViewportContentMounter /> | ||
{/if} |
34 changes: 34 additions & 0 deletions
34
...s/bits-ui/src/lib/bits/navigation-menu-2/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,34 @@ | ||
<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 indicatorState.position} | ||
{#if child} | ||
{@render child({ props: mergedProps })} | ||
{:else} | ||
<div {...mergedProps}> | ||
{@render children?.()} | ||
</div> | ||
{/if} | ||
{/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
5 changes: 5 additions & 0 deletions
5
...src/lib/bits/navigation-menu-2/components/navigation-menu-viewport-content-mounter.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,5 @@ | ||
<script lang="ts"> | ||
import { useNavigationMenuViewportContentMounter } from "../navigation-menu.svelte.js"; | ||
useNavigationMenuViewportContentMounter(); | ||
</script> |
36 changes: 36 additions & 0 deletions
36
...es/bits-ui/src/lib/bits/navigation-menu-2/components/navigation-menu-viewport-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,36 @@ | ||
<script lang="ts"> | ||
import { box, mergeProps } from "svelte-toolbelt"; | ||
import type { NavigationMenuViewportProps } from "../types.js"; | ||
import { useNavigationMenuViewportImpl } from "../navigation-menu.svelte.js"; | ||
import NavigationMenuContentImpl from "./navigation-menu-content-impl.svelte"; | ||
import { useId } from "$lib/internal/use-id.js"; | ||
import PresenceLayer from "$lib/bits/utilities/presence-layer/presence-layer.svelte"; | ||
let { | ||
id = useId(), | ||
ref = $bindable(null), | ||
forceMount = false, | ||
...restProps | ||
}: NavigationMenuViewportProps = $props(); | ||
const viewportState = useNavigationMenuViewportImpl({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
const mergedProps = $derived(mergeProps(restProps, viewportState.props)); | ||
</script> | ||
|
||
<div {...mergedProps}> | ||
{#each Array.from(viewportState.context.viewportContent) as [value, item]} | ||
{@const isActive = viewportState.activeContentValue === value} | ||
<PresenceLayer id={item.id.current} present={forceMount || isActive}> | ||
{#snippet presence()} | ||
<NavigationMenuContentImpl {...item.props} /> | ||
{/snippet} | ||
</PresenceLayer> | ||
{/each} | ||
</div> |
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
Oops, something went wrong.