Skip to content
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

fix(Navigation): implement item provider to detect parents #4893

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/solid-socks-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ultraviolet/plus": patch
---

Fix `<Navigation.Item />` not to spread `hasParent` in the DOM and use a provider instead
45 changes: 17 additions & 28 deletions packages/plus/src/components/Navigation/components/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import type {
} from 'react'
import {
Children,
cloneElement,
isValidElement,
memo,
useCallback,
useContext,
useEffect,
useMemo,
useReducer,
Expand All @@ -41,6 +41,7 @@ import type { PascalToCamelCaseWithoutSuffix } from '../../../types'
import { useNavigation } from '../NavigationProvider'
import { ANIMATION_DURATION, shrinkHeight } from '../constants'
import type { PinUnPinType } from '../types'
import { ItemContext, ItemProvider } from './ItemProvider'

const RelativeDiv = styled.div`
position: relative;
Expand Down Expand Up @@ -325,10 +326,6 @@ type ItemProps = {
* You don't need to use this prop it's used internally to control the type of the item
*/
type?: ItemType
/**
* You don't need to use this prop it's used internally to control if the item has a parent
*/
hasParents?: boolean
/**
* You don't need to use this prop it's used internally for pinned item to be reorganized with drag and drop
*/
Expand Down Expand Up @@ -372,7 +369,6 @@ export const Item = memo(
active,
noPinButton,
type = 'default',
hasParents,
as,
disabled,
noExpand = false,
Expand All @@ -387,6 +383,9 @@ export const Item = memo(
)
}

const itemProvider = useContext(ItemContext)
const hasParents = !!itemProvider

const {
expanded,
locales,
Expand Down Expand Up @@ -528,14 +527,6 @@ export const Item = memo(

// This content is when the navigation is expanded
if (expanded || (!expanded && animation === 'expand')) {
const renderChildren = Children.map(children, child =>
isValidElement<ItemProps>(child)
? cloneElement(child, {
hasParents: true,
})
: child,
)

return (
<>
<Container
Expand Down Expand Up @@ -708,14 +699,18 @@ export const Item = memo(
{children ? (
<>
{!noExpand ? (
<Expandable
opened={internalExpanded}
animationDuration={expandableAnimationDuration}
>
<PaddedStack>{renderChildren}</PaddedStack>
</Expandable>
<ItemProvider>
<Expandable
opened={internalExpanded}
animationDuration={expandableAnimationDuration}
>
<PaddedStack>{children}</PaddedStack>
</Expandable>
</ItemProvider>
) : (
<PaddedStack>{renderChildren}</PaddedStack>
<ItemProvider>
<PaddedStack>{children}</PaddedStack>
</ItemProvider>
)}
</>
) : null}
Expand Down Expand Up @@ -755,13 +750,7 @@ export const Item = memo(
}
placement="right"
>
{Children.map(children, child =>
isValidElement<ItemProps>(child)
? cloneElement(child, {
hasParents: true,
})
: child,
)}
<ItemProvider>{children}</ItemProvider>
</StyledMenu>
) : (
<Tooltip text={label} placement="right" tabIndex={-1}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { ReactNode } from 'react'
import { createContext, useMemo } from 'react'

// Create the context with a default value
export const ItemContext = createContext(false)

type ItemProviderProps = {
children: ReactNode
}

export const ItemProvider = ({ children }: ItemProviderProps) => {
const value = useMemo(() => true, [])

return <ItemContext.Provider value={value}>{children}</ItemContext.Provider>
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export const PinnedItems = ({
onClickPinUnpin={
items[itemId]?.onClickPinUnpin ?? undefined
}
hasParents
/>
</RelativeDiv>
) : null,
Expand Down
Loading