Skip to content

Commit fc79da0

Browse files
scroll behaviour initial commit - wip
1 parent 40f82ac commit fc79da0

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/UnderlineNav2/UnderlineNav.tsx

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {useResizeObserver, ResizeObserverEntry} from '../hooks/useResizeObserver
88
import {useFocusZone} from '../hooks/useFocusZone'
99
import {FocusKeys} from '@primer/behaviors'
1010

11-
type Overflow = 'auto' | 'menu' | 'scroll'
1211
type ChildWidthArray = Array<{width: number}>
1312
type ResponsiveProps = {
1413
items: Array<React.ReactElement>
@@ -40,16 +39,23 @@ const overflowEffect = (
4039
items.push(...childArray)
4140
} else {
4241
iconsVisible = false
43-
// This is only for the overflow behaviour (for fine pointers)
44-
// if we can't fit all the items without icons, we keep the icons hidden and show the rest in the menu
45-
for (const [index, child] of childArray.entries()) {
46-
if (index < numberOfItemsWithoutIconPossible) {
47-
items.push(child)
48-
} else {
49-
actions.push(child)
42+
// determine the media query pointer.
43+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
44+
const isCoarsePointer = window.matchMedia && window.matchMedia('(pointer:coarse)').matches
45+
// TODO: refactor this to avoid nested if else
46+
if (isCoarsePointer) {
47+
// TODO: handle scroll overflow here for media course pointer
48+
} else {
49+
// This is only for the overflow behaviour (for fine pointers)
50+
// if we can't fit all the items without icons, we keep the icons hidden and show the rest in the menu
51+
for (const [index, child] of childArray.entries()) {
52+
if (index < numberOfItemsWithoutIconPossible) {
53+
items.push(child)
54+
} else {
55+
actions.push(child)
56+
}
5057
}
5158
}
52-
// TODO: Scroll behaviour to implement (for coarse pointers)
5359
}
5460

5561
callback({items, actions}, iconsVisible)
@@ -78,7 +84,6 @@ function calculatePossibleItems(childWidthArray: ChildWidthArray, width: number)
7884
export type UnderlineNavProps = {
7985
label: string
8086
as?: React.ElementType
81-
overflow?: Overflow
8287
align?: 'right'
8388
sx?: SxProp
8489
variant?: 'default' | 'small'
@@ -88,16 +93,7 @@ export type UnderlineNavProps = {
8893

8994
export const UnderlineNav = forwardRef(
9095
(
91-
{
92-
as = 'nav',
93-
overflow = 'auto',
94-
align,
95-
label,
96-
sx: sxProp = {},
97-
afterSelect,
98-
variant = 'default',
99-
children
100-
}: UnderlineNavProps,
96+
{as = 'nav', align, label, sx: sxProp = {}, afterSelect, variant = 'default', children}: UnderlineNavProps,
10197
forwardedRef
10298
) => {
10399
const backupRef = useRef<HTMLElement>(null)
@@ -120,7 +116,12 @@ export const UnderlineNav = forwardRef(
120116
align: 'row',
121117
alignItems: 'center'
122118
}
123-
const overflowStyles = overflow === 'scroll' ? {overflowX: 'auto', whiteSpace: 'nowrap'} : {}
119+
120+
const overflowStyles = {
121+
overflowX: 'auto',
122+
whiteSpace: 'nowrap'
123+
}
124+
124125
const ulStyles = {
125126
display: 'flex',
126127
listStyle: 'none',
@@ -170,13 +171,11 @@ export const UnderlineNav = forwardRef(
170171
// resizeObserver calls this function infinitely without a useCallback
171172
const resizeObserverCallback = useCallback(
172173
(resizeObserverEntries: ResizeObserverEntry[]) => {
173-
if (overflow === 'auto' || overflow === 'menu') {
174-
const childArray = getValidChildren(children)
175-
const navWidth = resizeObserverEntries[0].contentRect.width
176-
overflowEffect(navWidth, childArray, childWidthArray, noIconChildWidthArray, callback)
177-
}
174+
const childArray = getValidChildren(children)
175+
const navWidth = resizeObserverEntries[0].contentRect.width
176+
overflowEffect(navWidth, childArray, childWidthArray, noIconChildWidthArray, callback)
178177
},
179-
[callback, childWidthArray, noIconChildWidthArray, children, overflow]
178+
[callback, childWidthArray, noIconChildWidthArray, children]
180179
)
181180
useResizeObserver(resizeObserverCallback, newRef as RefObject<HTMLElement>)
182181
return (

src/UnderlineNav2/examples.stories.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@ export const InternalResponsiveNav = (args: UnderlineNavProps) => {
119119
}
120120

121121
export const HorizontalScrollNav = (args: UnderlineNavProps) => {
122+
const [selectedIndex, setSelectedIndex] = React.useState<number | null>(2)
122123
return (
123-
<UnderlineNav {...args} overflow="scroll">
124-
{items.map(item => (
125-
<UnderlineNav.Item key={item} leadingIcon={EyeIcon}>
124+
<UnderlineNav {...args}>
125+
{items.map((item, index) => (
126+
<UnderlineNav.Item
127+
key={item}
128+
leadingIcon={EyeIcon}
129+
selected={index === selectedIndex}
130+
onSelect={() => setSelectedIndex(index)}
131+
>
126132
{item}
127133
</UnderlineNav.Item>
128134
))}

0 commit comments

Comments
 (0)