Building a design system?

Hi Guys, I'm taking first steps to build a design system... would love your advice before i go too deep. I'm using tailwind-styled-components: https://github.com/MathiasGilson/tailwind-styled-component For my first component, i created a Button, and used it, like so: src/designsys/Button.tsx
import tw from "tailwind-styled-components";

const Button = tw.div`
flex
w-48
items-center
rounded-full
bg-white/10
px-4
py-2
font-semibold
text-white
no-underline
transition
hover:bg-white/20
`;

export default Button;
import tw from "tailwind-styled-components";

const Button = tw.div`
flex
w-48
items-center
rounded-full
bg-white/10
px-4
py-2
font-semibold
text-white
no-underline
transition
hover:bg-white/20
`;

export default Button;
I then use it in my google login button:
import React from "react";
import { FaGoogle } from "react-icons/fa";
import { signIn } from "next-auth/react";
import Button from "src/designsys/Button";

type ButtonProps = {
prompt?: string;
provider?: "google" | "gmail";
};

const GoogleLoginButton: React.FC<ButtonProps> = ({
prompt = "Login with Google",
provider = "google",
}) => {
return (
<Button onClick={() => signIn(provider)}>
<span>
<FaGoogle />
</span>
&nbsp;&nbsp;
{prompt}
</Button>
);
};

export default GoogleLoginButton;
import React from "react";
import { FaGoogle } from "react-icons/fa";
import { signIn } from "next-auth/react";
import Button from "src/designsys/Button";

type ButtonProps = {
prompt?: string;
provider?: "google" | "gmail";
};

const GoogleLoginButton: React.FC<ButtonProps> = ({
prompt = "Login with Google",
provider = "google",
}) => {
return (
<Button onClick={() => signIn(provider)}>
<span>
<FaGoogle />
</span>
&nbsp;&nbsp;
{prompt}
</Button>
);
};

