@tamagui/next-theme 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/cjs/NextTheme.js +9 -273
  2. package/dist/cjs/NextTheme.js.map +3 -3
  3. package/dist/cjs/NextThemeProvider.js +245 -0
  4. package/dist/cjs/NextThemeProvider.js.map +7 -0
  5. package/dist/cjs/ThemeSettingContext.js +36 -0
  6. package/dist/cjs/ThemeSettingContext.js.map +7 -0
  7. package/dist/cjs/UseThemeProps.js +17 -0
  8. package/dist/cjs/UseThemeProps.js.map +7 -0
  9. package/dist/cjs/constants.js +35 -0
  10. package/dist/cjs/constants.js.map +7 -0
  11. package/dist/cjs/helpers.js +52 -0
  12. package/dist/cjs/helpers.js.map +7 -0
  13. package/dist/cjs/types.js +17 -0
  14. package/dist/cjs/types.js.map +7 -0
  15. package/dist/cjs/useIsomorphicLayoutEffect.js +30 -0
  16. package/dist/cjs/useIsomorphicLayoutEffect.js.map +7 -0
  17. package/dist/cjs/useRootTheme.js +49 -0
  18. package/dist/cjs/useRootTheme.js.map +7 -0
  19. package/dist/cjs/useTheme.js +34 -0
  20. package/dist/cjs/useTheme.js.map +7 -0
  21. package/dist/esm/NextTheme.js +8 -265
  22. package/dist/esm/NextTheme.js.map +3 -3
  23. package/dist/esm/NextThemeProvider.js +215 -0
  24. package/dist/esm/NextThemeProvider.js.map +7 -0
  25. package/dist/esm/ThemeSettingContext.js +12 -0
  26. package/dist/esm/ThemeSettingContext.js.map +7 -0
  27. package/dist/esm/UseThemeProps.js +1 -0
  28. package/dist/esm/UseThemeProps.js.map +7 -0
  29. package/dist/esm/constants.js +9 -0
  30. package/dist/esm/constants.js.map +7 -0
  31. package/dist/esm/helpers.js +26 -0
  32. package/dist/esm/helpers.js.map +7 -0
  33. package/dist/esm/types.js +1 -0
  34. package/dist/esm/types.js.map +7 -0
  35. package/dist/esm/useIsomorphicLayoutEffect.js +6 -0
  36. package/dist/esm/useIsomorphicLayoutEffect.js.map +7 -0
  37. package/dist/esm/useRootTheme.js +19 -0
  38. package/dist/esm/useRootTheme.js.map +7 -0
  39. package/dist/esm/useTheme.js +9 -0
  40. package/dist/esm/useTheme.js.map +7 -0
  41. package/package.json +3 -3
  42. package/src/NextTheme.tsx +10 -408
  43. package/src/NextThemeProvider.tsx +297 -0
  44. package/src/ThemeSettingContext.tsx +9 -0
  45. package/src/UseThemeProps.tsx +46 -0
  46. package/src/constants.tsx +3 -0
  47. package/src/helpers.tsx +25 -0
  48. package/src/types.tsx +5 -0
  49. package/src/useIsomorphicLayoutEffect.tsx +4 -0
  50. package/src/useRootTheme.tsx +21 -0
  51. package/src/useTheme.tsx +11 -0
  52. package/types/NextTheme.d.ts +8 -53
  53. package/types/NextThemeProvider.d.ts +4 -0
  54. package/types/ThemeSettingContext.d.ts +4 -0
  55. package/types/UseThemeProps.d.ts +43 -0
  56. package/types/constants.d.ts +4 -0
  57. package/types/helpers.d.ts +4 -0
  58. package/types/types.d.ts +5 -0
  59. package/types/useIsomorphicLayoutEffect.d.ts +3 -0
  60. package/types/useRootTheme.d.ts +3 -0
  61. package/types/useTheme.d.ts +6 -0
