@slipstream-ui/core 0.8.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 +184 -8
- package/dist/index.d.ts +184 -8
- package/dist/index.js +1609 -887
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1651 -933
- package/dist/index.mjs.map +1 -1
- package/dist/manifests/accordion.manifest.json +2 -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 +1 -1
- package/dist/manifests/autocomplete.manifest.json +1 -1
- package/dist/manifests/avatar.manifest.json +1 -1
- package/dist/manifests/badge.manifest.json +1 -1
- package/dist/manifests/block.manifest.json +1 -1
- package/dist/manifests/bottom-nav.manifest.json +1 -1
- 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 +1 -1
- 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 +1 -1
- package/dist/manifests/multi-select.manifest.json +1 -1
- package/dist/manifests/newsletter.manifest.json +1 -1
- package/dist/manifests/page-header.manifest.json +1 -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/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/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/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/page-header/PageHeader.js +1 -1
- package/dist/src/components/page-header/PageHeader.js.map +1 -1
- package/dist/src/components/page-header/PageHeader.mjs +1 -1
- 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/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 +3 -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 +1 -1
- 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/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/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/page-header/PageHeader.tsx +1 -1
- 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 +3 -0
- package/src/manifests/accordion.manifest.json +2 -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 +1 -1
- package/src/manifests/autocomplete.manifest.json +1 -1
- package/src/manifests/avatar.manifest.json +1 -1
- package/src/manifests/badge.manifest.json +1 -1
- package/src/manifests/block.manifest.json +1 -1
- package/src/manifests/bottom-nav.manifest.json +1 -1
- 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 +1 -1
- 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 +1 -1
- package/src/manifests/multi-select.manifest.json +1 -1
- package/src/manifests/newsletter.manifest.json +1 -1
- package/src/manifests/page-header.manifest.json +1 -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,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
|
+
}
|