Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { Text, View } from "react-native";
import { View as RNView } from "react-native";

import { styled, VariableContextProvider } from "react-native-css";
import { View } from "react-native-css/components";

import "../global.css";

const CustomView = styled(RNView, { className: "style" });

export default function App() {
return (
<>
<View className="justify-center items-center h-full">
<Text className="text-red-500">Test Component</Text>
</View>
</>
<View className="flex-1 items-center justify-center">
<VariableContextProvider
value={{
"--custom": "pink",
}}
>
<View className="bg-[var(--custom,purple)] w-10 h-10" />

<CustomView className="bg-[var(--custom,purple)] w-10 h-10" />
</VariableContextProvider>
</View>
);
}
2 changes: 1 addition & 1 deletion src/__tests__/native/units.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { View } from "react-native";
import { act, renderHook } from "@testing-library/react-native";
import { registerCSS } from "react-native-css/jest";
import { useNativeCss } from "react-native-css/runtime/native";
import { VariableContext } from "react-native-css/style-collection";

import {
dimensions,
VAR_SYMBOL,
VariableContext,
vh,
vw,
} from "../../runtime/native/reactivity";
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/native/variables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { memo, useEffect } from "react";
import type { ViewProps } from "react-native";

import { render, screen } from "@testing-library/react-native";
import { styled, VariableContextProvider } from "react-native-css";
import { View } from "react-native-css/components/View";
import { registerCSS, testID } from "react-native-css/jest";
import { styled, VariableContextProvider } from "react-native-css/runtime";

test("inline variable", () => {
registerCSS(`.my-class { width: var(--my-var); --my-var: 10px; }`);
Expand Down
3 changes: 1 addition & 2 deletions src/components/react-native-safe-area-context.native.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useContext, useMemo, type PropsWithChildren } from "react";

import { VariableContext } from "react-native-css/style-collection";
import {
SafeAreaProvider as OriginalSafeAreaProvider,
useSafeAreaInsets,
type SafeAreaProviderProps,
} from "react-native-safe-area-context";

import { VariableContext } from "../runtime/native/reactivity";

export * from "react-native-safe-area-context";

export function SafeAreaProvider({
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/native/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { useContext, useMemo, useState, type PropsWithChildren } from "react";
import { Appearance } from "react-native";

import { VariableContext } from "react-native-css/style-collection";

import type { StyleDescriptor } from "../../compiler";
import type {
ColorScheme,
Expand All @@ -15,14 +17,16 @@ import { usePassthrough } from "./react/usePassthrough";
import {
colorScheme as colorSchemeObs,
VAR_SYMBOL,
VariableContext,
type Effect,
type Getter,
type VariableContextValue,
} from "./reactivity";
import { resolveValue } from "./styles/resolve";

export { StyleCollection } from "react-native-css/style-collection";
export {
StyleCollection,
VariableContext,
} from "react-native-css/style-collection";

export { useNativeCss };

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/native/react/useNativeCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
} from "react";
import { Pressable, View } from "react-native";

import { VariableContext } from "react-native-css/style-collection";

import type { StyledConfiguration } from "../../runtime.types";
import { testGuards, type RenderGuard } from "../conditions/guards";
import {
cleanupEffect,
ContainerContext,
VariableContext,
type ContainerContextValue,
type Effect,
type Getter,
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/native/reactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,10 @@ export function weakFamily<Key extends WeakKey, Args = undefined, Result = Key>(

/********************************* Variables **********************************/

export const VAR_SYMBOL = Symbol("react-native-css.var");
export const VAR_SYMBOL = Symbol.for("react-native-css.var");
export type VariableContextValue = Record<string, StyleDescriptor> & {
[VAR_SYMBOL]: true;
};
export const VariableContext = createContext<VariableContextValue>({
[VAR_SYMBOL]: true,
});

/** Pseudo Classes ************************************************************/

Expand Down
14 changes: 14 additions & 0 deletions src/style-collection/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createContext, type Context } from "react";

import type {
Animation,
ReactNativeCssStyleSheet,
Expand All @@ -9,7 +11,9 @@ import {
family,
observable,
observableBatch,
VAR_SYMBOL,
type Observable,
type VariableContextValue,
} from "../runtime/native/reactivity";
import { rootVariables, universalVariables } from "./root";

Expand All @@ -23,6 +27,9 @@ interface StyleCollectionType {

declare global {
var __react_native_css_style_collection: StyleCollectionType | undefined;
var __react_native_css_variable_context:
| Context<VariableContextValue>
| undefined;
}

globalThis.__react_native_css_style_collection ??= {
Expand Down Expand Up @@ -140,3 +147,10 @@ function isDeepEqual(a: unknown, b: unknown): boolean {

return true;
}

globalThis.__react_native_css_variable_context ??=
createContext<VariableContextValue>({
[VAR_SYMBOL]: true,
});

export const VariableContext = globalThis.__react_native_css_variable_context;