Skip to content
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

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions packages/flash-calendar/src/components/Calendar.tsx
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,
Expand Down Expand Up @@ -68,8 +68,25 @@ export interface CalendarProps extends UseCalendarParams {
calendarMonthHeaderHeight?: number;
/** Theme to customize the calendar component. */
theme?: CalendarTheme;
/** Enable dark mode */
darkMode?: boolean;
}
Copy link
Owner

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:

  
  /**
   * When set, Flash Calendar will use this color scheme instead of the system's
   * value (`light|dark`). This is useful if your app doesn't support dark-mode,
   * for example.
   *
   * We don't advise using this prop - ideally, your app should reflect the
   * user's preferences.
   */
  colorSchemeToOverride?: ColorSchemeName;


const calendarContext = createContext<{ darkMode?: boolean } | undefined>(
undefined
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of darkMode, let's use the idea from above:

type CalendarContextType = {
  /** The overridden color scheme */
  colorScheme?: ColorSchemeName
}

const calendarThemeContext = createContext<CalendarContextType | undefined>(undefined)

);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this to a useCalendarTheme.ts file?

export const useCalendarContext = () => {
const context = useContext(calendarContext);

if (!context) {
throw new Error(
"useCalendarContext must be called inside <calendarContext.Provider>"
);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this to a useCalendarTheme file?

const BaseCalendar = memo(
({
onCalendarDayPress,
Expand Down Expand Up @@ -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",
Expand All @@ -168,7 +190,11 @@ export const Calendar = memo(
*/
}, [calendarActiveDateRanges, calendarMonthId]);

return <BaseCalendar {...props} calendarMonthId={calendarMonthId} />;
return (
<calendarContext.Provider value={{ darkMode }}>
<BaseCalendar {...props} calendarMonthId={calendarMonthId} />
Copy link
Owner

Choose a reason for hiding this comment

The 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>
);
}
);

Expand Down
11 changes: 11 additions & 0 deletions packages/flash-calendar/src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import { useColorScheme } from "react-native";

import type { BaseTheme } from "@/helpers/tokens";
import { darkTheme, lightTheme } from "@/helpers/tokens";
import { useCalendarContext } from "@/components/Calendar";

export const useTheme = (): BaseTheme => {
const appearance = useColorScheme();
const darkMode = useCalendarContext();

if (darkMode) {
return darkTheme;
}

if (darkMode === false) {
return lightTheme;
}

return appearance === "dark" ? darkTheme : lightTheme;
};
Loading