@marktiderman/genesis-ui-native 3.11.0 → 3.12.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/README.md +69 -0
- package/dist/{chunk-ZUR4PK77.js → chunk-47CDRP43.js} +45 -166
- package/dist/chunk-47CDRP43.js.map +1 -0
- package/dist/{chunk-SG7LYX7Z.js → chunk-BQYT6TH2.js} +14 -6
- package/dist/chunk-BQYT6TH2.js.map +1 -0
- package/dist/{chunk-EAHK7D7N.js → chunk-C2ARUGT7.js} +146 -42
- package/dist/chunk-C2ARUGT7.js.map +1 -0
- package/dist/{chunk-VEWSY4TZ.js → chunk-D4UU6KIF.js} +236 -21
- package/dist/chunk-D4UU6KIF.js.map +1 -0
- package/dist/{chunk-GGH3ZQNO.js → chunk-TUDJDIGC.js} +4 -4
- package/dist/{chunk-GGH3ZQNO.js.map → chunk-TUDJDIGC.js.map} +1 -1
- package/dist/{chunk-PMS2V46D.js → chunk-VHUUOH46.js} +2 -2
- package/dist/{chunk-PMS2V46D.js.map → chunk-VHUUOH46.js.map} +1 -1
- package/dist/{chunk-IWWXERKQ.js → chunk-X5CPLUPZ.js} +1236 -136
- package/dist/chunk-X5CPLUPZ.js.map +1 -0
- package/dist/components/index.d.ts +510 -25
- package/dist/components/index.js +5 -5
- package/dist/data/index.d.ts +167 -33
- package/dist/data/index.js +1082 -760
- package/dist/data/index.js.map +1 -1
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +1 -1
- package/dist/icons/lucide.d.ts +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +7 -7
- package/dist/layouts/index.d.ts +79 -6
- package/dist/layouts/index.js +5 -5
- package/dist/{native-modules-Bkm1jHO-.d.ts → native-modules-6JnyeGR-.d.ts} +55 -1
- package/package.json +70 -4
- package/src/components/__tests__/combobox.test.tsx +20 -0
- package/src/components/__tests__/number-input.test.tsx +112 -1
- package/src/components/__tests__/parity-controls.test.tsx +366 -0
- package/src/components/__tests__/parity-display.test.tsx +295 -0
- package/src/components/__tests__/twin-props.test.tsx +114 -0
- package/src/components/badge.tsx +3 -0
- package/src/components/button-group.tsx +50 -0
- package/src/components/calendar.tsx +183 -0
- package/src/components/carousel.tsx +175 -0
- package/src/components/chart.tsx +268 -0
- package/src/components/chip-group.tsx +34 -14
- package/src/components/choice-card.tsx +56 -19
- package/src/components/combobox.tsx +6 -5
- package/src/components/empty-state.tsx +40 -0
- package/src/components/flatten-children.ts +13 -0
- package/src/components/index.ts +49 -1
- package/src/components/inline-alert.tsx +3 -1
- package/src/components/input-group.tsx +99 -0
- package/src/components/input-otp.tsx +124 -0
- package/src/components/input.tsx +40 -12
- package/src/components/list-item.tsx +20 -4
- package/src/components/number-input.tsx +45 -15
- package/src/components/radio-group.tsx +5 -0
- package/src/components/resource.tsx +227 -0
- package/src/components/settings-row.tsx +194 -0
- package/src/components/slider.tsx +3 -9
- package/src/components/sortable-list.tsx +287 -0
- package/src/components/stat-card.tsx +106 -0
- package/src/components/stepper.tsx +4 -2
- package/src/components/widget-tile.tsx +66 -0
- package/src/components/workspace-switcher.tsx +217 -0
- package/src/data/NativeCustomView.tsx +117 -0
- package/src/data/NativeFormFields.tsx +312 -0
- package/src/data/NativeRecordPeek.tsx +130 -0
- package/src/data/NativeResourceFormField.tsx +32 -0
- package/src/data/__tests__/parity-data.test.tsx +350 -0
- package/src/data/index.ts +13 -0
- package/src/layouts/__tests__/keyboard-scroll.test.tsx +58 -2
- package/src/layouts/__tests__/notifications-page.test.tsx +12 -0
- package/src/layouts/index.ts +13 -0
- package/src/layouts/keyboard.tsx +22 -2
- package/src/layouts/notifications-page.tsx +13 -35
- package/src/layouts/pages.tsx +262 -0
- package/src/native-modules.tsx +37 -0
- package/dist/chunk-EAHK7D7N.js.map +0 -1
- package/dist/chunk-IWWXERKQ.js.map +0 -1
- package/dist/chunk-SG7LYX7Z.js.map +0 -1
- package/dist/chunk-VEWSY4TZ.js.map +0 -1
- package/dist/chunk-ZUR4PK77.js.map +0 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The native twins take the web twins' props: NativeChipGroup's `mode` union and `allowEmpty`,
|
|
3
|
+
* NativeChoiceCard's `value` in a NativeRadioGroup and `multiple` with `onSelectedChange`, and
|
|
4
|
+
* NotificationsPage's `timeZone`.
|
|
5
|
+
*/
|
|
6
|
+
import { useState } from "react";
|
|
7
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
8
|
+
import { describe, expect, it, vi } from "vitest";
|
|
9
|
+
import { NativeChip, NativeChipGroup } from "../chip-group";
|
|
10
|
+
import { NativeChoiceCard } from "../choice-card";
|
|
11
|
+
import { NativeRadioGroup } from "../radio-group";
|
|
12
|
+
import { NotificationsPage } from "../../layouts/notifications-page";
|
|
13
|
+
|
|
14
|
+
describe("NativeChipGroup, as the web's ChipGroup", () => {
|
|
15
|
+
it("in single mode gives one string, reports the chosen chip pressed again as before, and clears it with allowEmpty", () => {
|
|
16
|
+
const onChange = vi.fn<(value: string) => void>();
|
|
17
|
+
const { rerender } = render(
|
|
18
|
+
<NativeChipGroup mode="single" value="all" onChange={onChange}>
|
|
19
|
+
<NativeChip value="all">All</NativeChip>
|
|
20
|
+
<NativeChip value="new">New</NativeChip>
|
|
21
|
+
</NativeChipGroup>,
|
|
22
|
+
);
|
|
23
|
+
// As on main: the chosen chip pressed again still calls onChange with its value.
|
|
24
|
+
fireEvent.click(screen.getByTestId("chip-all"));
|
|
25
|
+
expect(onChange).toHaveBeenLastCalledWith("all");
|
|
26
|
+
fireEvent.click(screen.getByTestId("chip-new"));
|
|
27
|
+
expect(onChange).toHaveBeenLastCalledWith("new");
|
|
28
|
+
rerender(
|
|
29
|
+
<NativeChipGroup mode="single" value="all" onChange={onChange} allowEmpty>
|
|
30
|
+
<NativeChip value="all">All</NativeChip>
|
|
31
|
+
<NativeChip value="new">New</NativeChip>
|
|
32
|
+
</NativeChipGroup>,
|
|
33
|
+
);
|
|
34
|
+
fireEvent.click(screen.getByTestId("chip-all"));
|
|
35
|
+
expect(onChange).toHaveBeenLastCalledWith("");
|
|
36
|
+
});
|
|
37
|
+
it("in multiple mode gives a list", () => {
|
|
38
|
+
const onChange = vi.fn<(value: string[]) => void>();
|
|
39
|
+
render(
|
|
40
|
+
<NativeChipGroup mode="multiple" value={["mon"]} onChange={onChange}>
|
|
41
|
+
<NativeChip value="mon">Mon</NativeChip>
|
|
42
|
+
<NativeChip value="tue">Tue</NativeChip>
|
|
43
|
+
</NativeChipGroup>,
|
|
44
|
+
);
|
|
45
|
+
fireEvent.click(screen.getByTestId("chip-tue"));
|
|
46
|
+
expect(onChange).toHaveBeenLastCalledWith(["mon", "tue"]);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("NativeChoiceCard, as the web's ChoiceCard", () => {
|
|
51
|
+
it("is chosen by its value in a NativeRadioGroup", () => {
|
|
52
|
+
function Plans({ onValueChange }: { onValueChange: (v: string) => void }) {
|
|
53
|
+
const [plan, setPlan] = useState("monthly");
|
|
54
|
+
return (
|
|
55
|
+
<NativeRadioGroup
|
|
56
|
+
value={plan}
|
|
57
|
+
onValueChange={(v) => {
|
|
58
|
+
setPlan(v);
|
|
59
|
+
onValueChange(v);
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
<NativeChoiceCard value="monthly" title="Monthly" testID="card-monthly" />
|
|
63
|
+
<NativeChoiceCard value="yearly" title="Yearly" testID="card-yearly" />
|
|
64
|
+
</NativeRadioGroup>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const onValueChange = vi.fn();
|
|
68
|
+
render(<Plans onValueChange={onValueChange} />);
|
|
69
|
+
expect(screen.getByTestId("card-monthly").getAttribute("aria-checked")).toBe("true");
|
|
70
|
+
fireEvent.click(screen.getByTestId("card-yearly"));
|
|
71
|
+
expect(onValueChange).toHaveBeenLastCalledWith("yearly");
|
|
72
|
+
expect(screen.getByTestId("card-yearly").getAttribute("aria-checked")).toBe("true");
|
|
73
|
+
expect(screen.getByTestId("card-yearly").getAttribute("role")).toBe("radio");
|
|
74
|
+
});
|
|
75
|
+
it("with multiple is a checkbox that reports its next state", () => {
|
|
76
|
+
const onSelectedChange = vi.fn();
|
|
77
|
+
render(<NativeChoiceCard multiple selected title="Extras" onSelectedChange={onSelectedChange} />);
|
|
78
|
+
fireEvent.click(screen.getByTestId("native-choice-card"));
|
|
79
|
+
expect(onSelectedChange).toHaveBeenLastCalledWith(false);
|
|
80
|
+
expect(screen.getByTestId("native-choice-card").getAttribute("role")).toBe("checkbox");
|
|
81
|
+
});
|
|
82
|
+
it("still takes selected with onPress", () => {
|
|
83
|
+
const onPress = vi.fn();
|
|
84
|
+
render(<NativeChoiceCard selected={false} title="Monthly" onPress={onPress} />);
|
|
85
|
+
fireEvent.click(screen.getByTestId("native-choice-card"));
|
|
86
|
+
expect(onPress).toHaveBeenCalledTimes(1);
|
|
87
|
+
});
|
|
88
|
+
it("says what's wrong when a value has no group", () => {
|
|
89
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
90
|
+
expect(() => render(<NativeChoiceCard value="x" title="Lost" />)).toThrow(/NativeRadioGroup/);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("NotificationsPage's timeZone", () => {
|
|
95
|
+
it("counts days on the zone given, as the web's does, not the device's", () => {
|
|
96
|
+
const env = (globalThis as unknown as { process: { env: Record<string, string | undefined> } }).process.env;
|
|
97
|
+
const before = env.TZ;
|
|
98
|
+
env.TZ = "UTC";
|
|
99
|
+
vi.useFakeTimers({ toFake: ["Date"], now: new Date("2026-06-11T01:00:00Z") });
|
|
100
|
+
try {
|
|
101
|
+
// 14:00 UTC on 10 June: yesterday on a UTC device, but today in Pago Pago (UTC-11), where it is still 10 June.
|
|
102
|
+
const items = [{ id: "1", title: "Booked", timestamp: "2026-06-10T14:00:00Z" }];
|
|
103
|
+
const { unmount } = render(<NotificationsPage items={items} />);
|
|
104
|
+
expect(screen.getByText("Yesterday")).toBeTruthy();
|
|
105
|
+
unmount();
|
|
106
|
+
render(<NotificationsPage items={items} timeZone="Pacific/Pago_Pago" />);
|
|
107
|
+
expect(screen.getByText("Today")).toBeTruthy();
|
|
108
|
+
} finally {
|
|
109
|
+
vi.useRealTimers();
|
|
110
|
+
if (before === undefined) delete env.TZ;
|
|
111
|
+
else env.TZ = before;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
package/src/components/badge.tsx
CHANGED
|
@@ -20,6 +20,8 @@ const badgeVariants = cva(
|
|
|
20
20
|
warning: "border-transparent bg-warning/15",
|
|
21
21
|
info: "border-transparent bg-info/15",
|
|
22
22
|
muted: "border-transparent bg-muted",
|
|
23
|
+
// The tinted red (StatusBadge's offTrack / blocked, a "danger" chip); `destructive` is the solid fill.
|
|
24
|
+
danger: "border-transparent bg-destructive/15 dark:bg-destructive/20",
|
|
23
25
|
},
|
|
24
26
|
},
|
|
25
27
|
defaultVariants: {
|
|
@@ -49,6 +51,7 @@ const badgeTextVariants = cva("text-xs font-medium", {
|
|
|
49
51
|
warning: "text-amber-800 dark:text-amber-200",
|
|
50
52
|
info: "text-blue-700 dark:text-blue-300",
|
|
51
53
|
muted: "text-muted-foreground",
|
|
54
|
+
danger: "text-red-700 dark:text-red-300",
|
|
52
55
|
},
|
|
53
56
|
},
|
|
54
57
|
defaultVariants: {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ButtonGroup — NativeButtons joined into one control: the twin of genesis-ui's ButtonGroup.
|
|
3
|
+
*
|
|
4
|
+
* Pass NativeButtons as children: the group squares the inner corners and draws them edge to
|
|
5
|
+
* edge, across (`orientation="horizontal"`, the default) or down. For a choice of one value use
|
|
6
|
+
* NativeSegmentedControl; a button group is separate actions that belong together ("Previous",
|
|
7
|
+
* "Next").
|
|
8
|
+
*
|
|
9
|
+
* @stability Beta
|
|
10
|
+
*/
|
|
11
|
+
import { Children, cloneElement, isValidElement, type ReactElement, type ReactNode } from "react";
|
|
12
|
+
import { View } from "react-native";
|
|
13
|
+
import { cn } from "../utils";
|
|
14
|
+
|
|
15
|
+
export interface NativeButtonGroupProps {
|
|
16
|
+
orientation?: "horizontal" | "vertical";
|
|
17
|
+
/** NativeButtons (or anything taking `className`). */
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
/** What the group is for, read by screen readers ("Pagination"). */
|
|
20
|
+
accessibilityLabel?: string;
|
|
21
|
+
className?: string;
|
|
22
|
+
testID?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const CORNERS = {
|
|
26
|
+
horizontal: { first: "rounded-r-none", last: "rounded-l-none", middle: "rounded-none", join: "-ml-px" },
|
|
27
|
+
vertical: { first: "rounded-b-none", last: "rounded-t-none", middle: "rounded-none", join: "-mt-px" },
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
export function NativeButtonGroup({ orientation = "horizontal", children, accessibilityLabel, className, testID }: NativeButtonGroupProps) {
|
|
31
|
+
const items = Children.toArray(children).filter(isValidElement) as ReactElement<{ className?: string }>[];
|
|
32
|
+
const c = CORNERS[orientation];
|
|
33
|
+
return (
|
|
34
|
+
<View
|
|
35
|
+
testID={testID ?? "native-button-group"}
|
|
36
|
+
accessibilityRole="toolbar"
|
|
37
|
+
accessibilityLabel={accessibilityLabel}
|
|
38
|
+
className={cn(orientation === "horizontal" ? "flex-row self-start" : "flex-col self-start", className)}
|
|
39
|
+
>
|
|
40
|
+
{items.map((child, i) => {
|
|
41
|
+
if (items.length === 1) return child;
|
|
42
|
+
const first = i === 0;
|
|
43
|
+
const last = i === items.length - 1;
|
|
44
|
+
return cloneElement(child, {
|
|
45
|
+
className: cn(child.props.className, first ? c.first : last ? c.last : c.middle, !first && c.join),
|
|
46
|
+
});
|
|
47
|
+
})}
|
|
48
|
+
</View>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calendar — a month grid to pick a day, several days or a range: the twin of genesis-ui's
|
|
3
|
+
* Calendar (react-day-picker on the web), with the same `mode`, `selected` and `onSelect`.
|
|
4
|
+
*
|
|
5
|
+
* Its weeks come from genesis-core (`monthWeeks`, on calendar days), the same weeks a view's
|
|
6
|
+
* calendar mode lays out. For one date in a form field, NativeDatePicker (the system picker) is
|
|
7
|
+
* usually the better fit; use the grid when the month itself is the point (booking, a streak).
|
|
8
|
+
*
|
|
9
|
+
* Every day is a button a screen reader names in full ("Wednesday, 3 June 2026"), with its
|
|
10
|
+
* selected and disabled state; the previous and next month buttons are named too.
|
|
11
|
+
*
|
|
12
|
+
* @stability Beta
|
|
13
|
+
*/
|
|
14
|
+
import { useMemo, useState } from "react";
|
|
15
|
+
import { Pressable, Text, View } from "react-native";
|
|
16
|
+
import { addMonths, startOfMonth, type Day } from "@marktiderman/genesis-core/dates";
|
|
17
|
+
import { dateOfDay, dayInRange, dayOfDate, monthWeeks, pickRangeDay, weekdayDays } from "@marktiderman/genesis-core/headless";
|
|
18
|
+
import { cn } from "../utils";
|
|
19
|
+
|
|
20
|
+
/** A picked range, as react-day-picker's `DateRange`. */
|
|
21
|
+
export interface NativeCalendarRange {
|
|
22
|
+
from: Date | undefined;
|
|
23
|
+
to?: Date | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface BaseProps {
|
|
27
|
+
/** The month shown, to control it. */
|
|
28
|
+
month?: Date;
|
|
29
|
+
/** The month shown first, uncontrolled. Default the selection's, else today's. */
|
|
30
|
+
defaultMonth?: Date;
|
|
31
|
+
onMonthChange?: (month: Date) => void;
|
|
32
|
+
/** Days that can't be picked. */
|
|
33
|
+
disabled?: (date: Date) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* 0 Sunday to 6 Saturday. Default 0, as the web's Calendar (react-day-picker) draws an en-US
|
|
36
|
+
* month; genesis-core's own default is 1, so it is passed to `monthWeeks` and `weekdayDays`.
|
|
37
|
+
*/
|
|
38
|
+
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
39
|
+
/** Draw the days of the months before and after. Default true. */
|
|
40
|
+
showOutsideDays?: boolean;
|
|
41
|
+
/** BCP 47 locale for the month and weekday names. Default the device's. */
|
|
42
|
+
locale?: string;
|
|
43
|
+
/** Labels for the month buttons. */
|
|
44
|
+
labels?: { previousMonth?: string; nextMonth?: string };
|
|
45
|
+
/** Today, for its mark. Default now. */
|
|
46
|
+
today?: Date;
|
|
47
|
+
className?: string;
|
|
48
|
+
testID?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type NativeCalendarProps =
|
|
52
|
+
| (BaseProps & { mode?: "single"; selected?: Date | undefined; onSelect?: (date: Date | undefined) => void })
|
|
53
|
+
| (BaseProps & { mode: "multiple"; selected?: Date[] | undefined; onSelect?: (dates: Date[]) => void })
|
|
54
|
+
| (BaseProps & { mode: "range"; selected?: NativeCalendarRange | undefined; onSelect?: (range: NativeCalendarRange | undefined) => void });
|
|
55
|
+
|
|
56
|
+
function firstSelected(props: NativeCalendarProps): Date | undefined {
|
|
57
|
+
if (props.mode === "multiple") return props.selected?.[0];
|
|
58
|
+
if (props.mode === "range") return props.selected?.from;
|
|
59
|
+
return props.selected;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function NativeCalendar(props: NativeCalendarProps) {
|
|
63
|
+
const { month: monthProp, defaultMonth, onMonthChange, disabled, weekStartsOn = 0, showOutsideDays = true, locale, labels = {}, today = new Date(), className, testID = "native-calendar" } = props;
|
|
64
|
+
const [monthState, setMonthState] = useState<Day>(() => startOfMonth(dayOfDate(defaultMonth ?? firstSelected(props) ?? today)));
|
|
65
|
+
const month = monthProp ? startOfMonth(dayOfDate(monthProp)) : monthState;
|
|
66
|
+
const weeks = useMemo(() => monthWeeks(month, weekStartsOn), [month, weekStartsOn]);
|
|
67
|
+
const todayDay = dayOfDate(today);
|
|
68
|
+
const monthKey = month.slice(0, 7);
|
|
69
|
+
|
|
70
|
+
const fmt = (options: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(locale, options);
|
|
71
|
+
const title = fmt({ month: "long", year: "numeric" }).format(dateOfDay(month));
|
|
72
|
+
const weekdayShort = fmt({ weekday: "short" });
|
|
73
|
+
const weekdayLong = fmt({ weekday: "long" });
|
|
74
|
+
const fullDate = fmt({ weekday: "long", day: "numeric", month: "long", year: "numeric" });
|
|
75
|
+
|
|
76
|
+
const go = (delta: number) => {
|
|
77
|
+
const next = addMonths(month, delta);
|
|
78
|
+
if (!monthProp) setMonthState(next);
|
|
79
|
+
onMonthChange?.(dateOfDay(next));
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const isSelected = (day: Day): boolean => {
|
|
83
|
+
if (props.mode === "multiple") return (props.selected ?? []).some((d) => dayOfDate(d) === day);
|
|
84
|
+
if (props.mode === "range") {
|
|
85
|
+
const r = props.selected;
|
|
86
|
+
return dayInRange(day, { from: r?.from ? dayOfDate(r.from) : undefined, to: r?.to ? dayOfDate(r.to) : undefined });
|
|
87
|
+
}
|
|
88
|
+
return props.selected ? dayOfDate(props.selected) === day : false;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const pick = (day: Day) => {
|
|
92
|
+
const date = dateOfDay(day);
|
|
93
|
+
if (props.mode === "multiple") {
|
|
94
|
+
const current = props.selected ?? [];
|
|
95
|
+
const has = current.some((d) => dayOfDate(d) === day);
|
|
96
|
+
props.onSelect?.(has ? current.filter((d) => dayOfDate(d) !== day) : [...current, date]);
|
|
97
|
+
} else if (props.mode === "range") {
|
|
98
|
+
const r = props.selected;
|
|
99
|
+
const next = pickRangeDay(r?.from ? { from: dayOfDate(r.from), to: r.to ? dayOfDate(r.to) : undefined } : undefined, day);
|
|
100
|
+
props.onSelect?.({ from: dateOfDay(next.from), to: next.to ? dateOfDay(next.to) : undefined });
|
|
101
|
+
} else {
|
|
102
|
+
const same = props.selected && dayOfDate(props.selected) === day;
|
|
103
|
+
props.onSelect?.(same ? undefined : date);
|
|
104
|
+
}
|
|
105
|
+
if (day.slice(0, 7) !== monthKey) go(day < month ? -1 : 1);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<View testID={testID} className={cn("w-full gap-2 bg-background p-3", className)}>
|
|
110
|
+
<View className="flex-row items-center justify-between">
|
|
111
|
+
<MonthButton testID={`${testID}-previous`} label={labels.previousMonth ?? "Previous month"} glyph="‹" onPress={() => go(-1)} />
|
|
112
|
+
<Text testID={`${testID}-title`} accessibilityRole="header" accessibilityLiveRegion="polite" className="text-sm font-medium text-foreground">
|
|
113
|
+
{title}
|
|
114
|
+
</Text>
|
|
115
|
+
<MonthButton testID={`${testID}-next`} label={labels.nextMonth ?? "Next month"} glyph="›" onPress={() => go(1)} />
|
|
116
|
+
</View>
|
|
117
|
+
<View className="flex-row">
|
|
118
|
+
{weekdayDays(weekStartsOn).map((d) => (
|
|
119
|
+
<Text
|
|
120
|
+
key={d}
|
|
121
|
+
accessibilityLabel={weekdayLong.format(dateOfDay(d))}
|
|
122
|
+
className="flex-1 text-center text-xs text-muted-foreground"
|
|
123
|
+
>
|
|
124
|
+
{weekdayShort.format(dateOfDay(d))}
|
|
125
|
+
</Text>
|
|
126
|
+
))}
|
|
127
|
+
</View>
|
|
128
|
+
{weeks.map((week) => (
|
|
129
|
+
<View key={week[0]} className="flex-row">
|
|
130
|
+
{week.map((day) => {
|
|
131
|
+
const outside = day.slice(0, 7) !== monthKey;
|
|
132
|
+
if (outside && !showOutsideDays) return <View key={day} className="h-10 flex-1" />;
|
|
133
|
+
const off = disabled?.(dateOfDay(day)) ?? false;
|
|
134
|
+
const selected = isSelected(day);
|
|
135
|
+
const isToday = day === todayDay;
|
|
136
|
+
return (
|
|
137
|
+
<Pressable
|
|
138
|
+
key={day}
|
|
139
|
+
testID={`${testID}-day-${day}`}
|
|
140
|
+
accessibilityRole="button"
|
|
141
|
+
accessibilityLabel={fullDate.format(dateOfDay(day))}
|
|
142
|
+
accessibilityState={{ selected, disabled: off }}
|
|
143
|
+
disabled={off}
|
|
144
|
+
onPress={() => pick(day)}
|
|
145
|
+
className={cn(
|
|
146
|
+
"m-0.5 h-10 flex-1 items-center justify-center rounded-md",
|
|
147
|
+
selected && "bg-primary",
|
|
148
|
+
!selected && isToday && "bg-accent",
|
|
149
|
+
off && "opacity-40"
|
|
150
|
+
)}
|
|
151
|
+
>
|
|
152
|
+
<Text
|
|
153
|
+
className={cn(
|
|
154
|
+
"text-sm",
|
|
155
|
+
selected ? "text-primary-foreground" : outside ? "text-muted-foreground" : "text-foreground",
|
|
156
|
+
isToday && "font-semibold"
|
|
157
|
+
)}
|
|
158
|
+
>
|
|
159
|
+
{Number(day.slice(8))}
|
|
160
|
+
</Text>
|
|
161
|
+
</Pressable>
|
|
162
|
+
);
|
|
163
|
+
})}
|
|
164
|
+
</View>
|
|
165
|
+
))}
|
|
166
|
+
</View>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function MonthButton({ label, glyph, onPress, testID }: { label: string; glyph: string; onPress: () => void; testID: string }) {
|
|
171
|
+
return (
|
|
172
|
+
<Pressable
|
|
173
|
+
testID={testID}
|
|
174
|
+
accessibilityRole="button"
|
|
175
|
+
accessibilityLabel={label}
|
|
176
|
+
onPress={onPress}
|
|
177
|
+
hitSlop={6}
|
|
178
|
+
className="h-11 w-11 items-center justify-center rounded-md"
|
|
179
|
+
>
|
|
180
|
+
<Text className="text-xl text-foreground">{glyph}</Text>
|
|
181
|
+
</Pressable>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Carousel — slides a person swipes through, one page at a time: the twin of genesis-ui's
|
|
3
|
+
* Carousel (Embla on the web). Each child is a slide, as full width as the carousel.
|
|
4
|
+
*
|
|
5
|
+
* Swiping pages natively (a paging ScrollView); the dots and the previous and next buttons move
|
|
6
|
+
* it too. For a screen reader, each part is its own element, as on iOS: the slide shown (its
|
|
7
|
+
* buttons and text reachable one by one), the previous and next buttons, and the dots as one page
|
|
8
|
+
* control ("Featured, Slide 2 of 5", swipe up or down to move, like UIPageControl). With neither
|
|
9
|
+
* dots nor buttons, every slide stays reachable and the pages follow the screen reader's focus.
|
|
10
|
+
* Which slide a scroll rests on comes from genesis-core's `carouselIndex`.
|
|
11
|
+
*
|
|
12
|
+
* For photos with zoom and a full-screen view, use NativeLightbox; a carousel is for cards,
|
|
13
|
+
* onboarding pages and featured content.
|
|
14
|
+
*
|
|
15
|
+
* @stability Beta
|
|
16
|
+
*/
|
|
17
|
+
import { Children, useEffect, useRef, useState, type ReactNode } from "react";
|
|
18
|
+
import { Pressable, ScrollView, Text, View } from "react-native";
|
|
19
|
+
import { carouselIndex, carouselOffset } from "@marktiderman/genesis-core/headless";
|
|
20
|
+
import { cn } from "../utils";
|
|
21
|
+
|
|
22
|
+
export interface NativeCarouselProps {
|
|
23
|
+
/** The slides. */
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
/** The slide shown, to control it. */
|
|
26
|
+
index?: number;
|
|
27
|
+
/** The slide shown first, uncontrolled. Default 0. */
|
|
28
|
+
defaultIndex?: number;
|
|
29
|
+
onIndexChange?: (index: number) => void;
|
|
30
|
+
/** Dots under the slides. Default true. */
|
|
31
|
+
showDots?: boolean;
|
|
32
|
+
/** Previous and next buttons. Default false (a swipe is the phone's way). */
|
|
33
|
+
showControls?: boolean;
|
|
34
|
+
/** What the carousel shows, read by screen readers with the page control ("Featured classes"). */
|
|
35
|
+
accessibilityLabel?: string;
|
|
36
|
+
/** Words: "Slide {index} of {count}", and the buttons'. */
|
|
37
|
+
labels?: { slide?: string; previous?: string; next?: string };
|
|
38
|
+
className?: string;
|
|
39
|
+
slideClassName?: string;
|
|
40
|
+
testID?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function NativeCarousel({
|
|
44
|
+
children,
|
|
45
|
+
index: indexProp,
|
|
46
|
+
defaultIndex = 0,
|
|
47
|
+
onIndexChange,
|
|
48
|
+
showDots = true,
|
|
49
|
+
showControls = false,
|
|
50
|
+
accessibilityLabel,
|
|
51
|
+
labels = {},
|
|
52
|
+
className,
|
|
53
|
+
slideClassName,
|
|
54
|
+
testID = "native-carousel",
|
|
55
|
+
}: NativeCarouselProps) {
|
|
56
|
+
const slides = Children.toArray(children);
|
|
57
|
+
const count = slides.length;
|
|
58
|
+
const scroll = useRef<{ scrollTo?: (to: { x?: number; y?: number; animated?: boolean }) => void } | null>(null);
|
|
59
|
+
const [width, setWidth] = useState(0);
|
|
60
|
+
const [indexState, setIndexState] = useState(defaultIndex);
|
|
61
|
+
const index = Math.max(0, Math.min(count - 1, indexProp ?? indexState));
|
|
62
|
+
const slideWords = (i: number) => (labels.slide ?? "Slide {index} of {count}").replace("{index}", String(i + 1)).replace("{count}", String(count));
|
|
63
|
+
|
|
64
|
+
const settle = (next: number) => {
|
|
65
|
+
if (next === index) return;
|
|
66
|
+
if (indexProp === undefined) setIndexState(next);
|
|
67
|
+
onIndexChange?.(next);
|
|
68
|
+
};
|
|
69
|
+
// Where the slides were last sent (or came to rest), so the effect below doesn't redo a move:
|
|
70
|
+
// jumping there again without animation would cut short the slide a dot or arrow started.
|
|
71
|
+
const scrolledTo = useRef<number | null>(null);
|
|
72
|
+
const scrollTo = (x: number, animated: boolean) => {
|
|
73
|
+
scrolledTo.current = x;
|
|
74
|
+
scroll.current?.scrollTo?.({ x, animated });
|
|
75
|
+
};
|
|
76
|
+
const goTo = (next: number) => {
|
|
77
|
+
const target = Math.max(0, Math.min(count - 1, next));
|
|
78
|
+
scrollTo(carouselOffset(target, width), true);
|
|
79
|
+
settle(target);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// A controlled index that changes from outside (or a new width) scrolls the slides there.
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (width <= 0) return;
|
|
85
|
+
const x = carouselOffset(index, width);
|
|
86
|
+
if (scrolledTo.current !== x) scrollTo(x, false);
|
|
87
|
+
}, [index, width]);
|
|
88
|
+
|
|
89
|
+
// The slides off screen are hidden from screen readers only where something else moves the pages.
|
|
90
|
+
const navigable = showDots || showControls;
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<View
|
|
94
|
+
testID={testID}
|
|
95
|
+
className={cn("w-full", className)}
|
|
96
|
+
onLayout={(e: { nativeEvent?: { layout?: { width?: number } } }) => setWidth(e.nativeEvent?.layout?.width ?? 0)}
|
|
97
|
+
>
|
|
98
|
+
<ScrollView
|
|
99
|
+
ref={scroll}
|
|
100
|
+
testID={`${testID}-pages`}
|
|
101
|
+
horizontal
|
|
102
|
+
pagingEnabled
|
|
103
|
+
showsHorizontalScrollIndicator={false}
|
|
104
|
+
onMomentumScrollEnd={(e: { nativeEvent: { contentOffset: { x: number } } }) => {
|
|
105
|
+
scrolledTo.current = e.nativeEvent.contentOffset.x;
|
|
106
|
+
settle(carouselIndex(e.nativeEvent.contentOffset.x, width, count));
|
|
107
|
+
}}
|
|
108
|
+
>
|
|
109
|
+
{slides.map((slide, i) => (
|
|
110
|
+
<View
|
|
111
|
+
key={i}
|
|
112
|
+
testID={`${testID}-slide-${i}`}
|
|
113
|
+
style={width ? { width } : undefined}
|
|
114
|
+
className={cn("w-full", slideClassName)}
|
|
115
|
+
importantForAccessibility={!navigable || i === index ? "auto" : "no-hide-descendants"}
|
|
116
|
+
accessibilityElementsHidden={navigable && i !== index}
|
|
117
|
+
>
|
|
118
|
+
{slide}
|
|
119
|
+
</View>
|
|
120
|
+
))}
|
|
121
|
+
</ScrollView>
|
|
122
|
+
{showDots || showControls ? (
|
|
123
|
+
<View className="mt-3 flex-row items-center justify-center gap-3">
|
|
124
|
+
{showControls ? (
|
|
125
|
+
<ArrowButton testID={`${testID}-previous`} label={labels.previous ?? "Previous slide"} glyph="‹" disabled={index === 0} onPress={() => goTo(index - 1)} />
|
|
126
|
+
) : null}
|
|
127
|
+
{showDots ? (
|
|
128
|
+
// The page control: one adjustable element, as UIPageControl is; the dots are for touch.
|
|
129
|
+
<View
|
|
130
|
+
testID={`${testID}-dots`}
|
|
131
|
+
className="flex-row items-center gap-1.5"
|
|
132
|
+
accessible
|
|
133
|
+
accessibilityRole="adjustable"
|
|
134
|
+
accessibilityLabel={accessibilityLabel}
|
|
135
|
+
accessibilityValue={{ text: slideWords(index) }}
|
|
136
|
+
accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
|
|
137
|
+
onAccessibilityAction={(e: { nativeEvent: { actionName: string } }) => goTo(index + (e.nativeEvent.actionName === "increment" ? 1 : -1))}
|
|
138
|
+
>
|
|
139
|
+
{slides.map((_, i) => (
|
|
140
|
+
<Pressable
|
|
141
|
+
key={i}
|
|
142
|
+
testID={`${testID}-dot-${i}`}
|
|
143
|
+
accessible={false}
|
|
144
|
+
accessibilityLabel={slideWords(i)}
|
|
145
|
+
hitSlop={8}
|
|
146
|
+
onPress={() => goTo(i)}
|
|
147
|
+
className={cn("h-2 rounded-full", i === index ? "w-5 bg-primary" : "w-2 bg-muted-foreground/40")}
|
|
148
|
+
/>
|
|
149
|
+
))}
|
|
150
|
+
</View>
|
|
151
|
+
) : null}
|
|
152
|
+
{showControls ? (
|
|
153
|
+
<ArrowButton testID={`${testID}-next`} label={labels.next ?? "Next slide"} glyph="›" disabled={index >= count - 1} onPress={() => goTo(index + 1)} />
|
|
154
|
+
) : null}
|
|
155
|
+
</View>
|
|
156
|
+
) : null}
|
|
157
|
+
</View>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function ArrowButton({ label, glyph, disabled, onPress, testID }: { label: string; glyph: string; disabled: boolean; onPress: () => void; testID: string }) {
|
|
162
|
+
return (
|
|
163
|
+
<Pressable
|
|
164
|
+
testID={testID}
|
|
165
|
+
accessibilityRole="button"
|
|
166
|
+
accessibilityLabel={label}
|
|
167
|
+
accessibilityState={{ disabled }}
|
|
168
|
+
disabled={disabled}
|
|
169
|
+
onPress={onPress}
|
|
170
|
+
className={cn("h-9 w-9 items-center justify-center rounded-full border border-border bg-background", disabled && "opacity-40")}
|
|
171
|
+
>
|
|
172
|
+
<Text className="text-lg text-foreground">{glyph}</Text>
|
|
173
|
+
</Pressable>
|
|
174
|
+
);
|
|
175
|
+
}
|