Skip to content

Commit

Permalink
docs: navigation menu forceMount prop (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Feb 11, 2025
1 parent 5deb311 commit 99c3d5b
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const NAVIGATION_MENU_LIST_ATTR = "data-navigation-menu-list";
const NAVIGATION_MENU_TRIGGER_ATTR = "data-navigation-menu-trigger";
const NAVIGATION_MENU_CONTENT_ATTR = "data-navigation-menu-content";
const NAVIGATION_MENU_LINK_ATTR = "data-navigation-menu-link";
const NAVIGATION_MENU_VIEWPORT_ATTR = "data-navigation-menu-viewport";

type NavigationMenuProviderStateProps = ReadableBoxedValues<{
dir: Direction;
Expand Down Expand Up @@ -947,6 +948,7 @@ class NavigationMenuViewportState {
"--bits-navigation-menu-viewport-width": this.viewportWidth,
"--bits-navigation-menu-viewport-height": this.viewportHeight,
},
[NAVIGATION_MENU_VIEWPORT_ATTR]: "",
onpointerenter: this.context.onContentEnter,
onpointerleave: this.context.onContentLeave,
}) as const
Expand Down
25 changes: 24 additions & 1 deletion sites/docs/content/components/navigation-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ navLabel: New
---

<script>
import { APISection, ComponentPreviewV2, NavigationMenuDemo } from '$lib/components/index.js'
import { APISection, ComponentPreviewV2, NavigationMenuDemo, Callout, NavigationMenuDemoForceMount } from '$lib/components/index.js'
export let schemas;
</script>

