Skip to content

Commit 3c3891d

Browse files
committed
Improve docs
1 parent fbc2341 commit 3c3891d

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
},
99
"[typescript]": {
1010
"editor.defaultFormatter": "esbenp.prettier-vscode"
11+
},
12+
"editor.formatOnSave": true,
13+
"editor.codeActionsOnSave": {
14+
"source.fixAll.eslint": "explicit"
1115
}
1216
}

apps/docs/docs/fundamentals/tips-and-tricks.mdx

+36
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,39 @@ user system's theme.
166166
**Note**: you should avoid using this prop. Instead, your app should
167167
support dynamic themes that react to the user's system preferences. The prop is
168168
provided as an escape hatch for apps that doesn't support dynamic themes yet.
169+
170+
## Listening to the visible months
171+
172+
You can listen to which months are currently visible by hooking into the [`onViewableItemsChange`](https://shopify.github.io/flash-list/docs/usage#onviewableitemschanged) prop:
173+
174+
```tsx
175+
export const ListenToVisibleMonth = () => {
176+
const [selectedDate, setSelectedDate] = useState(today);
177+
const [visibleMonth, setVisibleMonth] = useState(today);
178+
179+
const handleViewableItemsChanged = useCallback<
180+
NonNullable<FlashListProps<CalendarMonth>["onViewableItemsChanged"]>
181+
>(({ viewableItems }) => {
182+
const firstVisibleItem = viewableItems.find((item) => item.isViewable);
183+
184+
if (firstVisibleItem) {
185+
setVisibleMonth(firstVisibleItem.item.id);
186+
}
187+
}, []);
188+
189+
return (
190+
<View style={{ flex: 1, gap: 24 }}>
191+
<View style={{ gap: 12 }}>
192+
<Text>Selected date: {selectedDate}</Text>
193+
<Text>Visible month: {visibleMonth}</Text>
194+
</View>
195+
<Calendar.List
196+
calendarActiveDateRanges={[{ startId: today, endId: today }]}
197+
calendarInitialMonthId="2024-11-01"
198+
onCalendarDayPress={setSelectedDate}
199+
onViewableItemsChanged={handleViewableItemsChanged}
200+
/>
201+
</View>
202+
);
203+
};
204+
```

apps/example/src/components/GithubIssues/CalendarListGithubIssues.stories.tsx

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type {
2+
CalendarMonth,
23
CalendarOnDayPress,
34
CalendarTheme,
45
} from "@marceloterreiro/flash-calendar";
56
import { Calendar, toDateId } from "@marceloterreiro/flash-calendar";
7+
import type { FlashListProps } from "@shopify/flash-list";
68
import type { Meta } from "@storybook/react";
7-
import React, { useCallback, useMemo, useState } from "react";
9+
import { useCallback, useMemo, useState } from "react";
810
import { Alert, Text, View } from "react-native";
911
import { useTheme } from "@/hooks/useTheme";
12+
import { VStack } from "@/components/VStack";
1013

1114
const CalendarMeta: Meta<typeof Calendar> = {
1215
title: "Calendar.List/Github Issues",
@@ -124,3 +127,34 @@ export const Issue38 = () => {
124127
</View>
125128
);
126129
};
130+
131+
// See more at: https://github.com/MarceloPrado/flash-calendar/issues/65
132+
export const ListenToVisibleMonth = () => {
133+
const [selectedDate, setSelectedDate] = useState(today);
134+
const [visibleMonth, setVisibleMonth] = useState(today);
135+
136+
const handleViewableItemsChanged = useCallback<
137+
NonNullable<FlashListProps<CalendarMonth>["onViewableItemsChanged"]>
138+
>(({ viewableItems }) => {
139+
const firstVisibleItem = viewableItems.find((item) => item.isViewable);
140+
141+
if (firstVisibleItem) {
142+
setVisibleMonth(firstVisibleItem.item.id);
143+
}
144+
}, []);
145+
146+
return (
147+
<View style={{ flex: 1, gap: 24 }}>
148+
<View style={{ gap: 12 }}>
149+
<Text>Selected date: {selectedDate}</Text>
150+
<Text>Visible month: {visibleMonth}</Text>
151+
</View>
152+
<Calendar.List
153+
calendarActiveDateRanges={[{ startId: today, endId: today }]}
154+
calendarInitialMonthId="2024-06-01"
155+
onCalendarDayPress={setSelectedDate}
156+
onViewableItemsChanged={handleViewableItemsChanged}
157+
/>
158+
</View>
159+
);
160+
};

0 commit comments

Comments
 (0)