Skip to content

Commit f3b9ec8

Browse files
feat(textProps): adding ability to spread TextProps to help with accessibility (#62)
* feat(textProps): adding ability to spread TextProps to help with accessibility * feat(textProps): adding ability to spread TextProps to help with accessibility * fix(ci): lint * fix(ci): lint * fix(textProps): adding textProps vs extending * fix(textProps): adding textProps vs extending * chore(docs): update docs with a simple example to override textProps * chore(textProps): cleanup spreading textProps.style * chore(docs): update docs
1 parent 2d42ca6 commit f3b9ec8

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

.changeset/curvy-files-teach.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@marceloterreiro/flash-calendar": minor
3+
---
4+
5+
Add the ability to pass TextProps to the <Text> fields especially when supporting accessibility

apps/docs/docs/fundamentals/customization.mdx

+62
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,65 @@ The sky is the limit here. Here are two demos from the example app:
157157
</VStack>
158158

159159
</HStack>
160+
161+
### Changing the text props
162+
163+
You can override the `textProps` to control the nested [Text components](https://reactnative.dev/docs/text). For example [disabling font scaling](https://reactnative.dev/docs/text#allowfontscaling) for accessibility on the Calendar's Day:
164+
165+
```tsx
166+
import {
167+
Calendar,
168+
useOptimizedDayMetadata,
169+
} from "@marceloterreiro/flash-calendar";
170+
import { Text } from "react-native";
171+
import type { CalendarItemDayWithContainerProps } from "@/components/CalendarItemDay";
172+
173+
import { useRenderCount } from "./useRenderCount";
174+
175+
export const PerfTestCalendarItemDayWithContainer = ({
176+
children,
177+
metadata: baseMetadata,
178+
onPress,
179+
theme,
180+
dayHeight,
181+
daySpacing,
182+
containerTheme,
183+
}: CalendarItemDayWithContainerProps) => {
184+
const metadata = useOptimizedDayMetadata(baseMetadata);
185+
const renderCounter = useRenderCount();
186+
187+
return (
188+
<Calendar.Item.Day.Container
189+
dayHeight={dayHeight}
190+
daySpacing={daySpacing}
191+
isStartOfWeek={metadata.isStartOfWeek}
192+
shouldShowActiveDayFiller={
193+
metadata.isRangeValid && !metadata.isEndOfWeek
194+
? !metadata.isEndOfRange
195+
: false
196+
}
197+
theme={containerTheme}
198+
>
199+
<Calendar.Item.Day
200+
textProps={{ allowFontScaling: false }}
201+
height={dayHeight}
202+
metadata={metadata}
203+
onPress={onPress}
204+
theme={theme}
205+
>
206+
{children}
207+
<Text
208+
style={{
209+
fontSize: 8,
210+
fontStyle: "italic",
211+
textAlign: "center",
212+
color: metadata.state === "active" ? "white" : "black",
213+
}}
214+
>
215+
{"\n"}render: {renderCounter}x
216+
</Text>
217+
</Calendar.Item.Day>
218+
</Calendar.Item.Day.Container>
219+
);
220+
};
221+
```

packages/flash-calendar/src/components/CalendarItemDay.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ReactNode } from "react";
22
import { useCallback, useMemo } from "react";
3-
import type { TextStyle, ViewStyle } from "react-native";
3+
import type { TextProps, TextStyle, ViewStyle } from "react-native";
44
import { Pressable, StyleSheet, Text, View } from "react-native";
55

66
import type { BaseTheme } from "@/helpers/tokens";
@@ -142,6 +142,8 @@ export interface CalendarItemDayProps {
142142
>;
143143
/** The cell's height */
144144
height: number;
145+
/** Optional TextProps to spread to the <Text> component. */
146+
textProps?: Omit<TextProps, "children" | "onPress">;
145147
}
146148

147149
/**
@@ -158,6 +160,7 @@ export const CalendarItemDay = ({
158160
theme,
159161
height,
160162
metadata,
163+
textProps,
161164
}: CalendarItemDayProps) => {
162165
const baseTheme = useTheme();
163166
const baseStyles = useMemo(() => {
@@ -196,8 +199,10 @@ export const CalendarItemDay = ({
196199
const { content } = baseStyles[metadata.state](params);
197200
return (
198201
<Text
202+
{...textProps}
199203
style={{
200204
...content,
205+
...(textProps?.style ?? ({} as object)),
201206
...theme?.base?.({ ...metadata, isPressed }).content,
202207
...theme?.[metadata.state]?.({ ...metadata, isPressed }).content,
203208
}}

packages/flash-calendar/src/components/CalendarItemWeekName.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ReactNode } from "react";
22
import { useMemo } from "react";
3-
import type { TextStyle, ViewStyle } from "react-native";
3+
import type { TextProps, TextStyle, ViewStyle } from "react-native";
44
import { StyleSheet, Text, View } from "react-native";
55

66
import { lightTheme } from "@/helpers/tokens";
@@ -29,27 +29,39 @@ export interface CalendarItemWeekNameProps {
2929
height: number;
3030
/** The theme of the week name, useful for customizing the component. */
3131
theme?: CalendarItemWeekNameTheme;
32+
/** Optional TextProps to spread to the <Text> component. */
33+
textProps?: Omit<TextProps, "children">;
3234
}
3335

3436
export const CalendarItemWeekName = ({
3537
children,
3638
height,
3739
theme,
40+
textProps,
3841
}: CalendarItemWeekNameProps) => {
3942
const { colors } = useTheme();
4043
const { containerStyles, contentStyles } = useMemo(() => {
4144
const containerStyles = [styles.container, { height }, theme?.container];
4245
const contentStyles = [
4346
styles.content,
4447
{ color: colors.content.primary },
48+
textProps?.style,
4549
theme?.content,
4650
];
4751
return { containerStyles, contentStyles };
48-
}, [colors.content.primary, height, theme?.container, theme?.content]);
52+
}, [
53+
colors.content.primary,
54+
height,
55+
theme?.container,
56+
theme?.content,
57+
textProps?.style,
58+
]);
4959

5060
return (
5161
<View style={containerStyles}>
52-
<Text style={contentStyles}>{children}</Text>
62+
<Text {...textProps} style={contentStyles}>
63+
{children}
64+
</Text>
5365
</View>
5466
);
5567
};

0 commit comments

Comments
 (0)