react-native-gifted-chat 3.1.4 → 3.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/README.md CHANGED
@@ -210,6 +210,7 @@ Messages, system messages, quick replies etc.: [data structure](src/Models.ts)
210
210
  - **`onSend`** _(Function)_ - Callback when sending a message
211
211
  - **`messageIdGenerator`** _(Function)_ - Generate an id for new messages. Defaults to a simple random string generator.
212
212
  - **`locale`** _(String)_ - Locale to localize the dates. You need first to import the locale you need (ie. `require('dayjs/locale/de')` or `import 'dayjs/locale/fr'`)
213
+ - **`colorScheme`** _('light' | 'dark')_ - Force color scheme (light/dark mode). When set to `'light'` or `'dark'`, it overrides the system color scheme. When `undefined`, it uses the system color scheme. Default is `undefined`.
213
214
 
214
215
  ### Refs
215
216
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-gifted-chat",
3
- "version": "3.1.4",
3
+ "version": "3.1.5",
4
4
  "description": "The most complete chat UI for React Native",
5
5
  "keywords": [
6
6
  "android",
package/src/Composer.tsx CHANGED
@@ -5,11 +5,11 @@ import {
5
5
  TextInputChangeEvent,
6
6
  TextInputContentSizeChangeEvent,
7
7
  TextInputProps,
8
- useColorScheme,
9
8
  View,
10
9
  } from 'react-native'
11
10
  import { TextInput } from 'react-native-gesture-handler'
12
11
  import { Color } from './Color'
12
+ import { useColorScheme } from './hooks/useColorScheme'
13
13
  import stylesCommon, { getColorSchemeStyle } from './styles'
14
14
 
15
15
  export interface ComposerProps {
@@ -10,6 +10,7 @@ import React, {
10
10
  import {
11
11
  View,
12
12
  LayoutChangeEvent,
13
+ useColorScheme,
13
14
  } from 'react-native'
14
15
  import {
15
16
  ActionSheetProvider,
@@ -48,6 +49,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
48
49
  user = {},
49
50
  onSend,
50
51
  locale = 'en',
52
+ colorScheme: colorSchemeProp,
51
53
  renderLoading,
52
54
  actionSheet,
53
55
  textInputProps,
@@ -56,6 +58,9 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
56
58
  isInverted = true,
57
59
  } = props
58
60
 
61
+ const systemColorScheme = useColorScheme()
62
+ const colorScheme = colorSchemeProp !== undefined ? colorSchemeProp : systemColorScheme
63
+
59
64
  const actionSheetRef = useRef<ActionSheetProviderRef>(null)
60
65
 
61
66
  const messagesContainerRef = useMemo(
@@ -236,8 +241,9 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
236
241
  actionSheetRef.current!.showActionSheetWithOptions,
237
242
  })),
238
243
  getLocale: () => locale,
244
+ getColorScheme: () => colorScheme,
239
245
  }),
240
- [actionSheet, locale]
246
+ [actionSheet, locale, colorScheme]
241
247
  )
242
248
 
243
249
  useEffect(() => {
@@ -45,6 +45,8 @@ export interface GiftedChatProps<TMessage extends IMessage> extends Partial<Mess
45
45
  user?: User
46
46
  /* Locale to localize the dates */
47
47
  locale?: string
48
+ /* Force color scheme (light/dark); default is undefined (uses system color scheme) */
49
+ colorScheme?: 'light' | 'dark'
48
50
  /* Format to use for rendering times; default is 'LT' */
49
51
  timeFormat?: string
50
52
  /* Format to use for rendering dates; default is 'll' */
@@ -11,6 +11,7 @@ export interface IGiftedChatContext {
11
11
  ) => void
12
12
  }
13
13
  getLocale(): string
14
+ getColorScheme(): 'light' | 'dark' | null | undefined
14
15
  }
15
16
 
