cschmatzler

cschmatzler

Turboprop - Toolkit to create accessible component libraries

Hey all!

There’s been a lot of conversation lately both around server/client-side interactivity and a bunch of component libraries popping up for Phoenix. That’s great - and if we’re honest, many of the components that are available for Phoenix are not fully accessible. It’s a hard problem to solve, with keyboard interaction, focus trapping, ARIA labeling and more to keep in mind.
Thankfully, it’s a mostly solved problem in the JavaScript world, with libraries like HeadlessUI, Radix and… Zag, which is a framework-agnostic library packing a bunch of state machines to build accessible components.

So I’ve gone on a mission to create some Phoenix hooks that wrap these state machines, and a handful (not nearly enough at the moment!) are ready to see the light of day.
Along the path, I realised that I want a way to easily write different variants for my components, and, given that overrides are needed, also intelligently merge Tailwind classes to avoid conflicts.

So, this small idea turned into a whole toolkit that offers accessibility-enabling hooks, a variant API and Tailwind class merging utilities.

It’s called Turboprop, it’s still extremely early with especially the hooks API probably changing a lot in the near future, but I’m excited for it, so it’s time to share it!

Here’s a bunch of examples:

Variants

@variants %{
  variants: %{
    variant: %{
      default: "bg-primary text-primary-foreground shadow hover:bg-primary/90",
      destructive: "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
      outline: "border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
      secondary: "bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
      ghost: "hover:bg-accent hover:text-accent-foreground",
      link: "text-primary underline-offset-4 hover:underline",
    },
    size: %{
      default: "h-9 px-4 py-2",
      sm: "h-8 rounded-md px-3 text-xs",
      lg: "h-10 rounded-md px-8",
      icon: "h-9 w-9",
    },
  },
}
variant(@variants, variant: "destructive", size: "sm")
"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90 h-8 rounded-md px-3 text-xs"

Merge

merge(["px-2 py-1 bg-red hover:bg-dark-red", "p-3 bg-[#B91C1C]"])
"hover:bg-dark-red p-3 bg-[#B91C1C]"

Hooks

<div {dialog()}>
  <button
    class="rounded-md bg-blue-500 px-3 py-1.5 text-sm text-white shadow-sm hover:bg-blue-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500"
    {dialog_trigger()}
  >
    Open dialog
  </button>
  <div class="absolute inset-0 w-full h-full bg-gray-200" {dialog_backdrop()}></div>
  <div class="fixed inset-0 z-10 w-screen overflow-y-auto flex min-h-full items-center justify-center p-4" {dialog_positioner()}>
    <div class="w-full max-w-md rounded-xl bg-white p-6 outline-0" {dialog_content()}>
      <h2 class="text-base font-medium" {dialog_title()}>Dialog</h2>
      <span class="mt-2 text-sm" {dialog_description()}>Welcome!</span>
      <div class="mt-4">
        <button
          class="rounded-md bg-blue-500 px-3 py-1.5 text-sm text-white shadow-sm hover:bg-blue-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500"
          {dialog_close_trigger()}
        >
          Close
        </button>
      </div>
    </div>
  </div>
</div>

I’ll be adding a bunch more hooks in the future, probably revisiting the hooks API and options, documentation is a work in progress as always… The entire thing is less than two weeks old, so it’ll be a bit before we’re reaching 1.0!

Let me know if there’s a hook you really want to see next, or if something is desperately missing in the documentation, or just let me know you’re gonna use this in your projects!

Maybe, just maybe, there’ll be a component library from me as well soon.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

I’ve soft-deprecated tails, primarily based on the issues your tests pointed out, and point folks at turboprop instead :slight_smile: I spent ~2 hours fixing it, and got some of the issues addressed, before I remembered that I maintain like 50 packages at this point and I just don’t realistically have the time to actually fix tails :slight_smile:

Flo0807

Flo0807

Thanks for your work! The package looks very promising.

I’d love to see a Combobox hook.

cschmatzler

cschmatzler

Surprise!
https://hexdocs.pm/turboprop/Turboprop.Hooks.Combobox.html

0.4 contains a ton of breaking changes (again, still playing around and experimenting a lot so don’t use this in a real project just yet), but also Accordion and Combobox components and a change to TypeScript which is nice I guess.

Take it for a rest ride and let me know if anything smells!

cschmatzler

cschmatzler

Yeah, there’s some really funky stuff you can do with Tailwind and supporting it all isn’t trivial. That’s why I did write a parser for it :smiley:
I mean, these are valid classes, what the hell:

      assert merge([
               "[&[data-foo][data-bar]:not([data-baz])]:nod:noa:[color:red]",
               "[&[data-foo][data-bar]:not([data-baz])]:noa:nod:[color:blue]"
             ]) == "[&[data-foo][data-bar]:not([data-baz])]:noa:nod:[color:blue]"

Also, not taking it as a challenge! There’s some really slow stuff in this library (count the map merges in ClassTree.generate/0 and it will haunt you in your dreams), which is why I also added the cache in front of it. The good thing is that basically every application has a finite amount of class combinations, so slowness doesn’t matter as much with a cache. Definitely still a thing I want to overhaul before calling this stable and usable.

03juan

03juan

Very nice idea to incorporate Zag like this. Will definitely take a look soon and give some feedback.

Have you had a look at @zachdaniel’s library? zachdaniel/tails: Utilities for working with tailwind classes, like semantic merge, and conditional class lists. (github.com) He describes a method to share themes between the tailwind compiler and Elixir.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Th...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
381 12391 119
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement