@flipova/foundation 2.2.0 → 2.3.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - f614759: Themes can be declared as light/dark variant pairs in design/themes.xml, and the provider takes a variant prop that is resolved against mode at runtime.
8
+
9
+ ## 2.2.1
10
+
11
+ ### Patch Changes
12
+
13
+ - cce3ac0: the provider publishes its theme colours in a context and useGluestackColors reads the mounted theme first, so an app that adopted the registries reads its own branding and not the package palette
14
+
3
15
  ## 2.2.0
4
16
 
5
17
  ### Minor Changes
@@ -63,10 +63,77 @@ export const colors = {
63
63
  '--info': '14 165 233',
64
64
  '--info-foreground': '255 255 255',
65
65
  },
66
+ 'spring-light': {
67
+ '--background': '240 253 244',
68
+ '--foreground': '20 83 45',
69
+ '--card': '255 255 255',
70
+ '--card-foreground': '20 83 45',
71
+ '--popover': '255 255 255',
72
+ '--popover-foreground': '20 83 45',
73
+ '--primary': '22 163 74',
74
+ '--primary-foreground': '255 255 255',
75
+ '--secondary': '34 197 94',
76
+ '--secondary-foreground': '255 255 255',
77
+ '--muted': '220 252 231',
78
+ '--muted-foreground': '21 128 61',
79
+ '--accent': '187 247 208',
80
+ '--accent-foreground': '20 83 45',
81
+ '--destructive': '220 38 38',
82
+ '--destructive-foreground': '255 255 255',
83
+ '--border': '187 247 208',
84
+ '--input': '187 247 208',
85
+ '--ring': '74 222 128',
86
+ '--success': '22 163 74',
87
+ '--success-foreground': '255 255 255',
88
+ '--warning': '217 119 6',
89
+ '--warning-foreground': '255 255 255',
90
+ '--error': '220 38 38',
91
+ '--error-foreground': '255 255 255',
92
+ '--info': '2 132 199',
93
+ '--info-foreground': '255 255 255',
94
+ },
95
+ 'spring-dark': {
96
+ '--background': '5 46 22',
97
+ '--foreground': '240 253 244',
98
+ '--card': '20 83 45',
99
+ '--card-foreground': '240 253 244',
100
+ '--popover': '20 83 45',
101
+ '--popover-foreground': '240 253 244',
102
+ '--primary': '74 222 128',
103
+ '--primary-foreground': '5 46 22',
104
+ '--secondary': '134 239 172',
105
+ '--secondary-foreground': '5 46 22',
106
+ '--muted': '20 83 45',
107
+ '--muted-foreground': '187 247 208',
108
+ '--accent': '22 101 52',
109
+ '--accent-foreground': '240 253 244',
110
+ '--destructive': '239 68 68',
111
+ '--destructive-foreground': '255 255 255',
112
+ '--border': '22 101 52',
113
+ '--input': '22 101 52',
114
+ '--ring': '134 239 172',
115
+ '--success': '74 222 128',
116
+ '--success-foreground': '5 46 22',
117
+ '--warning': '251 191 36',
118
+ '--warning-foreground': '5 46 22',
119
+ '--error': '248 113 113',
120
+ '--error-foreground': '5 46 22',
121
+ '--info': '56 189 248',
122
+ '--info-foreground': '5 46 22',
123
+ },
66
124
  };
67
125
 
68
126
  // Config for nativewind vars() - used by the provider
69
127
  export const config = {
70
- light: vars(colors.light),
71
- dark: vars(colors.dark),
128
+ light: vars(colors['light']),
129
+ dark: vars(colors['dark']),
130
+ 'spring-light': vars(colors['spring-light']),
131
+ 'spring-dark': vars(colors['spring-dark']),
132
+ };
133
+
134
+ // Theme variants: variant -> mode -> theme id, from the `variant` attribute in
135
+ // design/themes.xml. `default` holds the base light/dark pair.
136
+ export const variants = {
137
+ default: { dark: 'dark', light: 'light' },
138
+ spring: { dark: 'spring-dark', light: 'spring-light' },
72
139
  };
