Skip to content

Commit 6a76be5

Browse files
Fix bugs with different colors in dark mode
1 parent 1250017 commit 6a76be5

File tree

10 files changed

+58
-58
lines changed

10 files changed

+58
-58
lines changed

example/src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,17 @@ export default function AppWithProviders() {
315315
roundness: 10,
316316
colors: {
317317
...DarkTheme.colors,
318-
primary: '#F59E00',
319-
accent: '#FBBE5E',
318+
// primary: '#F59E00',
319+
// accent: '#FBBE5E',
320320
},
321321
}
322322
: {
323323
...DefaultTheme,
324324
roundness: 10,
325325
colors: {
326326
...DefaultTheme.colors,
327-
primary: '#F59E00',
328-
accent: '#FBBE5E',
327+
// primary: '#F59E00',
328+
// accent: '#FBBE5E',
329329
},
330330
}
331331
}

example/src/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ html,
88
#root {
99
display: flex;
1010
min-height: 100vh;
11+
max-height: 100vh;
1112
width: 100%;
1213
flex: 1;
1314
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,

src/Date/DatePickerModalContentHeader.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react'
22
import { View, StyleSheet } from 'react-native'
3-
import { IconButton, Text, useTheme } from 'react-native-paper'
3+
import { IconButton, Text } from 'react-native-paper'
44
import { ModeType } from './Calendar'
55
import { LocalState } from './DatePickerModalContent'
6+
import { useHeaderTextColor } from '../utils'
67

