Skip to content

Commit

Permalink
useSelectedLayoutSegment -> useSelectedLayoutSegments, add useSelecte…
Browse files Browse the repository at this point in the history
…dLayoutSegment with previous behavior (#41693)
  • Loading branch information
timneutkens committed Oct 24, 2022
1 parent 6687f28 commit 4f9e3bf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
19 changes: 17 additions & 2 deletions packages/next/client/components/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,26 @@ function getSelectedLayoutSegmentPath(

// TODO-APP: Expand description when the docs are written for it.
/**
* Get the canonical segment path from this level to the leaf node.
* Get the canonical segment path from the current level to the leaf node.
*/
export function useSelectedLayoutSegment(
export function useSelectedLayoutSegments(
parallelRouteKey: string = 'children'
): string[] {
const { tree } = useContext(LayoutRouterContext)
return getSelectedLayoutSegmentPath(tree, parallelRouteKey)
}

// TODO-APP: Expand description when the docs are written for it.
/**
* Get the segment below the current level
*/
export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children'
): string {
const selectedLayoutSegments = useSelectedLayoutSegments(parallelRouteKey)
if (selectedLayoutSegments.length === 0) {
throw new Error('No selected layout segment below the current level')
}

return selectedLayoutSegments[0]
}
4 changes: 2 additions & 2 deletions test/e2e/app-dir/app-edge/app/app-edge/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

// TODO-APP: support typing for useSelectedLayoutSegment
// @ts-ignore
import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

export default function Layout({ children }: { children: React.ReactNode }) {
// useSelectedLayoutSegment should not be thrown
useSelectedLayoutSegment()
useSelectedLayoutSegments()
return children
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

export default function Page() {
const selectedLayoutSegment = useSelectedLayoutSegment()
const selectedLayoutSegment = useSelectedLayoutSegments()

return (
<p id="page-layout-segments">{JSON.stringify(selectedLayoutSegment)}</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment()
const selectedLayoutSegment = useSelectedLayoutSegments()

return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment()
const selectedLayoutSegment = useSelectedLayoutSegments()

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/app/app/nested-navigation/CategoryNav.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import { TabNavItem } from './TabNavItem'
import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

const CategoryNav = ({ categories }) => {
const selectedLayoutSegment = useSelectedLayoutSegment()
const selectedLayoutSegment = useSelectedLayoutSegments()

return (
<div style={{ display: 'flex' }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import { TabNavItem } from '../TabNavItem'
import { useSelectedLayoutSegment } from 'next/navigation'
import { useSelectedLayoutSegments } from 'next/navigation'

const SubCategoryNav = ({ category }) => {
const selectedLayoutSegment = useSelectedLayoutSegment()
const selectedLayoutSegment = useSelectedLayoutSegments()

return (
<div style={{ display: 'flex' }}>
Expand Down

0 comments on commit 4f9e3bf

Please sign in to comment.