From 39d01ca6eaf5d7684b8223e784bee7a0fc8272bc Mon Sep 17 00:00:00 2001 From: Mark Lawlor Date: Fri, 20 May 2022 11:24:03 +1000 Subject: [PATCH] refactor: cleanup assertIsInContext --- src/context.ts | 14 +++++++++++++- src/use-tailwind.native.ts | 14 ++------------ src/use-tailwind.web.ts | 11 ++--------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/context.ts b/src/context.ts index 61ac400..1450484 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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({ 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; diff --git a/src/use-tailwind.native.ts b/src/use-tailwind.native.ts index b5eddff..c76940b 100644 --- a/src/use-tailwind.native.ts +++ b/src/use-tailwind.native.ts @@ -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

({ 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

({ className, @@ -62,11 +60,3 @@ export function useTailwind

({ 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." - ); - } -} diff --git a/src/use-tailwind.web.ts b/src/use-tailwind.web.ts index 8bb3889..7218b44 100644 --- a/src/use-tailwind.web.ts +++ b/src/use-tailwind.web.ts @@ -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

; export function useTailwind

(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 = "") => {