@@ -0,0 +1,9 @@
1
+ import { createContext } from 'react'
2
+
3
+ import { UseThemeProps } from './UseThemeProps'
4
+
5
+ export const ThemeSettingContext = createContext<UseThemeProps>({
6
+ toggle: () => {},
7
+ set: (_) => {},
8
+ themes: [],
9
+ })
@@ -0,0 +1,46 @@
1
+ import { ValueObject } from './types'
2
+
3
+ export interface UseThemeProps {
4
+ /** List of all available theme names */
5
+ themes: string[]
6
+ /** Forced theme name for the current page */
7
+ forcedTheme?: string
8
+ /** Update the theme */
9
+ set: (theme: string) => void
10
+ toggle: () => void
11
+ /** Active theme name - will return "system" if not overriden, see "resolvedTheme" for getting resolved system value */
12
+ current?: string
13
+ /** @deprecated Use `current` instead (deprecating avoid confusion with useTheme) */
14
+ theme?: string
15
+ /** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
16
+ resolvedTheme?: string
17
+ /** If enableSystem is true, returns the System theme preference ("dark" or "light"), regardless what the active theme is */
18
+ systemTheme?: 'dark' | 'light'
19
+ }
20
+
21
+ export interface ThemeProviderProps {
22
+ children?: any
23
+ /** List of all available theme names */
24
+ themes?: string[]
25
+ /** Forced theme name for the current page */
26
+ forcedTheme?: string
27
+ /** Whether to switch between dark and light themes based on prefers-color-scheme */
28
+ enableSystem?: boolean
29
+ systemTheme?: string
30
+ /** Disable all CSS transitions when switching themes */
31
+ disableTransitionOnChange?: boolean
32
+ /** Whether to indicate to browsers which color scheme is used (dark or light) for built-in UI like inputs and buttons */
33
+ enableColorScheme?: boolean
34
+ /** Key used to store theme setting in localStorage */
35
+ storageKey?: string
36
+ /** Default theme name (for v0.0.12 and lower the default was light). If `enableSystem` is false, the default theme is light */
37
+ defaultTheme?: string
38
+ /** HTML attribute modified based on the active theme. Accepts `class` and `data-*` (meaning any data attribute, `data-mode`, `data-color`, etc.) */
39
+ attribute?: string | 'class'
40
+ /** Mapping of theme name to HTML attribute value. Object where key is the theme name and value is the attribute value */
41
+ value?: ValueObject
42
+ onChangeTheme?: (name: string) => void
43
+
44
+ // avoids warning
45
+ skipNextHead?: boolean
46
+ }
@@ -0,0 +1,3 @@
1
+ export const constants = {}
2
+ export const colorSchemes = ['light', 'dark']
3
+ export const MEDIA = '(prefers-color-scheme: dark)'
@@ -0,0 +1,25 @@
1
+ import { MEDIA } from './constants'
2
+
3
+ export const helpers = {}
4
+ // Helpers
5
+
6
+ export const getTheme = (key: string, fallback?: string) => {
7
+ if (typeof window === 'undefined') return undefined
8
+ let theme
9
+ try {
10
+ theme = localStorage.getItem(key) || undefined
11
+ } catch (e) {
12
+ // Unsupported
13
+ }
14
+ return theme || fallback
15
+ }
16
+
17
+ export const getSystemTheme = (e?: MediaQueryList) => {
18
+ if (!e) {
19
+ e = window.matchMedia(MEDIA)
20
+ }
21
+
22
+ const isDark = e.matches
23
+ const systemTheme = isDark ? 'dark' : 'light'
24
+ return systemTheme
25
+ }
package/src/types.tsx ADDED
@@ -0,0 +1,5 @@
1
+ export type types = {}
2
+
3
+ export interface ValueObject {
4
+ [themeName: string]: string
5
+ }
@@ -0,0 +1,4 @@
1
+ import { useEffect, useLayoutEffect } from 'react'
2
+
3
+ export const useIsomorphicLayoutEffect =
4
+ typeof window !== 'undefined' ? useLayoutEffect : useEffect
@@ -0,0 +1,21 @@
1
+ import * as React from 'react'
2
+ import { useLayoutEffect, useState } from 'react'
3
+
4
+ // note this only works for light being default for now...
5
+
6
+ export const useRootTheme = () => {
7
+ const [val, setVal] = useState('light')
8
+
9
+ if (typeof document !== 'undefined') {
10
+ useLayoutEffect(() => {
11
+ // @ts-ignore
12
+ const classes = [...document.documentElement.classList]
13
+ const isDark = classes.includes('t_dark')
14
+ React.startTransition(() => {
15
+ setVal(isDark ? 'dark' : 'light')
16
+ })
17
+ }, [])
18
+ }
19
+
20
+ return [val, setVal] as const
21
+ }
@@ -0,0 +1,11 @@
1
+ import { useContext } from 'react'
2
+
3
+ import { ThemeSettingContext } from './ThemeSettingContext'
4
+
5
+ /**
6
+ * @deprecated renamed to `useThemeSetting` to avoid confusion with core `useTheme` hook
7
+ */
8
+
9
+ export const useTheme = () => useContext(ThemeSettingContext)
10
+
11
+ export const useThemeSetting = () => useContext(ThemeSettingContext)
@@ -1,54 +1,9 @@
1
- import * as React from 'react';
2
- export interface UseThemeProps {
3
- /** List of all available theme names */
4
- themes: string[];
5
- /** Forced theme name for the current page */
6
- forcedTheme?: string;
7
- /** Update the theme */
8
- set: (theme: string) => void;
9
- toggle: () => void;
10
- /** Active theme name - will return "system" if not overriden, see "resolvedTheme" for getting resolved system value */
11
- current?: string;
12
- /** @deprecated Use `current` instead (deprecating avoid confusion with useTheme) */
13
- theme?: string;
14
- /** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
15
- resolvedTheme?: string;
16
- /** If enableSystem is true, returns the System theme preference ("dark" or "light"), regardless what the active theme is */
17
- systemTheme?: 'dark' | 'light';
18
- }
19
- export interface ThemeProviderProps {
20
- children?: any;
21
- /** List of all available theme names */
22
- themes?: string[];
23
- /** Forced theme name for the current page */
24
- forcedTheme?: string;
25
- /** Whether to switch between dark and light themes based on prefers-color-scheme */
26
- enableSystem?: boolean;
27
- systemTheme?: string;
28
- /** Disable all CSS transitions when switching themes */
29
- disableTransitionOnChange?: boolean;
30
- /** Whether to indicate to browsers which color scheme is used (dark or light) for built-in UI like inputs and buttons */
31
- enableColorScheme?: boolean;
32
- /** Key used to store theme setting in localStorage */
33
- storageKey?: string;
34
- /** Default theme name (for v0.0.12 and lower the default was light). If `enableSystem` is false, the default theme is light */
35
- defaultTheme?: string;
36
- /** HTML attribute modified based on the active theme. Accepts `class` and `data-*` (meaning any data attribute, `data-mode`, `data-color`, etc.) */
37
- attribute?: string | 'class';
38
- /** Mapping of theme name to HTML attribute value. Object where key is the theme name and value is the attribute value */
39
- value?: ValueObject;
40
- onChangeTheme?: (name: string) => void;
41
- skipNextHead?: boolean;
42
- }
43
- /**
44
- * @deprecated renamed to `useThemeSetting` to avoid confusion with core `useTheme` hook
45
- */
46
- export declare const useTheme: () => UseThemeProps;
47
- export declare const useThemeSetting: () => UseThemeProps;
48
- interface ValueObject {
49
- [themeName: string]: string;
50
- }
51
- export declare const useRootTheme: () => readonly [string, React.Dispatch<React.SetStateAction<string>>];
52
- export declare const NextThemeProvider: React.FC<ThemeProviderProps>;
53
- export {};
1
+ export * from './NextThemeProvider';
2
+ export * from './ThemeSettingContext';
3
+ export * from './UseThemeProps';
4
+ export * from './helpers';
5
+ export * from './constants';
6
+ export * from './useTheme';
7
+ export * from './types';
8
+ export * from './useRootTheme';
54
9
  //# sourceMappingURL=NextTheme.d.ts.map
