Examples
Habit streak
A running log: tap the days you ran on the calendar (it won't take days that haven't happened), see the streak, set the morning reminder, and pick the colour the calendar marks your runs in.
Loading…
import { useMemo, useState } from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; import { addDays, ColorPickerField, DatePicker, Heading, Text, TimePickerField, toDateKey, useTheme } from 'patine-ui'; const COLOURS = [ { value: '#30A46C', label: 'Green' }, { value: '#0090FF', label: 'Blue' }, { value: '#8E4EC6', label: 'Purple' }, { value: '#E93D82', label: 'Pink' }, { value: '#F76B15', label: 'Orange' }, { value: '#A18072', label: 'Brown' }, ]; const RAN_DAYS_AGO = [1, 2, 3, 5, 6, 8, 9, 10, 13]; /** Days in a row up to yesterday or today. */ const streak = (days: Date[], today: Date) => { const ran = new Set(days.map(toDateKey)); let count = 0; let day = ran.has(toDateKey(today)) ? today : addDays(today, -1); while (ran.has(toDateKey(day))) { count++; day = addDays(day, -1); } return count; }; export default function HabitStreak() { const { colors } = useTheme(); const today = useMemo(() => { const day = new Date(); day.setHours(0, 0, 0, 0); return day; }, []); const [days, setDays] = useState(() => RAN_DAYS_AGO.map((n) => addDays(today, -n))); const [colour, setColour] = useState('#30A46C'); const [reminder, setReminder] = useState(() => { const at = new Date(today); at.setHours(6, 45); return at; }); const current = streak(days, today); return ( <ScrollView style={{ backgroundColor: colors.background }} contentContainerStyle={styles.content}> <View style={styles.titles}> <Heading level={2}>Morning run</Heading> <Text color="textSecondary"> {days.length} runs logged · {current ? `${current}-day streak` : 'start a streak today'} </Text> </View> <DatePicker mode="multiple" value={days} onValueChange={setDays} maxDate={today} dayHeight={40} theme={{ colors: { primary: colour } }} /> <TimePickerField label="Daily reminder" value={reminder} onConfirm={setReminder} minuteInterval={5} /> <ColorPickerField label="Colour" value={colour} onConfirm={setColour} colors={COLOURS} showCustom={false} /> </ScrollView> ); } const styles = StyleSheet.create({ content: { gap: 16, padding: 20 }, titles: { gap: 4 }, });
Components used
- DatePicker:
mode="multiple"for the days you ran,maxDateso the future can't be logged, andthemeto colour it - TimePickerField: the reminder, in 5-minute steps
- ColorPickerField: a fixed palette with
showCustom={false} - Heading, Text
How it works
- The streak is counted from the data, one
toDateKeyat a time back from today, so it's right across months and daylight-saving changes. - A component-level theme.
theme={{ colors: { primary } }}recolours only this calendar, so the picked colour shows on the runs without touching the rest of the app.