mirror of
https://github.com/zhigang1992/nativewind.git
synced 2026-06-13 17:35:52 +08:00
refactor: cleanup assertIsInContext
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { createContext } from "react";
|
||||
import { createContext, useContext } from "react";
|
||||
import { ColorSchemeName } from "react-native";
|
||||
import { MediaRecord, Style } from "./types/common";
|
||||
|
||||
@@ -38,6 +38,18 @@ export const TailwindContext = createContext<TailwindContext>({
|
||||
preview: false,
|
||||
});
|
||||
|
||||
export function useTailwindContext() {
|
||||
const context = useContext(TailwindContext);
|
||||
|
||||
if (!context.platform) {
|
||||
throw new Error(
|
||||
"No platform details found. Make sure all components are within a TailwindProvider with the platform attribute set."
|
||||
);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
export interface ComponentContext {
|
||||
hover: boolean;
|
||||
focus: boolean;
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
ImageStyle,
|
||||
StyleProp,
|
||||
} from "react-native";
|
||||
import { ComponentContext, TailwindContext } from "./context";
|
||||
import {
|
||||
RWNCssStyle,
|
||||
UseTailwindCallback,
|
||||
@@ -14,6 +13,7 @@ import {
|
||||
UseTailwindOptions,
|
||||
} from "./use-tailwind";
|
||||
|
||||
import { ComponentContext, useTailwindContext } from "./context";
|
||||
import { getRuntimeStyles } from "./runtime-styles";
|
||||
import { AtRuleRecord } from "./types/common";
|
||||
import { ChildClassNameSymbol } from "./with-styled-props";
|
||||
@@ -36,11 +36,9 @@ export function useTailwind<P>({
|
||||
active = false,
|
||||
flatten = true,
|
||||
}: UseTailwindOptions = {}) {
|
||||
const tailwindContext = useContext(TailwindContext);
|
||||
const tailwindContext = useTailwindContext();
|
||||
const componentInteraction = useContext(ComponentContext);
|
||||
|
||||
assertInContext(tailwindContext.platform);
|
||||
|
||||
return (className = "") => {
|
||||
const [styles, childStyles] = getRuntimeStyles<P>({
|
||||
className,
|
||||
@@ -62,11 +60,3 @@ export function useTailwind<P>({
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
function assertInContext(platform: string): asserts platform is string {
|
||||
if (!platform) {
|
||||
throw new Error(
|
||||
"No platform details found. Make sure all components are within a TailwindProvider with the platform attribute set."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useContext } from "react";
|
||||
import { ImageStyle, TextStyle, ViewStyle } from "react-native";
|
||||
import { TailwindContext } from "./context";
|
||||
import { useTailwindContext } from "./context";
|
||||
import {
|
||||
RWNCssStyle,
|
||||
UseTailwindCallback,
|
||||
@@ -19,13 +18,7 @@ export function useTailwind<
|
||||
P extends ViewStyle | TextStyle | ImageStyle | RWNCssStyle
|
||||
>(options?: UseTailwindOptions): UseTailwindCallback<P>;
|
||||
export function useTailwind<P>(options?: UseTailwindOptions) {
|
||||
const { platform, preview } = useContext(TailwindContext);
|
||||
|
||||
if (!platform) {
|
||||
throw new Error(
|
||||
"No platform details found. Make sure all components are within a TailwindProvider with the platform attribute set."
|
||||
);
|
||||
}
|
||||
const { platform, preview } = useTailwindContext();
|
||||
|
||||
if (platform === "web" && preview) {
|
||||
return (className = "") => {
|
||||
|
||||
Reference in New Issue
Block a user