@@ -0,0 +1,4 @@
1
+ import * as React from 'react';
2
+ import { ThemeProviderProps } from './UseThemeProps';
3
+ export declare const NextThemeProvider: React.FC<ThemeProviderProps>;
4
+ //# sourceMappingURL=NextThemeProvider.d.ts.map
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import { UseThemeProps } from './UseThemeProps';
3
+ export declare const ThemeSettingContext: import("react").Context<UseThemeProps>;
4
+ //# sourceMappingURL=ThemeSettingContext.d.ts.map
@@ -0,0 +1,43 @@
1
+ import { ValueObject } from './types';
2
+ export interface UseThemeProps {
3
+ /** List of all available theme names */
4
+ themes: string[];
5
+ /** Forced theme name for the current page */
6
+ forcedTheme?: string;
7
+ /** Update the theme */
8
+ set: (theme: string) => void;
9
+ toggle: () => void;
10
+ /** Active theme name - will return "system" if not overriden, see "resolvedTheme" for getting resolved system value */
11
+ current?: string;
12
+ /** @deprecated Use `current` instead (deprecating avoid confusion with useTheme) */
13
+ theme?: string;
14
+ /** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
15
+ resolvedTheme?: string;
16
+ /** If enableSystem is true, returns the System theme preference ("dark" or "light"), regardless what the active theme is */
17
+ systemTheme?: 'dark' | 'light';
18
+ }
19
+ export interface ThemeProviderProps {
20
+ children?: any;
21
+ /** List of all available theme names */
22
+ themes?: string[];
23
+ /** Forced theme name for the current page */
24
+ forcedTheme?: string;
25
+ /** Whether to switch between dark and light themes based on prefers-color-scheme */
26
+ enableSystem?: boolean;
27
+ systemTheme?: string;
28
+ /** Disable all CSS transitions when switching themes */
29
+ disableTransitionOnChange?: boolean;
30
+ /** Whether to indicate to browsers which color scheme is used (dark or light) for built-in UI like inputs and buttons */
31
+ enableColorScheme?: boolean;
32
+ /** Key used to store theme setting in localStorage */
33
+ storageKey?: string;
34
+ /** Default theme name (for v0.0.12 and lower the default was light). If `enableSystem` is false, the default theme is light */
35
+ defaultTheme?: string;
36
+ /** HTML attribute modified based on the active theme. Accepts `class` and `data-*` (meaning any data attribute, `data-mode`, `data-color`, etc.) */
37
+ attribute?: string | 'class';
38
+ /** Mapping of theme name to HTML attribute value. Object where key is the theme name and value is the attribute value */
39
+ value?: ValueObject;
40
+ onChangeTheme?: (name: string) => void;
41
+ skipNextHead?: boolean;
42
+ }
43
+ //# sourceMappingURL=UseThemeProps.d.ts.map
@@ -0,0 +1,4 @@
1
+ export declare const constants: {};
2
+ export declare const colorSchemes: string[];
3
+ export declare const MEDIA = "(prefers-color-scheme: dark)";
4
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,4 @@
1
+ export declare const helpers: {};
2
+ export declare const getTheme: (key: string, fallback?: string) => any;
3
+ export declare const getSystemTheme: (e?: MediaQueryList) => "light" | "dark";
4
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1,5 @@
1
+ export type types = {};
2
+ export interface ValueObject {
3
+ [themeName: string]: string;
4
+ }
5
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { useLayoutEffect } from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
3
+ //# sourceMappingURL=useIsomorphicLayoutEffect.d.ts.map
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ export declare const useRootTheme: () => readonly [string, React.Dispatch<React.SetStateAction<string>>];
3
+ //# sourceMappingURL=useRootTheme.d.ts.map
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @deprecated renamed to `useThemeSetting` to avoid confusion with core `useTheme` hook
3
+ */
4
+ export declare const useTheme: () => import("./UseThemeProps").UseThemeProps;
5
+ export declare const useThemeSetting: () => import("./UseThemeProps").UseThemeProps;
6
+ //# sourceMappingURL=useTheme.d.ts.map