16
17
  export const GiftedChatContext = createContext<IGiftedChatContext>({
@@ -18,6 +19,7 @@ export const GiftedChatContext = createContext<IGiftedChatContext>({
18
19
  actionSheet: () => ({
19
20
  showActionSheetWithOptions: () => {},
20
21
  }),
22
+ getColorScheme: () => undefined,
21
23
  })
22
24
 
23
25
  export const useChatContext = () => useContext(GiftedChatContext)
@@ -1,9 +1,10 @@
1
1
  import React, { useMemo } from 'react'
2
- import { StyleSheet, View, StyleProp, ViewStyle, useColorScheme } from 'react-native'
2
+ import { StyleSheet, View, StyleProp, ViewStyle } from 'react-native'
3
3
 
4
4
  import { Actions, ActionsProps } from './Actions'
5
5
  import { Color } from './Color'
6
6
  import { Composer, ComposerProps } from './Composer'
7
+ import { useColorScheme } from './hooks/useColorScheme'
7
8
  import { IMessage } from './Models'
8
9
  import { Send, SendProps } from './Send'
9
10
  import { getColorSchemeStyle } from './styles'
package/src/Send.tsx CHANGED
@@ -4,7 +4,6 @@ import {
4
4
  StyleProp,
5
5
  ViewStyle,
6
6
  TextStyle,
7
- useColorScheme,
8
7
  } from 'react-native'
9
8
  import { Text } from 'react-native-gesture-handler'
10
9
  import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-native-reanimated'
@@ -12,6 +11,7 @@ import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-na
12
11
  import { Color } from './Color'
13
12
  import { TouchableOpacity, TouchableOpacityProps } from './components/TouchableOpacity'
14
13
  import { TEST_ID } from './Constant'
14
+ import { useColorScheme } from './hooks/useColorScheme'
15
15
  import { IMessage } from './Models'
16
16
  import { getColorSchemeStyle } from './styles'
17
17
 
@@ -28,3 +28,33 @@ it('should render <GiftedChat/> and compare with snapshot', () => {
28
28
 
29
29
  expect(toJSON()).toMatchSnapshot()
30
30
  })
31
+
32
+ it('should render <GiftedChat/> with light colorScheme and compare with snapshot', () => {
33
+ const { toJSON } = render(
34
+ <GiftedChat
35
+ messages={messages}
36
+ onSend={() => {}}
37
+ user={{
38
+ _id: 1,
39
+ }}
40
+ colorScheme='light'
41
+ />
42
+ )
43
+
44
+ expect(toJSON()).toMatchSnapshot()
45
+ })
46
+
47
+ it('should render <GiftedChat/> with dark colorScheme and compare with snapshot', () => {
48
+ const { toJSON } = render(
49
+ <GiftedChat
50
+ messages={messages}
51
+ onSend={() => {}}
52
+ user={{
53
+ _id: 1,
54
+ }}
55
+ colorScheme='dark'
56
+ />
57
+ )
58
+
59
+ expect(toJSON()).toMatchSnapshot()
60
+ })
@@ -46,3 +46,97 @@ exports[`should render <GiftedChat/> and compare with snapshot 1`] = `
46
46
  </KeyboardProvider>
47
47
  </View>
48
48
  `;
49
+
50
+ exports[`should render <GiftedChat/> with dark colorScheme and compare with snapshot 1`] = `
51
+ <View
52
+ style={
53
+ {
54
+ "flex": 1,
55
+ }
56
+ }
57
+ >
58
+ <KeyboardProvider
59
+ navigationBarTranslucent={true}
60
+ statusBarTranslucent={true}
61
+ >
62
+ <View
63
+ style={
64
+ {
65
+ "flex": 1,
66
+ }
67
+ }
68
+ >
69
+ <View
70
+ behavior="padding"
71
+ style={
72
+ {
73
+ "flex": 1,
74
+ }
75
+ }
76
+ >
77
+ <View
78
+ onLayout={[Function]}
79
+ style={
80
+ [
81
+ {
82
+ "flex": 1,
83
+ },
84
+ {
85
+ "overflow": "hidden",
86
+ },
87
+ ]
88
+ }
89
+ testID="GC_WRAPPER"
90
+ />
91
+ </View>
92
+ </View>
93
+ </KeyboardProvider>
94
+ </View>
95
+ `;
96
+
97
+ exports[`should render <GiftedChat/> with light colorScheme and compare with snapshot 1`] = `
98
+ <View
99
+ style={
100
+ {
101
+ "flex": 1,
102
+ }
103
+ }
104
+ >
105
+ <KeyboardProvider
106
+ navigationBarTranslucent={true}
107
+ statusBarTranslucent={true}
108
+ >
109
+ <View
110
+ style={
111
+ {
112
+ "flex": 1,
113
+ }
114
+ }
115
+ >
116
+ <View
117
+ behavior="padding"
118
+ style={
119
+ {
120
+ "flex": 1,
121
+ }
122
+ }
123
+ >
124
+ <View
125
+ onLayout={[Function]}
126
+ style={
127
+ [
128
+ {
129
+ "flex": 1,
130
+ },
131
+ {
132
+ "overflow": "hidden",
133
+ },
134
+ ]
135
+ }
136
+ testID="GC_WRAPPER"
137
+ />
138
+ </View>
139
+ </View>
140
+ </KeyboardProvider>
141
+ </View>
142
+ `;
@@ -0,0 +1,18 @@
1
+ import { useColorScheme as useRNColorScheme } from 'react-native'
2
+ import { useChatContext } from '../GiftedChatContext'
3
+
4
+ /**
5
+ * Custom hook that returns the color scheme from GiftedChat context if provided,
6
+ * otherwise falls back to the system color scheme from React Native.
7
+ *
8
+ * @returns The current color scheme ('light', 'dark', null, or undefined)
9
+ */
10
+ export function useColorScheme() {
11
+ const { getColorScheme } = useChatContext()
12
+ const contextColorScheme = getColorScheme()
13
+ const systemColorScheme = useRNColorScheme()
14
+
15
+ return contextColorScheme !== undefined && contextColorScheme !== null
16
+ ? contextColorScheme
17
+ : systemColorScheme
18
+ }
package/src/index.ts CHANGED
@@ -20,3 +20,4 @@ export { Time } from './Time'
20
20
  export { GiftedAvatar } from './GiftedAvatar'
21
21
  export { MessageAudio } from './MessageAudio'
22
22
  export { MessageVideo } from './MessageVideo'
23
+ export { useColorScheme } from './hooks/useColorScheme'