@@ -1,5 +1,5 @@
1
1
  import React, { useEffect } from 'react';
2
- import { config } from './config';
2
+ import { config, colors, variants } from './config';
3
3
  import { View, ViewProps } from 'react-native';
4
4
  import { OverlayProvider } from '@gluestack-ui/core/overlay/creator';
5
5
  import { ToastProvider } from '@gluestack-ui/core/toast/creator';
@@ -8,15 +8,39 @@ import {
8
8
  useGluestackColors as useGluestackColorsHook,
9
9
  useCalendarTheme as useCalendarThemeHook,
10
10
  } from './useGluestackColors';
11
+ import { ThemeContext, MountedThemeContext, resolveThemeId } from './theme-context';
12
+
13
+ export {
14
+ ThemeContext,
15
+ MountedThemeContext,
16
+ useMountedColors,
17
+ useMountedTheme,
18
+ resolveThemeId,
19
+ } from './theme-context';
11
20
 
12
21
  export type ModeType = 'light' | 'dark' | 'system';
13
22
 
23
+ /** A light/dark pair declared in `design/themes.xml` (e.g. `spring`). */
24
+ export type ThemeVariant = keyof typeof variants | (string & {});
25
+
14
26
  /**
15
27
  * The theme a provider is built from: the same shape `config.ts` generates, so
16
28
  * the file an app regenerates from its own registries is accepted as it is.
17
29
  */
18
30
  export type GluestackThemeConfig = Record<string, Record<string, string>>;
19
31
 
32
+ /**
33
+ * What a provider is built from: the `colors` the generated theme exports, and
34
+ * optionally the matching `vars()` config. Passing the whole module works:
35
+ * `createGluestackUIProvider({ colors, config })`.
36
+ */
37
+ export interface GluestackTheme {
38
+ colors: GluestackThemeConfig;
39
+ config?: Record<string, unknown>;
40
+ /** variant -> mode -> theme id, generated from the `variant` attribute. */
41
+ variants?: Record<string, Record<string, string>>;
42
+ }
43
+
20
44
  // Re-export color hooks
21
45
  export const useGluestackColors = useGluestackColorsHook;
22
46
  export const useCalendarTheme = useCalendarThemeHook;
@@ -24,6 +48,8 @@ export type { GluestackColors } from './useGluestackColors';
24
48
 
25
49
  export interface GluestackUIProviderProps {
26
50
  mode?: ModeType;
51
+ /** Which light/dark pair to mount (e.g. `spring`). Defaults to `default`. */
52
+ variant?: ThemeVariant;
27
53
  children?: React.ReactNode;
28
54
  style?: ViewProps['style'];
29
55
  }
