Closed Teqshark closed 1 year ago
this is most often caused by failure to connect the tw function to the source of runtime device information as described in the readme here:
https://github.com/jaredh159/tailwind-react-native-classnames#enabling-device-context-prefixes
Did you do that step?
@jaredh159 Thanks so much I dont know how I missed that after looking over the docs so many times ! That is the only thing i had not done!
no worries, it's easy to miss stuff like that 😄
@Teqshark how did you get it working with expo? I am using expo-router, and I cannot execute the hook at the root level as it is not exposed, where should I call it to make it work ? :) @jaredh159
@tomh4 i would just try executing the hook at the highest point of the hierarchy that i control. you just need it higher than any of your usages of the tw
fn, so it should still work. (provided you're targeting RN native, and not RN web).
@jaredh159 thanks, ok, I tried that but it seems not to work correctly. Basically I am using Expo Router, targeting iOS, and calling it inside the _layout.tsx directly inside the app folder, so it is the first thing that is loaded when the app starts. It stays in bright mode though :(
hmmm..... have you looked at this issue? https://github.com/jaredh159/tailwind-react-native-classnames/issues/112
wondering if there is some similar memoization going on with expo router. i'm not familiar at all with that package. otherwise, is you project open source where i could look at the code in it's entirety?
the project is not open source, but it is basically a fresh project. I pushed it here to this public remote, take a look if you want :) https://github.com/tomh4/tailwind-expo-router
@jaredh159 does the project work with dark/bright mode for you ?
same issue here, useDeviceContext
only works at a single component level, no effect at root level.
@tomh4 i pulled your project down, but couldn't get the dependencies installed, got an error:
CommandError: It looks like you're trying to use web support but don't have the required dependencies installed.
Please install react-dom@18.2.0 by running:
npx expo install react-dom@18.2.0
If you're not using web, please ensure you remove the "web" string from the platforms array in the project Expo config.
but i've never used expo before, and a minute or two of poking around and i couldn't find an expo config file.
edit: ran the expo install command, and i can get it building, but i'm getting errors in the ios simulator, so can't tell.
did you look into issue #112?
Okay, so I had a similar problem working with an Expo app, except mine didn't get resolved even after checking if I had added useDeviceContext (I had).
Go to the app.json
file in your project root, and set the following:
{
"expo": {
"userInterfaceStyle": "automatic"
}
}
For some reason, Expo keeps the default as "light" which forces your app to run in light mode, regardless of system preferences. https://docs.expo.dev/develop/user-interface/color-themes/
thanks @anirudhgray , mine was set as light as well so that was definitely one of the issues. However, it is still not loading my dark attributes, can you tell me where you did put the useDeviceContext?
@tomh4 I've put useDeviceContext in App.js
since that's my root component (my navigation container and all is in there). Not sure how to go about it in your case with Expo router.
I'm using Expo router,
On every page, I need to add useDeviceContext(tw)
or useColorScheme()
to make the page responsive to Appearance theme changes,
if I didn't add these hooks, the page will not change even tw.setColorScheme
manually.
same , useDeviceContext(tw) not working on top app.js
Same for me, with Expo Router useDeviceContext not working on top app.js
A temp workaround is to change the route stack key :
// app/_layout.tsx
function useRouterKey(): string {
const dimensions = useWindowDimensions();
const colorScheme = useColorScheme();
return useMemo(() => {
return JSON.stringify({ ...dimensions, colorScheme });
}, [dimensions, colorScheme]);
}
export function RootLayout() {
useDeviceContext(tw);
const routerKey = useRouterKey();
return (
<ReactNavigationTheme>
<Stack key={routerKey}>
<Stack.Screen name="(auth)" options={{ headerShown: false }} />
<Stack.Screen name="(main)" options={{ headerShown: false }} />
</Stack>
</ReactNavigationTheme>
);
}
I'll be taking a closer look in the near future to find a permanent solution.
I have been unable to get the modifiers (screen, state, etc) to work in my expo react native project. using twrnc^3.4.1 Most of everything else seems to work fine. I configured the tailwind.config file like below and my custom colors worked fine using this config file however custom breakpoints never worked nor did the default ones at all not even when I try on a fresh tailwind integration before trying to customize it.
Also I noticed none of the state modifier have worked i.e. (hover:, ios:, android:, dark:, light: etc.) as stated none of them have worked even before i configure the config file.
I looked at the tailwindcss docs and stackoverlfow for example config files and while the custom colors and classes work fine the screens/breakpoints and default modifiers never do.
here is my tailwind.config.js :
`/* @type {import('tailwindcss').Config} / // tailwind.config.js const { plugin } = require('twrnc'); const defaultTheme = require('tailwindcss/defaultTheme') // or, you can use tailwinds plugin function: // const plugin = require('tailwindcss/plugin');
module.exports = { important: true, // purge: [], content: ["./src/*/.{js,jsx,ts,tsx}", "./app.js"], // this line is important to use tailwind CSS. theme: { screens: { 'sm': '640px', // => @media (min-width: 640px) { ... }
}, // variants: {}, plugins: [ plugin(({ addUtilities }) => { addUtilities({
], // content: [],
}
`
package.json :
{ "name": "adriancodes", "version": "1.0.0", "main": "./entry.js", "scripts": { "start": "expo start", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web" }, "dependencies": { "@expo/webpack-config": "^0.17.2", "@react-native-community/blur": "^4.3.0", "debounce": "^1.2.1", "dom-to-image": "^2.6.0", "expo": "~47.0.6", "expo-blur": "~12.0.1", "expo-gl": "~12.0.1", "expo-image-picker": "~14.0.2", "expo-media-library": "~15.0.0", "expo-splash-screen": "~0.17.5", "expo-status-bar": "~1.4.2", "expo-three": "^7.0.0", "firebase": "^9.15.0", "hsl-to-hex-v2": "^1.0.0", "react": "18.1.0", "react-dom": "18.1.0", "react-hook-form": "^7.40.0", "react-native": "0.70.5", "react-native-canvas": "^0.1.38", "react-native-gesture-handler": "~2.8.0", "react-native-picker-select": "^8.0.4", "react-native-reanimated": "~2.12.0", "react-native-view-shot": "3.4.0", "react-native-web": "~0.18.9", "simplex-noise": "^4.0.1", "three": "^0.147.0", "twrnc": "^3.4.1" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/plugin-proposal-export-namespace-from": "^7.18.9" }, "private": true }
tailwind.js :
`import {create } from 'twrnc';
const tw = create(require('../../tailwind.config.js'));
export default tw; `
console.logging gives the expected object when not using modifiers like so:
console.log('tw-', tw
bg-gold-400);
and gives empty objects when trying to use the modifiers:console.log('tw-', tw
light:bg-red-500 dark:bg-blue-500);
was really looking forward to using this package however i will have to use an alternative if i can't get the modifiers to work
please help!