Skip to content

Commit

Permalink
feat: allow user to disable auto theme changing with a prop
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed Oct 23, 2021
1 parent 5d91d90 commit 6462472
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ describe("<DashProvider>", () => {
});

expect(styles.tokens.light.colors.primary).toBe("red");
expect((styles.themes.light as any).colors.primary).toBe('red');
expect((styles.themes.light as any).colors.primary).toBe("red");
});

it("should set a default theme", () => {
Expand Down Expand Up @@ -1258,6 +1258,32 @@ describe("<DashProvider>", () => {
expect(onThemeChange).not.toBeCalled();
});

it("should not react to useColorScheme changes if disableAutoThemeChange", () => {
const { DashProvider } = createStyles(stylesConfig);
const onThemeChange = jest.fn();
jest.spyOn(RN.Appearance, "addChangeListener");
const listeners: any[] = [];
// @ts-expect-error
RN.Appearance.addChangeListener.mockImplementation((listener) => {
listeners.push(listener);
});

render(
<DashProvider
defaultTheme="light"
disableAutoThemeChange
onThemeChange={onThemeChange}
/>
);

colorScheme = "dark";
act(() => {
listeners.forEach((listener) => listener(colorScheme));
});

expect(onThemeChange).not.toBeCalled();
});

it("should not react to useColorScheme changes if themes are non-standard", () => {
const { DashProvider } = createStyles({
...stylesConfig,
Expand Down
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,13 @@ export function createStyles<
theme: controlledTheme,
defaultTheme,
onThemeChange,
disableAutoThemeChange,
children,
}: {
defaultTheme?: typeof currentTheme;
theme?: typeof currentTheme;
onThemeChange?: (theme: typeof currentTheme) => void;
disableAutoThemeChange?: boolean;
children?: React.ReactNode;
}) {
const colorScheme = RN.useColorScheme();
Expand Down Expand Up @@ -387,7 +389,8 @@ export function createStyles<
!controlledTheme &&
colorScheme &&
colorScheme !== theme &&
isAutoThemeable
isAutoThemeable &&
!disableAutoThemeChange
) {
currentTheme = colorScheme as keyof T;
setTheme(currentTheme);
Expand Down

0 comments on commit 6462472

Please sign in to comment.