-
How do you implement a reusable component that passes through classNames which may not be tailwind utility classes? From the docs, the recommendation is to pass through component consumer's className to the const Button = (props) => {
const instanceStyles = apply(`ban-red ...`)
return <button {...props} className={tw(instanceStyles, props.className)}>
}
...
// my issue:
// doesn't work
<Button className={ tw( apply(`text-white ...`) ) } />
// works but users are confused whether to use `tw` here or not
<Button className="text-white ...." /> My problem with this is that the consumers of this component has to use tailwind's classes for this component to work, and. also introduces confusion on when to use tw or not. Right now, what I do is to just do concat ( or clsx ) on these components. // this is how I do it right now
const Button = props => <button {...props} className={`${tw(apply`bg-red ...`)} ${props.className}`} />
// now I know that this will just be passed through the component
<Button className={tw`text-white`} /> This is way less confusing for me as a user of this component as I know that If I wanted to use tailwind classes, I just use the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
The last example you have is what I'd have done as well. By the way, this may interest you: https://github.com/tw-in-js/twind-jsx-preprocessor |
Beta Was this translation helpful? Give feedback.
-
Have you taken a look at twind/style and its companion @twind/react ? Both support passing |
Beta Was this translation helpful? Give feedback.
Have you taken a look at twind/style and its companion @twind/react ? Both support passing
className
through and add thetw
prop which are tokens/class names that are passed to thetw
function.