Skip to content

Commit

Permalink
refactor: organizing theme component
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Lüders committed May 8, 2022
1 parent 91435a3 commit 0b02b7f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 42 deletions.
6 changes: 0 additions & 6 deletions src/hooks/useTheme.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/lib/components/Accordion/AccordionContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cn from 'classnames';
import { ComponentProps, FC } from 'react';
import { useTheme } from '../../../hooks/useTheme';
import { useTheme } from '../Flowbite/ThemeContext';

import { useAccordionContext } from './AccordionPanelContext';

export const AccordionContent: FC<ComponentProps<'div'>> = ({ children, ...props }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Accordion/AccordionTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentProps, FC } from 'react';
import cn from 'classnames';
import { HiChevronDown } from 'react-icons/hi';
import { useAccordionContext } from './AccordionPanelContext';
import { useTheme } from '../../../hooks/useTheme';
import { useTheme } from '../Flowbite/ThemeContext';

export type AccordionTitleProps = ComponentProps<'button'> & {
arrowIcon?: FC<ComponentProps<'svg'>>;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Accordion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AccordionPanel, AccordionPanelProps } from './AccordionPanel';
import { AccordionTitle } from './AccordionTitle';
import { AccordionContent } from './AccordionContent';
import cn from 'classnames';
import { useTheme } from '../../../hooks/useTheme';
import { useTheme } from '../Flowbite/ThemeContext';

export type AccordionProps = PropsWithChildren<{
flush?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/DarkThemeToggle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, useContext } from 'react';
import { HiMoon, HiSun } from 'react-icons/hi';
import { ThemeContext } from '../../contexts/ThemeContext';
import { ThemeContext } from '../Flowbite/ThemeContext';

export const DarkThemeToggle: FC = () => {
const { mode, toggleMode } = useContext(ThemeContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { useEffect, useState } from 'react';
import { Mode } from '../lib/contexts/ThemeContext';
import { FC, ReactNode, createContext, useContext, useState, useEffect } from 'react';
import defaultTheme from '../../theme/default';

export const useDarkMode = (
export type Mode = string | undefined | 'light' | 'dark';

interface ThemeContextProps {
theme: any;
mode?: Mode;
toggleMode?: () => void | null;
}

export const ThemeContext = createContext<ThemeContextProps>({
theme: defaultTheme,
});

interface ThemeProviderProps {
children: ReactNode;
value?: any;
}

export const ThemeProvider: FC<ThemeProviderProps> = ({ children, value }) => {
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
};

export function useTheme(): ThemeContextProps {
return useContext(ThemeContext);
}

export const useThemeMode = (
usePreferences: boolean,
): [Mode, React.Dispatch<React.SetStateAction<Mode>> | undefined, (() => void) | undefined] => {
if (!usePreferences) return [undefined, undefined, undefined];
Expand Down Expand Up @@ -33,5 +58,3 @@ export const useDarkMode = (

return [mode, setMode, toggleMode];
};

export default useDarkMode;
7 changes: 3 additions & 4 deletions src/lib/components/Flowbite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { FC, HTMLAttributes, useLayoutEffect, useMemo } from 'react';
import useDarkMode from '../../../hooks/useDarkMode';
import { ThemeContext } from '../../contexts/ThemeContext';
import { ThemeContext, useThemeMode } from './ThemeContext';
import { mergeDeep } from '../../helpers/mergeDeep';
import defaultTheme from '../../theme/default';

interface FlowbiteProps extends HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
theme?: any;
theme?: object;
dark?: boolean;
usePreferences?: boolean;
}

export const Flowbite: FC<FlowbiteProps> = ({ children, theme: customTheme, dark, usePreferences = true }) => {
const [mode, setMode, toggleMode] = useDarkMode(usePreferences);
const [mode, setMode, toggleMode] = useThemeMode(usePreferences);

const mergedTheme = mergeDeep(defaultTheme, customTheme);

Expand Down
23 changes: 0 additions & 23 deletions src/lib/contexts/ThemeContext.tsx

This file was deleted.

0 comments on commit 0b02b7f

Please sign in to comment.