@sovereignfs/ui 0.45.0 → 0.83.1
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/Accordion.module.css +1 -1
- package/dist/Alert.module.css +1 -1
- package/dist/AppsLauncher.module.css +96 -0
- package/dist/Avatar.module.css +1 -1
- package/dist/Badge.module.css +30 -7
- package/dist/BalanceChip.module.css +1 -1
- package/dist/Button.module.css +18 -2
- package/dist/Card.module.css +1 -1
- package/dist/CardTile.module.css +181 -0
- package/dist/CodeTextarea.module.css +1 -1
- package/dist/Collapsible.module.css +1 -1
- package/dist/ColorPicker.module.css +132 -0
- package/dist/Combobox.module.css +2 -2
- package/dist/Command.module.css +1 -1
- package/dist/ConfirmDialog.module.css +2 -2
- package/dist/ConsentPrompt.module.css +21 -0
- package/dist/DatePicker.module.css +1 -1
- package/dist/Dialog.module.css +130 -21
- package/dist/Drawer.module.css +36 -7
- package/dist/Header.module.css +62 -0
- package/dist/HeaderFooterLayout.module.css +21 -0
- package/dist/IconPicker.module.css +1 -1
- package/dist/Input.module.css +1 -1
- package/dist/Kbd.module.css +1 -1
- package/dist/LegalLinks.module.css +24 -0
- package/dist/Markdown.module.css +73 -0
- package/dist/Menu.module.css +1 -0
- package/dist/Message.module.css +1 -1
- package/dist/MessageScroller.module.css +12 -1
- package/dist/MobileAppsDrawer.module.css +82 -0
- package/dist/MobileFooter.module.css +73 -0
- package/dist/MobileHeader.module.css +76 -0
- package/dist/NavList.module.css +130 -0
- package/dist/NavTabs.module.css +1 -1
- package/dist/NotificationsPanel.module.css +258 -0
- package/dist/OverlayHeader.module.css +1 -1
- package/dist/PageContainer.module.css +82 -0
- package/dist/PageHeader.module.css +54 -0
- package/dist/PageLayout.module.css +51 -0
- package/dist/Popover.module.css +1 -1
- package/dist/QuantityStepper.module.css +1 -1
- package/dist/Resizable.module.css +3 -3
- package/dist/RootLayout.module.css +12 -0
- package/dist/SegmentedControl.module.css +1 -1
- package/dist/Select.module.css +1 -1
- package/dist/Sheet.module.css +4 -1
- package/dist/SplitPane.module.css +4 -4
- package/dist/StatusBadge.module.css +1 -1
- package/dist/StepStrip.module.css +53 -0
- package/dist/SuggestionInput.module.css +2 -2
- package/dist/SwipableMobileCarousel.module.css +64 -0
- package/dist/SwipableMobileCarouselDots.module.css +64 -0
- package/dist/SwipableMobileCarouselSlideParts.module.css +29 -0
- package/dist/SwipeStack.module.css +101 -0
- package/dist/SystemBanner.module.css +10 -0
- package/dist/Table.module.css +2 -2
- package/dist/Tabs.module.css +1 -1
- package/dist/TagInput.module.css +2 -2
- package/dist/Textarea.module.css +9 -1
- package/dist/ThreeColumnLayout.module.css +35 -0
- package/dist/Toast.module.css +62 -3
- package/dist/Tooltip.module.css +1 -1
- package/dist/UserMenu.module.css +90 -0
- package/dist/index.d.ts +1680 -30
- package/dist/index.js +4618 -1443
- package/dist/tokens/primitives.css +36 -0
- package/dist/tokens/semantic.css +32 -0
- package/dist/tokens/theme-presets.css +33 -0
- package/package.json +32 -12
- package/src/__tests__/package-json.test.ts +8 -0
- package/src/components/Accordion/Accordion.module.css +1 -1
- package/src/components/Alert/Alert.module.css +1 -1
- package/src/components/AppsLauncher/AppsLauncher.module.css +96 -0
- package/src/components/AppsLauncher/AppsLauncher.stories.tsx +58 -0
- package/src/components/AppsLauncher/AppsLauncher.tsx +117 -0
- package/src/components/AppsLauncher/__tests__/AppsLauncher.test.tsx +52 -0
- package/src/components/Avatar/Avatar.module.css +1 -1
- package/src/components/Badge/Badge.module.css +30 -7
- package/src/components/Badge/Badge.stories.tsx +16 -4
- package/src/components/Badge/Badge.tsx +8 -11
- package/src/components/Badge/__tests__/Badge.test.tsx +4 -2
- package/src/components/BalanceChip/BalanceChip.module.css +1 -1
- package/src/components/Button/Button.module.css +18 -2
- package/src/components/Card/Card.module.css +1 -1
- package/src/components/CardTile/CardTile.module.css +181 -0
- package/src/components/CardTile/CardTile.stories.tsx +86 -0
- package/src/components/CardTile/CardTile.tsx +128 -0
- package/src/components/CardTile/__tests__/CardTile.test.tsx +56 -0
- package/src/components/CodeTextarea/CodeTextarea.module.css +1 -1
- package/src/components/Collapsible/Collapsible.module.css +1 -1
- package/src/components/ColorPicker/ColorPicker.module.css +132 -0
- package/src/components/ColorPicker/ColorPicker.stories.tsx +80 -0
- package/src/components/ColorPicker/ColorPicker.tsx +157 -0
- package/src/components/ColorPicker/__tests__/ColorPicker.test.tsx +117 -0
- package/src/components/Combobox/Combobox.module.css +2 -2
- package/src/components/Command/Command.module.css +1 -1
- package/src/components/ConfirmDialog/ConfirmDialog.module.css +2 -2
- package/src/components/ConfirmDialog/ConfirmDialog.tsx +28 -2
- package/src/components/ConfirmDialog/__tests__/ConfirmDialog.test.tsx +71 -3
- package/src/components/ConsentPrompt/ConsentPrompt.module.css +21 -0
- package/src/components/ConsentPrompt/ConsentPrompt.stories.tsx +31 -0
- package/src/components/ConsentPrompt/ConsentPrompt.tsx +66 -0
- package/src/components/ContextMenu/ContextMenu.tsx +1 -1
- package/src/components/DatePicker/DatePicker.module.css +1 -1
- package/src/components/DeviceOnlyGate/DeviceOnlyGate.tsx +58 -0
- package/src/components/DeviceOnlyGate/__tests__/DeviceOnlyGate.test.tsx +45 -0
- package/src/components/DeviceStorageKeyGate/DeviceStorageKeyGate.tsx +100 -0
- package/src/components/DeviceStorageKeyGate/__tests__/DeviceStorageKeyGate.test.tsx +119 -0
- package/src/components/Dialog/Dialog.module.css +130 -21
- package/src/components/Dialog/Dialog.stories.tsx +179 -2
- package/src/components/Dialog/Dialog.tsx +137 -94
- package/src/components/Dialog/__tests__/Dialog.test.tsx +190 -9
- package/src/components/DragHandleRow/DragHandleRow.tsx +2 -21
- package/src/components/Drawer/Drawer.module.css +36 -7
- package/src/components/Drawer/Drawer.stories.tsx +44 -0
- package/src/components/Drawer/Drawer.tsx +27 -66
- package/src/components/Drawer/__tests__/Drawer.test.tsx +55 -6
- package/src/components/GripIcon/GripIcon.tsx +27 -0
- package/src/components/Header/Header.module.css +62 -0
- package/src/components/Header/Header.stories.tsx +452 -0
- package/src/components/Header/Header.tsx +102 -0
- package/src/components/Header/__tests__/Header.test.tsx +104 -0
- package/src/components/HeaderFooterLayout/HeaderFooterLayout.module.css +21 -0
- package/src/components/HeaderFooterLayout/HeaderFooterLayout.stories.tsx +111 -0
- package/src/components/HeaderFooterLayout/HeaderFooterLayout.tsx +54 -0
- package/src/components/HeaderFooterLayout/__tests__/HeaderFooterLayout.test.tsx +58 -0
- package/src/components/Icon/icons/arrow-left-right.tsx +23 -0
- package/src/components/Icon/icons/bold.tsx +20 -0
- package/src/components/Icon/icons/calendar.tsx +4 -4
- package/src/components/Icon/icons/carrot.tsx +3 -3
- package/src/components/Icon/icons/circle-chevron-left.tsx +21 -0
- package/src/components/Icon/icons/code.tsx +21 -0
- package/src/components/Icon/icons/copy.tsx +21 -0
- package/src/components/Icon/icons/credit-card.tsx +21 -0
- package/src/components/Icon/icons/database.tsx +22 -0
- package/src/components/Icon/icons/download.tsx +22 -0
- package/src/components/Icon/icons/file-text.tsx +24 -0
- package/src/components/Icon/icons/folder-closed.tsx +21 -0
- package/src/components/Icon/icons/folder-open.tsx +20 -0
- package/src/components/Icon/icons/folder.tsx +20 -0
- package/src/components/Icon/icons/folders.tsx +21 -0
- package/src/components/Icon/icons/hat-glasses.tsx +24 -0
- package/src/components/Icon/icons/history.tsx +22 -0
- package/src/components/Icon/icons/inbox.tsx +21 -0
- package/src/components/Icon/icons/index.ts +169 -1
- package/src/components/Icon/icons/italic.tsx +22 -0
- package/src/components/Icon/icons/layers.tsx +22 -0
- package/src/components/Icon/icons/layout-dashboard.tsx +23 -0
- package/src/components/Icon/icons/layout-grid.tsx +23 -0
- package/src/components/Icon/icons/link.tsx +21 -0
- package/src/components/Icon/icons/list-ordered.tsx +25 -0
- package/src/components/Icon/icons/list.tsx +25 -0
- package/src/components/Icon/icons/luggage.tsx +24 -0
- package/src/components/Icon/icons/map-pin.tsx +21 -0
- package/src/components/Icon/icons/menu.tsx +22 -0
- package/src/components/Icon/icons/minus.tsx +20 -0
- package/src/components/Icon/icons/paint-bucket.tsx +23 -0
- package/src/components/Icon/icons/panel-left.tsx +21 -0
- package/src/components/Icon/icons/pin.tsx +21 -0
- package/src/components/Icon/icons/rectangle-ellipsis.tsx +23 -0
- package/src/components/Icon/icons/refresh-cw.tsx +23 -0
- package/src/components/Icon/icons/rotate-cw.tsx +21 -0
- package/src/components/Icon/icons/route.tsx +22 -0
- package/src/components/Icon/icons/send.tsx +21 -0
- package/src/components/Icon/icons/share-2.tsx +24 -0
- package/src/components/Icon/icons/sheet.tsx +24 -0
- package/src/components/Icon/icons/smartphone.tsx +21 -0
- package/src/components/Icon/icons/square-kanban.tsx +23 -0
- package/src/components/Icon/icons/table.tsx +23 -0
- package/src/components/Icon/icons/user-round-plus.tsx +23 -0
- package/src/components/Icon/icons/users.tsx +23 -0
- package/src/components/IconPicker/IconPicker.module.css +1 -1
- package/src/components/Input/Input.module.css +1 -1
- package/src/components/Input/Input.tsx +8 -2
- package/src/components/Kbd/Kbd.module.css +1 -1
- package/src/components/LegalLinks/LegalLinks.module.css +24 -0
- package/src/components/LegalLinks/LegalLinks.stories.tsx +22 -0
- package/src/components/LegalLinks/LegalLinks.tsx +40 -0
- package/src/components/Markdown/Markdown.module.css +73 -0
- package/src/components/Markdown/Markdown.stories.tsx +40 -0
- package/src/components/Markdown/Markdown.tsx +305 -0
- package/src/components/Markdown/__tests__/Markdown.test.tsx +144 -0
- package/src/components/Menu/Menu.module.css +1 -0
- package/src/components/Menu/Menu.tsx +72 -22
- package/src/components/Menu/__tests__/Menu.test.tsx +30 -0
- package/src/components/Menubar/Menubar.tsx +1 -1
- package/src/components/Message/Message.module.css +1 -1
- package/src/components/MessageScroller/MessageScroller.module.css +12 -1
- package/src/components/MessageScroller/MessageScroller.tsx +12 -0
- package/src/components/MobileAppsDrawer/MobileAppsDrawer.module.css +82 -0
- package/src/components/MobileAppsDrawer/MobileAppsDrawer.tsx +78 -0
- package/src/components/MobileAppsDrawer/__tests__/MobileAppsDrawer.test.tsx +80 -0
- package/src/components/MobileFooter/MobileFooter.module.css +73 -0
- package/src/components/MobileFooter/MobileFooter.stories.tsx +47 -0
- package/src/components/MobileFooter/MobileFooter.tsx +122 -0
- package/src/components/MobileFooter/__tests__/MobileFooter.test.tsx +129 -0
- package/src/components/MobileHeader/MobileHeader.module.css +76 -0
- package/src/components/MobileHeader/MobileHeader.stories.tsx +57 -0
- package/src/components/MobileHeader/MobileHeader.tsx +90 -0
- package/src/components/MobileHeader/__tests__/MobileHeader.test.tsx +113 -0
- package/src/components/NavList/NavList.module.css +130 -0
- package/src/components/NavList/NavList.tsx +136 -0
- package/src/components/NavList/__tests__/NavList.test.tsx +109 -0
- package/src/components/NavTabs/NavTabs.module.css +1 -1
- package/src/components/NavTabs/NavTabs.tsx +39 -11
- package/src/components/NotificationsPanel/NotificationsPanel.module.css +258 -0
- package/src/components/NotificationsPanel/NotificationsPanel.stories.tsx +61 -0
- package/src/components/NotificationsPanel/NotificationsPanel.tsx +187 -0
- package/src/components/NotificationsPanel/__tests__/NotificationsPanel.test.tsx +78 -0
- package/src/components/OfflineGate/OfflineGate.tsx +44 -0
- package/src/components/OfflineGate/__tests__/OfflineGate.test.tsx +66 -0
- package/src/components/OverlayHeader/OverlayHeader.module.css +1 -1
- package/src/components/OverlayHeader/OverlayHeader.tsx +19 -4
- package/src/components/OverlayHeader/__tests__/OverlayHeader.test.tsx +18 -0
- package/src/components/PageContainer/PageContainer.module.css +82 -0
- package/src/components/PageContainer/PageContainer.tsx +64 -0
- package/src/components/PageContainer/__tests__/PageContainer.test.tsx +70 -0
- package/src/components/PageHeader/PageHeader.module.css +54 -0
- package/src/components/PageHeader/PageHeader.tsx +157 -2
- package/src/components/PageHeader/__tests__/PageHeader.test.tsx +103 -0
- package/src/components/PageLayout/PageLayout.module.css +51 -0
- package/src/components/PageLayout/PageLayout.stories.tsx +73 -0
- package/src/components/PageLayout/PageLayout.tsx +51 -0
- package/src/components/PageLayout/__tests__/PageLayout.test.tsx +34 -0
- package/src/components/Popover/Popover.module.css +1 -1
- package/src/components/Popover/Popover.tsx +86 -4
- package/src/components/Popover/__tests__/Popover.test.tsx +165 -0
- package/src/components/QuantityStepper/QuantityStepper.module.css +1 -1
- package/src/components/Resizable/Resizable.module.css +3 -3
- package/src/components/ResponsiveSurface/ResponsiveSurface.tsx +24 -0
- package/src/components/ResponsiveSurface/__tests__/ResponsiveSurface.test.tsx +73 -0
- package/src/components/RootLayout/RootLayout.module.css +12 -0
- package/src/components/RootLayout/RootLayout.stories.tsx +125 -0
- package/src/components/RootLayout/RootLayout.tsx +185 -0
- package/src/components/RootLayout/__tests__/RootLayout.test.tsx +161 -0
- package/src/components/SegmentedControl/SegmentedControl.module.css +1 -1
- package/src/components/Select/Select.module.css +1 -1
- package/src/components/Sheet/Sheet.module.css +4 -1
- package/src/components/Sheet/Sheet.tsx +14 -56
- package/src/components/SplitPane/SplitPane.module.css +4 -4
- package/src/components/StatusBadge/StatusBadge.module.css +1 -1
- package/src/components/StepStrip/StepStrip.module.css +53 -0
- package/src/components/StepStrip/StepStrip.tsx +74 -0
- package/src/components/SuggestionInput/SuggestionInput.module.css +2 -2
- package/src/components/SwipableMobileCarousel/SwipableMobileCarousel.module.css +64 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarousel.tsx +243 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarouselSlide.tsx +35 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarouselSlideBody.tsx +42 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarouselSlideFooter.tsx +18 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarouselSlideHeader.tsx +21 -0
- package/src/components/SwipableMobileCarousel/SwipableMobileCarouselSlideParts.module.css +29 -0
- package/src/components/SwipableMobileCarousel/__tests__/SwipableMobileCarousel.test.tsx +214 -0
- package/src/components/SwipableMobileCarousel/__tests__/SwipableMobileCarouselDevGuard.test.tsx +61 -0
- package/src/components/SwipableMobileCarousel/context.ts +13 -0
- package/src/components/SwipableMobileCarouselDots/SwipableMobileCarouselDots.module.css +64 -0
- package/src/components/SwipableMobileCarouselDots/SwipableMobileCarouselDots.tsx +73 -0
- package/src/components/SwipableMobileCarouselDots/__tests__/SwipableMobileCarouselDots.test.tsx +63 -0
- package/src/components/SwipeStack/SwipeStack.module.css +101 -0
- package/src/components/SwipeStack/SwipeStack.tsx +199 -0
- package/src/components/SwipeStack/SwipeStackCard.tsx +22 -0
- package/src/components/SwipeStack/__tests__/SwipeStack.test.tsx +169 -0
- package/src/components/SystemBanner/SystemBanner.module.css +10 -0
- package/src/components/Table/Table.module.css +2 -2
- package/src/components/Tabs/Tabs.module.css +1 -1
- package/src/components/TagInput/TagInput.module.css +2 -2
- package/src/components/Textarea/Textarea.module.css +9 -1
- package/src/components/Textarea/Textarea.tsx +28 -5
- package/src/components/Textarea/__tests__/Textarea.test.tsx +23 -1
- package/src/components/ThreeColumnLayout/ThreeColumnLayout.module.css +35 -0
- package/src/components/ThreeColumnLayout/ThreeColumnLayout.stories.tsx +152 -0
- package/src/components/ThreeColumnLayout/ThreeColumnLayout.tsx +87 -0
- package/src/components/ThreeColumnLayout/__tests__/ThreeColumnLayout.test.tsx +78 -0
- package/src/components/Toast/Toast.module.css +62 -3
- package/src/components/Toast/Toast.stories.tsx +1 -1
- package/src/components/Tooltip/Tooltip.module.css +1 -1
- package/src/components/UserMenu/UserMenu.module.css +90 -0
- package/src/components/UserMenu/UserMenu.stories.tsx +49 -0
- package/src/components/UserMenu/UserMenu.tsx +132 -0
- package/src/components/UserMenu/__tests__/UserMenu.test.tsx +77 -0
- package/src/hooks/__tests__/useCarouselRouteSync.test.tsx +126 -0
- package/src/hooks/__tests__/useIsOffline.test.tsx +53 -0
- package/src/hooks/__tests__/useOfflineTileState.test.tsx +69 -0
- package/src/hooks/__tests__/usePublishShellChromeHeight.test.tsx +118 -0
- package/src/hooks/__tests__/useReorderSensors.test.ts +26 -0
- package/src/hooks/__tests__/useResponsiveLayout.test.tsx +87 -0
- package/src/hooks/__tests__/useSnapCarousel.test.tsx +175 -0
- package/src/hooks/__tests__/useSwipeReveal.test.tsx +110 -0
- package/src/hooks/__tests__/useSwipeStack.test.tsx +155 -0
- package/src/hooks/index.ts +27 -0
- package/src/hooks/useCarouselRouteSync.ts +92 -0
- package/src/hooks/useIsOffline.ts +41 -0
- package/src/hooks/useOfflineTileState.ts +45 -0
- package/src/hooks/usePublishShellChromeHeight.ts +96 -0
- package/src/hooks/useReorderSensors.ts +88 -0
- package/src/hooks/useResponsiveLayout.ts +36 -0
- package/src/hooks/useSnapCarousel.ts +284 -0
- package/src/hooks/useSwipeReveal.ts +107 -0
- package/src/hooks/useSwipeStack.ts +235 -0
- package/src/index.ts +124 -3
- package/src/overlay-shell.ts +159 -0
- package/src/stories/DesignSystemOverview.stories.tsx +933 -6
- package/src/stories/DeviceOnlyGate.stories.tsx +35 -0
- package/src/stories/DeviceStorageKeyGate.stories.tsx +57 -0
- package/src/stories/GripIcon.stories.tsx +29 -0
- package/src/stories/InteractionHooks.stories.tsx +483 -0
- package/src/stories/LayoutSystem.stories.tsx +639 -0
- package/src/stories/MenuEntries.stories.tsx +86 -0
- package/src/stories/MobilePatterns.stories.tsx +247 -2
- package/src/stories/NavList.stories.tsx +147 -0
- package/src/stories/NavTabs.stories.tsx +18 -0
- package/src/stories/OfflineGate.stories.tsx +42 -0
- package/src/stories/PageContainer.stories.tsx +82 -0
- package/src/stories/PageHeader.stories.tsx +65 -1
- package/src/stories/ResponsiveSurface.stories.tsx +47 -0
- package/src/stories/StepStrip.stories.tsx +76 -0
- package/src/stories/SwipableMobileCarousel.stories.tsx +362 -0
- package/src/stories/SwipeStack.stories.tsx +200 -0
- package/src/stories/Textarea.stories.tsx +12 -0
- package/src/stories/TokenGallery.stories.tsx +115 -0
- package/src/tokens/primitives.css +36 -0
- package/src/tokens/semantic.css +32 -0
- package/src/tokens/theme-presets.css +33 -0
- package/src/tokens/theme-presets.ts +72 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Children,
|
|
5
|
+
isValidElement,
|
|
6
|
+
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
type ReactElement,
|
|
11
|
+
type ReactNode,
|
|
12
|
+
} from 'react';
|
|
13
|
+
import { useSnapCarousel } from '../../hooks/useSnapCarousel';
|
|
14
|
+
import { SwipableMobileCarouselDots } from '../SwipableMobileCarouselDots/SwipableMobileCarouselDots';
|
|
15
|
+
import { SwipableMobileCarouselSlide } from './SwipableMobileCarouselSlide';
|
|
16
|
+
import { CarouselSlideMountContext } from './context';
|
|
17
|
+
import styles from './SwipableMobileCarousel.module.css';
|
|
18
|
+
|
|
19
|
+
export interface SwipableMobileCarouselIndicatorProps {
|
|
20
|
+
count: number;
|
|
21
|
+
activeIndex: number;
|
|
22
|
+
labels: (string | undefined)[];
|
|
23
|
+
onJump: (index: number) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SwipableMobileCarouselProps {
|
|
27
|
+
/** Controlled active slide index — the caller owns this (typically via
|
|
28
|
+
* useCarouselRouteSync, or plain useState). */
|
|
29
|
+
activeIndex: number;
|
|
30
|
+
/** Called when a swipe gesture settles on a different index, or when the
|
|
31
|
+
* reorder-jump fix re-snaps after the active slide's position changes.
|
|
32
|
+
* The caller is responsible for updating `activeIndex` in response. */
|
|
33
|
+
onSettle: (index: number) => void;
|
|
34
|
+
/** How many slides on each side of the active one stay mounted. Default 1
|
|
35
|
+
* matches both sovereign-tasks' and sovereign-shopper's existing "swipe
|
|
36
|
+
* never shows a spinner" behavior. Slides outside this window keep their
|
|
37
|
+
* DOM slot (so scroll-snap's position-based indexing stays correct) but
|
|
38
|
+
* their children are not mounted at all. */
|
|
39
|
+
prefetchDistance?: number;
|
|
40
|
+
/** Passed straight through to the internal useSnapCarousel. */
|
|
41
|
+
settleDebounceMs?: number;
|
|
42
|
+
/** Required — labels the scroller region for assistive tech. No generic
|
|
43
|
+
* fallback, since one would be wrong for every actual consumer. */
|
|
44
|
+
'aria-label': string;
|
|
45
|
+
/** Renders the indicator. Defaults to SwipableMobileCarouselDots. Pass
|
|
46
|
+
* `null` to render no indicator at all. */
|
|
47
|
+
renderIndicator?: null | ((props: SwipableMobileCarouselIndicatorProps) => ReactNode);
|
|
48
|
+
className?: string;
|
|
49
|
+
/** Must be SwipableMobileCarouselSlide elements (nullish/boolean children
|
|
50
|
+
* are safely skipped for conditional slides). A dev-mode warning is
|
|
51
|
+
* logged for any other child type — see resolveSlides below. */
|
|
52
|
+
children: ReactNode;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface ResolvedSlide {
|
|
56
|
+
key: string;
|
|
57
|
+
label: string | undefined;
|
|
58
|
+
element: ReactElement;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function describeChild(child: unknown): string {
|
|
62
|
+
if (isValidElement(child)) {
|
|
63
|
+
const type = child.type;
|
|
64
|
+
if (typeof type === 'string') return `<${type}>`;
|
|
65
|
+
if (typeof type === 'function') {
|
|
66
|
+
const named = type as { displayName?: string; name?: string };
|
|
67
|
+
return `<${named.displayName ?? named.name ?? 'Component'}>`;
|
|
68
|
+
}
|
|
69
|
+
return '<unknown>';
|
|
70
|
+
}
|
|
71
|
+
if (typeof child === 'string') return `string "${child}"`;
|
|
72
|
+
return String(child);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function resolveSlides(children: ReactNode): ResolvedSlide[] {
|
|
76
|
+
const slides: ResolvedSlide[] = [];
|
|
77
|
+
Children.forEach(children, (child, index) => {
|
|
78
|
+
if (child === null || child === undefined || child === false || child === true) return;
|
|
79
|
+
if (!isValidElement(child) || child.type !== SwipableMobileCarouselSlide) {
|
|
80
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
81
|
+
console.error(
|
|
82
|
+
'[SwipableMobileCarousel] Every child must be a <SwipableMobileCarouselSlide>. ' +
|
|
83
|
+
`Found ${describeChild(child)} at position ${index}. A non-Slide child shifts every ` +
|
|
84
|
+
"subsequent slide's index, desyncing scroll-snap position from activeIndex.",
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const slideProps = child.props as { slideKey: string; label?: string };
|
|
90
|
+
if (process.env.NODE_ENV !== 'production' && !slideProps.label) {
|
|
91
|
+
console.warn(
|
|
92
|
+
`[SwipableMobileCarousel] SwipableMobileCarouselSlide "${slideProps.slideKey}" has no ` +
|
|
93
|
+
'label — it will show as "Slide N of count" in the default dots indicator.',
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
slides.push({ key: slideProps.slideKey, label: slideProps.label, element: child });
|
|
97
|
+
});
|
|
98
|
+
return slides;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* SwipableMobileCarousel — a compound component for swiping between full-
|
|
103
|
+
* width slides (each an independent route/view on mobile), wrapping
|
|
104
|
+
* useSnapCarousel. This library's first compound component: every other
|
|
105
|
+
* component here is flat/prop-based, but slide content is large, arbitrary,
|
|
106
|
+
* per-plugin subtrees that need a header-known/body-loading split an
|
|
107
|
+
* array-of-objects prop would only express with worse JSX ergonomics.
|
|
108
|
+
*
|
|
109
|
+
* Owns rendering and mount-window mechanics only — it has no opinion on
|
|
110
|
+
* where slide data lives. Do NOT: (a) aggregate cross-slide data inside a
|
|
111
|
+
* SwipableMobileCarouselSlideBody (that recomputes on every slide render
|
|
112
|
+
* regardless of which slide is active — belongs in the parent, which
|
|
113
|
+
* already knows every slide exists); (b) mount a detail overlay (Sheet,
|
|
114
|
+
* Dialog) inside a Slide's children — mount it as a sibling of
|
|
115
|
+
* SwipableMobileCarousel instead, controlled by the same state that would
|
|
116
|
+
* otherwise drive a routed page's overlay. sovereign-tasks' current
|
|
117
|
+
* hand-rolled carousel does both and is measurably laggier than
|
|
118
|
+
* sovereign-shopper's equivalent, which keeps its overlay in the routed pane.
|
|
119
|
+
*/
|
|
120
|
+
export function SwipableMobileCarousel({
|
|
121
|
+
activeIndex,
|
|
122
|
+
onSettle,
|
|
123
|
+
prefetchDistance = 1,
|
|
124
|
+
settleDebounceMs = 120,
|
|
125
|
+
'aria-label': ariaLabel,
|
|
126
|
+
renderIndicator,
|
|
127
|
+
className,
|
|
128
|
+
children,
|
|
129
|
+
}: SwipableMobileCarouselProps) {
|
|
130
|
+
const slides = useMemo(() => resolveSlides(children), [children]);
|
|
131
|
+
const count = slides.length;
|
|
132
|
+
const clampedActiveIndex = count === 0 ? 0 : Math.max(0, Math.min(count - 1, activeIndex));
|
|
133
|
+
|
|
134
|
+
// Initial scroll position, once — subsequent activeIndex changes come
|
|
135
|
+
// either from the user's own swipe (already reflected in DOM scroll
|
|
136
|
+
// position; re-syncing to the same index below is a harmless no-op) or an
|
|
137
|
+
// external navigation, which the effect below scrolls to explicitly.
|
|
138
|
+
const initialActiveIndexRef = useRef(clampedActiveIndex);
|
|
139
|
+
|
|
140
|
+
const { scrollRef, scrollToIndex, liveIndex } = useSnapCarousel({
|
|
141
|
+
itemCount: count,
|
|
142
|
+
onSettle,
|
|
143
|
+
debounceMs: settleDebounceMs,
|
|
144
|
+
initialIndex: initialActiveIndexRef.current,
|
|
145
|
+
});
|
|
146
|
+
useLayoutEffect(() => {
|
|
147
|
+
scrollToIndex(initialActiveIndexRef.current, 'instant');
|
|
148
|
+
// Runs once on mount only, matching both existing plugins' identical pattern.
|
|
149
|
+
}, []);
|
|
150
|
+
|
|
151
|
+
const didMountActiveIndexSyncRef = useRef(false);
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (!didMountActiveIndexSyncRef.current) {
|
|
154
|
+
didMountActiveIndexSyncRef.current = true;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
scrollToIndex(clampedActiveIndex, 'smooth');
|
|
158
|
+
}, [clampedActiveIndex, scrollToIndex]);
|
|
159
|
+
|
|
160
|
+
// Reorder-jump fix: scroll-snap position tracks DOM order, not slide
|
|
161
|
+
// identity. If the previously-active slide's key still exists but moved
|
|
162
|
+
// (a caller re-sorted their slide array while mounted), re-snap to its
|
|
163
|
+
// new position and report it via onSettle so the caller's activeIndex
|
|
164
|
+
// stays in sync — otherwise scroll position silently desyncs from slide
|
|
165
|
+
// identity. Runs every commit (no dependency array) since it needs to
|
|
166
|
+
// notice any children change, not just a specific prop.
|
|
167
|
+
const prevKeysRef = useRef<string[] | null>(null);
|
|
168
|
+
useLayoutEffect(() => {
|
|
169
|
+
const currentKeys = slides.map((s) => s.key);
|
|
170
|
+
const prevKeys = prevKeysRef.current;
|
|
171
|
+
if (prevKeys) {
|
|
172
|
+
const sameOrder =
|
|
173
|
+
prevKeys.length === currentKeys.length && prevKeys.every((k, i) => k === currentKeys[i]);
|
|
174
|
+
if (!sameOrder) {
|
|
175
|
+
const activeKey = prevKeys[clampedActiveIndex];
|
|
176
|
+
if (activeKey !== undefined) {
|
|
177
|
+
const newIndex = currentKeys.indexOf(activeKey);
|
|
178
|
+
if (newIndex !== -1 && newIndex !== clampedActiveIndex) {
|
|
179
|
+
scrollToIndex(newIndex, 'instant');
|
|
180
|
+
onSettle(newIndex);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
prevKeysRef.current = currentKeys;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
function handleJump(index: number) {
|
|
189
|
+
// scrollToIndex sets useSnapCarousel's own dedupe ref synchronously, so
|
|
190
|
+
// the natural scroll-settle detection will NOT re-report this index —
|
|
191
|
+
// onSettle must be called directly here, same pairing as the
|
|
192
|
+
// reorder-jump fix above.
|
|
193
|
+
scrollToIndex(index, 'smooth');
|
|
194
|
+
onSettle(index);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const labels = slides.map((s) => s.label);
|
|
198
|
+
const indicator =
|
|
199
|
+
renderIndicator === null ? null : renderIndicator ? (
|
|
200
|
+
renderIndicator({ count, activeIndex: clampedActiveIndex, labels, onJump: handleJump })
|
|
201
|
+
) : (
|
|
202
|
+
<SwipableMobileCarouselDots
|
|
203
|
+
count={count}
|
|
204
|
+
activeIndex={clampedActiveIndex}
|
|
205
|
+
labels={labels}
|
|
206
|
+
aria-label={ariaLabel}
|
|
207
|
+
onJump={handleJump}
|
|
208
|
+
/>
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<div className={[styles.wrap, className].filter(Boolean).join(' ')}>
|
|
213
|
+
<div className={styles.scroller} ref={scrollRef} role="region" aria-label={ariaLabel}>
|
|
214
|
+
{slides.map((slide, i) => (
|
|
215
|
+
<div className={styles.slide} key={slide.key}>
|
|
216
|
+
<CarouselSlideMountContext.Provider
|
|
217
|
+
value={{
|
|
218
|
+
// Union of the settled index and the live (real-time,
|
|
219
|
+
// pre-settle) scroll position — see useSnapCarousel's own
|
|
220
|
+
// liveIndex doc comment. A single fast flick can carry
|
|
221
|
+
// native scroll-snap momentum past an intermediate slide to
|
|
222
|
+
// one two-or-more away before settle-detection's debounce
|
|
223
|
+
// window ever reports it; without also checking liveIndex,
|
|
224
|
+
// the slide the container had already visually scrolled to
|
|
225
|
+
// stays unmounted (rendering nothing at all, not even a
|
|
226
|
+
// loading skeleton) for that entire window.
|
|
227
|
+
isMounted:
|
|
228
|
+
Math.abs(i - clampedActiveIndex) <= prefetchDistance ||
|
|
229
|
+
Math.abs(i - liveIndex) <= prefetchDistance,
|
|
230
|
+
}}
|
|
231
|
+
>
|
|
232
|
+
{slide.element}
|
|
233
|
+
</CarouselSlideMountContext.Provider>
|
|
234
|
+
</div>
|
|
235
|
+
))}
|
|
236
|
+
</div>
|
|
237
|
+
{/* Positioning (overlay the bottom of .wrap) lives here, applied around
|
|
238
|
+
whatever renderIndicator returns — see .indicatorSlot's own doc
|
|
239
|
+
comment for why this can't be delegated to the indicator itself. */}
|
|
240
|
+
{indicator && <div className={styles.indicatorSlot}>{indicator}</div>}
|
|
241
|
+
</div>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useContext, type ReactNode } from 'react';
|
|
4
|
+
import { CarouselSlideMountContext } from './context';
|
|
5
|
+
|
|
6
|
+
export interface SwipableMobileCarouselSlideProps {
|
|
7
|
+
/** Stable identity for this slide, independent of its position among
|
|
8
|
+
* siblings — required. This is what makes SwipableMobileCarousel's
|
|
9
|
+
* reorder-jump fix possible: a plain array index can't distinguish "this
|
|
10
|
+
* slide moved to position 2" from "this slide was removed and a
|
|
11
|
+
* different one is now at position 2." Reuse whatever id the caller
|
|
12
|
+
* already keys their `.map()` on (e.g. a list's own id). */
|
|
13
|
+
slideKey: string;
|
|
14
|
+
/** Accessible label surfaced to the default dots indicator (e.g.
|
|
15
|
+
* "Groceries"). Falls back to "Slide N of count" if omitted — omitting it
|
|
16
|
+
* logs a dev-mode warning, since every real slide usually has a title
|
|
17
|
+
* available synchronously already. */
|
|
18
|
+
label?: string;
|
|
19
|
+
/** SwipableMobileCarouselSlideHeader/Body/Footer, in any order/subset. */
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* SwipableMobileCarouselSlide — a single slide of a SwipableMobileCarousel.
|
|
25
|
+
* Renders its children only while within the carousel's mount window (the
|
|
26
|
+
* active slide ± its configured prefetchDistance); outside that window it
|
|
27
|
+
* renders nothing, matching both sovereign-tasks and sovereign-shopper's
|
|
28
|
+
* existing "never fetch/render a non-neighbor slide" behavior, now
|
|
29
|
+
* centralized instead of hand-rolled per plugin.
|
|
30
|
+
*/
|
|
31
|
+
export function SwipableMobileCarouselSlide({ children }: SwipableMobileCarouselSlideProps) {
|
|
32
|
+
const ctx = useContext(CarouselSlideMountContext);
|
|
33
|
+
if (!ctx?.isMounted) return null;
|
|
34
|
+
return <>{children}</>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import styles from './SwipableMobileCarouselSlideParts.module.css';
|
|
3
|
+
|
|
4
|
+
export interface SwipableMobileCarouselSlideBodyProps {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
/** While true, renders `loadingFallback` INSTEAD OF children — scoped to
|
|
7
|
+
* just this region, not the whole slide. This is what fixes the
|
|
8
|
+
* "whole slide blanks out, title included, until its own fetch resolves"
|
|
9
|
+
* bug: a caller renders `<SwipableMobileCarouselSlideHeader>` from
|
|
10
|
+
* already-known metadata unconditionally, and gates only this component's
|
|
11
|
+
* `loading` on the slower fetch — the natural way to compose
|
|
12
|
+
* Header/Body/Footer as siblings already produces that behavior, instead
|
|
13
|
+
* of gating the entire slide behind one boolean. */
|
|
14
|
+
loading?: boolean;
|
|
15
|
+
loadingFallback?: ReactNode;
|
|
16
|
+
className?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const defaultLoadingFallback = <div className={styles.loading}>Loading…</div>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* SwipableMobileCarouselSlideBody — the scrollable region of a slide.
|
|
23
|
+
*
|
|
24
|
+
* Deliberately does not set `touch-action: pan-y` on its scroll container
|
|
25
|
+
* (see SwipableMobileCarouselSlideParts.module.css's `.body` rule) — that
|
|
26
|
+
* would intersect-to-empty against the carousel's own horizontal
|
|
27
|
+
* touch-action handling (docs/architecture-rules.md's touch-action
|
|
28
|
+
* intersection rule), breaking both the vertical scroll here and the
|
|
29
|
+
* horizontal swipe between slides.
|
|
30
|
+
*/
|
|
31
|
+
export function SwipableMobileCarouselSlideBody({
|
|
32
|
+
children,
|
|
33
|
+
loading = false,
|
|
34
|
+
loadingFallback,
|
|
35
|
+
className,
|
|
36
|
+
}: SwipableMobileCarouselSlideBodyProps) {
|
|
37
|
+
return (
|
|
38
|
+
<div className={[styles.body, className].filter(Boolean).join(' ')}>
|
|
39
|
+
{loading ? (loadingFallback ?? defaultLoadingFallback) : children}
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import styles from './SwipableMobileCarouselSlideParts.module.css';
|
|
3
|
+
|
|
4
|
+
export interface SwipableMobileCarouselSlideFooterProps {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* SwipableMobileCarouselSlideFooter — always renders its children, no
|
|
11
|
+
* loading concept. See SwipableMobileCarouselSlideHeader's doc comment.
|
|
12
|
+
*/
|
|
13
|
+
export function SwipableMobileCarouselSlideFooter({
|
|
14
|
+
children,
|
|
15
|
+
className,
|
|
16
|
+
}: SwipableMobileCarouselSlideFooterProps) {
|
|
17
|
+
return <div className={[styles.footer, className].filter(Boolean).join(' ')}>{children}</div>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import styles from './SwipableMobileCarouselSlideParts.module.css';
|
|
3
|
+
|
|
4
|
+
export interface SwipableMobileCarouselSlideHeaderProps {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* SwipableMobileCarouselSlideHeader — always renders its children, no
|
|
11
|
+
* loading concept. Meant for content already known synchronously (a list's
|
|
12
|
+
* title, color, item count from data the caller already has) so it can
|
|
13
|
+
* render immediately even while SwipableMobileCarouselSlideBody's own
|
|
14
|
+
* content is still loading — see that component's doc comment.
|
|
15
|
+
*/
|
|
16
|
+
export function SwipableMobileCarouselSlideHeader({
|
|
17
|
+
children,
|
|
18
|
+
className,
|
|
19
|
+
}: SwipableMobileCarouselSlideHeaderProps) {
|
|
20
|
+
return <div className={[styles.header, className].filter(Boolean).join(' ')}>{children}</div>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
.header,
|
|
2
|
+
.footer {
|
|
3
|
+
flex: 0 0 auto;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.body {
|
|
7
|
+
flex: 1 1 auto;
|
|
8
|
+
min-height: 0; /* allow shrinking inside the slide's flex column, so it can scroll instead of overflowing */
|
|
9
|
+
overflow-y: auto;
|
|
10
|
+
overflow-x: hidden;
|
|
11
|
+
/* Deliberately does NOT set touch-action: pan-y here. touch-action's
|
|
12
|
+
effective value is the INTERSECTION of this element's and every
|
|
13
|
+
ancestor's value (docs/architecture-rules.md) — the ancestor .scroller
|
|
14
|
+
(SwipableMobileCarousel.module.css) needs the horizontal swipe gesture,
|
|
15
|
+
so pan-y here ∩ that horizontal handling would resolve to neither axis
|
|
16
|
+
working natively. Relying on the browser's default touch-action (auto)
|
|
17
|
+
here lets native gesture arbitration between the two nested scrollers
|
|
18
|
+
sort out which axis wins per-gesture — don't "fix" perceived nested-
|
|
19
|
+
scroll jank by adding pan-y; it silently breaks horizontal swipe instead. */
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.loading {
|
|
23
|
+
display: flex;
|
|
24
|
+
align-items: center;
|
|
25
|
+
justify-content: center;
|
|
26
|
+
height: 100%;
|
|
27
|
+
color: var(--sv-color-text-subtle);
|
|
28
|
+
font-size: var(--sv-font-size-sm);
|
|
29
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
|
|
4
|
+
import { SwipableMobileCarousel } from '../SwipableMobileCarousel';
|
|
5
|
+
import { SwipableMobileCarouselSlide } from '../SwipableMobileCarouselSlide';
|
|
6
|
+
import { SwipableMobileCarouselSlideHeader } from '../SwipableMobileCarouselSlideHeader';
|
|
7
|
+
import { SwipableMobileCarouselSlideBody } from '../SwipableMobileCarouselSlideBody';
|
|
8
|
+
|
|
9
|
+
// Same jsdom scroll polyfill/fake-timer setup as useSnapCarousel.test.tsx,
|
|
10
|
+
// since this component wraps that hook directly.
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.useFakeTimers();
|
|
13
|
+
if (!Element.prototype.scrollTo) {
|
|
14
|
+
Element.prototype.scrollTo = vi.fn();
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
vi.runOnlyPendingTimers();
|
|
20
|
+
vi.useRealTimers();
|
|
21
|
+
cleanup();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function setLayout(el: HTMLElement, clientWidth: number, scrollLeft: number) {
|
|
25
|
+
Object.defineProperty(el, 'clientWidth', { value: clientWidth, configurable: true });
|
|
26
|
+
Object.defineProperty(el, 'scrollLeft', { value: scrollLeft, configurable: true });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const KEYS = ['a', 'b', 'c', 'd', 'e'];
|
|
30
|
+
|
|
31
|
+
function FiveSlides({
|
|
32
|
+
activeIndex,
|
|
33
|
+
onSettle,
|
|
34
|
+
}: {
|
|
35
|
+
activeIndex: number;
|
|
36
|
+
onSettle: (i: number) => void;
|
|
37
|
+
}) {
|
|
38
|
+
return (
|
|
39
|
+
<SwipableMobileCarousel activeIndex={activeIndex} onSettle={onSettle} aria-label="Test slides">
|
|
40
|
+
{KEYS.map((key) => (
|
|
41
|
+
<SwipableMobileCarouselSlide key={key} slideKey={key} label={key}>
|
|
42
|
+
<SwipableMobileCarouselSlideHeader>{`header-${key}`}</SwipableMobileCarouselSlideHeader>
|
|
43
|
+
<SwipableMobileCarouselSlideBody>{`body-${key}`}</SwipableMobileCarouselSlideBody>
|
|
44
|
+
</SwipableMobileCarouselSlide>
|
|
45
|
+
))}
|
|
46
|
+
</SwipableMobileCarousel>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('SwipableMobileCarousel', () => {
|
|
51
|
+
it('only mounts the active slide and its immediate neighbors (default prefetchDistance=1)', () => {
|
|
52
|
+
render(<FiveSlides activeIndex={2} onSettle={() => {}} />);
|
|
53
|
+
// active (c) and neighbors (b, d) are mounted
|
|
54
|
+
expect(screen.getByText('header-b')).toBeDefined();
|
|
55
|
+
expect(screen.getByText('header-c')).toBeDefined();
|
|
56
|
+
expect(screen.getByText('header-d')).toBeDefined();
|
|
57
|
+
// out-of-window slides (a, e) render nothing at all
|
|
58
|
+
expect(screen.queryByText('header-a')).toBeNull();
|
|
59
|
+
expect(screen.queryByText('body-a')).toBeNull();
|
|
60
|
+
expect(screen.queryByText('header-e')).toBeNull();
|
|
61
|
+
expect(screen.queryByText('body-e')).toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("SlideBody's loading prop only hides that region, not the Header", () => {
|
|
65
|
+
render(
|
|
66
|
+
<SwipableMobileCarousel activeIndex={0} onSettle={() => {}} aria-label="Test slides">
|
|
67
|
+
<SwipableMobileCarouselSlide slideKey="only" label="Only">
|
|
68
|
+
<SwipableMobileCarouselSlideHeader>the-title</SwipableMobileCarouselSlideHeader>
|
|
69
|
+
<SwipableMobileCarouselSlideBody loading>tasks-content</SwipableMobileCarouselSlideBody>
|
|
70
|
+
</SwipableMobileCarouselSlide>
|
|
71
|
+
</SwipableMobileCarousel>,
|
|
72
|
+
);
|
|
73
|
+
expect(screen.getByText('the-title')).toBeDefined();
|
|
74
|
+
expect(screen.queryByText('tasks-content')).toBeNull();
|
|
75
|
+
expect(screen.getByText('Loading…')).toBeDefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('calls onSettle with the new index after a swipe settles', () => {
|
|
79
|
+
const onSettle = vi.fn();
|
|
80
|
+
render(<FiveSlides activeIndex={0} onSettle={onSettle} />);
|
|
81
|
+
const scroller = screen.getByRole('region', { name: 'Test slides' });
|
|
82
|
+
fireEvent.touchStart(scroller);
|
|
83
|
+
setLayout(scroller, 300, 300); // scrollLeft 300 / width 300 -> index 1
|
|
84
|
+
fireEvent.scroll(scroller);
|
|
85
|
+
act(() => void vi.advanceTimersByTime(120));
|
|
86
|
+
expect(onSettle).toHaveBeenCalledWith(1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('clicking a dot calls onSettle directly with that index', () => {
|
|
90
|
+
const onSettle = vi.fn();
|
|
91
|
+
render(<FiveSlides activeIndex={0} onSettle={onSettle} />);
|
|
92
|
+
const dots = screen.getAllByRole('tab');
|
|
93
|
+
const fourthDot = dots[3];
|
|
94
|
+
if (!fourthDot) throw new Error('expected a 4th dot');
|
|
95
|
+
setLayout(screen.getByRole('region', { name: 'Test slides' }), 300, 0);
|
|
96
|
+
fireEvent.click(fourthDot);
|
|
97
|
+
expect(onSettle).toHaveBeenCalledWith(3);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('positions both the default and a custom renderIndicator inside the same overlay slot', () => {
|
|
101
|
+
// Regression test: the overlay-positioning class used to be applied
|
|
102
|
+
// directly to the default SwipableMobileCarouselDots instance, so any
|
|
103
|
+
// caller-supplied renderIndicator rendered as a plain static element
|
|
104
|
+
// instead — pushed out of view below the full-height scroller. The
|
|
105
|
+
// carousel must own positioning for whatever renderIndicator returns.
|
|
106
|
+
const { unmount } = render(<FiveSlides activeIndex={0} onSettle={() => {}} />);
|
|
107
|
+
const defaultTablist = screen.getByRole('tablist');
|
|
108
|
+
expect(defaultTablist.parentElement?.className).toMatch(/indicatorSlot/);
|
|
109
|
+
unmount();
|
|
110
|
+
|
|
111
|
+
render(
|
|
112
|
+
<SwipableMobileCarousel
|
|
113
|
+
activeIndex={0}
|
|
114
|
+
onSettle={() => {}}
|
|
115
|
+
aria-label="Test slides"
|
|
116
|
+
renderIndicator={({ count, activeIndex, onJump }) => (
|
|
117
|
+
<div role="tablist" aria-label="Custom">
|
|
118
|
+
{Array.from({ length: count }, (_, i) => (
|
|
119
|
+
<button key={i} type="button" role="tab" onClick={() => onJump(i)}>
|
|
120
|
+
{i === activeIndex ? 'active' : 'inactive'}
|
|
121
|
+
</button>
|
|
122
|
+
))}
|
|
123
|
+
</div>
|
|
124
|
+
)}
|
|
125
|
+
>
|
|
126
|
+
{KEYS.map((key) => (
|
|
127
|
+
<SwipableMobileCarouselSlide key={key} slideKey={key} label={key}>
|
|
128
|
+
{`slide-${key}`}
|
|
129
|
+
</SwipableMobileCarouselSlide>
|
|
130
|
+
))}
|
|
131
|
+
</SwipableMobileCarousel>,
|
|
132
|
+
);
|
|
133
|
+
const customTablist = screen.getByRole('tablist', { name: 'Custom' });
|
|
134
|
+
expect(customTablist.parentElement?.className).toMatch(/indicatorSlot/);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('re-snaps and reports the new index when the active slide moves position (reorder-jump fix)', () => {
|
|
138
|
+
function Reorderable({ order, onSettle }: { order: string[]; onSettle: (i: number) => void }) {
|
|
139
|
+
return (
|
|
140
|
+
<SwipableMobileCarousel activeIndex={1} onSettle={onSettle} aria-label="Test slides">
|
|
141
|
+
{order.map((key) => (
|
|
142
|
+
<SwipableMobileCarouselSlide key={key} slideKey={key} label={key}>
|
|
143
|
+
<SwipableMobileCarouselSlideHeader>{`header-${key}`}</SwipableMobileCarouselSlideHeader>
|
|
144
|
+
</SwipableMobileCarouselSlide>
|
|
145
|
+
))}
|
|
146
|
+
</SwipableMobileCarousel>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
const onSettle = vi.fn();
|
|
150
|
+
const { rerender } = render(<Reorderable order={['a', 'b', 'c']} onSettle={onSettle} />);
|
|
151
|
+
const scroller = screen.getByRole('region', { name: 'Test slides' });
|
|
152
|
+
setLayout(scroller, 300, 0);
|
|
153
|
+
const spy = vi.spyOn(scroller, 'scrollTo');
|
|
154
|
+
|
|
155
|
+
// 'b' (index 1, active) moves to index 0.
|
|
156
|
+
rerender(<Reorderable order={['b', 'a', 'c']} onSettle={onSettle} />);
|
|
157
|
+
|
|
158
|
+
expect(onSettle).toHaveBeenCalledWith(0);
|
|
159
|
+
expect(spy).toHaveBeenCalledWith({ left: 0, behavior: 'instant' });
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('mounts a slide reached by a fast multi-slide flick before settle-detection reports it', () => {
|
|
163
|
+
// Regression test, found via a user screen recording: a fast flick can
|
|
164
|
+
// carry native scroll-snap momentum straight past an intermediate slide
|
|
165
|
+
// to one two-or-more away in a single continuous gesture. Settle-
|
|
166
|
+
// detection deliberately still waits out the full debounce window before
|
|
167
|
+
// calling onSettle (to distinguish a real settle from drift — see
|
|
168
|
+
// useSnapCarousel's chainTrustedRef comment), but the mount window used
|
|
169
|
+
// to be keyed *only* off that settled index — so the slide the container
|
|
170
|
+
// had already scrolled to, visually, sat completely unmounted (not even
|
|
171
|
+
// a loading skeleton, per SwipableMobileCarouselSlide's `return null`)
|
|
172
|
+
// for the entire debounce window. liveIndex closes that gap.
|
|
173
|
+
const onSettle = vi.fn();
|
|
174
|
+
const KEYS8 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
|
175
|
+
function EightSlides({ onSettle }: { onSettle: (i: number) => void }) {
|
|
176
|
+
return (
|
|
177
|
+
<SwipableMobileCarousel activeIndex={0} onSettle={onSettle} aria-label="Test slides">
|
|
178
|
+
{KEYS8.map((key) => (
|
|
179
|
+
<SwipableMobileCarouselSlide key={key} slideKey={key} label={key}>
|
|
180
|
+
<SwipableMobileCarouselSlideHeader>{`header-${key}`}</SwipableMobileCarouselSlideHeader>
|
|
181
|
+
</SwipableMobileCarouselSlide>
|
|
182
|
+
))}
|
|
183
|
+
</SwipableMobileCarousel>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
render(<EightSlides onSettle={onSettle} />);
|
|
187
|
+
const scroller = screen.getByRole('region', { name: 'Test slides' });
|
|
188
|
+
// Slide 'e' (index 4) is well outside the default prefetchDistance=1
|
|
189
|
+
// window around the settled index 0.
|
|
190
|
+
expect(screen.queryByText('header-e')).toBeNull();
|
|
191
|
+
|
|
192
|
+
fireEvent.touchStart(scroller);
|
|
193
|
+
setLayout(scroller, 300, 1200); // scrollLeft 1200 / width 300 -> index 4
|
|
194
|
+
fireEvent.scroll(scroller);
|
|
195
|
+
// Well short of the 120ms settle debounce — onSettle must not have fired
|
|
196
|
+
// yet, but the rAF-throttled liveIndex update has had time to run.
|
|
197
|
+
act(() => void vi.advanceTimersByTime(20));
|
|
198
|
+
expect(onSettle).not.toHaveBeenCalled();
|
|
199
|
+
expect(screen.getByText('header-e')).toBeDefined();
|
|
200
|
+
|
|
201
|
+
act(() => void vi.advanceTimersByTime(120));
|
|
202
|
+
expect(onSettle).toHaveBeenCalledWith(4);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('does not re-snap when slide order is unchanged', () => {
|
|
206
|
+
function Reorderable({ onSettle }: { onSettle: (i: number) => void }) {
|
|
207
|
+
return <FiveSlides activeIndex={2} onSettle={onSettle} />;
|
|
208
|
+
}
|
|
209
|
+
const onSettle = vi.fn();
|
|
210
|
+
const { rerender } = render(<Reorderable onSettle={onSettle} />);
|
|
211
|
+
rerender(<Reorderable onSettle={onSettle} />);
|
|
212
|
+
expect(onSettle).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
});
|
package/src/components/SwipableMobileCarousel/__tests__/SwipableMobileCarouselDevGuard.test.tsx
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { cleanup, render } from '@testing-library/react';
|
|
4
|
+
import { SwipableMobileCarousel } from '../SwipableMobileCarousel';
|
|
5
|
+
import { SwipableMobileCarouselSlide } from '../SwipableMobileCarouselSlide';
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
if (!Element.prototype.scrollTo) {
|
|
9
|
+
Element.prototype.scrollTo = vi.fn();
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
cleanup();
|
|
15
|
+
vi.restoreAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
function renderWithStrayChild() {
|
|
19
|
+
return render(
|
|
20
|
+
<SwipableMobileCarousel activeIndex={0} onSettle={() => {}} aria-label="Test slides">
|
|
21
|
+
<SwipableMobileCarouselSlide slideKey="a" label="A">
|
|
22
|
+
content
|
|
23
|
+
</SwipableMobileCarouselSlide>
|
|
24
|
+
{/* stray non-Slide child — shifts index math for anything after it */}
|
|
25
|
+
<div>oops</div>
|
|
26
|
+
<SwipableMobileCarouselSlide slideKey="b" label="B">
|
|
27
|
+
content
|
|
28
|
+
</SwipableMobileCarouselSlide>
|
|
29
|
+
</SwipableMobileCarousel>,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('SwipableMobileCarousel dev-mode child-type guard', () => {
|
|
34
|
+
it('warns via console.error in development, mentioning the offending child', () => {
|
|
35
|
+
const originalEnv = process.env.NODE_ENV;
|
|
36
|
+
vi.stubEnv('NODE_ENV', 'development');
|
|
37
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
38
|
+
|
|
39
|
+
expect(() => renderWithStrayChild()).not.toThrow();
|
|
40
|
+
|
|
41
|
+
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('<div>'));
|
|
42
|
+
vi.stubEnv('NODE_ENV', originalEnv ?? 'test');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('does not call console.error in production', () => {
|
|
46
|
+
const originalEnv = process.env.NODE_ENV;
|
|
47
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
48
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
49
|
+
|
|
50
|
+
expect(() => renderWithStrayChild()).not.toThrow();
|
|
51
|
+
|
|
52
|
+
expect(errorSpy).not.toHaveBeenCalled();
|
|
53
|
+
vi.stubEnv('NODE_ENV', originalEnv ?? 'test');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('never throws, even with a stray child (a miscounted slide degrades, it does not crash)', () => {
|
|
57
|
+
vi.stubEnv('NODE_ENV', 'development');
|
|
58
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
59
|
+
expect(() => renderWithStrayChild()).not.toThrow();
|
|
60
|
+
});
|
|
61
|
+
});
|