DEV Community

Rails Designer
Rails Designer

Posted on • Edited on • Originally published at railsdesigner.com

1

Use CSS' `only-child` instead of `if/else` logic

This article was originally published on Rails Designer


Rails developers are spoiled when talking about not needing to write code. Lots of the tedious things are done for you. Hotwire leveled up the amount of custom JavaScript that is not needed (to be written).

But CSS has become really powerful to over the past years. Whenever I can I try to use CSS to do the logic for me.

One of those situations is empty states. For example, a messages inbox:

Image description

The HTML looks something like this:

<ul class="divide-y divide-gray-100">
  <li>
    <!-- Message preview here -->
  </li>
</ul>
Enter fullscreen mode Exit fullscreen mode

Now previously you might solve this with a if/else statement:

<ul class="divide-y divide-gray-100">
    <li>
      <!-- Message preview here -->
    </li>

    <⁠% if @messages.none? %>
      <li>
        <p class="text-base font-normal leading-tight text-center text-gray-400">
          No messages yet
        </p>
      </li>
    <⁠% end %>
</ul>
Enter fullscreen mode Exit fullscreen mode

But with the only-child pseudo-class (I am using Tailwind CSS' only utility class here, you can write it like so:

<ul class="divide-y divide-gray-100">
  <li>
    <!-- Message preview here -->
  </li>

  <li class="hidden only:flex">
    <p class="text-base font-normal leading-tight text-center text-gray-400">
      No messages yet
    </p>
  </li>
</ul>
Enter fullscreen mode Exit fullscreen mode

Here the (wrapping) li-element is hidden (display: none;) by default and only(!) if it's the only element display it (eg. display: flex).

The vanilla CSS could be written like this:

.li {
  display: none;
}

.li:only-child {
  display: flex;
}
Enter fullscreen mode Exit fullscreen mode

One big upside of this solution, is that if you remove the messages via Turbo Streams, the “logic” is automatically applied without needing to update the complete list (ul-element) for the logic to kick in.

Pretty cool, right?

This is the first article in a collection of Selector Logic

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (2)

Collapse
 
railsdesigner profile image
Rails Designer

Got other ideas & cases how you could only-child?

Collapse
 
railsdesigner profile image
Rails Designer

Discussed on:

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more