export default GoogleLoginButton;
It's simple and working nicely. Does anyone have any comments on how to do this better? Thanks!
GitHub
GitHub - MathiasGilson/Tailwind-Styled-Component: Create Tailwind C...
Create Tailwind CSS React components like styled components with class names on multiple lines and conditional class rendering - GitHub - MathiasGilson/Tailwind-Styled-Component: Create Tailwind CS...
88 Replies
oljimenez
oljimenez•2y ago
i think cva is better for creating design systems with tailwind css. The other way is using a design system focus library like vanilla extract or stitches. https://github.com/joe-bell/cva
GitHub
GitHub - joe-bell/cva: Class Variance Authority
Class Variance Authority. Contribute to joe-bell/cva development by creating an account on GitHub.
Valhalaar
Valhalaar•2y ago
Why did you decide to go with styled-components? Not sure I'd recommend that, especially since you're just starting out and can choose something else that's more performant (i.e. vanilla extract like mentioned above).
Brendonovich
Brendonovich•2y ago
In Spacedrive we use that same styled components approach using a custom, more lightweight version of the tw function, but only for basic elements that don't need multiple variants. For more complex styles with multiple options class variance authority is definitely your friend.
fotoflo
fotofloOP•2y ago
ooo a lot to think about guys thanks I started with styled components cause I've used it for years
Brendonovich
Brendonovich•2y ago
For example our Tabs ui is just a restyled version of Radix UI, but our Dropdown ui is done with CVA and is more complex
fotoflo
fotofloOP•2y ago
im also familiar with passing variables in and building themes with cva, can you pass props in and make themes @Brendonovich ?
fotoflo
fotofloOP•2y ago
https://vanilla-extract.style/ looks like you clearly can
vanilla-extract
vanilla-extract — Zero-runtime Stylesheets-in-TypeScript.
Zero-runtime Stylesheets-in-TypeScript.
Brendonovich
Brendonovich•2y ago
CVA just gives you a function that returns styles and provides Typescript types that match your variants. You pass your props to that function and then apply the styles to your elements. vanilla extract is CVA but for actual css and not tailwind
oljimenez
oljimenez•2y ago
is more than that
Brendonovich
Brendonovich•2y ago
Here's out Input component, notice how we pass props to inputStyles and the return value is used as class names
oljimenez
oljimenez•2y ago
and is for css modules, not pure css this is so clean
Brendonovich
Brendonovich•2y ago
ehh same thing though it handles themeing too right?
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
this looks a little scary
oljimenez
oljimenez•2y ago
lol
Valhalaar
Valhalaar•2y ago
it's just building a string
fotoflo
fotofloOP•2y ago
Brendonovich
Brendonovich•2y ago
Oh dw about clsx and props.className inputStyles just returns a string
fotoflo
fotofloOP•2y ago
so builds these class strings and typechecks on tailwind
Brendonovich
Brendonovich•2y ago
it typechecks the input and gives a string as output yeah
fotoflo
fotofloOP•2y ago
? and StyledComponent replaces the element call
Brendonovich
Brendonovich•2y ago
hmm?
fotoflo
fotofloOP•2y ago
i created <Button> with styled components. with cva I would use regular dom components and do <div className={button()} /> (or some syntax like that)
Brendonovich
Brendonovich•2y ago
Yep
fotoflo
fotofloOP•2y ago
Got it (i think)
Brendonovich
Brendonovich•2y ago
As you expand your design system you'll likely need to switch to cva for stuff like buttons, since they by design need some level of customisation
fotoflo
fotofloOP•2y ago
or i would create <BlueButton> and <GreenButton> which would get messy
Valhalaar
Valhalaar•2y ago
I wouldn't recommend that ^ just for colors you'll have an explosion of buttons
fotoflo
fotofloOP•2y ago
just as an example
Valhalaar
Valhalaar•2y ago
yeah
Brendonovich
Brendonovich•2y ago
ye just cva it, you'll have typesafety with what variants you can pick and you'll have a great time
Brendonovich
Brendonovich•2y ago
btw the typesafety comes from cva's VariantProps helper
Brendonovich
Brendonovich•2y ago
This example adds on some additional, non-styling related props to those from cva
fotoflo
fotofloOP•2y ago
interesting the CVA syntax still feels daunting... but the explosion of buttons sounds worse and the typesafety is nice
Brendonovich
Brendonovich•2y ago
trust me, you'll come to love it
fotoflo
fotofloOP•2y ago
but i like calling <Button>
Brendonovich
Brendonovich•2y ago
if u want to pick a button color it'll be like <Button color="red" size="lg"/>
fotoflo
fotofloOP•2y ago
btw thank you for this guidance
Valhalaar
Valhalaar•2y ago
or instead of color you can drive it via variant which handles things like colors behind the scenes
NekoChan
NekoChan•2y ago
Vercel
YouTube
Tru Narla: Building a design system in Next.js with Tailwind
Learn how to set up a design system in a Next.js application, and how to build an accessible and easily customizable user interface with Tru Narla, Software Engineer at Discord. Design at the moment of inspiration: https://vercel.fyi/WEIb8yH Speaker: https://twitter.com/trunarla
Brendonovich
Brendonovich•2y ago
ye we use the variant approach was just gonna grab that video haha
Valhalaar
Valhalaar•2y ago
yeah great video
fotoflo
fotofloOP•2y ago
If i want to pick a button color with styled <Button> I would do <Button className="bg-red-500"> and make a mess... thanks guys gonna watch that video
Brendonovich
Brendonovich•2y ago
ehh it's not so bad, having the ability to override is always nice just don't do it too often
Valhalaar
Valhalaar•2y ago
can have the best of both worlds
fotoflo
fotofloOP•2y ago
yeah i dont think its so bad
Brendonovich
Brendonovich•2y ago
i will say that what this approach doesn't address is themeing
fotoflo
fotofloOP•2y ago
im doing it already in my second component
Valhalaar
Valhalaar•2y ago
Your Button can take variant prop and also spread ...delegated or w/e you wanna call other props to override
fotoflo
fotofloOP•2y ago
ive done styled compoinent themeing before with the styled theming HOC
Valhalaar
Valhalaar•2y ago
depends on how strict you wanna be I guess... normally a design system I don't think would allow for such a freeform escape hatch like ...delegated, but it depends
fotoflo
fotofloOP•2y ago
watching that video
Brendonovich
Brendonovich•2y ago
yeah styled does everything at runtime i think so they can just swap out colors and such, we have a custom themeing system built on css variables
fotoflo
fotofloOP•2y ago
good stuff to think about
Brendonovich
Brendonovich•2y ago
instead of using regular tailwind colors we use predefined ones that can be altered with variables
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
from a simple project i did
Brendonovich
Brendonovich•2y ago
classic styled components
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
click the little sware square >yeah styled does everything at runtime when does cva do it? build time? (this is my first time using tailwind btw)
Brendonovich
Brendonovich•2y ago
yeah tailwind classes generate css at build time
Brendonovich
Brendonovich•2y ago
to remedy that we've added colors to our tailwind config that are derived from css variables
fotoflo
fotofloOP•2y ago
how do you call those colors? i think i have to watch a couple videos on tailwind 🙂
Brendonovich
Brendonovich•2y ago
just like regular tailwind colors bg-blue-500 can be bg-app-button
fotoflo
fotofloOP•2y ago
className="ink-dull"?
Brendonovich
Brendonovich•2y ago
text-gray-200 can be text-ink-faint
fotoflo
fotofloOP•2y ago
oh i see they can extend the type of enity that's getting selected
Brendonovich
Brendonovich•2y ago
yeah u can fully customise tailwind stuff
fotoflo
fotofloOP•2y ago
cool @Brendonovich thank you. You dont know wht you dont know... lot to know here
Brendonovich
Brendonovich•2y ago
happy to help haha
fotoflo
fotofloOP•2y ago
gonna deep dive Ohh coming back to this - is Styld components slow for some reason? i think its compiled by swc?
Valhalaar
Valhalaar•2y ago
It does all of its style shit at runtime
Valhalaar
Valhalaar•2y ago
Anvil
Measuring React styled-components performance & best practices
Learn how to measure the performance of your React styled-components and implement best practices to keep your code concise and efficient.
Valhalaar
Valhalaar•2y ago
for small apps or w/e it's probably not gonna matter too much but if I were starting a new project today I would not choose a runtime CSS-in-JS solution idk if your particular tailwind variety of styled-components does something differently I would probably go for vanilla extract like someone mentioned higher up in the thread since there's no runtime cost to my knowledge
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
That looks much neater thank you HerrSchade.
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Brendonovich
Brendonovich•2y ago
Oh I didn’t know that haha
Kseikyo
Kseikyo•2y ago
Random question: In that second picture, why do you have that undefined selected variant? Wouldn’t that be solved with a defaultVariant?
Brendonovich
Brendonovich•2y ago
yeah it would, i didn't make those styles tho so it's been done that way haha will refactor a bunch of that stuff at some point
benten
benten•2y ago
could also use clazzx
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
hi @Kharann i dont recall having a problem with the color preview
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
this one right?
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
sorry what is the question ?
Want results from more Discord servers?
Add your server