78
export interface HeaderPickProps {
89
label?: string
@@ -20,8 +21,6 @@ export interface HeaderContentProps extends HeaderPickProps {
2021
}
2122

2223
export default function DatePickerModalHeader(props: HeaderContentProps) {
23-
const theme = useTheme()
24-
2524
const { onToggle, collapsed, mode } = props
2625

2726
const label = props.label
@@ -30,7 +29,7 @@ export default function DatePickerModalHeader(props: HeaderContentProps) {
3029
? 'Select period'
3130
: 'Select date'
3231

33-
const color = theme.dark ? '#fff' : '#000'
32+
const color = useHeaderTextColor()
3433

3534
return (
3635
<View style={[styles.header]}>

src/Date/DatePickerModalHeader.tsx

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react'
22
import { Animated, StyleSheet, SafeAreaView } from 'react-native'
3-
import { Appbar, Button, overlay, useTheme } from 'react-native-paper'
3+
import { Appbar, Button } from 'react-native-paper'
44

5-
import { useColorOnPrimaryBackground } from '../utils'
5+
import { useHeaderTextColor } from '../utils'
66

77
export interface DatePickerModalHeaderProps {
88
disableSafeTop?: boolean
@@ -14,39 +14,18 @@ export interface DatePickerModalHeaderProps {
1414
export default function DatePickerModalHeader(
1515
props: DatePickerModalHeaderProps
1616
) {
17-
const theme = useTheme()
18-
1917
const { saveLabel = 'Save', disableSafeTop } = props
20-
21-
const backgroundColor =
22-
theme.dark && theme.mode === 'adaptive'
23-
? overlay(4, theme.colors.surface)
24-
: theme.colors.primary
25-
26-
const color = useColorOnPrimaryBackground(backgroundColor)
27-
18+
const color = useHeaderTextColor()
2819
return (
2920
<>
30-
<Animated.View
31-
style={[
32-
styles.animated,
33-
{
34-
backgroundColor,
35-
},
36-
]}
37-
>
21+
<Animated.View style={styles.animated}>
3822
<SafeAreaView
3923
style={[
4024
styles.safeContent,
4125
disableSafeTop && styles.safeContentNoTop,
4226
]}
4327
>
44-
<Appbar
45-
style={[
46-
styles.appbarHeader,
47-
{ backgroundColor: backgroundColor.toString() },
48-
]}
49-
>
28+
<Appbar style={styles.appbarHeader}>
5029
<Appbar.Action
5130
icon="close"
5231
onPress={props.onDismiss}
@@ -98,6 +77,7 @@ const styles = StyleSheet.create({
9877
},
9978
appbarHeader: {
10079
elevation: 0,
80+
backgroundColor: 'transparent',
10181
// alignItems:'center'
10282
},
10383
})

src/Date/DatePickerModalHeaderBackground.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import { overlay, useTheme } from 'react-native-paper'
21
import { Animated, SafeAreaView, StyleSheet } from 'react-native'
32
import * as React from 'react'
3+
import { useHeaderBackgroundColor } from '../utils'
44

55
export default function DatePickerModalHeaderBackground({
66
children,
77
}: {
88
children: any
99
}) {
10-
const theme = useTheme()
11-
const backgroundColor =
12-
theme.dark && theme.mode === 'adaptive'
13-
? overlay(4, theme.colors.surface)
14-
: theme.colors.primary
10+
const backgroundColor = useHeaderBackgroundColor()
1511

1612
return (
1713
<Animated.View

src/Date/Day.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Text, TouchableRipple, useTheme } from 'react-native-paper'
33
import { StyleSheet, View } from 'react-native'
44
import DayRange from './DayRange'
55
import { daySize } from './dateUtils'
6-
import { useColorOnPrimaryBackground } from '../utils'
6+
import { useTextColorOnPrimary } from '../utils'
77

88
function EmptyDayPure() {
99
return <View style={styles.empty} />
@@ -40,8 +40,12 @@ function Day(props: {
4040
const onPress = React.useCallback(() => {
4141
onPressDate(new Date(year, month, day))
4242
}, [onPressDate, year, month, day])
43-
const color = useColorOnPrimaryBackground()
44-
const todayColor = theme.dark ? '#fff' : '#000'
43+
const color = useTextColorOnPrimary()
44+
45+
const borderColor =
46+
selected || (inRange && theme.dark) ? color : theme.dark ? '#fff' : '#000'
47+
const textColor = selected || (inRange && theme.dark) ? color : undefined
48+
4549
return (
4650
<View style={[styles.root]}>
4751
<DayRange
@@ -62,16 +66,11 @@ function Day(props: {
6266
<View
6367
style={[
6468
styles.day,
65-
isToday
66-
? { borderColor: selected || inRange ? color : todayColor }
67-
: null,
69+
isToday ? { borderColor: borderColor } : null,
6870
selected ? { backgroundColor: primaryColor } : null,
6971
]}
7072
>
71-
<Text
72-
style={selected || inRange ? { color } : null}
73-
selectable={false}
74-
>
73+
<Text style={textColor && { color: textColor }} selectable={false}>
7574
{day}
7675
</Text>
7776
</View>

src/Time/AnalogClock.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ function AnalogClock({
107107
}
108108
}
109109
},
110-
[focusedRef, is24HourRef, hoursRef, onChangeRef, minutesRef]
110+
[
111+
focusedRef,
112+
is24HourRef,
113+
hoursRef,
114+
onChangeRef,
115+
minutesRef,
116+
elementX,
117+
elementY,
118+
]
111119
)
112120

113121
const panResponder = React.useRef(
@@ -130,6 +138,7 @@ function AnalogClock({
130138
if (!clockRef.current) {
131139
return
132140
}
141+
133142
clockRef.current.measureInWindow((x, y) => {
134143
elementX.current = x
135144
elementY.current = y

src/Time/AnalogClockHours.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react'
22
import { View, StyleSheet } from 'react-native'
33
import { Text } from 'react-native-paper'
44
import { circleSize } from './AnalogClock'
5-
import { useColorOnPrimaryBackground } from '../utils'
5+
import { useTextColorOnPrimary } from '../utils'
66

77
function AnalogClockHours({
88
is24Hour,
@@ -13,7 +13,7 @@ function AnalogClockHours({
1313
}) {
1414
const outerRange = getHourNumbers(false, circleSize, 12)
1515
const innerRange = getHourNumbers(true, circleSize, 12)
16-
const color = useColorOnPrimaryBackground()
16+
const color = useTextColorOnPrimary()
1717
return (
1818
<>
1919
{outerRange.map((a, i) => (

src/Time/AnalogClockMinutes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as React from 'react'
22
import { View, StyleSheet } from 'react-native'
33
import { Text } from 'react-native-paper'
44
import { circleSize } from './AnalogClock'
5-
import { useColorOnPrimaryBackground } from '../utils'
5+
import { useTextColorOnPrimary } from '../utils'
66

77
function AnalogClockMinutes({ minutes }: { minutes: number }) {
88
const range = getMinuteNumbers(circleSize, 12)
9-
const color = useColorOnPrimaryBackground()
9+
const color = useTextColorOnPrimary()
1010
return (
1111
<>
1212
{range.map((a, i) => {

src/utils.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import { useTheme } from 'react-native-paper'
2+
import { overlay, useTheme } from 'react-native-paper'
33
import Color from 'color'
44

55
export function useLatest<T>(value: T) {
@@ -10,9 +10,25 @@ export function useLatest<T>(value: T) {
1010
return valueRef
1111
}
1212

13-
export function useColorOnPrimaryBackground(background?: any) {
13+
export function useHeaderBackgroundColor() {
1414
const theme = useTheme()
15-
const isDark = !Color(background || theme.colors.primary).isLight()
15+
return theme.dark && theme.mode === 'adaptive'
16+
? overlay(4, theme.colors.surface)
17+
: theme.colors.primary
18+
}
19+
export function useHeaderTextColor() {
20+
const theme = useTheme()
21+
const background =
22+
theme.dark && theme.mode === 'adaptive'
23+
? theme.colors.surface
24+
: theme.colors.primary
25+
const isDark = !Color(background).isLight()
26+
return isDark ? '#fff' : '#000'
27+
}
28+
29+
export function useTextColorOnPrimary() {
30+
const theme = useTheme()
31+
const isDark = !Color(theme.colors.primary).isLight()
1632
return isDark ? '#fff' : '#000'
1733
}
1834

0 commit comments

Comments
 (0)