-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from thomasKn/thomas/fv-186-header
Add shadcn navigation menu
- Loading branch information
Showing
17 changed files
with
369 additions
and
238 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
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
import {cn} from '~/lib/utils'; | ||
|
||
import type {IconProps} from './Icon'; | ||
|
||
import {Icon} from './Icon'; | ||
|
||
export function IconChevron( | ||
props: {direction: 'down' | 'left' | 'right' | 'up'} & IconProps, | ||
) { | ||
return ( | ||
<Icon | ||
className={cn('size-5', props.className)} | ||
fill="none" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="2" | ||
viewBox="0 0 24 24" | ||
> | ||
<title>Chevron</title> | ||
{props.direction === 'down' && <path d="m6 9 6 6 6-6" />} | ||
{props.direction === 'up' && <path d="m18 15-6-6-6 6" />} | ||
{props.direction === 'left' && <path d="m15 18-6-6 6-6" />} | ||
{props.direction === 'right' && <path d="m9 18 6-6-6-6" />} | ||
</Icon> | ||
); | ||
} |
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
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,38 +1,106 @@ | ||
import type {InferType} from 'groqd'; | ||
|
||
import * as NavigationMenu from '@radix-ui/react-navigation-menu'; | ||
import {useEffect, useRef, useState} from 'react'; | ||
|
||
import type {HEADER_QUERY} from '~/qroq/queries'; | ||
|
||
import {SanityExternalLink} from '../sanity/link/SanityExternalLink'; | ||
import {SanityInternalLink} from '../sanity/link/SanityInternalLink'; | ||
import { | ||
NavigationMenu, | ||
NavigationMenuItem, | ||
NavigationMenuList, | ||
navigationMenuTriggerStyle, | ||
} from '../ui/NavigationMenu'; | ||
import {NestedNavigation} from './NestedNavigation'; | ||
|
||
type HeaderQuery = InferType<typeof HEADER_QUERY>; | ||
type NavigationProps = NonNullable<HeaderQuery>['menu']; | ||
|
||
export function Navigation(props: {data?: NavigationProps}) { | ||
const menuRef = useRef<HTMLUListElement>(null); | ||
const [activeItem, setActiveItem] = useState<null | string | undefined>(null); | ||
const dropdownWidth = 200; | ||
const viewportPosition = useViewportPosition( | ||
menuRef, | ||
activeItem, | ||
dropdownWidth, | ||
); | ||
|
||
return ( | ||
<NavigationMenu.Root> | ||
<NavigationMenu.List className="flex gap-5"> | ||
<NavigationMenu id="header-nav"> | ||
<CssVars | ||
dropdownWidth={dropdownWidth} | ||
viewportPosition={viewportPosition} | ||
/> | ||
<NavigationMenuList ref={menuRef}> | ||
{props.data && | ||
props.data?.length > 0 && | ||
props.data?.map((item) => { | ||
return ( | ||
<NavigationMenu.Item className="relative" key={item._key}> | ||
{item._type === 'internalLink' && ( | ||
<SanityInternalLink data={item} /> | ||
)} | ||
{item._type === 'externalLink' && ( | ||
<SanityExternalLink data={item} /> | ||
)} | ||
{item._type === 'nestedNavigation' && ( | ||
<NestedNavigation data={item} /> | ||
)} | ||
</NavigationMenu.Item> | ||
); | ||
})} | ||
</NavigationMenu.List> | ||
</NavigationMenu.Root> | ||
props.data?.map((item) => ( | ||
<NavigationMenuItem id={item._key!} key={item._key}> | ||
{item._type === 'internalLink' && ( | ||
<SanityInternalLink | ||
className={navigationMenuTriggerStyle()} | ||
data={item} | ||
/> | ||
)} | ||
{item._type === 'externalLink' && ( | ||
<SanityExternalLink | ||
className={navigationMenuTriggerStyle()} | ||
data={item} | ||
/> | ||
)} | ||
{item._type === 'nestedNavigation' && ( | ||
<NestedNavigation data={item} setActiveItem={setActiveItem} /> | ||
)} | ||
</NavigationMenuItem> | ||
))} | ||
</NavigationMenuList> | ||
</NavigationMenu> | ||
); | ||
} | ||
|
||
function CssVars(props: {dropdownWidth: number; viewportPosition: number}) { | ||
const cssVar = ` | ||
#header-nav { | ||
--viewport-position: ${props.viewportPosition}%; | ||
--dropdown-width: ${props.dropdownWidth}px; | ||
} | ||
`; | ||
|
||
return <style dangerouslySetInnerHTML={{__html: cssVar}} />; | ||
} | ||
|
||
// Dynamically calculate the position of the <NavigationMenuPrimitive.Viewport /> based on the active item | ||
function useViewportPosition( | ||
menuRef: React.RefObject<HTMLUListElement>, | ||
activeItem: null | string | undefined, | ||
dropdownWidth: number, | ||
) { | ||
const [viewportPosition, setViewportPosition] = useState(0); | ||
|
||
useEffect(() => { | ||
const menuElement = menuRef.current; | ||
|
||
if (!menuElement) return; | ||
|
||
const menuWidth = menuElement.offsetWidth; | ||
const menuLeft = menuElement.getBoundingClientRect().left; | ||
const activeChild = Array.from(menuElement.children).find( | ||
(child) => child.id === activeItem, | ||
); | ||
const dropdownWidthPercentage = (dropdownWidth / menuWidth) * 100; | ||
const rect = activeChild?.getBoundingClientRect(); | ||
const positionPercentage = rect | ||
? ((rect.left - menuLeft) / menuWidth) * 100 | ||
: 0; | ||
|
||
if (positionPercentage + dropdownWidthPercentage > 100) { | ||
setViewportPosition(100 - dropdownWidthPercentage); | ||
} else { | ||
setViewportPosition(positionPercentage); | ||
} | ||
}, [menuRef, activeItem, dropdownWidth]); | ||
|
||
return viewportPosition; | ||
} |
Oops, something went wrong.