← Back to context

Comment by h4x0rr

12 days ago

Yeah let's do that. You have everything related to your component on place instead of jumping between files.

Vue, Svelte, and Surface manage to do this without forcing you to inline all your styles

  • Jumping up and down in the file is not much better and you still need to come up with names for classes. I want to look at an element and immediately know how it's styled.

    • From Tailwind's home page:

      <div class="h-112 p-4 sm:p-8 relative overflow-hidden rounded-lg bg-gray-950/[2.5%] after:pointer-events-none after:absolute after:inset-0 after:rounded-lg after:inset-ring after:inset-ring-gray-950/5 dark:after:inset-ring-white/10 bg-[image:radial-gradient(var(--pattern-fg)_1px,_transparent_0)] bg-[size:10px_10px] bg-fixed [--pattern-fg:var(--color-gray-950)]/5 dark:[--pattern-fg:var(--color-white)]/10">[...]

      "immediately" is a stretch

      4 replies →

  • I worked with both. Scoped styles are nice. Tailwind is better - no naming of every element, no mental tax of jumping around in the file -

What stops you from doing the same thing in CSS? It is trivial to assign a specific CSS class to an element that is the root node of a "component" and scope rules under that.

Is jumping between files supposed to be difficult or something?

  • Colocation is a useful principle in component-based architecture.

    • In my lived experience, shared components just become another problem. Especially in a fledgling company, the iteration velocity is actually negatively affected by shared libs because there's always overhead to (not) break legacy. so shared components bloat to address every evolving need.

      And now with AI generated code i see so many wrapper patterns that forward endless props down, it's crazy!

      TLDR: i almost always end up branching out into evergreen "reusable" components anyway.

      Very unlikely the component library the CTO asked claude to DRY up the code with, is the one to rule them all.

      1 reply →

  • Without a lot of discipline it is very easy to end up with a css with lots of unclear and hard to guess effects. Eg consider the case of <A type=1><B><A type=2></A></B></A> where A and B are complex templates. Any selector with the " " operator on A risk expanding to the inner A even if it was intended only for the outer. Similarly a :has selector might catch a descendant of the wrong element.

    @scope fixes a lot of this, but it is a complex problem. With tailwind you mostly have to worry about inheritance

  • I find it to be more difficult. Especially if I can't pane the files in view comfortably (ie. beyond 2 or 3 it gets significantly harder to work across them).

    Some frameworks or coding styles really lean into having lots of tiny files. That necessitates a more complicated directory structure for the project. Locating files eventually tends to requires search capability rather than being able to look through the tree in a sidebar.

    None of this is "hard" per se but I find the opposite is nicer to work with typically.

  • Yes.

    The problem is that the styles for something can be defined in multiple places, and that makes it hard. Especially with CSS and (potentially) having specificity issues if things aren't managed well. Having them as a part of the component means that problem goes away.

  • Is staying in one file supposed to be difficult or something?

    • this is grey text from tailwindcss.com, I wouldn't call it easy and readable.

      <div class="relative before:absolute before:top-0 before:h-px before:w-[200vw] before:bg-gray-950/5 dark:before:bg-white/10 before:-left-[100vw] after:absolute after:bottom-0 after:h-px after:w-[200vw] after:bg-gray-950/5 dark:after:bg-white/10 after:-left-[100vw]"><p class="max-w-(--breakpoint-md) px-2 text-base/7 text-gray-600 max-sm:px-4 dark:text-gray-400">Because Tailwind is so low-level, it never encourages you to design the same site twice. Some of your favorite sites are built with Tailwind, and you probably had no idea.</p></div>

      7 replies →

  • You can literally command click a class to go into a styled components css file. I do not understand what the big issue is.

    • Cognitive load of looking at 12 open files trying to understand what’s happening. Well, in fairness some of those 12 are the same file because we have one part for the default CSS and then one for the media query that’s 900 lines further down the file.

      5 replies →

  • Also modern CSS is often written in a <style> tag either in a native web component or in a framework which supports single file component like vue or svelte.