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

style={tailwind()} vs className #2

Closed
ghost opened this issue Mar 9, 2020 · 7 comments
Closed

style={tailwind()} vs className #2

ghost opened this issue Mar 9, 2020 · 7 comments

Comments

@ghost
Copy link

ghost commented Mar 9, 2020

Is there a reason why you chose to do this implementation vs className?

<Text style={tailwind('text-white p-4')}>Hi</Text

vs

<Text className="text-white p-4">Hi</Text
@ecreeth
Copy link

ecreeth commented Mar 9, 2020

className is not a valid attribute in React Navive, so we would have to create our own implementation of the components to support that attr

@vadimdemedes
Copy link
Owner

The benefit of having a simple function that returns native styles is that you're free to use it however and wherever you want. Having a className would require re-exporting all view components - something I'm not interested in doing, as it has no benefit.

@Faisal-Manzer
Copy link

Can I disagree little? I know that is a lot of work. But many projects are using mono repo or react-native-web to make ReactJS and react-native work side-by-side. className will enable us to work in a very consistent manner.

@vadimdemedes
Copy link
Owner

@Faisal-Manzer In that case I think it's up to React Native to introduce this change first, not each individual library, otherwise it'll never become standard.

@gablabelle
Copy link

gablabelle commented Mar 13, 2021

I think it's an interesting point. As a React dev new to the react-native world and I was hoping to find something that would allow me to write my universal component styles like this

import React from 'react';
import { Header, Text } from '@ui';

function Example() {
  return (
    <Header className="flex flex-1 items-center justify-center">
      <Text className="text-lg md:text-2xl">Example</Text>
    </Header>
  );
}

export default Example;

Where Header is from an abstraction of @expo/html-elements + a react-native lib that supports tailwind like tailwind-rn

import {
  // ...
  Header as HeaderComp
} from '@expo/html-elements';
import {
  // ...
  Text as TextComp,
} from 'react-native';
import tailwind from 'tailwind-rn';

export const Header = ({
  children,
  className = '',
  ...props
}: ViewProps & { className?: string; children: ReactNode }) => {
  return (
    <HeaderComp style={tailwind(className)} {...props}>
      {typeof children === 'string' ? (
        <TextComp>{children}</TextComp>
      ) : (
        children
      )}
    </HeaderComp>
  );
};

Coupled with responsive tailwind classes support, this would make of your lib a no brainer choice for all the tailwind fan boys, like me, that wish to use react-native. 😄

@theoludwig
Copy link

Could you reopen the issue, please ? @vadimdemedes

I wish the same thing as @gablabelle, how could we reuse components as much as possible between react-dom and react-native with tailwindcss ?

It would be really cool, if we could develop with the web version with something like storybook, use tailwindcss as we normally would, and all that good stuff + ability to import theses components into react-native applications.

@renatodex
Copy link

renatodex commented Apr 16, 2021

Just an addition to this: In my tests, passing the className property do not yield any errors on React Native.
I've made the follow component as a Wrapper for the React Native View:

import React from 'react';
import { View } from 'react-native'
import tailwind from 'tailwind-rn';

export default function AppView (props) {
  const className = props.className

  return (
    <View
      {...props}
      style={{ ...props['style'], ...tailwind(className)}}
    >
      {props.children}
    </View>
  )
}

And in order to use it I can do:

import AppView from './app_view'

export default function App() {
   return (
       <AppView style={styles.container}>
           <AppView className="w-64 bg-purple-400 h-60 p-6 rounded-xl">
              <Text>Simple colored background</Text>
           </AppView>
       </AppView>
   )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants