-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support tablet and phone media queries
- Loading branch information
1 parent
d5767a2
commit 9100a79
Showing
8 changed files
with
176 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from "react"; | ||
import { DefaultNDSThemeType } from "../theme.type"; | ||
import Reset from "./Reset"; | ||
import ModalStyleOverride from "./ModalStyleOverride"; | ||
import GlobalStyles from "./GlobalStyles"; | ||
|
||
type GlobalStylesComposerProps = { | ||
theme?: DefaultNDSThemeType; | ||
locale?: string; | ||
disableGlobalStyles?: boolean; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
export default function GlobalStylesComposer({ | ||
theme, | ||
locale, | ||
disableGlobalStyles, | ||
children, | ||
}: GlobalStylesComposerProps) { | ||
if (disableGlobalStyles) { | ||
return <>{children}</>; | ||
} | ||
|
||
return ( | ||
<> | ||
<Reset /> | ||
<ModalStyleOverride theme={theme} locale={locale} /> | ||
<GlobalStyles theme={theme} locale={locale}> | ||
{children} | ||
</GlobalStyles> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useState, useEffect } from "react"; | ||
import { desktop, themes } from "../theme"; | ||
import { ThemeType, DefaultNDSThemeType, Breakpoints } from "../theme.type"; | ||
import useMediaQuery from "../hooks/useMediaQuery"; | ||
import { mergeThemes } from "./mergeThemes.util"; | ||
import { ComponentVariant } from "./ComponentVariantContext"; | ||
|
||
const THEME_VARIANTS: ReadonlySet<ComponentVariant> = new Set(["desktop", "touch"]); | ||
|
||
export const buildBreakPoints = (breakpointsConfig: Readonly<Breakpoints>) => ({ | ||
...breakpointsConfig, | ||
map: function <T>(fn: (value: string) => T) { | ||
return Object.values(breakpointsConfig).map(fn); | ||
}, | ||
}); | ||
|
||
const validateVariantOrThrow = (variant: ComponentVariant): void => { | ||
if (!THEME_VARIANTS.has(variant)) { | ||
throw new Error( | ||
`Invalid variant "${variant}" provided to NDSProvider. Valid variants are: ${Array.from(THEME_VARIANTS).join( | ||
", " | ||
)}.` | ||
); | ||
} | ||
}; | ||
|
||
export const getThemeByVariant = (variant: ComponentVariant, isTabletSize: boolean): DefaultNDSThemeType => { | ||
if (variant === "touch") { | ||
return isTabletSize ? themes.tablet : themes.phone; | ||
} | ||
return themes.desktop; | ||
}; | ||
|
||
export function useNDSTheme(variant: ComponentVariant = "desktop", customTheme?: ThemeType): DefaultNDSThemeType { | ||
validateVariantOrThrow(variant); | ||
|
||
const [themeVariant, setThemeVariant] = useState<DefaultNDSThemeType>(desktop); | ||
const isTabletSize = useMediaQuery(`(min-width: ${desktop.breakpoints.small})`); | ||
|
||
useEffect(() => { | ||
const newTheme = getThemeByVariant(variant, isTabletSize); | ||
setThemeVariant(newTheme); | ||
}, [variant, isTabletSize]); | ||
|
||
const mergedTheme = mergeThemes(themeVariant, customTheme); | ||
return { | ||
...mergedTheme, | ||
breakpoints: buildBreakPoints(mergedTheme.breakpoints), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters