@registrucentras/rc-ses-react-components 2.0.0-rc.7 → 2.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/common/Accordion/hooks/useAccordionController.d.ts +2 -2
- package/dist/components/common/Accordion/types/AccordionControllerState.d.ts +2 -2
- package/dist/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.d.ts +2 -2
- package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.d.ts +2 -2
- package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.d.ts +2 -2
- package/dist/components/common/Badge/index.d.ts +2 -2
- package/dist/components/common/Breadcrumbs/index.d.ts +4 -4
- package/dist/components/common/Card/index.d.ts +2 -2
- package/dist/components/common/CardShell/CardFooter/index.d.ts +1 -1
- package/dist/components/common/CardShell/CardFooter/types.d.ts +4 -9
- package/dist/components/common/CardShell/types.d.ts +4 -4
- package/dist/components/common/IconWithCircularBackground.d.ts +2 -2
- package/dist/components/common/IconWithSquareBackground/index.d.ts +2 -2
- package/dist/components/common/ImageCard/index.d.ts +2 -2
- package/dist/components/common/SelectableCardList/components/SelectableCardListItem.d.ts +2 -2
- package/dist/components/common/SelectableCardList/index.d.ts +2 -2
- package/dist/components/common/ServiceLinks/types.d.ts +6 -6
- package/dist/components/common/SideNav/SideNav.types.d.ts +9 -6
- package/dist/components/common/SideNav/SideNavLayout.d.ts +1 -1
- package/dist/components/common/SideNav/components/SideNavPill.d.ts +2 -2
- package/dist/components/common/SideNav/components/SideNavPillList.d.ts +2 -2
- package/dist/components/common/SideNav/components/SideNavRow.d.ts +4 -3
- package/dist/components/common/SideNav/hooks/useElementHeight.d.ts +11 -0
- package/dist/components/common/SideNav/hooks/useKeepActiveItemInView.d.ts +19 -0
- package/dist/components/common/SideNav/hooks/useSideNavScrollSpy.d.ts +2 -2
- package/dist/components/common/SideNav/index.d.ts +2 -2
- package/dist/components/common/Tabs/TabPanel.d.ts +2 -2
- package/dist/components/common/Tabs/TabsWrapper.d.ts +2 -2
- package/dist/components/common/TitleBlock/types.d.ts +4 -4
- package/dist/components/form/components/FormControlWrapper/components/FieldSuffix.d.ts +2 -2
- package/dist/components/form/components/FormControlWrapper/index.d.ts +2 -2
- package/dist/components/form/inputs/RadioButtonGroup.d.ts +3 -3
- package/dist/components/form/inputs/SearchableField.d.ts +2 -2
- package/dist/components/form/inputs/Select/Select.types.d.ts +2 -2
- package/dist/components/layout/Footer/index.d.ts +2 -2
- package/dist/components/layout/ServiceFormActions.d.ts +2 -2
- package/dist/components/layout/ServiceFormContainer/AccordionFormContainer/components/AccordionCollapseControls.d.ts +2 -2
- package/dist/components/layout/ServiceFormContainer/AccordionFormContainer/index.d.ts +2 -2
- package/dist/components/layout/ServiceHeader.d.ts +2 -2
- package/dist/components/layout/ServicePage.d.ts +2 -2
- package/dist/components/utils/normalizeSx.d.ts +1 -1
- package/dist/library/types.d.ts +2 -2
- package/dist/registrucentras-rc-ses-react-components.cjs +1 -1
- package/dist/registrucentras-rc-ses-react-components.js +1660 -1589
- package/dist/types/assets/IconProps.d.ts +2 -2
- package/dist/types/cards/CardServiceProps.d.ts +2 -2
- package/dist/types/common/QuickLinkType.d.ts +2 -2
- package/dist/types/inputs/InputIconProps.d.ts +2 -2
- package/dist/types/lists/ListNewsItemType.d.ts +2 -2
- package/dist/types/lists/ListServicesItemType.d.ts +3 -3
- package/package.json +2 -1
- package/src/components/common/Accordion/hooks/useAccordionController.tsx +1 -1
- package/src/components/common/Accordion/index.tsx +1 -4
- package/src/components/common/Accordion/types/AccordionControllerState.ts +1 -1
- package/src/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.ts +1 -1
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.tsx +1 -1
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +1 -1
- package/src/components/common/AdvancedList/components/AdvancedListItem/index.test.tsx +2 -2
- package/src/components/common/Badge/index.tsx +3 -3
- package/src/components/common/Breadcrumbs/index.tsx +5 -2
- package/src/components/common/Card/index.tsx +1 -1
- package/src/components/common/CardShell/CardFooter/index.tsx +6 -13
- package/src/components/common/CardShell/CardFooter/types.ts +2 -7
- package/src/components/common/CardShell/types.ts +2 -2
- package/src/components/common/IconWithCircularBackground.tsx +7 -8
- package/src/components/common/IconWithSquareBackground/index.tsx +7 -8
- package/src/components/common/ImageCard/index.tsx +1 -1
- package/src/components/common/SegmentedControl/components/SegmentButton/index.tsx +18 -19
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +1 -1
- package/src/components/common/SelectableCardList/index.tsx +1 -1
- package/src/components/common/ServiceLinks/types.ts +3 -3
- package/src/components/common/SideNav/SideNav.types.ts +22 -4
- package/src/components/common/SideNav/SideNavLayout.test.tsx +52 -0
- package/src/components/common/SideNav/SideNavLayout.tsx +23 -3
- package/src/components/common/SideNav/components/SideNavPill.tsx +1 -1
- package/src/components/common/SideNav/components/SideNavPillList.tsx +8 -23
- package/src/components/common/SideNav/components/SideNavRow.tsx +6 -2
- package/src/components/common/SideNav/hooks/useElementHeight.test.ts +135 -0
- package/src/components/common/SideNav/hooks/useElementHeight.ts +46 -0
- package/src/components/common/SideNav/hooks/useKeepActiveItemInView.test.ts +258 -0
- package/src/components/common/SideNav/hooks/useKeepActiveItemInView.ts +80 -0
- package/src/components/common/SideNav/hooks/useSideNavScrollSpy.test.ts +9 -2
- package/src/components/common/SideNav/hooks/useSideNavScrollSpy.ts +4 -3
- package/src/components/common/SideNav/index.test.tsx +51 -0
- package/src/components/common/SideNav/index.tsx +58 -13
- package/src/components/common/Snackbar/index.test.tsx +40 -0
- package/src/components/common/Snackbar/index.tsx +9 -3
- package/src/components/common/Tabs/TabPanel.tsx +1 -1
- package/src/components/common/Tabs/TabsWrapper.tsx +1 -1
- package/src/components/common/TitleBlock/index.test.tsx +9 -7
- package/src/components/common/TitleBlock/index.tsx +13 -9
- package/src/components/common/TitleBlock/types.ts +2 -2
- package/src/components/form/components/FormControlWrapper/components/FieldSuffix.tsx +1 -1
- package/src/components/form/components/FormControlWrapper/index.tsx +1 -1
- package/src/components/form/inputs/Datepicker/index.test.tsx +1 -1
- package/src/components/form/inputs/FileDropzone.tsx +1 -4
- package/src/components/form/inputs/NumberStepper.test.tsx +83 -0
- package/src/components/form/inputs/NumberStepper.tsx +7 -8
- package/src/components/form/inputs/RadioButtonGroup.test.tsx +1 -1
- package/src/components/form/inputs/RadioButtonGroup.tsx +2 -2
- package/src/components/form/inputs/SearchInput/index.tsx +1 -1
- package/src/components/form/inputs/SearchableField.test.tsx +2 -2
- package/src/components/form/inputs/SearchableField.tsx +1 -1
- package/src/components/form/inputs/Select/Select.types.ts +1 -1
- package/src/components/form/inputs/Select/index.test.tsx +3 -3
- package/src/components/layout/Footer/index.tsx +1 -1
- package/src/components/layout/ServiceFormActions.tsx +1 -1
- package/src/components/layout/ServiceFormContainer/AccordionFormContainer/components/AccordionCollapseControls.tsx +1 -1
- package/src/components/layout/ServiceFormContainer/AccordionFormContainer/index.tsx +1 -1
- package/src/components/layout/ServiceHeader.tsx +1 -1
- package/src/components/layout/ServicePage.tsx +1 -1
- package/src/components/utils/normalizeSx.ts +2 -4
- package/src/examples/ListWithPagination/index.tsx +0 -2
- package/src/examples/MultipleStepForm/components/AdditionalServicesForm.tsx +1 -1
- package/src/examples/MultipleStepForm/components/ServiceIssuanceForm.tsx +1 -1
- package/src/examples/MultipleStepForm/components/TermsAndConditionsForm.tsx +1 -1
- package/src/examples/MultipleStepForm/types/MultiStepFormModel.ts +1 -1
- package/src/examples/SingleStepForm/components/ObjectIdentifierSearchModal.tsx +2 -2
- package/src/examples/SingleStepForm/types/SingleStepFormModel.ts +1 -1
- package/src/library/types.ts +2 -1
- package/src/stories/atoms/Button.stories.tsx +9 -14
- package/src/stories/atoms/Checkbox.stories.tsx +1 -1
- package/src/stories/atoms/Icons.stories.tsx +1 -1
- package/src/stories/atoms/NumberStepper.stories.tsx +1 -1
- package/src/stories/atoms/PhoneInput.stories.tsx +1 -1
- package/src/stories/atoms/Select.stories.tsx +7 -9
- package/src/stories/atoms/Switch.stories.tsx +1 -1
- package/src/stories/molecules/Alert.stories.tsx +2 -2
- package/src/stories/molecules/CardShell.stories.tsx +4 -4
- package/src/stories/molecules/CheckboxFormControl.stories.tsx +13 -21
- package/src/stories/molecules/RadioButtonGroup.stories.tsx +1 -1
- package/src/stories/molecules/Snackbar.stories.tsx +2 -2
- package/src/stories/molecules/TitleBlock.stories.tsx +40 -1
- package/src/stories/organisms/AdvancedList.stories.tsx +4 -4
- package/src/stories/organisms/FileDropzone.stories.tsx +1 -1
- package/src/stories/organisms/FileUpload.stories.tsx +1 -1
- package/src/stories/organisms/SideNav.stories.tsx +41 -3
- package/src/stubs/Component.tsx +4 -4
- package/src/types/assets/IconProps.tsx +1 -1
- package/src/types/cards/CardServiceProps.tsx +1 -1
- package/src/types/common/QuickLinkType.tsx +1 -1
- package/src/types/inputs/InputIconProps.tsx +1 -1
- package/src/types/lists/ListNewsItemType.tsx +1 -1
- package/src/types/lists/ListServicesItemType.tsx +2 -2
- package/src/vite-env.d.ts +2 -2
- package/dist/stories/atoms/Badge.stories.d.ts +0 -9
- package/dist/stories/atoms/Button.stories.d.ts +0 -107
- package/dist/stories/atoms/Checkbox.stories.d.ts +0 -21
- package/dist/stories/atoms/IconWithCircularBackground.stories.d.ts +0 -6
- package/dist/stories/atoms/IconWithSquareBackground.stories.d.ts +0 -7
- package/dist/stories/atoms/Icons.stories.d.ts +0 -7
- package/dist/stories/atoms/Loader.stories.d.ts +0 -36
- package/dist/stories/atoms/LoadingSpinner.stories.d.ts +0 -62
- package/dist/stories/atoms/Logo.stories.d.ts +0 -12
- package/dist/stories/atoms/NumberStepper.stories.d.ts +0 -6
- package/dist/stories/atoms/PhoneInput.stories.d.ts +0 -10
- package/dist/stories/atoms/SearchInput.stories.d.ts +0 -11
- package/dist/stories/atoms/Select.stories.d.ts +0 -7
- package/dist/stories/atoms/SimpleCheckbox.stories.d.ts +0 -19
- package/dist/stories/atoms/Switch.stories.d.ts +0 -17
- package/dist/stories/atoms/Tab.stories.d.ts +0 -8
- package/dist/stories/atoms/TextField.stories.d.ts +0 -6
- package/dist/stories/atoms/Tooltip.stories.d.ts +0 -48
- package/dist/stories/foundations/ThemedMuiComponents.stories.d.ts +0 -25
- package/dist/stories/molecules/Alert.stories.d.ts +0 -14
- package/dist/stories/molecules/Breadcrumbs.stories.d.ts +0 -6
- package/dist/stories/molecules/ButtonWithPopover.stories.d.ts +0 -7
- package/dist/stories/molecules/Card.stories.d.ts +0 -8
- package/dist/stories/molecules/CardShell.stories.d.ts +0 -18
- package/dist/stories/molecules/CheckboxFormControl.stories.d.ts +0 -9
- package/dist/stories/molecules/DatePicker.stories.d.ts +0 -152
- package/dist/stories/molecules/FormControlWrapper.stories.d.ts +0 -7
- package/dist/stories/molecules/ImageCard.stories.d.ts +0 -8
- package/dist/stories/molecules/ListWithIcons.stories.d.ts +0 -34
- package/dist/stories/molecules/PhoneInputFormControl.stories.d.ts +0 -6
- package/dist/stories/molecules/Popover.stories.d.ts +0 -12
- package/dist/stories/molecules/RadioButtonGroup.stories.d.ts +0 -6
- package/dist/stories/molecules/SearchableField.stories.d.ts +0 -6
- package/dist/stories/molecules/SegmentedControl.stories.d.ts +0 -202
- package/dist/stories/molecules/Snackbar.stories.d.ts +0 -143
- package/dist/stories/molecules/TabPanel.stories.d.ts +0 -7
- package/dist/stories/molecules/Tabs.stories.d.ts +0 -7
- package/dist/stories/molecules/TabsWrapper.stories.d.ts +0 -6
- package/dist/stories/molecules/TitleBlock.stories.d.ts +0 -22
- package/dist/stories/organisms/Accordion.stories.d.ts +0 -18
- package/dist/stories/organisms/AdvancedList.stories.d.ts +0 -33
- package/dist/stories/organisms/AdvancedListItem.stories.d.ts +0 -33
- package/dist/stories/organisms/CardFormContainer.stories.d.ts +0 -7
- package/dist/stories/organisms/DataPagination.stories.d.ts +0 -47
- package/dist/stories/organisms/Dialog.stories.d.ts +0 -15
- package/dist/stories/organisms/FileDropzone.stories.d.ts +0 -6
- package/dist/stories/organisms/FileUpload.stories.d.ts +0 -18
- package/dist/stories/organisms/Footer.stories.d.ts +0 -6
- package/dist/stories/organisms/FullPageLoader.stories.d.ts +0 -37
- package/dist/stories/organisms/Modal.stories.d.ts +0 -10
- package/dist/stories/organisms/SelectableCardList.stories.d.ts +0 -37
- package/dist/stories/organisms/ServiceFormActions.stories.d.ts +0 -6
- package/dist/stories/organisms/ServiceFormContainer.stories.d.ts +0 -15
- package/dist/stories/organisms/ServiceHeader.stories.d.ts +0 -7
- package/dist/stories/organisms/ServiceLinks.stories.d.ts +0 -28
- package/dist/stories/organisms/ServiceWizardStepper.stories.d.ts +0 -7
- package/dist/stories/organisms/SideNav.stories.d.ts +0 -7
- package/dist/stories/templates/ServicePage.stories.d.ts +0 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Box } from '@mui/material'
|
|
2
|
-
import { useRef } from 'react'
|
|
2
|
+
import { CSSProperties, useRef, useState } from 'react'
|
|
3
3
|
|
|
4
4
|
import normalizeSx from '@/components/utils/normalizeSx'
|
|
5
5
|
|
|
@@ -13,17 +13,24 @@ function RcSesSideNavLayout({
|
|
|
13
13
|
activeItemId: controlledActiveItemId,
|
|
14
14
|
onItemClick,
|
|
15
15
|
title,
|
|
16
|
+
overflow,
|
|
16
17
|
offset,
|
|
17
18
|
sidebarWidth = '17rem',
|
|
18
19
|
sx,
|
|
19
20
|
}: RcSesSideNavLayoutProps) {
|
|
20
21
|
const contentRef = useRef<HTMLDivElement>(null)
|
|
21
22
|
|
|
23
|
+
// The mobile bar is sticky and sits on top of the content, so a section
|
|
24
|
+
// scrolled to at `offset` would land underneath it - heading first. Everything
|
|
25
|
+
// that scrolls the page has to clear the bar as well as the page header.
|
|
26
|
+
const [overlayHeight, setOverlayHeight] = useState(0)
|
|
27
|
+
const scrollOffset = (offset ?? 0) + overlayHeight
|
|
28
|
+
|
|
22
29
|
const isControlled = controlledActiveItemId !== undefined
|
|
23
30
|
const itemIds = items.map((item) => item.id)
|
|
24
31
|
const scrollSpy = useSideNavScrollSpy({
|
|
25
32
|
itemIds: isControlled ? [] : itemIds,
|
|
26
|
-
offset,
|
|
33
|
+
offset: scrollOffset,
|
|
27
34
|
scope: contentRef,
|
|
28
35
|
})
|
|
29
36
|
const activeItemId = isControlled ? controlledActiveItemId : scrollSpy.activeItemId
|
|
@@ -48,6 +55,9 @@ function RcSesSideNavLayout({
|
|
|
48
55
|
activeItemId={activeItemId}
|
|
49
56
|
onItemClick={handleItemClick}
|
|
50
57
|
title={title}
|
|
58
|
+
overflow={overflow}
|
|
59
|
+
offset={offset}
|
|
60
|
+
onOverlayHeightChange={setOverlayHeight}
|
|
51
61
|
sx={{
|
|
52
62
|
position: 'sticky',
|
|
53
63
|
top: `${offset ?? 0}px`,
|
|
@@ -56,7 +66,17 @@ function RcSesSideNavLayout({
|
|
|
56
66
|
flexShrink: 0,
|
|
57
67
|
}}
|
|
58
68
|
/>
|
|
59
|
-
|
|
69
|
+
{/*
|
|
70
|
+
Sections are the consumer's, so the layout cannot set their
|
|
71
|
+
scroll-margin itself; it publishes the offset they need instead:
|
|
72
|
+
`scrollMarginTop: 'var(--rc-ses-sidenav-scroll-offset)'`. Inline rather
|
|
73
|
+
than in sx, so a resize rewrites one property instead of minting a class.
|
|
74
|
+
*/}
|
|
75
|
+
<Box
|
|
76
|
+
ref={contentRef}
|
|
77
|
+
style={{ '--rc-ses-sidenav-scroll-offset': `${scrollOffset}px` } as CSSProperties}
|
|
78
|
+
sx={{ flex: 1, minWidth: 0 }}
|
|
79
|
+
>
|
|
60
80
|
{children}
|
|
61
81
|
</Box>
|
|
62
82
|
</Box>
|
|
@@ -4,19 +4,20 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
4
4
|
import palette, { common } from '@/theme/palette'
|
|
5
5
|
|
|
6
6
|
import { RcSesSideNavItem } from '../SideNav.types'
|
|
7
|
+
import useKeepActiveItemInView from '../hooks/useKeepActiveItemInView'
|
|
7
8
|
import SideNavPill from './SideNavPill'
|
|
8
9
|
|
|
9
10
|
const FADE_WIDTH = '2rem'
|
|
10
11
|
const SCROLL_EPSILON = 1
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
interface Props {
|
|
13
14
|
items: RcSesSideNavItem[]
|
|
14
15
|
activeItemId?: string
|
|
15
16
|
onItemClick?: (id: string) => void
|
|
16
17
|
getItemAriaLabel: (item: RcSesSideNavItem) => string | undefined
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
interface ScrollState {
|
|
20
21
|
hasOverflow: boolean
|
|
21
22
|
showRightFade: boolean
|
|
22
23
|
thumbWidth: number
|
|
@@ -89,27 +90,11 @@ function SideNavPillList({ items, activeItemId, onItemClick, getItemAriaLabel }:
|
|
|
89
90
|
}
|
|
90
91
|
}, [items, updateScrollState])
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
)
|
|
98
|
-
if (!container || !target) return
|
|
99
|
-
|
|
100
|
-
// Viewport rects on both, so they agree: offsetLeft would be relative to the
|
|
101
|
-
// positioned wrapper, not the scroll container.
|
|
102
|
-
const containerRect = container.getBoundingClientRect()
|
|
103
|
-
const targetRect = target.getBoundingClientRect()
|
|
104
|
-
const overflowLeft = containerRect.left - targetRect.left
|
|
105
|
-
const overflowRight = targetRect.right - containerRect.right
|
|
106
|
-
|
|
107
|
-
if (overflowLeft > 0) {
|
|
108
|
-
container.scrollLeft -= overflowLeft
|
|
109
|
-
} else if (overflowRight > 0) {
|
|
110
|
-
container.scrollLeft += overflowRight
|
|
111
|
-
}
|
|
112
|
-
}, [activeItemId])
|
|
93
|
+
useKeepActiveItemInView({
|
|
94
|
+
containerRef: scrollRef,
|
|
95
|
+
activeItemId,
|
|
96
|
+
axis: 'horizontal',
|
|
97
|
+
})
|
|
113
98
|
|
|
114
99
|
return (
|
|
115
100
|
<Box>
|
|
@@ -4,7 +4,8 @@ import palette from '@/theme/palette'
|
|
|
4
4
|
|
|
5
5
|
const BORDER_WIDTH = '0.125rem'
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
interface Props {
|
|
8
|
+
itemId: string
|
|
8
9
|
label: string
|
|
9
10
|
count?: number
|
|
10
11
|
ariaLabel?: string
|
|
@@ -12,12 +13,13 @@ type Props = {
|
|
|
12
13
|
onClick?: () => void
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
function SideNavRow({ label, count, ariaLabel, active, onClick }: Props) {
|
|
16
|
+
function SideNavRow({ itemId, label, count, ariaLabel, active, onClick }: Props) {
|
|
16
17
|
return (
|
|
17
18
|
<Box
|
|
18
19
|
component={onClick ? 'button' : 'div'}
|
|
19
20
|
type={onClick ? 'button' : undefined}
|
|
20
21
|
onClick={onClick}
|
|
22
|
+
data-item-id={itemId}
|
|
21
23
|
aria-current={active ? 'page' : undefined}
|
|
22
24
|
aria-label={ariaLabel}
|
|
23
25
|
sx={{
|
|
@@ -32,6 +34,8 @@ function SideNavRow({ label, count, ariaLabel, active, onClick }: Props) {
|
|
|
32
34
|
backgroundColor: active ? palette.primary[50] : 'transparent',
|
|
33
35
|
padding: '0.75rem 1rem',
|
|
34
36
|
textAlign: 'left',
|
|
37
|
+
// A scrolling list (overflow='scroll') must not shrink its rows to fit.
|
|
38
|
+
flexShrink: 0,
|
|
35
39
|
cursor: onClick ? 'pointer' : 'default',
|
|
36
40
|
'&:hover': onClick && !active ? { backgroundColor: palette.grey[50] } : undefined,
|
|
37
41
|
'&:focus-visible': {
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { act, renderHook } from '@testing-library/react'
|
|
2
|
+
import { createRef } from 'react'
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import useElementHeight from './useElementHeight'
|
|
6
|
+
|
|
7
|
+
type ResizeCallback = () => void
|
|
8
|
+
|
|
9
|
+
const resizeCallbacks: ResizeCallback[] = []
|
|
10
|
+
const disconnect = vi.fn()
|
|
11
|
+
|
|
12
|
+
// Matches the sibling SideNav suites: a plain function, so `new` hands back the
|
|
13
|
+
// object below rather than a class instance ESLint wants methods on.
|
|
14
|
+
function ResizeObserverMock(callback: ResizeCallback) {
|
|
15
|
+
resizeCallbacks.push(callback)
|
|
16
|
+
return { observe: () => {}, unobserve: () => {}, disconnect }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function elementOfHeight(height: number) {
|
|
20
|
+
const element = document.createElement('div')
|
|
21
|
+
vi.spyOn(element, 'getBoundingClientRect').mockImplementation(
|
|
22
|
+
() => ({ height }) as DOMRect,
|
|
23
|
+
)
|
|
24
|
+
return element
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function remeasureAt(element: HTMLElement, height: number) {
|
|
28
|
+
vi.spyOn(element, 'getBoundingClientRect').mockImplementation(
|
|
29
|
+
() => ({ height }) as DOMRect,
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function renderWithElement(element: HTMLElement | null) {
|
|
34
|
+
const ref = createRef<HTMLElement>()
|
|
35
|
+
Object.assign(ref, { current: element })
|
|
36
|
+
return renderHook(() => useElementHeight(ref))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('useElementHeight', () => {
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
resizeCallbacks.length = 0
|
|
42
|
+
disconnect.mockClear()
|
|
43
|
+
vi.unstubAllGlobals()
|
|
44
|
+
vi.restoreAllMocks()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('returns 0 when there is no element to measure', () => {
|
|
48
|
+
const { result } = renderWithElement(null)
|
|
49
|
+
|
|
50
|
+
expect(result.current).toBe(0)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('measures the element on mount', () => {
|
|
54
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
55
|
+
|
|
56
|
+
const { result } = renderWithElement(elementOfHeight(62))
|
|
57
|
+
|
|
58
|
+
expect(result.current).toBe(62)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// A hidden element has no box at all, which is what lets the caller use the
|
|
62
|
+
// height as a breakpoint test.
|
|
63
|
+
it('reports 0 for an element the media query switched off', () => {
|
|
64
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
65
|
+
|
|
66
|
+
const { result } = renderWithElement(elementOfHeight(0))
|
|
67
|
+
|
|
68
|
+
expect(result.current).toBe(0)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('rounds to whole pixels', () => {
|
|
72
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
73
|
+
|
|
74
|
+
const { result } = renderWithElement(elementOfHeight(61.6))
|
|
75
|
+
|
|
76
|
+
expect(result.current).toBe(62)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('re-measures when the element resizes in place', () => {
|
|
80
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
81
|
+
const element = elementOfHeight(62)
|
|
82
|
+
|
|
83
|
+
const { result } = renderWithElement(element)
|
|
84
|
+
remeasureAt(element, 104)
|
|
85
|
+
act(() => resizeCallbacks.forEach((callback) => callback()))
|
|
86
|
+
|
|
87
|
+
expect(result.current).toBe(104)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// A viewport resize that changes the element's box is a resize of the element,
|
|
91
|
+
// so the observer already reports it and a second listener would only measure
|
|
92
|
+
// the same thing twice.
|
|
93
|
+
it('does not listen for viewport resizes alongside the observer', () => {
|
|
94
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
95
|
+
const addEventListener = vi.spyOn(window, 'addEventListener')
|
|
96
|
+
|
|
97
|
+
renderWithElement(elementOfHeight(62))
|
|
98
|
+
|
|
99
|
+
expect(addEventListener).not.toHaveBeenCalledWith('resize', expect.anything())
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('falls back to a viewport resize listener where ResizeObserver is missing', () => {
|
|
103
|
+
vi.stubGlobal('ResizeObserver', undefined)
|
|
104
|
+
const element = elementOfHeight(62)
|
|
105
|
+
|
|
106
|
+
const { result } = renderWithElement(element)
|
|
107
|
+
expect(result.current).toBe(62)
|
|
108
|
+
|
|
109
|
+
remeasureAt(element, 0)
|
|
110
|
+
act(() => window.dispatchEvent(new Event('resize')))
|
|
111
|
+
|
|
112
|
+
expect(result.current).toBe(0)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('disconnects the observer once unmounted', () => {
|
|
116
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
117
|
+
|
|
118
|
+
const { unmount } = renderWithElement(elementOfHeight(62))
|
|
119
|
+
unmount()
|
|
120
|
+
|
|
121
|
+
expect(disconnect).toHaveBeenCalled()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('drops the fallback listener once unmounted', () => {
|
|
125
|
+
vi.stubGlobal('ResizeObserver', undefined)
|
|
126
|
+
const element = elementOfHeight(62)
|
|
127
|
+
|
|
128
|
+
const { result, unmount } = renderWithElement(element)
|
|
129
|
+
unmount()
|
|
130
|
+
remeasureAt(element, 104)
|
|
131
|
+
act(() => window.dispatchEvent(new Event('resize')))
|
|
132
|
+
|
|
133
|
+
expect(result.current).toBe(62)
|
|
134
|
+
})
|
|
135
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { RefObject, useCallback, useLayoutEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rendered height of an element, in pixels.
|
|
5
|
+
*
|
|
6
|
+
* 0 while the element is absent or `display: none`, which is what makes this
|
|
7
|
+
* usable as a breakpoint test: the nav's mobile bar and desktop column are
|
|
8
|
+
* swapped by a media query, so measuring the one that is switched off yields 0
|
|
9
|
+
* without the hook having to know anything about breakpoints.
|
|
10
|
+
*/
|
|
11
|
+
function useElementHeight(ref: RefObject<HTMLElement>): number {
|
|
12
|
+
const [height, setHeight] = useState(0)
|
|
13
|
+
|
|
14
|
+
const measure = useCallback((element: HTMLElement | null) => {
|
|
15
|
+
setHeight(element ? Math.round(element.getBoundingClientRect().height) : 0)
|
|
16
|
+
}, [])
|
|
17
|
+
|
|
18
|
+
// Before paint, not after: the height feeds the offset the page is scrolled
|
|
19
|
+
// by, and a consumer that scrolls on mount - honouring a #hash deep link, say
|
|
20
|
+
// - runs its own effect after this one and would otherwise read 0.
|
|
21
|
+
useLayoutEffect(() => {
|
|
22
|
+
const element = ref.current
|
|
23
|
+
measure(element)
|
|
24
|
+
if (!element) return undefined
|
|
25
|
+
|
|
26
|
+
// The bar changes height in place - the pills wrap, a late web font lands,
|
|
27
|
+
// the media query switches it off entirely - and a ResizeObserver reports
|
|
28
|
+
// all of it, viewport resizes included. So the window listener is the
|
|
29
|
+
// fallback for where ResizeObserver is missing, not a second opinion
|
|
30
|
+
// running alongside it.
|
|
31
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
32
|
+
const observer = new ResizeObserver(() => measure(element))
|
|
33
|
+
observer.observe(element)
|
|
34
|
+
return () => observer.disconnect()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const onResize = () => measure(element)
|
|
38
|
+
window.addEventListener('resize', onResize)
|
|
39
|
+
return () => window.removeEventListener('resize', onResize)
|
|
40
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- ref is a ref, its identity is stable
|
|
41
|
+
}, [measure])
|
|
42
|
+
|
|
43
|
+
return height
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default useElementHeight
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react'
|
|
2
|
+
import { RefObject } from 'react'
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import useKeepActiveItemInView from './useKeepActiveItemInView'
|
|
6
|
+
|
|
7
|
+
interface Rect {
|
|
8
|
+
top: number
|
|
9
|
+
bottom: number
|
|
10
|
+
left: number
|
|
11
|
+
right: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CONTAINER_RECT: Rect = { top: 100, bottom: 400, left: 100, right: 400 }
|
|
15
|
+
|
|
16
|
+
// jsdom has no layout, so every rect the hook reads has to be supplied here.
|
|
17
|
+
const stubRect = (element: HTMLElement, rect: Rect) => {
|
|
18
|
+
element.getBoundingClientRect = () => rect as DOMRect
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const setup = (itemRect: Rect, itemId = 'documents') => {
|
|
22
|
+
const container = document.createElement('div')
|
|
23
|
+
const item = document.createElement('div')
|
|
24
|
+
item.setAttribute('data-item-id', itemId)
|
|
25
|
+
container.appendChild(item)
|
|
26
|
+
document.body.appendChild(container)
|
|
27
|
+
|
|
28
|
+
stubRect(container, CONTAINER_RECT)
|
|
29
|
+
stubRect(item, itemRect)
|
|
30
|
+
|
|
31
|
+
// Element.scrollTo is not implemented in jsdom; the hook falls back to the
|
|
32
|
+
// scrollTop/scrollLeft assignment without it, which would hide the smooth path.
|
|
33
|
+
const scrollTo = vi.fn()
|
|
34
|
+
container.scrollTo = scrollTo
|
|
35
|
+
|
|
36
|
+
let scrollTop = 0
|
|
37
|
+
let scrollLeft = 0
|
|
38
|
+
Object.defineProperty(container, 'scrollTop', {
|
|
39
|
+
get: () => scrollTop,
|
|
40
|
+
set: (value: number) => {
|
|
41
|
+
scrollTop = value
|
|
42
|
+
},
|
|
43
|
+
configurable: true,
|
|
44
|
+
})
|
|
45
|
+
Object.defineProperty(container, 'scrollLeft', {
|
|
46
|
+
get: () => scrollLeft,
|
|
47
|
+
set: (value: number) => {
|
|
48
|
+
scrollLeft = value
|
|
49
|
+
},
|
|
50
|
+
configurable: true,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
containerRef: { current: container } as RefObject<HTMLElement>,
|
|
55
|
+
scrollTo,
|
|
56
|
+
getScrollTop: () => scrollTop,
|
|
57
|
+
getScrollLeft: () => scrollLeft,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const prefersReducedMotion = (matches: boolean) =>
|
|
62
|
+
vi.stubGlobal(
|
|
63
|
+
'matchMedia',
|
|
64
|
+
vi.fn(() => ({ matches })),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
document.body.innerHTML = ''
|
|
69
|
+
prefersReducedMotion(false)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
afterEach(() => {
|
|
73
|
+
vi.unstubAllGlobals()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('useKeepActiveItemInView', () => {
|
|
77
|
+
it('scrolls up by exactly the amount the item overflows the top edge', () => {
|
|
78
|
+
const { containerRef, getScrollTop } = setup({
|
|
79
|
+
top: 60,
|
|
80
|
+
bottom: 100,
|
|
81
|
+
left: 100,
|
|
82
|
+
right: 400,
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
renderHook(() =>
|
|
86
|
+
useKeepActiveItemInView({
|
|
87
|
+
containerRef,
|
|
88
|
+
activeItemId: 'documents',
|
|
89
|
+
axis: 'vertical',
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
expect(getScrollTop()).toBe(-40)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('scrolls down by exactly the amount the item overflows the bottom edge', () => {
|
|
97
|
+
const { containerRef, getScrollTop } = setup({
|
|
98
|
+
top: 380,
|
|
99
|
+
bottom: 425,
|
|
100
|
+
left: 100,
|
|
101
|
+
right: 400,
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
renderHook(() =>
|
|
105
|
+
useKeepActiveItemInView({
|
|
106
|
+
containerRef,
|
|
107
|
+
activeItemId: 'documents',
|
|
108
|
+
axis: 'vertical',
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
expect(getScrollTop()).toBe(25)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('leaves the scroll position alone when the item is already visible', () => {
|
|
116
|
+
const { containerRef, getScrollTop, scrollTo } = setup({
|
|
117
|
+
top: 200,
|
|
118
|
+
bottom: 240,
|
|
119
|
+
left: 100,
|
|
120
|
+
right: 400,
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
renderHook(() =>
|
|
124
|
+
useKeepActiveItemInView({
|
|
125
|
+
containerRef,
|
|
126
|
+
activeItemId: 'documents',
|
|
127
|
+
axis: 'vertical',
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
expect(getScrollTop()).toBe(0)
|
|
132
|
+
expect(scrollTo).not.toHaveBeenCalled()
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('scrolls the horizontal axis by scrollLeft', () => {
|
|
136
|
+
const { containerRef, getScrollLeft } = setup({
|
|
137
|
+
top: 100,
|
|
138
|
+
bottom: 140,
|
|
139
|
+
left: 380,
|
|
140
|
+
right: 460,
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
renderHook(() =>
|
|
144
|
+
useKeepActiveItemInView({
|
|
145
|
+
containerRef,
|
|
146
|
+
activeItemId: 'documents',
|
|
147
|
+
axis: 'horizontal',
|
|
148
|
+
}),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
expect(getScrollLeft()).toBe(60)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('animates when asked to and the user has no reduced-motion preference', () => {
|
|
155
|
+
const { containerRef, scrollTo } = setup({
|
|
156
|
+
top: 380,
|
|
157
|
+
bottom: 425,
|
|
158
|
+
left: 100,
|
|
159
|
+
right: 400,
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
renderHook(() =>
|
|
163
|
+
useKeepActiveItemInView({
|
|
164
|
+
containerRef,
|
|
165
|
+
activeItemId: 'documents',
|
|
166
|
+
axis: 'vertical',
|
|
167
|
+
behavior: 'smooth',
|
|
168
|
+
}),
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
expect(scrollTo).toHaveBeenCalledWith({ top: 25, behavior: 'smooth' })
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('jumps instead of animating under prefers-reduced-motion', () => {
|
|
175
|
+
prefersReducedMotion(true)
|
|
176
|
+
const { containerRef, scrollTo, getScrollTop } = setup({
|
|
177
|
+
top: 380,
|
|
178
|
+
bottom: 425,
|
|
179
|
+
left: 100,
|
|
180
|
+
right: 400,
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
renderHook(() =>
|
|
184
|
+
useKeepActiveItemInView({
|
|
185
|
+
containerRef,
|
|
186
|
+
activeItemId: 'documents',
|
|
187
|
+
axis: 'vertical',
|
|
188
|
+
behavior: 'smooth',
|
|
189
|
+
}),
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
expect(scrollTo).not.toHaveBeenCalled()
|
|
193
|
+
expect(getScrollTop()).toBe(25)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('does nothing while disabled', () => {
|
|
197
|
+
const { containerRef, getScrollTop } = setup({
|
|
198
|
+
top: 380,
|
|
199
|
+
bottom: 425,
|
|
200
|
+
left: 100,
|
|
201
|
+
right: 400,
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
renderHook(() =>
|
|
205
|
+
useKeepActiveItemInView({
|
|
206
|
+
containerRef,
|
|
207
|
+
activeItemId: 'documents',
|
|
208
|
+
axis: 'vertical',
|
|
209
|
+
enabled: false,
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
expect(getScrollTop()).toBe(0)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('does not throw when no item matches the active id', () => {
|
|
217
|
+
const { containerRef, getScrollTop } = setup({
|
|
218
|
+
top: 380,
|
|
219
|
+
bottom: 425,
|
|
220
|
+
left: 100,
|
|
221
|
+
right: 400,
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
expect(() =>
|
|
225
|
+
renderHook(() =>
|
|
226
|
+
useKeepActiveItemInView({
|
|
227
|
+
containerRef,
|
|
228
|
+
activeItemId: 'missing',
|
|
229
|
+
axis: 'vertical',
|
|
230
|
+
}),
|
|
231
|
+
),
|
|
232
|
+
).not.toThrow()
|
|
233
|
+
expect(getScrollTop()).toBe(0)
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
// An id is an arbitrary string, not a CSS identifier, so quotes and
|
|
237
|
+
// backslashes have to survive the lookup.
|
|
238
|
+
it.each(['te"ma 1', 'tema\\1', 'trailing\\'])(
|
|
239
|
+
'still finds the item for the awkward id %j',
|
|
240
|
+
(itemId) => {
|
|
241
|
+
const { containerRef, getScrollTop } = setup(
|
|
242
|
+
{ top: 380, bottom: 425, left: 100, right: 400 },
|
|
243
|
+
itemId,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
expect(() =>
|
|
247
|
+
renderHook(() =>
|
|
248
|
+
useKeepActiveItemInView({
|
|
249
|
+
containerRef,
|
|
250
|
+
activeItemId: itemId,
|
|
251
|
+
axis: 'vertical',
|
|
252
|
+
}),
|
|
253
|
+
),
|
|
254
|
+
).not.toThrow()
|
|
255
|
+
expect(getScrollTop()).toBe(25)
|
|
256
|
+
},
|
|
257
|
+
)
|
|
258
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { RefObject, useEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
interface UseKeepActiveItemInViewOptions {
|
|
4
|
+
// The element with `overflow: auto`. Its items are matched by `data-item-id`.
|
|
5
|
+
containerRef: RefObject<HTMLElement>
|
|
6
|
+
activeItemId?: string
|
|
7
|
+
axis: 'horizontal' | 'vertical'
|
|
8
|
+
// Off while the container cannot scroll at all, so no work is done for the
|
|
9
|
+
// variant that hugs its content.
|
|
10
|
+
enabled?: boolean
|
|
11
|
+
// 'smooth' is downgraded to 'auto' under prefers-reduced-motion.
|
|
12
|
+
behavior?: ScrollBehavior
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Scrolls the container the minimum amount needed to bring the active item into
|
|
17
|
+
* view - the nav's own equivalent of `scrollIntoView({ block: 'nearest' })`.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately not `scrollIntoView`: that also scrolls every scrollable
|
|
20
|
+
* ancestor, so bringing a nav item into view would drag the page along with it
|
|
21
|
+
* and fight the scroll-spy that picked the item in the first place.
|
|
22
|
+
*/
|
|
23
|
+
function useKeepActiveItemInView({
|
|
24
|
+
containerRef,
|
|
25
|
+
activeItemId,
|
|
26
|
+
axis,
|
|
27
|
+
enabled = true,
|
|
28
|
+
behavior = 'auto',
|
|
29
|
+
}: UseKeepActiveItemInViewOptions) {
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!enabled || !activeItemId) return
|
|
32
|
+
|
|
33
|
+
const container = containerRef.current
|
|
34
|
+
// Compared as an attribute value rather than interpolated into a selector:
|
|
35
|
+
// ids are arbitrary strings, and quotes or backslashes break a selector.
|
|
36
|
+
const target = Array.from(
|
|
37
|
+
container?.querySelectorAll<HTMLElement>('[data-item-id]') ?? [],
|
|
38
|
+
).find((element) => element.dataset.itemId === activeItemId)
|
|
39
|
+
if (!container || !target) return
|
|
40
|
+
|
|
41
|
+
// Viewport rects on both, so they agree: offsetTop/offsetLeft would be
|
|
42
|
+
// relative to the nearest positioned ancestor, not the scroll container.
|
|
43
|
+
const containerRect = container.getBoundingClientRect()
|
|
44
|
+
const targetRect = target.getBoundingClientRect()
|
|
45
|
+
const isVertical = axis === 'vertical'
|
|
46
|
+
const overflowStart = isVertical
|
|
47
|
+
? containerRect.top - targetRect.top
|
|
48
|
+
: containerRect.left - targetRect.left
|
|
49
|
+
const overflowEnd = isVertical
|
|
50
|
+
? targetRect.bottom - containerRect.bottom
|
|
51
|
+
: targetRect.right - containerRect.right
|
|
52
|
+
|
|
53
|
+
let delta = 0
|
|
54
|
+
if (overflowStart > 0) delta = -overflowStart
|
|
55
|
+
else if (overflowEnd > 0) delta = overflowEnd
|
|
56
|
+
if (delta === 0) return
|
|
57
|
+
|
|
58
|
+
const property = isVertical ? 'scrollTop' : 'scrollLeft'
|
|
59
|
+
const position = container[property] + delta
|
|
60
|
+
const prefersReducedMotion = window.matchMedia?.(
|
|
61
|
+
'(prefers-reduced-motion: reduce)',
|
|
62
|
+
).matches
|
|
63
|
+
|
|
64
|
+
// The direct assignment is both what an instant scroll wants and the only
|
|
65
|
+
// path available in layout-less environments, where Element.scrollTo is absent.
|
|
66
|
+
if (
|
|
67
|
+
behavior === 'smooth' &&
|
|
68
|
+
!prefersReducedMotion &&
|
|
69
|
+
typeof container.scrollTo === 'function'
|
|
70
|
+
) {
|
|
71
|
+
container.scrollTo({ [isVertical ? 'top' : 'left']: position, behavior: 'smooth' })
|
|
72
|
+
} else {
|
|
73
|
+
container[property] = position
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- containerRef is a ref, its identity is stable
|
|
76
|
+
}, [activeItemId, axis, behavior, enabled])
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default useKeepActiveItemInView
|
|
80
|
+
export type { UseKeepActiveItemInViewOptions }
|
|
@@ -3,8 +3,15 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
3
3
|
|
|
4
4
|
import useSideNavScrollSpy from './useSideNavScrollSpy'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
interface Viewport {
|
|
7
|
+
scrollY: number
|
|
8
|
+
innerHeight: number
|
|
9
|
+
scrollHeight: number
|
|
10
|
+
}
|
|
11
|
+
interface SectionTop {
|
|
12
|
+
id: string
|
|
13
|
+
top: number
|
|
14
|
+
}
|
|
8
15
|
|
|
9
16
|
const setViewport = ({ scrollY, innerHeight, scrollHeight }: Viewport) => {
|
|
10
17
|
Object.defineProperty(window, 'scrollY', { value: scrollY, configurable: true })
|