Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it work with nativewind or support react-native? #98

Open
Tracked by #154
ammarfaris opened this issue Mar 20, 2023 · 12 comments
Open
Tracked by #154

Does it work with nativewind or support react-native? #98

ammarfaris opened this issue Mar 20, 2023 · 12 comments
Labels
documentation Improvements or additions to documentation

Comments

@ammarfaris
Copy link

ammarfaris commented Mar 20, 2023

Hi there, nice work on tw-classed! Just wondering if it work with nativewind https://github.com/marklawlor/nativewind (to make it work with react-native) or will you support react-native in the future?

Thanks!

@sannajammeh
Copy link
Owner

sannajammeh commented Mar 20, 2023

V3 preview of Nativewind has support and this library wont be needed then, but it has some Expo EAS issues.

We run Nativewind V2 + tw-classed in production without any issues.

Simple example:

import { Text as RNText } from "react-native";
import { styled } from "nativewind";
import { classed, deriveClassed } from "@tw-classed/react";

const StyledText = styled(RNText);

export const Text = classed(StyledText, {
  variants: {
    color: {
      blue: "text-blue-500",
      green: "text-green-500",
    }
  }
});

// In app
() => <Text color="blue" />

Complex example:

This is a more complex version button because React Native dont support style inheritance so we must define a base for the button and also a text component for its text. Then merge it together using deriveClassed to get full TS support.
See: https://tw-classed.vercel.app/docs/derived-components

import { styled } from "nativewind";
import { classed, deriveClassed } from "@tw-classed/react";

const StyledTouchableOpacity = styled(TouchableOpacity);

export const ButtonBase = classed(StyledTouchableOpacity, {
  variants: {
    color: {
      primary: "bg-primary",
      secondary: "bg-secondary",
      slate: "bg-slate-300",
    },
    size: {
      sm: "px-2 py-2",
      md: "px-4 py-3",
      lg: "px-6 py-4",
    },
    rounded: {
      sm: "rounded-sm",
      md: "rounded-md",
      lg: "rounded-lg",
      xl: "rounded-xl",
      "2xl": "rounded-2xl",
      full: "rounded-full",
    },
  },

  defaultVariants: {
    color: "primary",
    size: "md",
    rounded: "2xl",
  },
});

const StyledText = styled(Text);

export const ButtonText = classed(StyledText, {
  variants: {
    color: {
      primary: "text-white",
      secondary: "text-white",
      slate: "text-slate-800",
    },
  },

  defaultVariants: {
    color: "primary",
  },
});

// Final UI Button
// color is the shared prop between the two so we deconstruct and add to both
export const Button = deriveClassed<typeof ButtonBase>(
  ({ children, loading, color, ...props }, ref) => {
    return (
      <ButtonBase
        color={color}
        ref={ref}
        {...props}
      >
       <ButtonText
              color={color}
              className="text-center"
            >
              {children}
            </ButtonText>
      </ButtonBase>
    );
  }
);

@sannajammeh
Copy link
Owner

Since nativewind will have support for this in V3 natively, I will add some docs for doing it manually with tw-classed for now. Leaving issue open until docs are complete

@sannajammeh sannajammeh added the documentation Improvements or additions to documentation label Mar 20, 2023
@ammarfaris
Copy link
Author

thanks for your time and explanation ; and for the (future) docs, appreciate it!

@sannajammeh
Copy link
Owner

Docs are now up in the Guides section

@gvNN7
Copy link

gvNN7 commented Mar 5, 2024

I currently using tw-classed with nativewind@v2, and doesn't encounter any problems at all, it's perfect. But i'm little bit worried, since nativewind is dropping styled in @v4, how tw-classed can achieve the same result or similar in the "lastest" version?

Release note: https://www.nativewind.dev/v4/announcement#removal-of-styled

@sannajammeh
Copy link
Owner

I currently using tw-classed with nativewind@v2, and doens't encounter any problems at all, it's perfect. But i'm little bit worried, since nativewind is dropping styled in @v4, how tw-classed can achieve the same result or similar in the "lastest" version?

Release note: https://www.nativewind.dev/v4/announcement#removal-of-styled

According to the library author, it should work directly, but lets leave this open just in case :)

@sannajammeh sannajammeh reopened this Mar 6, 2024
@PrajTS
Copy link

PrajTS commented Jun 16, 2024

Hey @sannajammeh , this does not seem to work with v4. No styles seems to get applied.

Eg:
export const XStack = classes(View, 'flex-row')

seem to have no affect. It still renders a default View with flex-col

@sannajammeh
Copy link
Owner

Hey @sannajammeh , this does not seem to work with v4. No styles seems to get applied.

Eg:

export const XStack = classes(View, 'flex-row')

seem to have no affect. It still renders a default View with flex-col

Could you provide a Repro?

@PrajTS
Copy link

PrajTS commented Jun 17, 2024

@visualcookie
Copy link

I'm facing the same issue now too, but other than the View, it does not work for me on React Native's text component. I've created the following classed:

export const Text = classed(RNText, {
  variants: {
    hierarchy: {
      [Hierarchy.Primary]: 'text-white',
      [Hierarchy.Secondary]: 'text-primary',
      [Hierarchy.Link]: 'text-petrol',
    },
  },
});

But the library does not render any of these classes. From what I've seen in the React Native React Devtools is, that there is not CssInterop being set for the Text classed, while for my container, which is a Pressable there is, hence why the background switching per hierarchy for my buttons work, but the switching to a different text color doesn't. I also have another problem: the disabled state is also not working, even tho it is defined as per the docs.

From the looks at it, your library may still need an update for Nativewind v4.

@sannajammeh
Copy link
Owner

I'm facing the same issue now too, but other than the View, it does not work for me on React Native's text component. I've created the following classed:

export const Text = classed(RNText, {
  variants: {
    hierarchy: {
      [Hierarchy.Primary]: 'text-white',
      [Hierarchy.Secondary]: 'text-primary',
      [Hierarchy.Link]: 'text-petrol',
    },
  },
});

But the library does not render any of these classes. From what I've seen in the React Native React Devtools is, that there is not CssInterop being set for the Text classed, while for my container, which is a Pressable there is, hence why the background switching per hierarchy for my buttons work, but the switching to a different text color doesn't. I also have another problem: the disabled state is also not working, even tho it is defined as per the docs.

From the looks at it, your library may still need an update for Nativewind v4.

I'm on it! Quite busy with project releases with the coming weeks, but we should have this done very soon :)

@PrajTS
Copy link

PrajTS commented Sep 3, 2024

Hey, any update on this?

@sannajammeh sannajammeh mentioned this issue Sep 3, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants