@delacour/react-native-ui 0.1.0-alpha.20261006140628 → 0.1.0-alpha.20261006141648
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/package.json +5 -3
- package/src/components/calendar/AGENTS.md +142 -0
- package/src/components/calendar/calendar-caption.tsx +116 -0
- package/src/components/calendar/calendar-day.tsx +103 -0
- package/src/components/calendar/calendar-grid.tsx +183 -0
- package/src/components/calendar/calendar-header.tsx +34 -0
- package/src/components/calendar/calendar-nav.tsx +51 -0
- package/src/components/calendar/calendar-picker.tsx +128 -0
- package/src/components/calendar/calendar-weekdays.tsx +48 -0
- package/src/components/calendar/calendar.context.tsx +108 -0
- package/src/components/calendar/calendar.date.test.ts +456 -0
- package/src/components/calendar/calendar.date.ts +584 -0
- package/src/components/calendar/calendar.tsx +389 -0
- package/src/components/calendar/calendar.types.ts +87 -0
- package/src/components/calendar/calendar.variants.test.ts +363 -0
- package/src/components/calendar/calendar.variants.ts +426 -0
- package/src/components/calendar/index.ts +96 -0
- package/src/components/dialog/AGENTS.md +97 -0
- package/src/components/dialog/dialog-body.tsx +21 -0
- package/src/components/dialog/dialog-close.tsx +82 -0
- package/src/components/dialog/dialog-content.tsx +153 -0
- package/src/components/dialog/dialog-description.tsx +23 -0
- package/src/components/dialog/dialog-footer.tsx +32 -0
- package/src/components/dialog/dialog-header.tsx +20 -0
- package/src/components/dialog/dialog-title.tsx +42 -0
- package/src/components/dialog/dialog-trigger.tsx +54 -0
- package/src/components/dialog/dialog.context.tsx +90 -0
- package/src/components/dialog/dialog.tsx +119 -0
- package/src/components/dialog/dialog.variants.test.ts +224 -0
- package/src/components/dialog/dialog.variants.ts +173 -0
- package/src/components/dialog/index.ts +30 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.20261006141648",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,11 +39,13 @@
|
|
|
39
39
|
"./badge": "./src/components/badge/index.ts",
|
|
40
40
|
"./bottom-sheet": "./src/components/bottom-sheet/index.ts",
|
|
41
41
|
"./button": "./src/components/button/index.ts",
|
|
42
|
+
"./calendar": "./src/components/calendar/index.ts",
|
|
42
43
|
"./card": "./src/components/card/index.ts",
|
|
43
44
|
"./chart": "./src/components/chart/index.ts",
|
|
44
45
|
"./checkbox": "./src/components/checkbox/index.ts",
|
|
45
46
|
"./chip": "./src/components/chip/index.ts",
|
|
46
47
|
"./collapsible": "./src/components/collapsible/index.ts",
|
|
48
|
+
"./dialog": "./src/components/dialog/index.ts",
|
|
47
49
|
"./empty-state": "./src/components/empty-state/index.ts",
|
|
48
50
|
"./field": "./src/components/field/index.ts",
|
|
49
51
|
"./icon": "./src/components/icon/index.ts",
|
|
@@ -103,8 +105,8 @@
|
|
|
103
105
|
},
|
|
104
106
|
"peerDependencies": {
|
|
105
107
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
106
|
-
"@delacour/react-native-bottom-sheet": "0.1.0-alpha.
|
|
107
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
108
|
+
"@delacour/react-native-bottom-sheet": "0.1.0-alpha.20261006141648",
|
|
109
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20261006141648",
|
|
108
110
|
"@legendapp/list": ">=3.3",
|
|
109
111
|
"expo-linear-gradient": ">=15",
|
|
110
112
|
"expo-router": ">=57",
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Calendar
|
|
2
|
+
|
|
3
|
+
An always-visible month grid for picking one day, several days or a range. Compound root plus
|
|
4
|
+
`Calendar.Header`, `Calendar.Nav`, `Calendar.Caption`, `Calendar.Weekdays`, `Calendar.Grid`,
|
|
5
|
+
`Calendar.Day` and `Calendar.Picker`. Used inline on a screen, and as the body of the date pickers'
|
|
6
|
+
sheets.
|
|
7
|
+
|
|
8
|
+
`import { Calendar } from "@delacour/react-native-ui/calendar";`
|
|
9
|
+
|
|
10
|
+
## Files
|
|
11
|
+
|
|
12
|
+
| File | What it holds |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `index.ts` | → `@delacour/react-native-ui/calendar`; also re-exports every date helper and type |
|
|
15
|
+
| `calendar.tsx` | Root: selection and month state, bounds, the context value, the `Object.assign` surface |
|
|
16
|
+
| `calendar-header.tsx` | `Calendar.Header` — prev arrow, caption, next arrow by default |
|
|
17
|
+
| `calendar-nav.tsx` | `Calendar.Nav` — a ghost `Button` that disables itself at a bound |
|
|
18
|
+
| `calendar-caption.tsx` | `Calendar.Caption` — label, or the button that cycles days → months → years |
|
|
19
|
+
| `calendar-weekdays.tsx` | `Calendar.Weekdays` — short names shown, full names spoken |
|
|
20
|
+
| `calendar-grid.tsx` | `Calendar.Grid` — six weeks, the paging pan and slide, hosts the picker |
|
|
21
|
+
| `calendar-day.tsx` | `Calendar.Day` — one cell: band, circle, number or custom content |
|
|
22
|
+
| `calendar-picker.tsx` | `Calendar.Picker` — the months and years jump views |
|
|
23
|
+
| `calendar.context.tsx` | `CalendarContext`, `useCalendar()`, `useCalendarContext()`, `useCalendarPart()` |
|
|
24
|
+
| `calendar.types.ts` | `CalendarSelection` (the mode union), `CalendarProps`, `CalendarCaptionLayout` |
|
|
25
|
+
| `calendar.date.ts` | **Shared leaf.** `CalendarDate` maths, matchers, grid, selection rules, `Intl` formatters. No React Native |
|
|
26
|
+
| `calendar.date.test.ts` | |
|
|
27
|
+
| `calendar.variants.ts` | Slotted `tv()`, the day-state, range-role, bounds, axes and page-direction resolvers |
|
|
28
|
+
| `calendar.variants.test.ts` | |
|
|
29
|
+
|
|
30
|
+
## The date leaf
|
|
31
|
+
|
|
32
|
+
- **A day is `{ year, month, day }` and nothing else.** No `Date`, no time zone, no UTC
|
|
33
|
+
round-trip — a `Date` built at midnight in Auckland is the previous day in UTC, and that is the
|
|
34
|
+
bug every calendar built on `Date` ships once. `toDate`/`fromDate` cross over by **local**
|
|
35
|
+
fields, at the edge only.
|
|
36
|
+
- **`weekdayOf` is Sakamoto's method**, not `new Date().getDay()`, so the leaf never touches the
|
|
37
|
+
device clock or zone. A test checks it against the platform for every day of a leap year.
|
|
38
|
+
- **`calendar.date.ts` is the only module another folder may import from here.** `date-picker`
|
|
39
|
+
and `date-time-picker` reach `../calendar/calendar.date` — never `../calendar`, which would pull
|
|
40
|
+
the whole compound in (package rule 3).
|
|
41
|
+
- **`resolveWeekStart` prefers `Intl.Locale#getWeekInfo`/`weekInfo`** and falls back to a region
|
|
42
|
+
table, because Hermes support for either is not guaranteed. Unknown is Monday (ISO 8601).
|
|
43
|
+
- **Formatters never throw.** An unknown locale falls back to `en`; a formatter that throws inside
|
|
44
|
+
render takes the screen down for a typo.
|
|
45
|
+
- **`parseDate` is strict** — `YYYY-MM-DD` and a real day, or `null`. A lenient parser turns
|
|
46
|
+
`2026-02-31` into 3 March, which is a different booking.
|
|
47
|
+
|
|
48
|
+
## Selection
|
|
49
|
+
|
|
50
|
+
- **`mode` is the discriminant** of `CalendarSelection`, so `selected` and `onSelect` are typed for
|
|
51
|
+
the mode named. Internally one `CalendarSelectionState` union flows through
|
|
52
|
+
`useControllableState`, and `emitSelection` narrows back to the caller's shape.
|
|
53
|
+
- **Single:** a tap on the selected day clears it. **Multiple:** toggles; always sorted, deduped.
|
|
54
|
+
- **Range** (`applyRangeTap`): empty → start; start + any day → complete, in either direction,
|
|
55
|
+
with the earlier day as `start` (`normaliseRange`); complete + tap → a new start. Backwards
|
|
56
|
+
completes rather than moving the start, because tapping check-out first is as common as
|
|
57
|
+
check-in first and a tap that silently discards the first one reads as a bug. A range that would contain a disabled day
|
|
58
|
+
(`rangeContainsDisabled`) restarts at the tapped day instead of completing — a stay that spans a
|
|
59
|
+
closed night is not bookable, and silently accepting it is worse than making the user tap again.
|
|
60
|
+
- **Disabled vs read-only.** `isDisabled` makes everything inert and fades the root. `isReadOnly`
|
|
61
|
+
ignores day taps and leaves paging working — looking at another month changes nothing.
|
|
62
|
+
|
|
63
|
+
## Layout
|
|
64
|
+
|
|
65
|
+
- **Always six weeks** (`CALENDAR_WEEKS`). A month that fits in four or five rows still draws six,
|
|
66
|
+
so height and column positions never shift while paging. An outside day with
|
|
67
|
+
`showOutsideDays={false}` is an empty cell of the same height, never a missing one.
|
|
68
|
+
- **The whole cell is the target**, not the circle. A column on a phone is wider than the circle
|
|
69
|
+
is tall, and the gap belongs to whichever day it sits beside.
|
|
70
|
+
- **The cell is on the input scale** (`h-input-*`, `size-input-*`, `text-input-*`): sm 36, md 44,
|
|
71
|
+
lg 52. `md` and `lg` meet 44pt.
|
|
72
|
+
- **The jump view sits over the grid** (`absolute inset-0`) while the weeks are hidden with
|
|
73
|
+
`opacity-0`, not unmounted, so opening it never changes the calendar's height. The weekday row
|
|
74
|
+
hides the same way.
|
|
75
|
+
|
|
76
|
+
## Drawing
|
|
77
|
+
|
|
78
|
+
- **A day is three layers:** `band` (absolute, edge to edge, behind), `dayBase` (the circle) and
|
|
79
|
+
the label. The band spans the full cell so adjacent days join into one strip.
|
|
80
|
+
- **The band ends at the centre of each end cell, and the circle is the cap.** Start runs from its
|
|
81
|
+
cell's centre to the right edge, end from the left edge to its centre, middle spans the cell; none
|
|
82
|
+
is rounded. A cell is wider than its circle, so the first version — a band rounded across the
|
|
83
|
+
whole end cell — showed a crescent of band beside the circle at both ends. A lone `only` day
|
|
84
|
+
draws no band. At a week's edge the strip simply stops and resumes on the next row.
|
|
85
|
+
- **Today is a ring (`border-ring`) and `text-primary`, never a fill**, so it cannot be mistaken
|
|
86
|
+
for a selection.
|
|
87
|
+
- **Variant:** `primary` fills `bg-primary`, band `bg-primary/15`; `secondary` fills
|
|
88
|
+
`bg-primary/25` under `text-foreground`, band `bg-primary/10`. The primary band is a tint of the selection colour, not
|
|
89
|
+
`bg-accent`: light `--accent` is 0.97 on a 0.985 page, so the band all but vanished in light
|
|
90
|
+
while reading fine in dark. A tint of `primary` reads in both themes and joins the end caps
|
|
91
|
+
as one shape. `secondary` is tinted from `primary` for the same reason — `bg-secondary`
|
|
92
|
+
and `bg-muted` are 0.97 on the same 0.985 page, and the recorded preview showed a soft
|
|
93
|
+
selection with no visible circle at all. `isInvalid` overrides both with `bg-destructive` / `bg-destructive-soft`.
|
|
94
|
+
- **Colour is on the text slots only** (rule 1): `dayLabel`, `weekdayLabel`, `captionText`,
|
|
95
|
+
`pickerItemLabel`. A test holds every `View` slot to carrying no `text-*` colour.
|
|
96
|
+
- **Custom day content** gets `labelClassName` in its render props, so a price under the number
|
|
97
|
+
takes the selected colour with it rather than reimplementing the tone table.
|
|
98
|
+
|
|
99
|
+
## Paging
|
|
100
|
+
|
|
101
|
+
- **A Gesture Handler pan on `Calendar.Grid`**, `activeOffsetX` ±12 and `failOffsetY` ±12: it
|
|
102
|
+
claims a sideways drag and gives up a vertical one, so the calendar lives inside a `ScrollView`
|
|
103
|
+
or a sheet without stealing its scroll.
|
|
104
|
+
- **`resolvePageDirection` is pure and tested.** A quarter of the width, or a release faster than
|
|
105
|
+
500 pt/s, pages; a flick against the drag settles back; it never pages past a bound or on an
|
|
106
|
+
unmeasured grid.
|
|
107
|
+
- **Slide out, swap, slide in.** The old month slides off, the month changes on the JS thread, and
|
|
108
|
+
the new one slides in from the opposite side — the same entrance the arrows give. The grid
|
|
109
|
+
clips (`overflow-hidden`) so the swap frame is never seen.
|
|
110
|
+
- **Calm motion skips both slides.** The page change is behaviour and always happens.
|
|
111
|
+
- **A drag toward a bound resists** (20% travel) and settles back with a `light` haptic — the edge
|
|
112
|
+
is felt rather than the month silently refusing to turn.
|
|
113
|
+
- **Tapping an outside day** (with `selectOutsideDays`) selects it and pages to its month.
|
|
114
|
+
|
|
115
|
+
## Accessibility
|
|
116
|
+
|
|
117
|
+
- Each day is a button labelled with the full date (`formatDate(…, { style: "full" })`), hinted
|
|
118
|
+
"Today" / "Range start" / "Range end", with `selected` and `disabled` state.
|
|
119
|
+
- The grid is `role="grid"`, named by an enclosing `Field.Label` or else the month.
|
|
120
|
+
- **Paging for screen readers lives on the caption**, as increment/decrement actions. An
|
|
121
|
+
adjustable grid would fold its child buttons into itself on both platforms and make the days
|
|
122
|
+
unreachable.
|
|
123
|
+
- A month change is announced with `AccessibilityInfo.announceForAccessibility` — on change only,
|
|
124
|
+
never on mount, because the caption already says it.
|
|
125
|
+
- Today is re-read on `AppState` → `active`, so a calendar left open overnight marks the right day.
|
|
126
|
+
|
|
127
|
+
- **Parts derive their `testID` from the root's** (`resolvePartTestID`): `testID="booking"`
|
|
128
|
+
names the days `booking-day-2026-10-05`, the arrows `booking-prev`/`booking-next`, the caption
|
|
129
|
+
`booking-caption` and the jump-view items `booking-month-10`/`booking-year-2026`. The default
|
|
130
|
+
anatomy renders forty-odd parts no caller writes, and an end-to-end test or a recorded preview
|
|
131
|
+
flow has to reach one particular day all the same. No root `testID`, no part ids.
|
|
132
|
+
|
|
133
|
+
## Field
|
|
134
|
+
|
|
135
|
+
`resolveCalendarAxes` is the nearest-wins chain: the calendar's own `isDisabled` / `isInvalid` /
|
|
136
|
+
`isReadOnly` / `size` / `variant`, else the `Field`'s. The field's label names the grid. It does
|
|
137
|
+
not `registerPress` — there is no single focus target for a row tap to reach.
|
|
138
|
+
|
|
139
|
+
## Not here
|
|
140
|
+
|
|
141
|
+
Non-Gregorian calendars, side-by-side months, week numbers, drag-to-select ranges, time. The
|
|
142
|
+
anchored presentation belongs to `DatePicker`.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type ReactElement, useCallback } from "react";
|
|
2
|
+
import { type AccessibilityActionEvent, View, type ViewProps } from "react-native";
|
|
3
|
+
import { IconChevronDownSmall } from "../../icons/central";
|
|
4
|
+
import { Icon } from "../icon";
|
|
5
|
+
import { Pressable } from "../pressable";
|
|
6
|
+
import { Text } from "../text";
|
|
7
|
+
import { useCalendarPart } from "./calendar.context";
|
|
8
|
+
import { formatMonthYear, formatYear } from "./calendar.date";
|
|
9
|
+
import { calendarVariants, resolvePartTestID } from "./calendar.variants";
|
|
10
|
+
|
|
11
|
+
export type CalendarCaptionProps = Omit<ViewProps, "children"> & {
|
|
12
|
+
className?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const PAGE_ACTIONS = [
|
|
16
|
+
{ name: "decrement", label: "Previous" },
|
|
17
|
+
{ name: "increment", label: "Next" },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The month and year above the grid.
|
|
22
|
+
*
|
|
23
|
+
* With `captionLayout="picker"` it is a button: in the grid it opens the months view, in the
|
|
24
|
+
* months view the years view, and in the years view it returns to the grid. With `"label"` it is
|
|
25
|
+
* text.
|
|
26
|
+
*
|
|
27
|
+
* **It is where a screen-reader user pages.** A grid of buttons cannot itself be an adjustable
|
|
28
|
+
* element — iOS and Android both fold an accessible container's children into it, and the days
|
|
29
|
+
* would become unreachable — so the caption carries the increment and decrement actions instead:
|
|
30
|
+
* swipe up or down on "October 2026" to move a month. As a plain label it takes the `adjustable`
|
|
31
|
+
* role outright; as a button the two actions sit beside its activation.
|
|
32
|
+
*/
|
|
33
|
+
export function CalendarCaption({ className, ...props }: CalendarCaptionProps): ReactElement {
|
|
34
|
+
const {
|
|
35
|
+
canGoNext,
|
|
36
|
+
canGoPrev,
|
|
37
|
+
captionLayout,
|
|
38
|
+
goNext,
|
|
39
|
+
goPrev,
|
|
40
|
+
isDisabled,
|
|
41
|
+
locale,
|
|
42
|
+
setView,
|
|
43
|
+
size,
|
|
44
|
+
testID,
|
|
45
|
+
variant,
|
|
46
|
+
view,
|
|
47
|
+
visibleMonth,
|
|
48
|
+
yearSpan,
|
|
49
|
+
} = useCalendarPart("Calendar.Caption");
|
|
50
|
+
const slots = calendarVariants({ size, variant });
|
|
51
|
+
|
|
52
|
+
const text =
|
|
53
|
+
view === "days"
|
|
54
|
+
? formatMonthYear(visibleMonth, locale)
|
|
55
|
+
: view === "months"
|
|
56
|
+
? formatYear(visibleMonth.year, locale)
|
|
57
|
+
: `${formatYear(yearSpan.from, locale)} – ${formatYear(yearSpan.to, locale)}`;
|
|
58
|
+
|
|
59
|
+
const handleAction = useCallback(
|
|
60
|
+
(event: AccessibilityActionEvent) => {
|
|
61
|
+
if (event.nativeEvent.actionName === "increment" && canGoNext) goNext();
|
|
62
|
+
if (event.nativeEvent.actionName === "decrement" && canGoPrev) goPrev();
|
|
63
|
+
},
|
|
64
|
+
[canGoNext, canGoPrev, goNext, goPrev]
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const handlePress = useCallback(() => {
|
|
68
|
+
setView(view === "days" ? "months" : view === "months" ? "years" : "days");
|
|
69
|
+
}, [setView, view]);
|
|
70
|
+
|
|
71
|
+
const label = <Text className={slots.captionText()}>{text}</Text>;
|
|
72
|
+
|
|
73
|
+
if (captionLayout === "label") {
|
|
74
|
+
return (
|
|
75
|
+
<View
|
|
76
|
+
accessibilityActions={PAGE_ACTIONS}
|
|
77
|
+
accessibilityLabel={text}
|
|
78
|
+
accessibilityRole="adjustable"
|
|
79
|
+
accessible
|
|
80
|
+
className={slots.caption({ className })}
|
|
81
|
+
onAccessibilityAction={handleAction}
|
|
82
|
+
testID={resolvePartTestID(testID, "caption")}
|
|
83
|
+
{...props}
|
|
84
|
+
>
|
|
85
|
+
{label}
|
|
86
|
+
</View>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const hint =
|
|
91
|
+
view === "days"
|
|
92
|
+
? "Opens the month and year picker"
|
|
93
|
+
: view === "months"
|
|
94
|
+
? "Opens the year list"
|
|
95
|
+
: "Returns to the month";
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<Pressable
|
|
99
|
+
accessibilityActions={PAGE_ACTIONS}
|
|
100
|
+
accessibilityHint={hint}
|
|
101
|
+
accessibilityLabel={text}
|
|
102
|
+
className={slots.caption({ className })}
|
|
103
|
+
disabled={isDisabled}
|
|
104
|
+
feedback="fade"
|
|
105
|
+
haptic="selection"
|
|
106
|
+
onAccessibilityAction={handleAction}
|
|
107
|
+
onPress={handlePress}
|
|
108
|
+
testID={resolvePartTestID(testID, "caption")}
|
|
109
|
+
{...props}
|
|
110
|
+
>
|
|
111
|
+
{label}
|
|
112
|
+
<Icon color="muted-foreground" icon={IconChevronDownSmall} size="sm" />
|
|
113
|
+
</Pressable>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
CalendarCaption.displayName = "DelacourUI.Calendar.Caption";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useCallback } from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import { Pressable } from "../pressable";
|
|
4
|
+
import { Text } from "../text";
|
|
5
|
+
import { useCalendarPart } from "./calendar.context";
|
|
6
|
+
import { type CalendarDate, formatDate, isSameMonth, serialiseDate } from "./calendar.date";
|
|
7
|
+
import { type CalendarDayState, calendarVariants, resolveDayState, resolvePartTestID } from "./calendar.variants";
|
|
8
|
+
|
|
9
|
+
/** What a function child of `Calendar.Day` is handed. */
|
|
10
|
+
export type CalendarDayRenderProps = CalendarDayState & {
|
|
11
|
+
date: CalendarDate;
|
|
12
|
+
/** The class the default number is drawn with — size, weight and colour for this state. */
|
|
13
|
+
labelClassName: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type CalendarDayProps = {
|
|
17
|
+
/** The day this cell draws. */
|
|
18
|
+
date: CalendarDate;
|
|
19
|
+
/**
|
|
20
|
+
* Custom content inside the circle, in place of the number. A function is handed the day's
|
|
21
|
+
* state, so a price under the number can take the selected colour with it.
|
|
22
|
+
*/
|
|
23
|
+
children?: ReactNode | ((props: CalendarDayRenderProps) => ReactNode);
|
|
24
|
+
className?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** The state words a screen reader adds after the date. */
|
|
28
|
+
function describeState(state: CalendarDayState): string | undefined {
|
|
29
|
+
const parts: string[] = [];
|
|
30
|
+
if (state.isToday) parts.push("Today");
|
|
31
|
+
if (state.rangeRole === "start") parts.push("Range start");
|
|
32
|
+
if (state.rangeRole === "end") parts.push("Range end");
|
|
33
|
+
return parts.length > 0 ? parts.join(", ") : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* One day: its column share, the range band behind it, and the circle with the number.
|
|
38
|
+
*
|
|
39
|
+
* **The whole cell is the target**, not the circle — a column on a phone is wider than the
|
|
40
|
+
* circle is tall, and the gap between two circles belongs to whichever day it sits beside.
|
|
41
|
+
*
|
|
42
|
+
* The band is drawn for a range's start, middle and end and never for a lone day, which is a
|
|
43
|
+
* circle with nothing to join. An outside day with `showOutsideDays` off is an empty cell of the
|
|
44
|
+
* same height, so the grid's rows never move.
|
|
45
|
+
*
|
|
46
|
+
* Announced as a button with the full date — "Monday 5 October 2026" — plus today and the range
|
|
47
|
+
* end it is, and `selected` and `disabled` as accessibility state.
|
|
48
|
+
*/
|
|
49
|
+
export function CalendarDay({ date, children, className }: CalendarDayProps): ReactElement {
|
|
50
|
+
const calendar = useCalendarPart("Calendar.Day");
|
|
51
|
+
const state = resolveDayState({
|
|
52
|
+
cell: { date, isOutside: !isSameMonth(date, calendar.visibleMonth), weekIndex: 0, dayIndex: 0 },
|
|
53
|
+
isCalendarDisabled: calendar.isDisabled,
|
|
54
|
+
isDisabled: calendar.isDateDisabled,
|
|
55
|
+
isReadOnly: calendar.isReadOnly,
|
|
56
|
+
selectOutsideDays: calendar.selectOutsideDays,
|
|
57
|
+
selection: calendar.selection,
|
|
58
|
+
showOutsideDays: calendar.showOutsideDays,
|
|
59
|
+
today: calendar.today,
|
|
60
|
+
});
|
|
61
|
+
const slots = calendarVariants({
|
|
62
|
+
isDisabled: state.isDisabled,
|
|
63
|
+
isInvalid: calendar.isInvalid,
|
|
64
|
+
isOutside: state.isOutside,
|
|
65
|
+
isToday: state.isToday,
|
|
66
|
+
rangeRole: state.rangeRole ?? "none",
|
|
67
|
+
size: calendar.size,
|
|
68
|
+
tone: state.tone,
|
|
69
|
+
variant: calendar.variant,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const { select } = calendar;
|
|
73
|
+
const handlePress = useCallback(() => select(date), [date, select]);
|
|
74
|
+
|
|
75
|
+
if (state.isHidden) {
|
|
76
|
+
return <View className={slots.cell({ className })} importantForAccessibility="no-hide-descendants" />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const labelClassName = slots.dayLabel();
|
|
80
|
+
const content =
|
|
81
|
+
typeof children === "function"
|
|
82
|
+
? children({ ...state, date, labelClassName })
|
|
83
|
+
: (children ?? <Text className={labelClassName}>{date.day}</Text>);
|
|
84
|
+
const showsBand = state.rangeRole !== null && state.rangeRole !== "only";
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<Pressable
|
|
88
|
+
accessibilityHint={describeState(state)}
|
|
89
|
+
accessibilityLabel={formatDate(date, { locale: calendar.locale, style: "full" })}
|
|
90
|
+
accessibilityState={{ selected: state.isSelected }}
|
|
91
|
+
className={slots.cell({ className })}
|
|
92
|
+
disabled={!state.isPressable}
|
|
93
|
+
feedback="fade"
|
|
94
|
+
haptic="selection"
|
|
95
|
+
onPress={handlePress}
|
|
96
|
+
testID={resolvePartTestID(calendar.testID, `day-${serialiseDate(date)}`)}
|
|
97
|
+
>
|
|
98
|
+
{showsBand ? <View className={slots.band()} /> : null}
|
|
99
|
+
<View className={slots.dayBase()}>{content}</View>
|
|
100
|
+
</Pressable>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
CalendarDay.displayName = "DelacourUI.Calendar.Day";
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useCallback, useLayoutEffect, useMemo, useRef } from "react";
|
|
2
|
+
import { type LayoutChangeEvent, View, type ViewProps } from "react-native";
|
|
3
|
+
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
4
|
+
import Animated, {
|
|
5
|
+
Easing,
|
|
6
|
+
useAnimatedStyle,
|
|
7
|
+
useSharedValue,
|
|
8
|
+
withSequence,
|
|
9
|
+
withSpring,
|
|
10
|
+
withTiming,
|
|
11
|
+
} from "react-native-reanimated";
|
|
12
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
13
|
+
import { useCalmMotion } from "../../hooks/use-calm-motion";
|
|
14
|
+
import { playHaptic } from "../pressable";
|
|
15
|
+
import { useCalendarPart } from "./calendar.context";
|
|
16
|
+
import { type CalendarCell, formatMonthYear, monthGrid, serialiseDate } from "./calendar.date";
|
|
17
|
+
import { calendarVariants, resolvePageDirection } from "./calendar.variants";
|
|
18
|
+
import { CalendarDay } from "./calendar-day";
|
|
19
|
+
import { CalendarPicker } from "./calendar-picker";
|
|
20
|
+
|
|
21
|
+
export type CalendarGridProps = Omit<ViewProps, "children"> & {
|
|
22
|
+
className?: string;
|
|
23
|
+
/** Draws one cell. Omitted, a `Calendar.Day` for its date. */
|
|
24
|
+
renderDay?: (cell: CalendarCell) => ReactNode;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** How far a sideways drag travels before the grid claims it, and a vertical one before it gives up. */
|
|
28
|
+
const PAN_ACTIVATE_X = 12;
|
|
29
|
+
const PAN_FAIL_Y = 12;
|
|
30
|
+
|
|
31
|
+
/** How far a drag past a bound still follows the finger, as a fraction — enough to feel the edge. */
|
|
32
|
+
const BOUND_RESISTANCE = 0.2;
|
|
33
|
+
|
|
34
|
+
const SLIDE = { duration: 200, easing: Easing.out(Easing.cubic) } as const;
|
|
35
|
+
const SETTLE_SPRING = { damping: 20, stiffness: 260 } as const;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The six weeks of the visible month, and the surface a swipe pages on.
|
|
39
|
+
*
|
|
40
|
+
* **Paging is a slide out and a slide in.** A released swipe slides the old month off the side it
|
|
41
|
+
* was dragged towards, the month changes, and the new one slides in from the other side — the
|
|
42
|
+
* same entrance the arrows give it. Both off-screen positions are clipped, so the frame where the
|
|
43
|
+
* month swaps is never seen. With calm motion on, both slides are skipped and the page simply
|
|
44
|
+
* swaps; the page change itself is behaviour, not decoration, and always happens.
|
|
45
|
+
*
|
|
46
|
+
* **A drag towards a bound resists** rather than moving freely, then settles back with a light
|
|
47
|
+
* impact haptic — the edge is felt instead of the month silently refusing to turn.
|
|
48
|
+
*
|
|
49
|
+
* **The grid claims a sideways drag and gives up a vertical one**, so it lives inside a scrolling
|
|
50
|
+
* screen or a sheet without stealing the scroll.
|
|
51
|
+
*
|
|
52
|
+
* It also hosts `Calendar.Picker` over itself while a jump view is open, and hides its weeks
|
|
53
|
+
* rather than unmounting them, so the calendar's height never moves.
|
|
54
|
+
*/
|
|
55
|
+
export function CalendarGrid({ className, renderDay, ...props }: CalendarGridProps): ReactElement {
|
|
56
|
+
const calendar = useCalendarPart("Calendar.Grid");
|
|
57
|
+
const {
|
|
58
|
+
canGoNext,
|
|
59
|
+
canGoPrev,
|
|
60
|
+
goNext,
|
|
61
|
+
goPrev,
|
|
62
|
+
isDisabled,
|
|
63
|
+
label,
|
|
64
|
+
locale,
|
|
65
|
+
pageDirection,
|
|
66
|
+
size,
|
|
67
|
+
swipeToPage,
|
|
68
|
+
variant,
|
|
69
|
+
view,
|
|
70
|
+
} = calendar;
|
|
71
|
+
const { visibleMonth, weekStartsOn } = calendar;
|
|
72
|
+
const slots = calendarVariants({ size, variant });
|
|
73
|
+
const isCalm = useCalmMotion();
|
|
74
|
+
|
|
75
|
+
const width = useSharedValue(0);
|
|
76
|
+
const translateX = useSharedValue(0);
|
|
77
|
+
|
|
78
|
+
const handleLayout = useCallback(
|
|
79
|
+
(event: LayoutChangeEvent) => {
|
|
80
|
+
width.value = event.nativeEvent.layout.width;
|
|
81
|
+
},
|
|
82
|
+
[width]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const page = useCallback((direction: -1 | 1) => (direction === 1 ? goNext() : goPrev()), [goNext, goPrev]);
|
|
86
|
+
|
|
87
|
+
const pan = useMemo(
|
|
88
|
+
() =>
|
|
89
|
+
Gesture.Pan()
|
|
90
|
+
.enabled(swipeToPage && view === "days" && !isDisabled)
|
|
91
|
+
.activeOffsetX([-PAN_ACTIVATE_X, PAN_ACTIVATE_X])
|
|
92
|
+
.failOffsetY([-PAN_FAIL_Y, PAN_FAIL_Y])
|
|
93
|
+
.onUpdate((event) => {
|
|
94
|
+
"worklet";
|
|
95
|
+
const blocked = (event.translationX > 0 && !canGoPrev) || (event.translationX < 0 && !canGoNext);
|
|
96
|
+
translateX.value = blocked ? event.translationX * BOUND_RESISTANCE : event.translationX;
|
|
97
|
+
})
|
|
98
|
+
.onEnd((event) => {
|
|
99
|
+
"worklet";
|
|
100
|
+
const direction = resolvePageDirection({
|
|
101
|
+
canGoNext,
|
|
102
|
+
canGoPrev,
|
|
103
|
+
translationX: event.translationX,
|
|
104
|
+
velocityX: event.velocityX,
|
|
105
|
+
width: width.value,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (direction === 0) {
|
|
109
|
+
const blocked = (event.translationX > 0 && !canGoPrev) || (event.translationX < 0 && !canGoNext);
|
|
110
|
+
if (blocked) playHaptic("light");
|
|
111
|
+
translateX.value = withSpring(0, SETTLE_SPRING);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
playHaptic("selection");
|
|
116
|
+
if (isCalm) {
|
|
117
|
+
translateX.value = 0;
|
|
118
|
+
scheduleOnRN(page, direction);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
translateX.value = withTiming(-direction * width.value, SLIDE, (finished) => {
|
|
122
|
+
"worklet";
|
|
123
|
+
if (finished) scheduleOnRN(page, direction);
|
|
124
|
+
});
|
|
125
|
+
})
|
|
126
|
+
.onFinalize((_event, success) => {
|
|
127
|
+
"worklet";
|
|
128
|
+
if (!success) translateX.value = withSpring(0, SETTLE_SPRING);
|
|
129
|
+
}),
|
|
130
|
+
[canGoNext, canGoPrev, isCalm, isDisabled, page, swipeToPage, translateX, view, width]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// The new month slides in from the side the old one left by. Skipped on the first render — a
|
|
134
|
+
// calendar does not animate onto the screen — and for a jump, which has no side.
|
|
135
|
+
const monthKey = serialiseDate(visibleMonth);
|
|
136
|
+
const previousKey = useRef(monthKey);
|
|
137
|
+
useLayoutEffect(() => {
|
|
138
|
+
if (previousKey.current === monthKey) return;
|
|
139
|
+
previousKey.current = monthKey;
|
|
140
|
+
if (isCalm || pageDirection === 0 || width.value <= 0) {
|
|
141
|
+
translateX.value = 0;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
translateX.value = withSequence(withTiming(pageDirection * width.value, { duration: 0 }), withTiming(0, SLIDE));
|
|
145
|
+
}, [isCalm, monthKey, pageDirection, translateX, width]);
|
|
146
|
+
|
|
147
|
+
const slideStyle = useAnimatedStyle(() => ({ transform: [{ translateX: translateX.value }] }));
|
|
148
|
+
|
|
149
|
+
const weeks = useMemo(() => monthGrid(visibleMonth, weekStartsOn), [visibleMonth, weekStartsOn]);
|
|
150
|
+
const isPicking = view !== "days";
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<View className={slots.grid({ className })} onLayout={handleLayout} {...props}>
|
|
154
|
+
<GestureDetector gesture={pan}>
|
|
155
|
+
<Animated.View
|
|
156
|
+
accessibilityElementsHidden={isPicking}
|
|
157
|
+
accessibilityLabel={label ?? formatMonthYear(visibleMonth, locale)}
|
|
158
|
+
role="grid"
|
|
159
|
+
className={isPicking ? "opacity-0" : undefined}
|
|
160
|
+
importantForAccessibility={isPicking ? "no-hide-descendants" : "auto"}
|
|
161
|
+
pointerEvents={isPicking ? "none" : "auto"}
|
|
162
|
+
style={slideStyle}
|
|
163
|
+
>
|
|
164
|
+
{weeks.map((week) => (
|
|
165
|
+
<View className={slots.week()} key={serialiseDate(week[0]?.date ?? visibleMonth)}>
|
|
166
|
+
{week.map((cell) =>
|
|
167
|
+
renderDay ? (
|
|
168
|
+
<View className="flex-1" key={serialiseDate(cell.date)}>
|
|
169
|
+
{renderDay(cell)}
|
|
170
|
+
</View>
|
|
171
|
+
) : (
|
|
172
|
+
<CalendarDay date={cell.date} key={serialiseDate(cell.date)} />
|
|
173
|
+
)
|
|
174
|
+
)}
|
|
175
|
+
</View>
|
|
176
|
+
))}
|
|
177
|
+
</Animated.View>
|
|
178
|
+
</GestureDetector>
|
|
179
|
+
{isPicking ? <CalendarPicker /> : null}
|
|
180
|
+
</View>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
CalendarGrid.displayName = "DelacourUI.Calendar.Grid";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { View, type ViewProps } from "react-native";
|
|
3
|
+
import { useCalendarPart } from "./calendar.context";
|
|
4
|
+
import { calendarVariants } from "./calendar.variants";
|
|
5
|
+
import { CalendarCaption } from "./calendar-caption";
|
|
6
|
+
import { CalendarNav } from "./calendar-nav";
|
|
7
|
+
|
|
8
|
+
export type CalendarHeaderProps = ViewProps & {
|
|
9
|
+
className?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The row above the grid: the previous arrow, the caption and the next arrow.
|
|
14
|
+
*
|
|
15
|
+
* Omit `children` for that default; pass them to rearrange it — both arrows on one side, say, or
|
|
16
|
+
* a caption with a "Today" button beside it.
|
|
17
|
+
*/
|
|
18
|
+
export function CalendarHeader({ className, children, ...props }: CalendarHeaderProps): ReactElement {
|
|
19
|
+
const { size, variant } = useCalendarPart("Calendar.Header");
|
|
20
|
+
const slots = calendarVariants({ size, variant });
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<View className={slots.header({ className })} {...props}>
|
|
24
|
+
{children ?? (
|
|
25
|
+
<>
|
|
26
|
+
<CalendarNav direction="prev" />
|
|
27
|
+
<CalendarCaption />
|
|
28
|
+
<CalendarNav direction="next" />
|
|
29
|
+
</>
|
|
30
|
+
)}
|
|
31
|
+
</View>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
CalendarHeader.displayName = "DelacourUI.Calendar.Header";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { IconChevronLeft, IconChevronRight } from "../../icons/central";
|
|
3
|
+
import { Button, type ButtonProps } from "../button";
|
|
4
|
+
import { Icon } from "../icon";
|
|
5
|
+
import { useCalendarPart } from "./calendar.context";
|
|
6
|
+
import { resolvePartTestID } from "./calendar.variants";
|
|
7
|
+
|
|
8
|
+
export type CalendarNavProps = Omit<ButtonProps, "children" | "onPress"> & {
|
|
9
|
+
/** Which way the arrow pages. */
|
|
10
|
+
direction: "prev" | "next";
|
|
11
|
+
/** The arrow's accessible name. Defaults to "Previous month" / "Next month" — or year, in the months view. */
|
|
12
|
+
label?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One paging arrow: a ghost icon button that disables itself at the bound it points at.
|
|
17
|
+
*
|
|
18
|
+
* In the month grid it pages a month; in the months view, a year; in the years view, where the
|
|
19
|
+
* list scrolls instead, it is disabled. A disabled calendar disables it too — a read-only one
|
|
20
|
+
* does not, because looking at another month changes nothing.
|
|
21
|
+
*/
|
|
22
|
+
export function CalendarNav({ direction, label, isDisabled, ...props }: CalendarNavProps): ReactElement {
|
|
23
|
+
const {
|
|
24
|
+
canGoNext,
|
|
25
|
+
canGoPrev,
|
|
26
|
+
goNext,
|
|
27
|
+
goPrev,
|
|
28
|
+
isDisabled: calendarDisabled,
|
|
29
|
+
testID,
|
|
30
|
+
view,
|
|
31
|
+
} = useCalendarPart("Calendar.Nav");
|
|
32
|
+
const isPrev = direction === "prev";
|
|
33
|
+
const canGo = (isPrev ? canGoPrev : canGoNext) && view !== "years";
|
|
34
|
+
const unit = view === "months" ? "year" : "month";
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<Button
|
|
38
|
+
accessibilityLabel={label ?? `${isPrev ? "Previous" : "Next"} ${unit}`}
|
|
39
|
+
haptic="selection"
|
|
40
|
+
isDisabled={isDisabled ?? (calendarDisabled || !canGo)}
|
|
41
|
+
onPress={isPrev ? goPrev : goNext}
|
|
42
|
+
size="icon-sm"
|
|
43
|
+
testID={resolvePartTestID(testID, direction)}
|
|
44
|
+
variant="ghost"
|
|
45
|
+
{...props}
|
|
46
|
+
>
|
|
47
|
+
<Icon icon={isPrev ? IconChevronLeft : IconChevronRight} />
|
|
48
|
+
</Button>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
CalendarNav.displayName = "DelacourUI.Calendar.Nav";
|