@slipstream-ui/core 0.7.0 → 0.9.0
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/index.d.mts +317 -21
- package/dist/index.d.ts +317 -21
- package/dist/index.js +2004 -1069
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1998 -1068
- package/dist/index.mjs.map +1 -1
- package/dist/manifests/accordion.manifest.json +8 -2
- package/dist/manifests/alert.manifest.json +1 -1
- package/dist/manifests/announcement-bar.manifest.json +1 -1
- package/dist/manifests/app-shell.manifest.json +1 -1
- package/dist/manifests/applied-filters.manifest.json +1 -1
- package/dist/manifests/article-card.manifest.json +1 -1
- package/dist/manifests/auth-layout.manifest.json +19 -1
- package/dist/manifests/autocomplete.manifest.json +1 -1
- package/dist/manifests/avatar.manifest.json +1 -1
- package/dist/manifests/badge.manifest.json +13 -1
- package/dist/manifests/block.manifest.json +1 -1
- package/dist/manifests/bottom-nav.manifest.json +130 -0
- package/dist/manifests/breadcrumb.manifest.json +1 -1
- package/dist/manifests/button-group.manifest.json +1 -1
- package/dist/manifests/button.manifest.json +1 -1
- package/dist/manifests/calendar.manifest.json +175 -0
- package/dist/manifests/card.manifest.json +1 -1
- package/dist/manifests/checkbox.manifest.json +1 -1
- package/dist/manifests/comparison-table.manifest.json +1 -1
- package/dist/manifests/confirm-dialog.manifest.json +1 -1
- package/dist/manifests/contact-strip.manifest.json +1 -1
- package/dist/manifests/container.manifest.json +1 -1
- package/dist/manifests/cta-section.manifest.json +1 -1
- package/dist/manifests/data-table.manifest.json +8 -1
- package/dist/manifests/date-picker-popover.manifest.json +202 -0
- package/dist/manifests/date-picker.manifest.json +1 -1
- package/dist/manifests/date-range-picker.manifest.json +1 -1
- package/dist/manifests/deal-card.manifest.json +1 -1
- package/dist/manifests/dialog.manifest.json +1 -1
- package/dist/manifests/document-card.manifest.json +1 -1
- package/dist/manifests/drawer.manifest.json +48 -2
- package/dist/manifests/empty-state.manifest.json +1 -1
- package/dist/manifests/entity-card.manifest.json +1 -1
- package/dist/manifests/feature-grid.manifest.json +1 -1
- package/dist/manifests/feature-row.manifest.json +1 -1
- package/dist/manifests/fieldset.manifest.json +1 -1
- package/dist/manifests/file-upload.manifest.json +1 -1
- package/dist/manifests/filter-bar.manifest.json +1 -1
- package/dist/manifests/filter-chips.manifest.json +1 -1
- package/dist/manifests/filter-panel.manifest.json +1 -1
- package/dist/manifests/flex.manifest.json +1 -1
- package/dist/manifests/form-field.manifest.json +1 -1
- package/dist/manifests/form-layout.manifest.json +1 -1
- package/dist/manifests/form.manifest.json +1 -1
- package/dist/manifests/grid.manifest.json +1 -1
- package/dist/manifests/heading.manifest.json +1 -1
- package/dist/manifests/hero.manifest.json +1 -1
- package/dist/manifests/image.manifest.json +142 -0
- package/dist/manifests/input-group.manifest.json +1 -1
- package/dist/manifests/input.manifest.json +1 -1
- package/dist/manifests/label.manifest.json +1 -1
- package/dist/manifests/link-tabs.manifest.json +1 -1
- package/dist/manifests/link.manifest.json +1 -1
- package/dist/manifests/list.manifest.json +1 -1
- package/dist/manifests/load-more.manifest.json +1 -1
- package/dist/manifests/logo-cloud.manifest.json +1 -1
- package/dist/manifests/menu.manifest.json +1 -1
- package/dist/manifests/metric-card.manifest.json +36 -4
- package/dist/manifests/multi-select.manifest.json +1 -1
- package/dist/manifests/newsletter.manifest.json +1 -1
- package/dist/manifests/page-header.manifest.json +7 -1
- package/dist/manifests/pagination.manifest.json +1 -1
- package/dist/manifests/password-input.manifest.json +1 -1
- package/dist/manifests/popover.manifest.json +86 -4
- package/dist/manifests/process-steps.manifest.json +1 -1
- package/dist/manifests/progress.manifest.json +1 -1
- package/dist/manifests/prose.manifest.json +1 -1
- package/dist/manifests/radio.manifest.json +1 -1
- package/dist/manifests/range-slider.manifest.json +1 -1
- package/dist/manifests/search.manifest.json +1 -1
- package/dist/manifests/section-heading.manifest.json +1 -1
- package/dist/manifests/section.manifest.json +1 -1
- package/dist/manifests/segmented-control.manifest.json +1 -1
- package/dist/manifests/select-menu.manifest.json +1 -1
- package/dist/manifests/select.manifest.json +1 -1
- package/dist/manifests/separator.manifest.json +1 -1
- package/dist/manifests/site-footer.manifest.json +1 -1
- package/dist/manifests/site-header.manifest.json +13 -3
- package/dist/manifests/skeleton.manifest.json +1 -1
- package/dist/manifests/spinner.manifest.json +1 -1
- package/dist/manifests/stat-band.manifest.json +1 -1
- package/dist/manifests/stepper.manifest.json +7 -1
- package/dist/manifests/switch.manifest.json +1 -1
- package/dist/manifests/table-card.manifest.json +1 -1
- package/dist/manifests/table-of-contents.manifest.json +1 -1
- package/dist/manifests/table.manifest.json +1 -1
- package/dist/manifests/tabs.manifest.json +1 -1
- package/dist/manifests/testimonial.manifest.json +1 -1
- package/dist/manifests/text.manifest.json +1 -1
- package/dist/manifests/textarea.manifest.json +1 -1
- package/dist/manifests/theme-toggle.manifest.json +1 -1
- package/dist/manifests/time-picker.manifest.json +1 -1
- package/dist/manifests/timeline.manifest.json +1 -1
- package/dist/manifests/toast.manifest.json +1 -1
- package/dist/manifests/tombstone-card.manifest.json +1 -1
- package/dist/manifests/tooltip.manifest.json +1 -1
- package/dist/src/components/accordion/Accordion.d.ts +17 -1
- package/dist/src/components/accordion/Accordion.js +5 -1
- package/dist/src/components/accordion/Accordion.js.map +1 -1
- package/dist/src/components/accordion/Accordion.mjs +5 -1
- package/dist/src/components/accordion/Accordion.mjs.map +1 -1
- package/dist/src/components/alert/Alert.js +1 -1
- package/dist/src/components/alert/Alert.js.map +1 -1
- package/dist/src/components/alert/Alert.mjs +1 -1
- package/dist/src/components/alert/Alert.mjs.map +1 -1
- package/dist/src/components/app-shell/AppShell.js +1 -1
- package/dist/src/components/app-shell/AppShell.js.map +1 -1
- package/dist/src/components/app-shell/AppShell.mjs +1 -1
- package/dist/src/components/app-shell/AppShell.mjs.map +1 -1
- package/dist/src/components/article-card/ArticleCard.js +2 -2
- package/dist/src/components/article-card/ArticleCard.js.map +1 -1
- package/dist/src/components/article-card/ArticleCard.mjs +2 -2
- package/dist/src/components/article-card/ArticleCard.mjs.map +1 -1
- package/dist/src/components/auth-layout/AuthLayout.d.ts +14 -2
- package/dist/src/components/auth-layout/AuthLayout.js +10 -2
- package/dist/src/components/auth-layout/AuthLayout.js.map +1 -1
- package/dist/src/components/auth-layout/AuthLayout.mjs +10 -2
- package/dist/src/components/auth-layout/AuthLayout.mjs.map +1 -1
- package/dist/src/components/badge/Badge.d.ts +2 -0
- package/dist/src/components/badge/Badge.js +9 -3
- package/dist/src/components/badge/Badge.js.map +1 -1
- package/dist/src/components/badge/Badge.mjs +9 -3
- package/dist/src/components/badge/Badge.mjs.map +1 -1
- package/dist/src/components/bottom-nav/BottomNav.d.ts +36 -0
- package/dist/src/components/bottom-nav/BottomNav.js +90 -0
- package/dist/src/components/bottom-nav/BottomNav.js.map +1 -0
- package/dist/src/components/bottom-nav/BottomNav.mjs +66 -0
- package/dist/src/components/bottom-nav/BottomNav.mjs.map +1 -0
- package/dist/src/components/bottom-nav/index.d.ts +1 -0
- package/dist/src/components/bottom-nav/index.js +23 -0
- package/dist/src/components/bottom-nav/index.js.map +1 -0
- package/dist/src/components/bottom-nav/index.mjs +2 -0
- package/dist/src/components/bottom-nav/index.mjs.map +1 -0
- package/dist/src/components/calendar/Calendar.d.ts +34 -0
- package/dist/src/components/calendar/Calendar.js +233 -0
- package/dist/src/components/calendar/Calendar.js.map +1 -0
- package/dist/src/components/calendar/Calendar.mjs +208 -0
- package/dist/src/components/calendar/Calendar.mjs.map +1 -0
- package/dist/src/components/calendar/index.d.ts +1 -0
- package/dist/src/components/calendar/index.js +23 -0
- package/dist/src/components/calendar/index.js.map +1 -0
- package/dist/src/components/calendar/index.mjs +2 -0
- package/dist/src/components/calendar/index.mjs.map +1 -0
- package/dist/src/components/comparison-table/ComparisonTable.js +1 -1
- package/dist/src/components/comparison-table/ComparisonTable.js.map +1 -1
- package/dist/src/components/comparison-table/ComparisonTable.mjs +1 -1
- package/dist/src/components/comparison-table/ComparisonTable.mjs.map +1 -1
- package/dist/src/components/cta-section/CTASection.js +1 -1
- package/dist/src/components/cta-section/CTASection.js.map +1 -1
- package/dist/src/components/cta-section/CTASection.mjs +1 -1
- package/dist/src/components/cta-section/CTASection.mjs.map +1 -1
- package/dist/src/components/data-table/DataTable.d.ts +3 -1
- package/dist/src/components/data-table/DataTable.js +2 -4
- package/dist/src/components/data-table/DataTable.js.map +1 -1
- package/dist/src/components/data-table/DataTable.mjs +2 -4
- package/dist/src/components/data-table/DataTable.mjs.map +1 -1
- package/dist/src/components/date-picker-popover/DatePickerPopover.d.ts +33 -0
- package/dist/src/components/date-picker-popover/DatePickerPopover.js +117 -0
- package/dist/src/components/date-picker-popover/DatePickerPopover.js.map +1 -0
- package/dist/src/components/date-picker-popover/DatePickerPopover.mjs +83 -0
- package/dist/src/components/date-picker-popover/DatePickerPopover.mjs.map +1 -0
- package/dist/src/components/date-picker-popover/index.d.ts +1 -0
- package/dist/src/components/date-picker-popover/index.js +23 -0
- package/dist/src/components/date-picker-popover/index.js.map +1 -0
- package/dist/src/components/date-picker-popover/index.mjs +2 -0
- package/dist/src/components/date-picker-popover/index.mjs.map +1 -0
- package/dist/src/components/drawer/Drawer.d.ts +25 -2
- package/dist/src/components/drawer/Drawer.js +97 -6
- package/dist/src/components/drawer/Drawer.js.map +1 -1
- package/dist/src/components/drawer/Drawer.mjs +97 -6
- package/dist/src/components/drawer/Drawer.mjs.map +1 -1
- package/dist/src/components/empty-state/EmptyState.d.ts +13 -4
- package/dist/src/components/empty-state/EmptyState.js +13 -1
- package/dist/src/components/empty-state/EmptyState.js.map +1 -1
- package/dist/src/components/empty-state/EmptyState.mjs +13 -1
- package/dist/src/components/empty-state/EmptyState.mjs.map +1 -1
- package/dist/src/components/image/Image.d.ts +34 -0
- package/dist/src/components/image/Image.js +75 -0
- package/dist/src/components/image/Image.js.map +1 -0
- package/dist/src/components/image/Image.mjs +41 -0
- package/dist/src/components/image/Image.mjs.map +1 -0
- package/dist/src/components/image/index.d.ts +1 -0
- package/dist/src/components/image/index.js +29 -0
- package/dist/src/components/image/index.js.map +1 -0
- package/dist/src/components/image/index.mjs +5 -0
- package/dist/src/components/image/index.mjs.map +1 -0
- package/dist/src/components/link/Link.js +4 -4
- package/dist/src/components/link/Link.js.map +1 -1
- package/dist/src/components/link/Link.mjs +4 -4
- package/dist/src/components/link/Link.mjs.map +1 -1
- package/dist/src/components/metric-card/MetricCard.d.ts +23 -4
- package/dist/src/components/metric-card/MetricCard.js +32 -5
- package/dist/src/components/metric-card/MetricCard.js.map +1 -1
- package/dist/src/components/metric-card/MetricCard.mjs +32 -5
- package/dist/src/components/metric-card/MetricCard.mjs.map +1 -1
- package/dist/src/components/page-header/PageHeader.d.ts +4 -0
- package/dist/src/components/page-header/PageHeader.js +4 -3
- package/dist/src/components/page-header/PageHeader.js.map +1 -1
- package/dist/src/components/page-header/PageHeader.mjs +4 -3
- package/dist/src/components/page-header/PageHeader.mjs.map +1 -1
- package/dist/src/components/popover/Popover.d.ts +36 -3
- package/dist/src/components/popover/Popover.js +118 -22
- package/dist/src/components/popover/Popover.js.map +1 -1
- package/dist/src/components/popover/Popover.mjs +117 -22
- package/dist/src/components/popover/Popover.mjs.map +1 -1
- package/dist/src/components/section-heading/SectionHeading.js +1 -1
- package/dist/src/components/section-heading/SectionHeading.js.map +1 -1
- package/dist/src/components/section-heading/SectionHeading.mjs +1 -1
- package/dist/src/components/section-heading/SectionHeading.mjs.map +1 -1
- package/dist/src/components/separator/Separator.d.ts +3 -3
- package/dist/src/components/site-header/SiteHeader.d.ts +25 -3
- package/dist/src/components/site-header/SiteHeader.js +141 -6
- package/dist/src/components/site-header/SiteHeader.js.map +1 -1
- package/dist/src/components/site-header/SiteHeader.mjs +141 -6
- package/dist/src/components/site-header/SiteHeader.mjs.map +1 -1
- package/dist/src/components/stepper/Stepper.d.ts +18 -0
- package/dist/src/components/stepper/Stepper.js +2 -1
- package/dist/src/components/stepper/Stepper.js.map +1 -1
- package/dist/src/components/stepper/Stepper.mjs +2 -1
- package/dist/src/components/stepper/Stepper.mjs.map +1 -1
- package/dist/src/index.d.ts +4 -0
- package/dist/src/utils/date.d.ts +32 -0
- package/dist/src/utils/date.js +171 -0
- package/dist/src/utils/date.js.map +1 -0
- package/dist/src/utils/date.mjs +134 -0
- package/dist/src/utils/date.mjs.map +1 -0
- package/dist/src/utils/position.d.ts +42 -0
- package/dist/src/utils/position.js +91 -0
- package/dist/src/utils/position.js.map +1 -0
- package/dist/src/utils/position.mjs +67 -0
- package/dist/src/utils/position.mjs.map +1 -0
- package/dist/styles-full.css +1 -1
- package/dist/styles.css +1 -1
- package/package.json +3 -3
- package/src/components/accordion/Accordion.test.tsx +31 -0
- package/src/components/accordion/Accordion.tsx +27 -2
- package/src/components/alert/Alert.tsx +1 -1
- package/src/components/app-shell/AppShell.tsx +1 -1
- package/src/components/article-card/ArticleCard.tsx +2 -2
- package/src/components/auth-layout/AuthLayout.test.tsx +36 -0
- package/src/components/auth-layout/AuthLayout.tsx +20 -3
- package/src/components/badge/Badge.test.tsx +7 -0
- package/src/components/badge/Badge.tsx +8 -2
- package/src/components/bottom-nav/BottomNav.test.tsx +48 -0
- package/src/components/bottom-nav/BottomNav.tsx +117 -0
- package/src/components/bottom-nav/index.ts +1 -0
- package/src/components/calendar/Calendar.test.tsx +277 -0
- package/src/components/calendar/Calendar.tsx +273 -0
- package/src/components/calendar/index.ts +1 -0
- package/src/components/comparison-table/ComparisonTable.tsx +1 -1
- package/src/components/cta-section/CTASection.tsx +1 -1
- package/src/components/data-table/DataTable.test.tsx +21 -0
- package/src/components/data-table/DataTable.tsx +4 -1
- package/src/components/date-picker-popover/DatePickerPopover.test.tsx +223 -0
- package/src/components/date-picker-popover/DatePickerPopover.tsx +117 -0
- package/src/components/date-picker-popover/index.ts +1 -0
- package/src/components/drawer/Drawer.test.tsx +86 -1
- package/src/components/drawer/Drawer.tsx +135 -7
- package/src/components/empty-state/EmptyState.test.tsx +26 -1
- package/src/components/empty-state/EmptyState.tsx +40 -3
- package/src/components/image/Image.test.tsx +94 -0
- package/src/components/image/Image.tsx +71 -0
- package/src/components/image/index.ts +1 -0
- package/src/components/link/Link.test.tsx +2 -1
- package/src/components/link/Link.tsx +4 -4
- package/src/components/metric-card/MetricCard.test.tsx +32 -0
- package/src/components/metric-card/MetricCard.tsx +45 -7
- package/src/components/page-header/PageHeader.test.tsx +13 -0
- package/src/components/page-header/PageHeader.tsx +6 -3
- package/src/components/popover/Popover.test.tsx +307 -3
- package/src/components/popover/Popover.tsx +228 -91
- package/src/components/section-heading/SectionHeading.tsx +1 -1
- package/src/components/site-header/SiteHeader.test.tsx +103 -1
- package/src/components/site-header/SiteHeader.tsx +220 -16
- package/src/components/stepper/Stepper.test.tsx +12 -0
- package/src/components/stepper/Stepper.tsx +15 -1
- package/src/index.ts +4 -0
- package/src/manifests/accordion.manifest.json +8 -2
- package/src/manifests/alert.manifest.json +1 -1
- package/src/manifests/announcement-bar.manifest.json +1 -1
- package/src/manifests/app-shell.manifest.json +1 -1
- package/src/manifests/applied-filters.manifest.json +1 -1
- package/src/manifests/article-card.manifest.json +1 -1
- package/src/manifests/auth-layout.manifest.json +19 -1
- package/src/manifests/autocomplete.manifest.json +1 -1
- package/src/manifests/avatar.manifest.json +1 -1
- package/src/manifests/badge.manifest.json +13 -1
- package/src/manifests/block.manifest.json +1 -1
- package/src/manifests/bottom-nav.manifest.json +130 -0
- package/src/manifests/breadcrumb.manifest.json +1 -1
- package/src/manifests/button-group.manifest.json +1 -1
- package/src/manifests/button.manifest.json +1 -1
- package/src/manifests/calendar.manifest.json +175 -0
- package/src/manifests/card.manifest.json +1 -1
- package/src/manifests/checkbox.manifest.json +1 -1
- package/src/manifests/comparison-table.manifest.json +1 -1
- package/src/manifests/confirm-dialog.manifest.json +1 -1
- package/src/manifests/contact-strip.manifest.json +1 -1
- package/src/manifests/container.manifest.json +1 -1
- package/src/manifests/cta-section.manifest.json +1 -1
- package/src/manifests/data-table.manifest.json +8 -1
- package/src/manifests/date-picker-popover.manifest.json +202 -0
- package/src/manifests/date-picker.manifest.json +1 -1
- package/src/manifests/date-range-picker.manifest.json +1 -1
- package/src/manifests/deal-card.manifest.json +1 -1
- package/src/manifests/dialog.manifest.json +1 -1
- package/src/manifests/document-card.manifest.json +1 -1
- package/src/manifests/drawer.manifest.json +48 -2
- package/src/manifests/empty-state.manifest.json +1 -1
- package/src/manifests/entity-card.manifest.json +1 -1
- package/src/manifests/feature-grid.manifest.json +1 -1
- package/src/manifests/feature-row.manifest.json +1 -1
- package/src/manifests/fieldset.manifest.json +1 -1
- package/src/manifests/file-upload.manifest.json +1 -1
- package/src/manifests/filter-bar.manifest.json +1 -1
- package/src/manifests/filter-chips.manifest.json +1 -1
- package/src/manifests/filter-panel.manifest.json +1 -1
- package/src/manifests/flex.manifest.json +1 -1
- package/src/manifests/form-field.manifest.json +1 -1
- package/src/manifests/form-layout.manifest.json +1 -1
- package/src/manifests/form.manifest.json +1 -1
- package/src/manifests/grid.manifest.json +1 -1
- package/src/manifests/heading.manifest.json +1 -1
- package/src/manifests/hero.manifest.json +1 -1
- package/src/manifests/image.manifest.json +142 -0
- package/src/manifests/input-group.manifest.json +1 -1
- package/src/manifests/input.manifest.json +1 -1
- package/src/manifests/label.manifest.json +1 -1
- package/src/manifests/link-tabs.manifest.json +1 -1
- package/src/manifests/link.manifest.json +1 -1
- package/src/manifests/list.manifest.json +1 -1
- package/src/manifests/load-more.manifest.json +1 -1
- package/src/manifests/logo-cloud.manifest.json +1 -1
- package/src/manifests/menu.manifest.json +1 -1
- package/src/manifests/metric-card.manifest.json +36 -4
- package/src/manifests/multi-select.manifest.json +1 -1
- package/src/manifests/newsletter.manifest.json +1 -1
- package/src/manifests/page-header.manifest.json +7 -1
- package/src/manifests/pagination.manifest.json +1 -1
- package/src/manifests/password-input.manifest.json +1 -1
- package/src/manifests/popover.manifest.json +86 -4
- package/src/manifests/process-steps.manifest.json +1 -1
- package/src/manifests/progress.manifest.json +1 -1
- package/src/manifests/prose.manifest.json +1 -1
- package/src/manifests/radio.manifest.json +1 -1
- package/src/manifests/range-slider.manifest.json +1 -1
- package/src/manifests/search.manifest.json +1 -1
- package/src/manifests/section-heading.manifest.json +1 -1
- package/src/manifests/section.manifest.json +1 -1
- package/src/manifests/segmented-control.manifest.json +1 -1
- package/src/manifests/select-menu.manifest.json +1 -1
- package/src/manifests/select.manifest.json +1 -1
- package/src/manifests/separator.manifest.json +1 -1
- package/src/manifests/site-footer.manifest.json +1 -1
- package/src/manifests/site-header.manifest.json +13 -3
- package/src/manifests/skeleton.manifest.json +1 -1
- package/src/manifests/spinner.manifest.json +1 -1
- package/src/manifests/stat-band.manifest.json +1 -1
- package/src/manifests/stepper.manifest.json +7 -1
- package/src/manifests/switch.manifest.json +1 -1
- package/src/manifests/table-card.manifest.json +1 -1
- package/src/manifests/table-of-contents.manifest.json +1 -1
- package/src/manifests/table.manifest.json +1 -1
- package/src/manifests/tabs.manifest.json +1 -1
- package/src/manifests/testimonial.manifest.json +1 -1
- package/src/manifests/text.manifest.json +1 -1
- package/src/manifests/textarea.manifest.json +1 -1
- package/src/manifests/theme-toggle.manifest.json +1 -1
- package/src/manifests/time-picker.manifest.json +1 -1
- package/src/manifests/timeline.manifest.json +1 -1
- package/src/manifests/toast.manifest.json +1 -1
- package/src/manifests/tombstone-card.manifest.json +1 -1
- package/src/manifests/tooltip.manifest.json +1 -1
- package/src/utils/date.test.ts +190 -0
- package/src/utils/date.ts +162 -0
- package/src/utils/position.test.ts +227 -0
- package/src/utils/position.ts +129 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
addDays,
|
|
4
|
+
addMonths,
|
|
5
|
+
calendarWeeks,
|
|
6
|
+
dateKeyOf,
|
|
7
|
+
daysInMonth,
|
|
8
|
+
formatDateLabel,
|
|
9
|
+
isValidDateKey,
|
|
10
|
+
monthLabel,
|
|
11
|
+
moveDateKey,
|
|
12
|
+
parseDateKey,
|
|
13
|
+
startOfWeek,
|
|
14
|
+
todayKey,
|
|
15
|
+
weekdayLabels,
|
|
16
|
+
} from './date';
|
|
17
|
+
|
|
18
|
+
describe('date keys', () => {
|
|
19
|
+
it('builds and parses a key', () => {
|
|
20
|
+
expect(dateKeyOf(2026, 10, 13)).toBe('2026-11-13');
|
|
21
|
+
expect(parseDateKey('2026-11-13')).toEqual({ year: 2026, month: 10, day: 13 });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('rejects impossible dates', () => {
|
|
25
|
+
expect(isValidDateKey('2026-13-01')).toBe(false);
|
|
26
|
+
expect(isValidDateKey('2026-02-30')).toBe(false);
|
|
27
|
+
expect(isValidDateKey('2026-1-1')).toBe(false);
|
|
28
|
+
expect(isValidDateKey('not-a-date')).toBe(false);
|
|
29
|
+
expect(parseDateKey('2026-02-30')).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('accepts a leap day only in a leap year', () => {
|
|
33
|
+
expect(isValidDateKey('2028-02-29')).toBe(true);
|
|
34
|
+
expect(isValidDateKey('2026-02-29')).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('round-trips today', () => {
|
|
38
|
+
const now = new Date(2026, 10, 13);
|
|
39
|
+
expect(todayKey(now)).toBe('2026-11-13');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('arithmetic', () => {
|
|
44
|
+
it('adds days across month and year boundaries', () => {
|
|
45
|
+
expect(addDays('2026-11-30', 1)).toBe('2026-12-01');
|
|
46
|
+
expect(addDays('2026-12-31', 1)).toBe('2027-01-01');
|
|
47
|
+
expect(addDays('2026-03-01', -1)).toBe('2026-02-28');
|
|
48
|
+
expect(addDays('2028-03-01', -1)).toBe('2028-02-29');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('clamps the day when adding months', () => {
|
|
52
|
+
expect(addMonths('2026-01-31', 1)).toBe('2026-02-28');
|
|
53
|
+
expect(addMonths('2028-01-31', 1)).toBe('2028-02-29');
|
|
54
|
+
expect(addMonths('2026-03-31', -1)).toBe('2026-02-28');
|
|
55
|
+
expect(addMonths('2026-12-15', 1)).toBe('2027-01-15');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('weeks', () => {
|
|
60
|
+
it('starts on Sunday or Monday', () => {
|
|
61
|
+
expect(startOfWeek('2026-11-13', 0)).toBe('2026-11-08');
|
|
62
|
+
expect(startOfWeek('2026-11-13', 1)).toBe('2026-11-09');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns six weeks of seven days with neighbour flags', () => {
|
|
66
|
+
const weeks = calendarWeeks(2026, 10, 0);
|
|
67
|
+
expect(weeks).toHaveLength(6);
|
|
68
|
+
expect(weeks.every((week) => week.length === 7)).toBe(true);
|
|
69
|
+
expect(weeks[0]?.[0]?.key).toBe('2026-11-01');
|
|
70
|
+
expect(weeks[0]?.[0]?.inMonth).toBe(true);
|
|
71
|
+
expect(weeks.flat().filter((day) => day.inMonth)).toHaveLength(30);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('marks leading days from the previous month', () => {
|
|
75
|
+
const weeks = calendarWeeks(2026, 10, 1);
|
|
76
|
+
expect(weeks[0]?.[0]?.key).toBe('2026-10-26');
|
|
77
|
+
expect(weeks[0]?.[0]?.inMonth).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('moveDateKey', () => {
|
|
82
|
+
it('moves by day and by week', () => {
|
|
83
|
+
expect(moveDateKey('2026-11-13', 'left')).toBe('2026-11-12');
|
|
84
|
+
expect(moveDateKey('2026-11-13', 'right')).toBe('2026-11-14');
|
|
85
|
+
expect(moveDateKey('2026-11-13', 'up')).toBe('2026-11-06');
|
|
86
|
+
expect(moveDateKey('2026-11-13', 'down')).toBe('2026-11-20');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('moves to week bounds for either week start', () => {
|
|
90
|
+
expect(moveDateKey('2026-11-13', 'home', 0)).toBe('2026-11-08');
|
|
91
|
+
expect(moveDateKey('2026-11-13', 'end', 0)).toBe('2026-11-14');
|
|
92
|
+
expect(moveDateKey('2026-11-13', 'home', 1)).toBe('2026-11-09');
|
|
93
|
+
expect(moveDateKey('2026-11-13', 'end', 1)).toBe('2026-11-15');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('pages months with clamping', () => {
|
|
97
|
+
expect(moveDateKey('2026-01-31', 'pagedown')).toBe('2026-02-28');
|
|
98
|
+
expect(moveDateKey('2026-03-31', 'pageup')).toBe('2026-02-28');
|
|
99
|
+
expect(moveDateKey('2026-01-15', 'pageup')).toBe('2025-12-15');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('labels', () => {
|
|
104
|
+
it('formats month and weekday labels', () => {
|
|
105
|
+
expect(monthLabel(2026, 10, 'en-US')).toBe('November 2026');
|
|
106
|
+
expect(weekdayLabels('en-US', 0)).toHaveLength(7);
|
|
107
|
+
expect(weekdayLabels('en-US', 0)[0]).toBe('Sun');
|
|
108
|
+
expect(weekdayLabels('es-ES', 1)[0]).not.toBe('Sun');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('formats a full date for screen readers', () => {
|
|
112
|
+
expect(formatDateLabel('2026-11-13', 'en-US')).toContain('2026');
|
|
113
|
+
expect(formatDateLabel('bad', 'en-US')).toBe('bad');
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('daysInMonth', () => {
|
|
118
|
+
it('knows month lengths including leap years', () => {
|
|
119
|
+
expect(daysInMonth(2026, 1)).toBe(28);
|
|
120
|
+
expect(daysInMonth(2028, 1)).toBe(29);
|
|
121
|
+
expect(daysInMonth(2026, 7)).toBe(31);
|
|
122
|
+
expect(daysInMonth(2026, 11)).toBe(31);
|
|
123
|
+
expect(daysInMonth(2026, 3)).toBe(30);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('bad input', () => {
|
|
128
|
+
it('rejects impossible month and day numbers', () => {
|
|
129
|
+
expect(parseDateKey('2026-00-10')).toBeNull();
|
|
130
|
+
expect(parseDateKey('2026-13-01')).toBeNull();
|
|
131
|
+
expect(parseDateKey('2026-11-00')).toBeNull();
|
|
132
|
+
expect(parseDateKey('2026-11-32')).toBeNull();
|
|
133
|
+
expect(isValidDateKey('2026-00-10')).toBe(false);
|
|
134
|
+
expect(isValidDateKey('2026-11-00')).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('rejects malformed shapes', () => {
|
|
138
|
+
expect(parseDateKey('')).toBeNull();
|
|
139
|
+
expect(parseDateKey('2026-11-13T00:00')).toBeNull();
|
|
140
|
+
expect(parseDateKey(' 2026-11-13')).toBeNull();
|
|
141
|
+
expect(parseDateKey('2026-11-13 ')).toBeNull();
|
|
142
|
+
expect(parseDateKey('20261113')).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('returns the input unchanged instead of inventing a date', () => {
|
|
146
|
+
expect(addDays('nope', 1)).toBe('nope');
|
|
147
|
+
expect(addDays('2026-02-30', 1)).toBe('2026-02-30');
|
|
148
|
+
expect(addMonths('nope', 1)).toBe('nope');
|
|
149
|
+
expect(startOfWeek('nope')).toBe('nope');
|
|
150
|
+
expect(moveDateKey('nope', 'down')).toBe('nope');
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('defaults and week grids', () => {
|
|
155
|
+
it('defaults to a Sunday week start', () => {
|
|
156
|
+
expect(startOfWeek('2026-11-13')).toBe('2026-11-08');
|
|
157
|
+
expect(moveDateKey('2026-11-13', 'home')).toBe('2026-11-08');
|
|
158
|
+
expect(weekdayLabels()[0]).toBe('Sun');
|
|
159
|
+
expect(monthLabel(2026, 10)).toBe('November 2026');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('produces 42 consecutive days for every month', () => {
|
|
163
|
+
for (const [year, month] of [
|
|
164
|
+
[2026, 0],
|
|
165
|
+
[2026, 7],
|
|
166
|
+
[2028, 1],
|
|
167
|
+
[2026, 11],
|
|
168
|
+
] as const) {
|
|
169
|
+
const weeks = calendarWeeks(year, month, 0);
|
|
170
|
+
const cells = weeks.flat();
|
|
171
|
+
expect(cells).toHaveLength(42);
|
|
172
|
+
for (let index = 1; index < cells.length; index += 1) {
|
|
173
|
+
expect(cells[index]?.key).toBe(addDays(cells[index - 1]?.key ?? '', 1));
|
|
174
|
+
}
|
|
175
|
+
expect(cells.filter((day) => day.inMonth)).toHaveLength(daysInMonth(year, month));
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('includes trailing days from the next month', () => {
|
|
180
|
+
const weeks = calendarWeeks(2026, 7, 0);
|
|
181
|
+
const last = weeks[5]?.[6];
|
|
182
|
+
expect(last?.inMonth).toBe(false);
|
|
183
|
+
expect(last?.key.startsWith('2026-09')).toBe(true);
|
|
184
|
+
expect(last?.day).toBe(Number(last?.key.slice(-2)));
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('round-trips today without an argument', () => {
|
|
188
|
+
expect(todayKey()).toBe(todayKey(new Date()));
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
export type DateParts = { year: number; month: number; day: number };
|
|
2
|
+
|
|
3
|
+
export type CalendarMove =
|
|
4
|
+
| 'left'
|
|
5
|
+
| 'right'
|
|
6
|
+
| 'up'
|
|
7
|
+
| 'down'
|
|
8
|
+
| 'home'
|
|
9
|
+
| 'end'
|
|
10
|
+
| 'pageup'
|
|
11
|
+
| 'pagedown';
|
|
12
|
+
|
|
13
|
+
const DAY_MS = 86_400_000;
|
|
14
|
+
|
|
15
|
+
function pad(value: number): string {
|
|
16
|
+
return String(value).padStart(2, '0');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Builds an ISO date key from zero-based month. */
|
|
20
|
+
export function dateKeyOf(year: number, month: number, day: number): string {
|
|
21
|
+
return `${year}-${pad(month + 1)}-${pad(day)}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** True for a real calendar day in `YYYY-MM-DD` form. */
|
|
25
|
+
export function isValidDateKey(value: string): boolean {
|
|
26
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
|
27
|
+
const parts = parseDateKey(value);
|
|
28
|
+
if (!parts) return false;
|
|
29
|
+
return dateKeyOf(parts.year, parts.month, parts.day) === value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Parses a key, or returns null when it is not a real date. */
|
|
33
|
+
export function parseDateKey(value: string): DateParts | null {
|
|
34
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
35
|
+
if (!match) return null;
|
|
36
|
+
const year = Number(match[1]);
|
|
37
|
+
const month = Number(match[2]) - 1;
|
|
38
|
+
const day = Number(match[3]);
|
|
39
|
+
if (month < 0 || month > 11 || day < 1 || day > daysInMonth(year, month)) return null;
|
|
40
|
+
return { year, month, day };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function daysInMonth(year: number, month: number): number {
|
|
44
|
+
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function utc(key: string): Date {
|
|
48
|
+
const parts = parseDateKey(key);
|
|
49
|
+
if (!parts) return new Date(Date.UTC(1970, 0, 1));
|
|
50
|
+
return new Date(Date.UTC(parts.year, parts.month, parts.day));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Local-calendar key for a real-world Date (today, a picked day). */
|
|
54
|
+
export function toDateKey(date: Date): string {
|
|
55
|
+
return dateKeyOf(date.getFullYear(), date.getMonth(), date.getDate());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Key for a Date built with Date.UTC, so arithmetic never drifts by zone. */
|
|
59
|
+
function utcKey(date: Date): string {
|
|
60
|
+
return dateKeyOf(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function todayKey(now = new Date()): string {
|
|
64
|
+
return toDateKey(now);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function addDays(key: string, delta: number): string {
|
|
68
|
+
if (!parseDateKey(key)) return key;
|
|
69
|
+
const date = utc(key);
|
|
70
|
+
return utcKey(new Date(date.getTime() + delta * DAY_MS));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Month move that clamps the day instead of rolling into the next month. */
|
|
74
|
+
export function addMonths(key: string, delta: number): string {
|
|
75
|
+
const parts = parseDateKey(key);
|
|
76
|
+
if (!parts) return key;
|
|
77
|
+
const index = parts.year * 12 + parts.month + delta;
|
|
78
|
+
const year = Math.floor(index / 12);
|
|
79
|
+
const month = index % 12;
|
|
80
|
+
return dateKeyOf(year, month, Math.min(parts.day, daysInMonth(year, month)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function startOfWeek(key: string, weekStartsOn: 0 | 1 = 0): string {
|
|
84
|
+
if (!parseDateKey(key)) return key;
|
|
85
|
+
const date = utc(key);
|
|
86
|
+
const offset = (date.getUTCDay() - weekStartsOn + 7) % 7;
|
|
87
|
+
return addDays(key, -offset);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Next date key for a keyboard move. */
|
|
91
|
+
export function moveDateKey(key: string, move: CalendarMove, weekStartsOn: 0 | 1 = 0): string {
|
|
92
|
+
switch (move) {
|
|
93
|
+
case 'left':
|
|
94
|
+
return addDays(key, -1);
|
|
95
|
+
case 'right':
|
|
96
|
+
return addDays(key, 1);
|
|
97
|
+
case 'up':
|
|
98
|
+
return addDays(key, -7);
|
|
99
|
+
case 'down':
|
|
100
|
+
return addDays(key, 7);
|
|
101
|
+
case 'home':
|
|
102
|
+
return startOfWeek(key, weekStartsOn);
|
|
103
|
+
case 'end':
|
|
104
|
+
return addDays(startOfWeek(key, weekStartsOn), 6);
|
|
105
|
+
case 'pageup':
|
|
106
|
+
return addMonths(key, -1);
|
|
107
|
+
case 'pagedown':
|
|
108
|
+
return addMonths(key, 1);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type CalendarDay = {
|
|
113
|
+
key: string;
|
|
114
|
+
day: number;
|
|
115
|
+
inMonth: boolean;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** Six stable weeks of cells, including leading/trailing neighbour days. */
|
|
119
|
+
export function calendarWeeks(
|
|
120
|
+
year: number,
|
|
121
|
+
month: number,
|
|
122
|
+
weekStartsOn: 0 | 1 = 0
|
|
123
|
+
): CalendarDay[][] {
|
|
124
|
+
const first = dateKeyOf(year, month, 1);
|
|
125
|
+
const start = startOfWeek(first, weekStartsOn);
|
|
126
|
+
return Array.from({ length: 6 }, (_, week) =>
|
|
127
|
+
Array.from({ length: 7 }, (_, day) => {
|
|
128
|
+
const key = addDays(start, week * 7 + day);
|
|
129
|
+
const parts = parseDateKey(key);
|
|
130
|
+
return {
|
|
131
|
+
key,
|
|
132
|
+
day: parts?.day ?? 1,
|
|
133
|
+
inMonth: parts?.month === month && parts?.year === year,
|
|
134
|
+
};
|
|
135
|
+
})
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function monthLabel(year: number, month: number, locale = 'en-US'): string {
|
|
140
|
+
return new Intl.DateTimeFormat(locale, {
|
|
141
|
+
month: 'long',
|
|
142
|
+
year: 'numeric',
|
|
143
|
+
timeZone: 'UTC',
|
|
144
|
+
}).format(new Date(Date.UTC(year, month, 1)));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function weekdayLabels(locale = 'en-US', weekStartsOn: 0 | 1 = 0): string[] {
|
|
148
|
+
return Array.from({ length: 7 }, (_, index) =>
|
|
149
|
+
new Intl.DateTimeFormat(locale, { weekday: 'short', timeZone: 'UTC' }).format(
|
|
150
|
+
new Date(Date.UTC(2024, 0, 7 + weekStartsOn + index))
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function formatDateLabel(key: string, locale = 'en-US'): string {
|
|
156
|
+
const parts = parseDateKey(key);
|
|
157
|
+
if (!parts) return key;
|
|
158
|
+
return new Intl.DateTimeFormat(locale, {
|
|
159
|
+
dateStyle: 'medium',
|
|
160
|
+
timeZone: 'UTC',
|
|
161
|
+
}).format(new Date(Date.UTC(parts.year, parts.month, parts.day)));
|
|
162
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { computePosition, type Rect } from './position';
|
|
3
|
+
|
|
4
|
+
const anchor: Rect = { top: 200, left: 100, width: 120, height: 36 };
|
|
5
|
+
const content = { width: 288, height: 180 };
|
|
6
|
+
const viewport = { width: 1024, height: 768 };
|
|
7
|
+
|
|
8
|
+
describe('computePosition', () => {
|
|
9
|
+
it('places the panel below the anchor by default', () => {
|
|
10
|
+
const result = computePosition({ anchor, content, viewport });
|
|
11
|
+
expect(result).toEqual({ top: 244, left: 100, side: 'bottom' });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('honours sideOffset and alignOffset', () => {
|
|
15
|
+
const result = computePosition({
|
|
16
|
+
anchor,
|
|
17
|
+
content,
|
|
18
|
+
viewport,
|
|
19
|
+
sideOffset: 12,
|
|
20
|
+
alignOffset: -6,
|
|
21
|
+
});
|
|
22
|
+
expect(result.top).toBe(248);
|
|
23
|
+
expect(result.left).toBe(94);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('supports every side', () => {
|
|
27
|
+
expect(computePosition({ anchor, content, viewport, side: 'top' })).toMatchObject({
|
|
28
|
+
top: 12,
|
|
29
|
+
side: 'top',
|
|
30
|
+
});
|
|
31
|
+
expect(computePosition({ anchor, content, viewport, side: 'right' })).toMatchObject({
|
|
32
|
+
left: 228,
|
|
33
|
+
side: 'right',
|
|
34
|
+
});
|
|
35
|
+
// Room on both sides, so the preferred side survives.
|
|
36
|
+
expect(
|
|
37
|
+
computePosition({ anchor: { ...anchor, left: 600 }, content, viewport, side: 'left' })
|
|
38
|
+
).toMatchObject({ left: 304, side: 'left' });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('aligns start, center and end on the cross axis', () => {
|
|
42
|
+
expect(computePosition({ anchor, content, viewport, align: 'start' }).left).toBe(100);
|
|
43
|
+
expect(computePosition({ anchor, content, viewport, align: 'center' }).left).toBe(16);
|
|
44
|
+
expect(computePosition({ anchor, content, viewport, align: 'end' }).left).toBe(8);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('flips above when there is no room below', () => {
|
|
48
|
+
const low = { ...anchor, top: 700 };
|
|
49
|
+
const result = computePosition({ anchor: low, content, viewport });
|
|
50
|
+
expect(result.side).toBe('top');
|
|
51
|
+
expect(result.top).toBe(700 - 180 - 8);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('flips left when there is no room on the right', () => {
|
|
55
|
+
const wide = { ...anchor, left: 900 };
|
|
56
|
+
const result = computePosition({ anchor: wide, content, viewport, side: 'right' });
|
|
57
|
+
expect(result.side).toBe('left');
|
|
58
|
+
expect(result.left).toBe(900 - 288 - 8);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('keeps the preferred side when neither side fits', () => {
|
|
62
|
+
const result = computePosition({
|
|
63
|
+
anchor: { top: 40, left: 100, width: 20, height: 20 },
|
|
64
|
+
content,
|
|
65
|
+
viewport: { width: 300, height: 100 },
|
|
66
|
+
});
|
|
67
|
+
expect(result.side).toBe('bottom');
|
|
68
|
+
expect(result.top).toBe(8);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('clamps to collision padding on both axes', () => {
|
|
72
|
+
const corner = { top: 760, left: 1000, width: 20, height: 20 };
|
|
73
|
+
const result = computePosition({ anchor: corner, content, viewport, collisionPadding: 16 });
|
|
74
|
+
expect(result.left).toBe(1024 - 288 - 16);
|
|
75
|
+
expect(result.top).toBeGreaterThanOrEqual(16);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('honours a larger collision padding on the cross axis', () => {
|
|
79
|
+
const result = computePosition({
|
|
80
|
+
anchor,
|
|
81
|
+
content: { width: 1000, height: 180 },
|
|
82
|
+
viewport,
|
|
83
|
+
collisionPadding: 24,
|
|
84
|
+
});
|
|
85
|
+
expect(result.left).toBe(24);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('never returns a negative offset when the panel is wider than the viewport', () => {
|
|
89
|
+
const result = computePosition({
|
|
90
|
+
anchor,
|
|
91
|
+
content: { width: 1200, height: 180 },
|
|
92
|
+
viewport,
|
|
93
|
+
});
|
|
94
|
+
expect(result.left).toBe(8);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('centres a panel that is taller than the viewport', () => {
|
|
98
|
+
const result = computePosition({
|
|
99
|
+
anchor,
|
|
100
|
+
content: { width: 200, height: 900 },
|
|
101
|
+
viewport,
|
|
102
|
+
});
|
|
103
|
+
expect(result.top).toBe(8);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('computePosition edge cases', () => {
|
|
108
|
+
it('flips from top to bottom when there is no room above', () => {
|
|
109
|
+
const result = computePosition({
|
|
110
|
+
anchor: { ...anchor, top: 20 },
|
|
111
|
+
content,
|
|
112
|
+
viewport,
|
|
113
|
+
side: 'top',
|
|
114
|
+
});
|
|
115
|
+
expect(result.side).toBe('bottom');
|
|
116
|
+
expect(result).toEqual({ top: 64, left: 100, side: 'bottom' });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('flips from left to right when there is no room on the left', () => {
|
|
120
|
+
const result = computePosition({
|
|
121
|
+
anchor: { ...anchor, left: 20 },
|
|
122
|
+
content,
|
|
123
|
+
viewport,
|
|
124
|
+
side: 'left',
|
|
125
|
+
});
|
|
126
|
+
expect(result).toEqual({ top: 200, left: 148, side: 'right' });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('does not flip when the preferred side fits exactly', () => {
|
|
130
|
+
// spaceFor('bottom') === content.height, and the flip test is strict.
|
|
131
|
+
const result = computePosition({
|
|
132
|
+
anchor: { ...anchor, top: 544 },
|
|
133
|
+
content,
|
|
134
|
+
viewport,
|
|
135
|
+
});
|
|
136
|
+
expect(result.side).toBe('bottom');
|
|
137
|
+
// Still clamped to viewport - content - collisionPadding.
|
|
138
|
+
expect(result.top).toBe(768 - 180 - 8);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('aligns end and center without clamping when there is room', () => {
|
|
142
|
+
const wideAnchor = { ...anchor, left: 500 };
|
|
143
|
+
expect(computePosition({ anchor: wideAnchor, content, viewport, align: 'end' }).left).toBe(332);
|
|
144
|
+
expect(computePosition({ anchor: wideAnchor, content, viewport, align: 'center' }).left).toBe(
|
|
145
|
+
416
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('clamps the cross axis against the viewport height for horizontal sides', () => {
|
|
150
|
+
const result = computePosition({
|
|
151
|
+
anchor: { top: 700, left: 300, width: 120, height: 36 },
|
|
152
|
+
content: { width: 200, height: 600 },
|
|
153
|
+
viewport,
|
|
154
|
+
side: 'right',
|
|
155
|
+
});
|
|
156
|
+
expect(result.top).toBe(160);
|
|
157
|
+
expect(result.left).toBe(428);
|
|
158
|
+
expect(result.side).toBe('right');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('clamps the main axis when a horizontal panel is wider than the viewport', () => {
|
|
162
|
+
const result = computePosition({
|
|
163
|
+
anchor: { ...anchor, left: 500 },
|
|
164
|
+
content: { width: 1200, height: 100 },
|
|
165
|
+
viewport,
|
|
166
|
+
side: 'right',
|
|
167
|
+
});
|
|
168
|
+
expect(result.left).toBe(8);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('clamps a negative alignOffset back inside the padding', () => {
|
|
172
|
+
const result = computePosition({
|
|
173
|
+
anchor: { top: 200, left: 0, width: 120, height: 36 },
|
|
174
|
+
content,
|
|
175
|
+
viewport,
|
|
176
|
+
alignOffset: -40,
|
|
177
|
+
});
|
|
178
|
+
expect(result.left).toBe(8);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('clamps an alignOffset that pushes past the right edge', () => {
|
|
182
|
+
const result = computePosition({
|
|
183
|
+
anchor: { top: 200, left: 900, width: 120, height: 36 },
|
|
184
|
+
content,
|
|
185
|
+
viewport,
|
|
186
|
+
alignOffset: 100,
|
|
187
|
+
});
|
|
188
|
+
expect(result.left).toBe(1024 - 288 - 8);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('handles a zero-size anchor at the origin', () => {
|
|
192
|
+
expect(
|
|
193
|
+
computePosition({
|
|
194
|
+
anchor: { top: 0, left: 0, width: 0, height: 0 },
|
|
195
|
+
content,
|
|
196
|
+
viewport,
|
|
197
|
+
})
|
|
198
|
+
).toEqual({ top: 8, left: 8, side: 'bottom' });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('treats explicit defaults the same as omitted options', () => {
|
|
202
|
+
expect(
|
|
203
|
+
computePosition({
|
|
204
|
+
anchor,
|
|
205
|
+
content,
|
|
206
|
+
viewport,
|
|
207
|
+
side: 'bottom',
|
|
208
|
+
align: 'start',
|
|
209
|
+
sideOffset: 8,
|
|
210
|
+
alignOffset: 0,
|
|
211
|
+
collisionPadding: 8,
|
|
212
|
+
})
|
|
213
|
+
).toEqual(computePosition({ anchor, content, viewport }));
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('flips a top panel down and keeps it inside the viewport', () => {
|
|
217
|
+
const result = computePosition({
|
|
218
|
+
anchor: { top: 8, left: 24, width: 160, height: 40 },
|
|
219
|
+
content: { width: 288, height: 400 },
|
|
220
|
+
viewport,
|
|
221
|
+
side: 'top',
|
|
222
|
+
});
|
|
223
|
+
expect(result.side).toBe('bottom');
|
|
224
|
+
expect(result.top).toBeGreaterThanOrEqual(8);
|
|
225
|
+
expect(result.top + 400).toBeLessThanOrEqual(768 - 8);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export type Side = 'top' | 'right' | 'bottom' | 'left';
|
|
2
|
+
export type Align = 'start' | 'center' | 'end';
|
|
3
|
+
|
|
4
|
+
export type Rect = {
|
|
5
|
+
top: number;
|
|
6
|
+
left: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type Size = {
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type PositionOptions = {
|
|
17
|
+
/** The element the panel is anchored to, in viewport coordinates. */
|
|
18
|
+
anchor: Rect;
|
|
19
|
+
/** Measured panel size. */
|
|
20
|
+
content: Size;
|
|
21
|
+
/** Viewport size (normally window.innerWidth/innerHeight). */
|
|
22
|
+
viewport: Size;
|
|
23
|
+
side?: Side;
|
|
24
|
+
align?: Align;
|
|
25
|
+
/** Distance between the anchor and the panel, along the side axis. */
|
|
26
|
+
sideOffset?: number;
|
|
27
|
+
/** Nudge along the cross axis. */
|
|
28
|
+
alignOffset?: number;
|
|
29
|
+
/** Minimum distance the panel keeps from the viewport edges. */
|
|
30
|
+
collisionPadding?: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type PositionResult = {
|
|
34
|
+
top: number;
|
|
35
|
+
left: number;
|
|
36
|
+
/** The side actually used after collision handling. */
|
|
37
|
+
side: Side;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const OPPOSITE: Record<Side, Side> = {
|
|
41
|
+
top: 'bottom',
|
|
42
|
+
bottom: 'top',
|
|
43
|
+
left: 'right',
|
|
44
|
+
right: 'left',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const isHorizontal = (side: Side) => side === 'left' || side === 'right';
|
|
48
|
+
|
|
49
|
+
function clamp(value: number, min: number, max: number): number {
|
|
50
|
+
if (max < min) return min;
|
|
51
|
+
return Math.min(Math.max(value, min), max);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function spaceFor(side: Side, anchor: Rect, viewport: Size, padding: number): number {
|
|
55
|
+
switch (side) {
|
|
56
|
+
case 'top':
|
|
57
|
+
return anchor.top - padding;
|
|
58
|
+
case 'bottom':
|
|
59
|
+
return viewport.height - (anchor.top + anchor.height) - padding;
|
|
60
|
+
case 'left':
|
|
61
|
+
return anchor.left - padding;
|
|
62
|
+
case 'right':
|
|
63
|
+
return viewport.width - (anchor.left + anchor.width) - padding;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Places a floating panel beside an anchor, flipping to the opposite side when
|
|
69
|
+
* the preferred side has less room, then clamping to the viewport.
|
|
70
|
+
*
|
|
71
|
+
* Pure so the arithmetic can be tested without a DOM: the component only
|
|
72
|
+
* measures and applies the result.
|
|
73
|
+
*/
|
|
74
|
+
export function computePosition({
|
|
75
|
+
anchor,
|
|
76
|
+
content,
|
|
77
|
+
viewport,
|
|
78
|
+
side = 'bottom',
|
|
79
|
+
align = 'start',
|
|
80
|
+
sideOffset = 8,
|
|
81
|
+
alignOffset = 0,
|
|
82
|
+
collisionPadding = 8,
|
|
83
|
+
}: PositionOptions): PositionResult {
|
|
84
|
+
const crossExtent = isHorizontal(side) ? content.height : content.width;
|
|
85
|
+
const mainExtent = isHorizontal(side) ? content.width : content.height;
|
|
86
|
+
const vertical = !isHorizontal(side);
|
|
87
|
+
|
|
88
|
+
const preferredSpace = spaceFor(side, anchor, viewport, collisionPadding);
|
|
89
|
+
const oppositeSpace = spaceFor(OPPOSITE[side], anchor, viewport, collisionPadding);
|
|
90
|
+
const resolvedSide =
|
|
91
|
+
preferredSpace < mainExtent && oppositeSpace > preferredSpace ? OPPOSITE[side] : side;
|
|
92
|
+
|
|
93
|
+
// Cross-axis offset from the anchor's leading edge.
|
|
94
|
+
const anchorCrossStart = vertical ? anchor.left : anchor.top;
|
|
95
|
+
const anchorCrossSize = vertical ? anchor.width : anchor.height;
|
|
96
|
+
let cross =
|
|
97
|
+
align === 'start'
|
|
98
|
+
? anchorCrossStart
|
|
99
|
+
: align === 'end'
|
|
100
|
+
? anchorCrossStart + anchorCrossSize - crossExtent
|
|
101
|
+
: anchorCrossStart + (anchorCrossSize - crossExtent) / 2;
|
|
102
|
+
cross += alignOffset;
|
|
103
|
+
|
|
104
|
+
// Main-axis offset from the anchor's edge.
|
|
105
|
+
let main: number;
|
|
106
|
+
switch (resolvedSide) {
|
|
107
|
+
case 'bottom':
|
|
108
|
+
main = anchor.top + anchor.height + sideOffset;
|
|
109
|
+
break;
|
|
110
|
+
case 'top':
|
|
111
|
+
main = anchor.top - content.height - sideOffset;
|
|
112
|
+
break;
|
|
113
|
+
case 'right':
|
|
114
|
+
main = anchor.left + anchor.width + sideOffset;
|
|
115
|
+
break;
|
|
116
|
+
default:
|
|
117
|
+
main = anchor.left - content.width - sideOffset;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const crossLimit = (vertical ? viewport.width : viewport.height) - crossExtent - collisionPadding;
|
|
121
|
+
const boundedCross = clamp(cross, collisionPadding, crossLimit);
|
|
122
|
+
|
|
123
|
+
const mainLimit = (vertical ? viewport.height : viewport.width) - mainExtent - collisionPadding;
|
|
124
|
+
const boundedMain = clamp(main, collisionPadding, mainLimit);
|
|
125
|
+
|
|
126
|
+
return vertical
|
|
127
|
+
? { top: boundedMain, left: boundedCross, side: resolvedSide }
|
|
128
|
+
: { top: boundedCross, left: boundedMain, side: resolvedSide };
|
|
129
|
+
}
|