Expand Down Expand Up @@ -248,4 +248,27 @@ Combining these with `position: absolute;` allows you to create smooth overlappi
}
```

### Force Mounting

You may wish for the links in the Navigation Menu to persist in the DOM, regardless of whether the menu is open or not. This is particularly useful for SEO purposes. You can achieve this by using the `forceMount` prop on the `NavigationMenu.Content` and `NavigationMenu.Viewport` components.

<Callout type="warning">

**Note:** Using `forceMount` requires you to manage the visibility of the elements yourself, using the `data-state` attributes on the `NavigationMenu.Content` and `NavigationMenu.Viewport` components.

</Callout>

```svelte /forceMount/
<NavigationMenu.Content forceMount></NavigationMenu.Content>
<NavigationMenu.Viewport forceMount></NavigationMenu.Viewport>
```

<ComponentPreviewV2 name="navigation-menu-demo-force-mount" comp="Navigation Menu">

{#snippet preview()}
<NavigationMenuDemoForceMount />
{/snippet}

</ComponentPreviewV2>

<APISection {schemas} />
1 change: 1 addition & 0 deletions sites/docs/src/lib/components/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { default as SelectDemoMultiple } from "./select-demo-multiple.svelte";
export { default as SelectDemoTransition } from "./select-demo-transition.svelte";
export { default as MenubarDemo } from "./menubar-demo.svelte";
export { default as NavigationMenuDemo } from "./navigation-menu-demo.svelte";
export { default as NavigationMenuDemoForceMount } from "./navigation-menu-demo-force-mount.svelte";
export { default as PaginationDemo } from "./pagination-demo.svelte";
export { default as PinInputDemo } from "./pin-input-demo.svelte";
export { default as PopoverDemo } from "./popover-demo.svelte";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<script lang="ts">
import { NavigationMenu } from "bits-ui";
import CaretDown from "phosphor-svelte/lib/CaretDown";
import { cn } from "$lib/utils/styles.js";
const components: { title: string; href: string; description: string }[] = [
{
title: "Alert Dialog",
href: "/docs/components/alert-dialog",
description:
"A modal dialog that interrupts the user with important content and expects a response.",
},
{
title: "Link Preview",
href: "/docs/components/link-preview",
description: "For sighted users to preview content available behind a link.",
},
{
title: "Progress",
href: "/docs/components/progress",
description:
"Displays an indicator showing the completion progress of a task, typically displayed as a progress bar.",
},
{
title: "Scroll Area",
href: "/docs/components/scroll-area",
description: "Visually or semantically separates content.",
},
{
title: "Tabs",
href: "/docs/components/tabs",
description:
"A set of layered sections of content—known as tab panels—that are displayed one at a time.",
},
{
title: "Tooltip",
href: "/docs/components/tooltip",
description:
"A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.",
},
];
type ListItemProps = {
className?: string;
title: string;
href: string;
content: string;
};
</script>

{#snippet ListItem({ className, title, content, href }: ListItemProps)}
<li>
<NavigationMenu.Link
class={cn(
"block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground",
className
)}
{href}
>
<div class="text-sm font-medium leading-none">{title}</div>
<p class="line-clamp-2 text-sm leading-snug text-muted-foreground">
{content}
</p>
</NavigationMenu.Link>
</li>
{/snippet}

<NavigationMenu.Root class="relative z-10 flex w-full justify-center">
<NavigationMenu.List class="group flex list-none items-center justify-center p-1">
<NavigationMenu.Item value="getting-started">
<NavigationMenu.Trigger
class="group inline-flex h-10 w-max items-center justify-center rounded-md bg-background px-4 py-2 text-sm font-medium transition-colors hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground focus:outline-none disabled:pointer-events-none disabled:opacity-50 data-[active]:bg-muted/50 data-[state=open]:bg-muted/50"
>
Getting started
<CaretDown
class="duration-[250] relative top-[1px] ml-1 size-3 transition-transform group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="absolute left-0 top-0 w-full data-[state=closed]:hidden data-[motion=from-end]:animate-enterFromRight data-[motion=from-start]:animate-enterFromLeft data-[motion=to-end]:animate-exitToRight data-[motion=to-start]:animate-exitToLeft sm:w-auto"
forceMount
>
<ul
class="m-0 grid list-none gap-x-2.5 p-[22px] sm:w-[600px] sm:grid-flow-col sm:grid-rows-3"
>
<li class="row-span-3">
<NavigationMenu.Link
href="/"
class="flex h-full w-full select-none flex-col justify-end rounded-md bg-gradient-to-b from-muted/50 to-muted p-6 no-underline outline-none focus:shadow-md"
>
<!-- <Icons.logo class="h-6 w-6" /> -->
<div class="mb-2 mt-4 text-lg font-medium">Bits UI</div>
<p class="text-sm leading-tight text-muted-foreground">
The headless components for Svelte.
</p>
</NavigationMenu.Link>
</li>

{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Headless components for Svelte and SvelteKit",
})}
{@render ListItem({
href: "/docs/getting-started",
title: "Getting Started",
content: "How to install and use Bits UI",
})}
{@render ListItem({
href: "/docs/styling",
title: "Styling",
content: "How to style Bits UI components",
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger
class="group inline-flex h-10 w-max items-center justify-center rounded-md bg-background px-4 py-2 text-sm font-medium transition-colors hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground focus:outline-none disabled:pointer-events-none disabled:opacity-50 data-[active]:bg-muted/50 data-[state=open]:bg-muted/50"
>
Components
<CaretDown
class="duration-[250] relative top-[1px] ml-1 size-3 transition-transform group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
forceMount
class="absolute left-0 top-0 w-full data-[state=closed]:hidden data-[motion=from-end]:animate-enterFromRight data-[motion=from-start]:animate-enterFromLeft data-[motion=to-end]:animate-exitToRight data-[motion=to-start]:animate-exitToLeft sm:w-auto"
>
<ul class="grid w-[400px] gap-3 p-6 md:w-[500px] md:grid-cols-2 lg:w-[600px]">
{#each components as component (component.title)}
{@render ListItem({
href: component.href,
title: component.title,
content: component.description,
})}
{/each}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Link
class="group inline-flex h-10 w-max items-center justify-center rounded-md bg-background px-4 py-2 text-sm font-medium transition-colors hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground focus:outline-none disabled:pointer-events-none disabled:opacity-50 data-[active]:bg-muted/50 data-[state=open]:bg-muted/50"
href="/docs"
>
Documentation
</NavigationMenu.Link>
</NavigationMenu.Item>
<NavigationMenu.Indicator
class="top-full z-10 flex h-2.5 items-end justify-center overflow-hidden opacity-100 transition-[all,transform_250ms_ease] duration-200 data-[state=hidden]:animate-fadeOut data-[state=visible]:animate-fadeIn data-[state=hidden]:opacity-0"
>
<div
class="relative top-[70%] size-2.5 rotate-[45deg] rounded-tl-[2px] bg-border"
></div>
</NavigationMenu.Indicator>
</NavigationMenu.List>
<div class="absolute left-0 top-full flex w-full justify-center perspective-[2000px]">
<NavigationMenu.Viewport
forceMount
class="text-popover-foreground relative mt-2.5 h-[var(--bits-navigation-menu-viewport-height)] w-full origin-[top_center] overflow-hidden rounded-md border bg-background shadow-lg transition-[width,_height] duration-300 data-[state=closed]:hidden data-[state=closed]:animate-scaleOut data-[state=open]:animate-scaleIn sm:w-[var(--bits-navigation-menu-viewport-width)]"
/>
</div>
</NavigationMenu.Root>

0 comments on commit 99c3d5b

Please sign in to comment.