-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure darkmode with props #28
Changes from 1 commit
7a27f49
d43810c
430899f
a97867a
dd68c15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { memo, useEffect } from "react"; | ||
import { memo, createContext, useEffect, useContext } from "react"; | ||
|
||
import type { | ||
CalendarItemDayContainerProps, | ||
|
@@ -68,8 +68,25 @@ export interface CalendarProps extends UseCalendarParams { | |
calendarMonthHeaderHeight?: number; | ||
/** Theme to customize the calendar component. */ | ||
theme?: CalendarTheme; | ||
/** Enable dark mode */ | ||
darkMode?: boolean; | ||
} | ||
|
||
const calendarContext = createContext<{ darkMode?: boolean } | undefined>( | ||
undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of
|
||
); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move this to a |
||
export const useCalendarContext = () => { | ||
const context = useContext(calendarContext); | ||
|
||
if (!context) { | ||
throw new Error( | ||
"useCalendarContext must be called inside <calendarContext.Provider>" | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's also change the error message to reflect the new context name |
||
} | ||
return context; | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move this to a |
||
const BaseCalendar = memo( | ||
({ | ||
onCalendarDayPress, | ||
|
@@ -150,7 +167,12 @@ const BaseCalendar = memo( | |
BaseCalendar.displayName = "BaseCalendar"; | ||
|
||
export const Calendar = memo( | ||
({ calendarActiveDateRanges, calendarMonthId, ...props }: CalendarProps) => { | ||
({ | ||
calendarActiveDateRanges, | ||
calendarMonthId, | ||
darkMode, | ||
...props | ||
}: CalendarProps) => { | ||
useEffect(() => { | ||
activeDateRangesEmitter.emit( | ||
"onSetActiveDateRanges", | ||
|
@@ -168,7 +190,11 @@ export const Calendar = memo( | |
*/ | ||
}, [calendarActiveDateRanges, calendarMonthId]); | ||
|
||
return <BaseCalendar {...props} calendarMonthId={calendarMonthId} />; | ||
return ( | ||
<calendarContext.Provider value={{ darkMode }}> | ||
<BaseCalendar {...props} calendarMonthId={calendarMonthId} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to memo this value to avoid re-renders: const calendarThemeContextValue = useMemo<CalendarThemeContext>(() => ({ colorScheme: colorSchemeToOverride }), [colorSchemeToOverride]) |
||
</calendarContext.Provider> | ||
); | ||
} | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this more flexible by changing to: