@ecohouse/ui 0.1.4 → 0.1.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecohouse/ui",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "Cross-platform presentation-only UI components for EcoHouse (React Native + React Native Web)",
@@ -6,7 +6,6 @@ import {
6
6
  useRef,
7
7
  useState,
8
8
  type ComponentRef,
9
- type ReactNode,
10
9
  } from "react";
11
10
  import {
12
11
  Image,
@@ -24,20 +23,17 @@ import { ChevronDownIcon, UserIcon } from "../../icons";
24
23
  import { avatarTypography, colors } from "../../theme";
25
24
  import { mergeRefs } from "../../utils/mergeRefs";
26
25
  import { useApplyWebClassName } from "../../utils/useApplyWebClassName";
26
+ import { MenuItem, type MenuItemProps } from "../../primitives";
27
27
 
28
- export interface AvatarMenuItem {
29
- /** Stable identity for React keys / hover tracking; falls back to `label`. */
28
+ /** Data shape for an Avatar dropdown option — same fields as `MenuItem`. */
29
+ export type AvatarMenuItem = Pick<
30
+ MenuItemProps,
31
+ "icon" | "label" | "disabled" | "backgroundColor" | "hoverColor"
32
+ > & {
33
+ /** Stable identity for React keys; falls back to `label`. */
30
34
  key?: string;
31
- /** Leading icon, e.g. `<CalendarIcon />` — pass it pre-colored. */
32
- icon?: ReactNode;
33
- label: string;
34
35
  onPress: () => void;
35
- disabled?: boolean;
36
- /** Row background at rest. Defaults to transparent. */
37
- backgroundColor?: string;
38
- /** Row background on hover/press. Defaults to `colors.grey600`. */
39
- hoverColor?: string;
40
- }
36
+ };
41
37
 
42
38
  export interface AvatarProps extends Omit<
43
39
  PressableProps,
@@ -58,7 +54,7 @@ export interface AvatarProps extends Omit<
58
54
  /**
59
55
  * Account-menu options, supplied entirely by the consumer (icon, label,
60
56
  * action, and per-item colors). When set, pressing the chip opens a
61
- * dropdown listing them.
57
+ * dropdown listing them via `MenuItem`.
62
58
  */
63
59
  menuItems?: AvatarMenuItem[];
64
60
  /** Controlled open state for the menu. */
@@ -77,8 +73,6 @@ const CONTAINER_RADIUS = 71;
77
73
  const CHEVRON_SIZE = 20;
78
74
  const FALLBACK_ICON_SIZE = 20;
79
75
  const MENU_RADIUS = 16;
80
- const MENU_ITEM_RADIUS = 12;
81
- const MENU_ITEM_ICON_SIZE = 16;
82
76
 
83
77
  export const Avatar = forwardRef<ComponentRef<typeof View>, AvatarProps>(function Avatar(
84
78
  {
@@ -105,9 +99,9 @@ export const Avatar = forwardRef<ComponentRef<typeof View>, AvatarProps>(functio
105
99
  const containerRef = useRef<ComponentRef<typeof View>>(null);
106
100
  const setContainerRef = useMemo(() => mergeRefs(containerRef, forwardedRef), [forwardedRef]);
107
101
  const [imageFailed, setImageFailed] = useState(false);
108
- const [hoveredKey, setHoveredKey] = useState<string | null>(null);
109
102
 
110
- const hasMenu = Boolean(menuItems && menuItems.length > 0);
103
+ const resolvedMenuItems = menuItems ?? [];
104
+ const hasMenu = resolvedMenuItems.length > 0;
111
105
  const isInteractive = onPress != null || hasMenu;
112
106
  const isOpenControlled = typeof openProp === "boolean";
113
107
  const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);
@@ -119,7 +113,6 @@ export const Avatar = forwardRef<ComponentRef<typeof View>, AvatarProps>(functio
119
113
  (next: boolean) => {
120
114
  if (!isOpenControlled) setUncontrolledOpen(next);
121
115
  onOpenChange?.(next);
122
- if (!next) setHoveredKey(null);
123
116
  },
124
117
  [isOpenControlled, onOpenChange],
125
118
  );
@@ -237,39 +230,18 @@ export const Avatar = forwardRef<ComponentRef<typeof View>, AvatarProps>(functio
237
230
 
238
231
  {hasMenu && isOpen ? (
239
232
  <View style={[styles.menu, menuWidth != null && { right: undefined, width: menuWidth }]}>
240
- {menuItems!.map((item) => {
233
+ {resolvedMenuItems.map((item) => {
241
234
  const itemKey = item.key ?? item.label;
242
- const isHovered = hoveredKey === itemKey;
243
- const backgroundColor = isHovered
244
- ? (item.hoverColor ?? colors.grey600)
245
- : (item.backgroundColor ?? "transparent");
246
-
247
235
  return (
248
- <Pressable
236
+ <MenuItem
249
237
  key={itemKey}
250
- onPress={() => handleItemPress(item)}
238
+ icon={item.icon}
239
+ label={item.label}
251
240
  disabled={item.disabled}
252
- onHoverIn={() => setHoveredKey(itemKey)}
253
- onHoverOut={() =>
254
- setHoveredKey((current) => (current === itemKey ? null : current))
255
- }
256
- onPressIn={() => setHoveredKey(itemKey)}
257
- onPressOut={() =>
258
- setHoveredKey((current) => (current === itemKey ? null : current))
259
- }
260
- accessibilityRole="menuitem"
261
- accessibilityState={{ disabled: item.disabled }}
262
- style={[
263
- styles.menuItem,
264
- { backgroundColor },
265
- item.disabled && styles.menuItemDisabled,
266
- ]}
267
- >
268
- {item.icon ? <View style={styles.menuItemIcon}>{item.icon}</View> : null}
269
- <Text style={styles.menuItemLabel} numberOfLines={1}>
270
- {item.label}
271
- </Text>
272
- </Pressable>
241
+ backgroundColor={item.backgroundColor}
242
+ hoverColor={item.hoverColor}
243
+ onPress={() => handleItemPress(item)}
244
+ />
273
245
  );
274
246
  })}
275
247
  </View>
@@ -362,28 +334,4 @@ const styles = StyleSheet.create({
362
334
  },
363
335
  }),
364
336
  },
365
- menuItem: {
366
- minHeight: 40,
367
- borderRadius: MENU_ITEM_RADIUS,
368
- paddingVertical: 12,
369
- paddingHorizontal: 12,
370
- flexDirection: "row",
371
- alignItems: "center",
372
- gap: 8,
373
- },
374
- menuItemDisabled: {
375
- opacity: 0.5,
376
- },
377
- menuItemIcon: {
378
- width: MENU_ITEM_ICON_SIZE,
379
- height: MENU_ITEM_ICON_SIZE,
380
- alignItems: "center",
381
- justifyContent: "center",
382
- flexShrink: 0,
383
- },
384
- menuItemLabel: {
385
- ...avatarTypography.menuItem,
386
- color: colors.white,
387
- flex: 1,
388
- },
389
337
  });
@@ -0,0 +1,172 @@
1
+ import { useState } from "react";
2
+ import type { Meta, StoryObj } from "@storybook/react-vite";
3
+ import { fn } from "storybook/test";
4
+ import { View, StyleSheet, Text } from "react-native";
5
+ import { DatePicker, type DateRange } from "./DatePicker";
6
+ import { colors } from "../../theme";
7
+
8
+ const meta = {
9
+ title: "Components/DatePicker",
10
+ component: DatePicker,
11
+ args: {
12
+ placeholder: "Select date",
13
+ defaultOpen: false,
14
+ disabled: false,
15
+ range: false,
16
+ onValueChange: fn(),
17
+ onOpenChange: fn(),
18
+ },
19
+ argTypes: {
20
+ range: { control: "boolean" },
21
+ onValueChange: { action: "valueChange" },
22
+ onOpenChange: { action: "openChange" },
23
+ },
24
+ parameters: {
25
+ backgrounds: { default: "black" },
26
+ },
27
+ } satisfies Meta<typeof DatePicker>;
28
+
29
+ export default meta;
30
+ type Story = StoryObj<typeof meta>;
31
+
32
+ export const Default: Story = {};
33
+
34
+ export const Open: Story = {
35
+ args: { defaultOpen: true },
36
+ };
37
+
38
+ export const WithValue: Story = {
39
+ args: {
40
+ defaultValue: new Date(2026, 8, 15),
41
+ defaultOpen: true,
42
+ },
43
+ };
44
+
45
+ export const Range: Story = {
46
+ args: {
47
+ range: true,
48
+ placeholder: "Select dates",
49
+ defaultValue: { start: new Date(2026, 8, 15), end: new Date(2026, 8, 24) },
50
+ defaultOpen: true,
51
+ },
52
+ };
53
+
54
+ export const RangeInProgress: Story = {
55
+ args: {
56
+ range: true,
57
+ placeholder: "Select dates",
58
+ defaultValue: { start: new Date(2026, 8, 15), end: null },
59
+ defaultOpen: true,
60
+ },
61
+ };
62
+
63
+ export const MinMaxDate: Story = {
64
+ args: {
65
+ defaultOpen: true,
66
+ minDate: new Date(2026, 8, 10),
67
+ maxDate: new Date(2026, 8, 20),
68
+ defaultVisibleMonth: new Date(2026, 8, 1),
69
+ },
70
+ };
71
+
72
+ export const KeepOpenOnSelect: Story = {
73
+ args: {
74
+ defaultOpen: true,
75
+ closeOnSelect: false,
76
+ },
77
+ };
78
+
79
+ export const Disabled: Story = {
80
+ args: { disabled: true, defaultValue: new Date(2026, 8, 15) },
81
+ };
82
+
83
+ /** Verifies that a disabled DatePicker stays non-interactive even when forced open. */
84
+ export const DisabledOpen: Story = {
85
+ args: {
86
+ disabled: true,
87
+ defaultOpen: true,
88
+ defaultValue: new Date(2026, 8, 15),
89
+ },
90
+ };
91
+
92
+ function ControlledDatePicker() {
93
+ const [value, setValue] = useState<Date | null>(null);
94
+ const [open, setOpen] = useState(false);
95
+
96
+ return (
97
+ <View style={storyStyles.column}>
98
+ <DatePicker
99
+ placeholder="Select date"
100
+ value={value}
101
+ onValueChange={setValue}
102
+ open={open}
103
+ onOpenChange={setOpen}
104
+ />
105
+ <Text style={storyStyles.meta}>
106
+ value: {value ? value.toDateString() : "—"} · open: {String(open)}
107
+ </Text>
108
+ </View>
109
+ );
110
+ }
111
+
112
+ function ControlledRangeDatePicker() {
113
+ const [value, setValue] = useState<DateRange>({ start: null, end: null });
114
+ const [open, setOpen] = useState(true);
115
+
116
+ return (
117
+ <View style={storyStyles.column}>
118
+ <DatePicker
119
+ range
120
+ placeholder="Select dates"
121
+ value={value}
122
+ onValueChange={setValue}
123
+ open={open}
124
+ onOpenChange={setOpen}
125
+ />
126
+ <Text style={storyStyles.meta}>
127
+ {value.start ? value.start.toDateString() : "—"} →{" "}
128
+ {value.end ? value.end.toDateString() : "—"}
129
+ </Text>
130
+ </View>
131
+ );
132
+ }
133
+
134
+ export const Controlled: Story = {
135
+ render: () => <ControlledDatePicker />,
136
+ };
137
+
138
+ export const ControlledRange: Story = {
139
+ render: () => <ControlledRangeDatePicker />,
140
+ };
141
+
142
+ export const AllStates: Story = {
143
+ render: () => (
144
+ <View style={storyStyles.row}>
145
+ <DatePicker placeholder="Select date" />
146
+ <DatePicker placeholder="Select date" defaultOpen defaultValue={new Date(2026, 8, 15)} />
147
+ <DatePicker
148
+ range
149
+ placeholder="Select dates"
150
+ defaultValue={{ start: new Date(2026, 8, 15), end: new Date(2026, 8, 24) }}
151
+ defaultOpen
152
+ />
153
+ </View>
154
+ ),
155
+ };
156
+
157
+ const storyStyles = StyleSheet.create({
158
+ column: {
159
+ gap: 12,
160
+ alignItems: "flex-start",
161
+ },
162
+ row: {
163
+ flexDirection: "row",
164
+ flexWrap: "wrap",
165
+ gap: 24,
166
+ alignItems: "flex-start",
167
+ },
168
+ meta: {
169
+ color: colors.grey100,
170
+ fontSize: 12,
171
+ },
172
+ });