@@ -36,9 +62,14 @@ export interface GluestackUIProviderProps {
36
62
  * the result here. The exported `GluestackUIProvider` is this factory applied to
37
63
  * the package's own theme, so nothing changes for an app that did not adopt.
38
64
  */
39
- export function createGluestackUIProvider(theme: GluestackThemeConfig) {
65
+ export function createGluestackUIProvider(theme: GluestackTheme) {
66
+ const vars = theme.config ?? theme.colors;
67
+ const variantMap = theme.variants;
68
+ const themeIds = Object.keys(vars);
69
+
40
70
  return function ThemedProvider({
41
71
  mode = 'light',
72
+ variant,
42
73
  ...props
43
74
  }: GluestackUIProviderProps) {
44
75
  const { colorScheme, setColorScheme } = useColorScheme();
@@ -48,20 +79,39 @@ export function createGluestackUIProvider(theme: GluestackThemeConfig) {
48
79
  // eslint-disable-next-line react-hooks/exhaustive-deps
49
80
  }, [mode]);
50
81
 
82
+ // Nativewind only reports light/dark, so `system` resolves through it while a
83
+ // named mode is taken as-is; either way the VARIANT picks the pair. Falling
84
+ // back to the mode keeps a theme set without variants working untouched.
85
+ const resolvedMode =
86
+ mode === 'system' ? colorScheme ?? 'light' : mode;
87
+ const themeId = resolveThemeId(variantMap, variant, resolvedMode);
88
+ const mountedTheme = React.useMemo(
89
+ () => ({ themeId, themeIds }),
90
+ [themeId, themeIds]
91
+ );
92
+
51
93
  return (
52
94
  <View
53
95
  style={[
54
- theme[colorScheme!],
96
+ (vars as Record<string, unknown>)[themeId] as ViewProps['style'],
55
97
  { flex: 1, height: '100%', width: '100%' },
56
98
  props.style,
57
99
  ]}
58
100
  >
59
- <OverlayProvider>
60
- <ToastProvider>{props.children}</ToastProvider>
61
- </OverlayProvider>
101
+ <ThemeContext.Provider value={theme.colors}>
102
+ <MountedThemeContext.Provider value={mountedTheme}>
103
+ <OverlayProvider>
104
+ <ToastProvider>{props.children}</ToastProvider>
105
+ </OverlayProvider>
106
+ </MountedThemeContext.Provider>
107
+ </ThemeContext.Provider>
62
108
  </View>
63
109
  );
64
110
  };
65
111
  }
66
112
 
67
- export const GluestackUIProvider = createGluestackUIProvider(config);
113
+ export const GluestackUIProvider = createGluestackUIProvider({
114
+ colors,
115
+ config,
116
+ variants,
117
+ });
@@ -1,13 +1,17 @@
1
1
  'use client';
2
2
  import React, { useEffect, useLayoutEffect } from 'react';
3
- import { config } from './config';
3
+ import { config, variants } from './config';
4
4
  import { OverlayProvider } from '@gluestack-ui/core/overlay/creator';
5
5
  import { ToastProvider } from '@gluestack-ui/core/toast/creator';
6
6
  import { setFlushStyles } from '@gluestack-ui/utils/nativewind-utils';
7
7
  import { script } from './script';
8
+ import { resolveThemeId } from './theme-context';
8
9
 
9
10
  export type ModeType = 'light' | 'dark' | 'system';
10
11
 
12
+ /** A light/dark pair declared in `design/themes.xml` (e.g. `spring`). */
13
+ export type ThemeVariant = keyof typeof variants | (string & {});
14
+
11
15
  const variableStyleTagId = 'nativewind-style';
12
16
  const createStyle = (styleTagId: string) => {
13
17
  const style = document.createElement('style');
@@ -21,40 +25,64 @@ export const useSafeLayoutEffect =
21
25
 
22
26
  export function GluestackUIProvider({
23
27
  mode = 'light',
28
+ variant,
24
29
  ...props
25
30
  }: {
26
31
  mode?: ModeType;
32
+ variant?: ThemeVariant;
27
33
  children?: React.ReactNode;
28
34
  }) {
35
+ // Every declared theme gets a CSS block. The two default ids keep their historic
36
+ // selectors (:root / .dark) so existing apps and Tailwind's dark: keep working;
37
+ // any OTHER theme (a variant pair such as spring-light/spring-dark) is keyed on
38
+ // [data-theme], which is emitted last so it wins over :root and .dark.
39
+ const themeIds = Object.keys(config);
40
+ const isDefaultId = (id: string) => id === 'light' || id === 'dark';
29
41
  let cssVariablesWithMode = ``;
30
- Object.keys(config).forEach((configKey) => {
31
- cssVariablesWithMode +=
32
- configKey === 'dark' ? `\n .dark {\n ` : `\n:root {\n`;
42
+ let variantCss = ``;
43
+ themeIds.forEach((configKey) => {
33
44
  const cssVariables = Object.keys(
34
45
  config[configKey as keyof typeof config]
35
46
  ).reduce((acc: string, curr: string) => {
36
47
  acc += `${curr}:${config[configKey as keyof typeof config][curr]}; `;
37
48
  return acc;
38
49
  }, '');
39
- cssVariablesWithMode += `${cssVariables} \n}`;
50
+ if (isDefaultId(configKey)) {
51
+ cssVariablesWithMode +=
52
+ configKey === 'dark' ? `\n .dark {\n ` : `\n:root {\n`;
53
+ cssVariablesWithMode += `${cssVariables} \n}`;
54
+ } else {
55
+ variantCss += `\n[data-theme='${configKey}'] {\n${cssVariables} \n}`;
56
+ }
40
57
  });
58
+ cssVariablesWithMode += variantCss;
41
59
 
42
60
  setFlushStyles(cssVariablesWithMode);
43
61
 
44
62
  const handleMediaQuery = React.useCallback((e: MediaQueryListEvent) => {
45
- script(e.matches ? 'dark' : 'light');
46
- }, []);
63
+ const resolved = e.matches ? 'dark' : 'light';
64
+ script(resolved, resolveThemeId(variants, variant, resolved), resolved);
65
+ }, [variant]);
66
+
67
+ // The script keeps the light/dark class AND the variant class/attribute in step.
68
+ const activeMode = mode === 'system' ? 'light' : mode;
69
+ const themeId = resolveThemeId(variants, variant, activeMode);
70
+ const modeClass = themeId === activeMode ? '' : themeId;
47
71
 
48
72
  useSafeLayoutEffect(() => {
49
73
  if (mode !== 'system') {
50
74
  const documentElement = document.documentElement;
51
75
  if (documentElement) {
76
+ documentElement.setAttribute('data-theme', themeId);
77
+ documentElement.classList.remove('light', 'dark', modeClass);
52
78
  documentElement.classList.add(mode);
53
- documentElement.classList.remove(mode === 'light' ? 'dark' : 'light');
79
+ if (modeClass) {
80
+ documentElement.classList.add(modeClass);
81
+ }
54
82
  documentElement.style.colorScheme = mode;
55
83
  }
56
84
  }
57
- }, [mode]);
85
+ }, [mode, themeId, modeClass]);
58
86
 
59
87
  useSafeLayoutEffect(() => {
60
88
  if (mode !== 'system') return;
@@ -85,7 +113,7 @@ export function GluestackUIProvider({
85
113
  <script
86
114
  suppressHydrationWarning
87
115
  dangerouslySetInnerHTML={{
88
- __html: `(${script.toString()})('${mode}')`,
116
+ __html: `(${script.toString()})('${mode}', '${themeId}', '${modeClass}')`,
89
117
  }}
90
118
  />
91
119
  <OverlayProvider>
@@ -1,4 +1,4 @@
1
- export const script = (mode: string) => {
1
+ export const script = (mode: string, themeId: string, modeClass: string) => {
2
2
  const documentElement = document.documentElement;
3
3
 
4
4
  function getSystemColorMode() {
@@ -9,10 +9,17 @@ export const script = (mode: string) => {
9
9
 
10
10
  try {
11
11
  const isSystem = mode === 'system';
12
- const theme = isSystem ? getSystemColorMode() : mode;
13
- documentElement.classList.remove(theme === 'light' ? 'dark' : 'light');
14
- documentElement.classList.add(theme);
15
- documentElement.style.colorScheme = theme;
12
+ const resolved = isSystem ? getSystemColorMode() : mode;
13
+ // `data-theme` carries the variant pair (spring-light, spring-dark, ...);
14
+ // the light/dark CLASS is kept alongside it so Tailwind's dark: variants
15
+ // keep working while a seasonal palette is mounted.
16
+ documentElement.setAttribute('data-theme', themeId);
17
+ documentElement.classList.remove('light', 'dark', modeClass);
18
+ documentElement.classList.add(resolved);
19
+ if (themeId !== resolved) {
20
+ documentElement.classList.add(modeClass);
21
+ }
22
+ documentElement.style.colorScheme = resolved;
16
23
  } catch (e) {
17
24
  console.error(e);
18
25
  }
@@ -0,0 +1,51 @@
1
+ import { createContext, useContext } from 'react';
2
+
3
+ /**
4
+ * The colour maps of the theme that is actually mounted.
5
+ *
6
+ * `useGluestackColors` used to close over the config this package generates,
7
+ * so an app that adopted the registries and mounted its own provider still read
8
+ * the package colours - the branding would have been cosmetic. The provider
9
+ * publishes what it was built with here instead, and the hook reads this first.
10
+ */
11
+ export type ThemeColors = Record<string, Record<string, string>>;
12
+
13
+ export const ThemeContext = createContext<ThemeColors | null>(null);
14
+
15
+ /** The mounted theme, or null when the package's own provider is not in the tree. */
16
+ export const useMountedColors = (): ThemeColors | null => useContext(ThemeContext);
17
+
18
+ /**
19
+ * Which theme of the mounted set is active.
20
+ *
21
+ * A theme set declares several light/dark pairs (variants: `spring-light` /
22
+ * `spring-dark`, ...). Nativewind only knows `light` and `dark`, so the provider
23
+ * resolves the pair itself and publishes the resulting id here - otherwise a hook
24
+ * reading colours would look up `colors['dark']` and miss the variant entirely.
25
+ */
26
+ export interface MountedTheme {
27
+ /** The active theme id, e.g. `spring-dark`. */
28
+ themeId: string;
29
+ /** Every id the mounted set declares, so a lookup can fall back safely. */
30
+ themeIds: string[];
31
+ }
32
+
33
+ export const MountedThemeContext = createContext<MountedTheme | null>(null);
34
+
35
+ export const useMountedTheme = (): MountedTheme | null => useContext(MountedThemeContext);
36
+
37
+ /**
38
+ * Resolve a (variant, mode) request to a theme id.
39
+ *
40
+ * The generated `variants` table is authoritative, so a theme id never has to
41
+ * follow a naming convention. An unknown variant falls back to the mode itself,
42
+ * which is exactly the pre-variant behaviour (the `light`/`dark` ids).
43
+ */
44
+ export function resolveThemeId(
45
+ variants: Record<string, Record<string, string>> | undefined,
46
+ variant: string | undefined,
47
+ mode: string
48
+ ): string {
49
+ const mapped = variant ? variants?.[variant]?.[mode] : undefined;
50
+ return mapped ?? mode;
51
+ }
@@ -1,5 +1,6 @@
1
1
  import { useColorScheme } from 'nativewind';
2
2
  import { colors } from './config';
3
+ import { useMountedColors, useMountedTheme } from './theme-context';
3
4
 
4
5
  /**
5
6
  * Convert CSS variable name to camelCase
@@ -40,12 +41,26 @@ function rgbToHex(rgbString: string): string {
40
41
  */
41
42
  export function useGluestackColors(): Record<string, string> {
42
43
  const { colorScheme } = useColorScheme();
43
- const theme = colors[colorScheme || 'light'];
44
+ // The mounted theme wins: an app that adopted the registries and built its own
45
+ // provider must read its own colours, not the ones shipped with the package.
46
+ const theme = useMountedColors() ?? colors;
47
+ const mounted = useMountedTheme();
48
+ // The provider resolves the active theme id (a variant pair such as
49
+ // `spring-dark`). Falling back to the raw scheme keeps a tree with no mounted
50
+ // provider working, and an unknown id must not hand `Object.entries` undefined.
51
+ // `theme` is the generated literal object, so index it through a widened view:
52
+ // the active id is only known at runtime.
53
+ const palettes = theme as Record<string, Record<string, string>>;
54
+ const palette =
55
+ palettes[mounted?.themeId ?? ''] ??
56
+ palettes[colorScheme || 'light'] ??
57
+ palettes[mounted?.themeIds[0] ?? ''] ??
58
+ {};
44
59
 
45
60
  // Dynamically convert all CSS variables to camelCase hex colors
46
61
  const result: Record<string, string> = {};
47
62
 
48
- Object.entries(theme).forEach(([key, value]) => {
63
+ Object.entries(palette).forEach(([key, value]) => {
49
64
  const camelKey = toCamelCase(key);
50
65
  result[camelKey] = rgbToHex(value as string);
51
66
  });
@@ -5,6 +5,8 @@
5
5
  # entries (register/id) — canonical sort by (register, id)
6
6
  themes/dark
7
7
  themes/light
8
+ themes/spring-dark
9
+ themes/spring-light
8
10
  tokens/breakpoints
9
11
  tokens/color
10
12
  tokens/duration
@@ -22,6 +24,7 @@ tokens/zIndex
22
24
 
23
25
  ## references (resolved, never dangling)
24
26
  $ref.tokens.color.black -> #000000
27
+ $ref.tokens.color.error.400 -> #F87171
25
28
  $ref.tokens.color.error.500 -> #EF4444
26
29
  $ref.tokens.color.error.600 -> #DC2626
27
30
  $ref.tokens.color.gray.50 -> #FAFAFA
@@ -32,6 +35,7 @@ $ref.tokens.color.gray.600 -> #52525B
32
35
  $ref.tokens.color.gray.700 -> #3F3F46
33
36
  $ref.tokens.color.gray.800 -> #27272A
34
37
  $ref.tokens.color.gray.900 -> #18181B
38
+ $ref.tokens.color.info.400 -> #38BDF8
35
39
  $ref.tokens.color.info.500 -> #0EA5E9
36
40
  $ref.tokens.color.info.600 -> #0284C7
37
41
  $ref.tokens.color.neutral.900 -> #262626
@@ -43,11 +47,21 @@ $ref.tokens.color.primary.600 -> #2563EB
43
47
  $ref.tokens.color.primary.900 -> #1E3A8A
44
48
  $ref.tokens.color.secondary.500 -> #64748B
45
49
  $ref.tokens.color.secondary.600 -> #475569
50
+ $ref.tokens.color.success.50 -> #F0FDF4
51
+ $ref.tokens.color.success.100 -> #DCFCE7
52
+ $ref.tokens.color.success.200 -> #BBF7D0
53
+ $ref.tokens.color.success.300 -> #86EFAC
54
+ $ref.tokens.color.success.400 -> #4ADE80
46
55
  $ref.tokens.color.success.500 -> #22C55E
47
56
  $ref.tokens.color.success.600 -> #16A34A
57
+ $ref.tokens.color.success.700 -> #15803D
58
+ $ref.tokens.color.success.800 -> #166534
59
+ $ref.tokens.color.success.900 -> #14532D
60
+ $ref.tokens.color.success.950 -> #052E16
61
+ $ref.tokens.color.warning.400 -> #FBBF24
48
62
  $ref.tokens.color.warning.500 -> #F59E0B
49
63
  $ref.tokens.color.warning.600 -> #D97706
50
64
  $ref.tokens.color.white -> #FFFFFF
51
65
 
52
66
  ## sha256
53
- a6278c2bf241fbb45ffa7e7ce58f8d9d362e08b536751fdac5e976cbe445842c
67
+ b330516c315d00effb781f75e1229580352989b12c7131a1b6265869e3b5a9f1
@@ -85,6 +85,8 @@
85
85
  <register name="themes">
86
86
  <item>light</item>
87
87
  <item>dark</item>
88
+ <item>spring-light</item>
89
+ <item>spring-dark</item>
88
90
  </register>
89
91
  </index>
90
92
  </foundationRegistry>
package/design/schema.xsd CHANGED
@@ -594,6 +594,17 @@
594
594
  <xs:documentation>Theme mode (e.g. "light", "dark").</xs:documentation>
595
595
  </xs:annotation>
596
596
  </xs:attribute>
597
+ <xs:attribute name="variant" type="xs:string" use="optional">
598
+ <xs:annotation>
599
+ <xs:documentation>
600
+ Variant this theme belongs to (e.g. "default", "spring"). A variant is a
601
+ coherent light/dark PAIR, so a themed product can offer several palettes
602
+ ("spring-light"/"spring-dark", "autumn-light"/"autumn-dark") that all
603
+ respond to the same light/dark mode. Defaults to "default" when absent,
604
+ which keeps the bare "light"/"dark" ids working unchanged.
605
+ </xs:documentation>
606
+ </xs:annotation>
607
+ </xs:attribute>
597
608
  </xs:complexType>
598
609
 
599
610
  <xs:complexType name="RoleMapType">