Since youâre reading this article, chances are you're either considering a component library for your project/organisation, or you've already made the decision to adopt one.
I love component-based development and can wholeheartedly say itâs the way to go unless you have a compelling reason not to.
Switching to a component-based approach brings a lot of benefits:
Letâs start with a brand new app called âcomponentsâ.
In your command line, enter:
rails new components --css tailwind
This will set up our app for us, along with Tailwind for our style system.
Once thatâs complete, cd into our project and start the server:
cd components
rails server
Visit http://localhost:3000/ and you should see this:
Yay, Rails!
Weâre now going to dive straight in and set up ViewComponent and Lookbook.
In the creatorsâ own words, ViewComponent is âa framework for creating reusable, testable and encapsulated view components, built to integrate seamlessly with Ruby on Rails.â
Lookbook, meanwhile, is âa tool to help develop, test and document ViewComponents in isolation.â
Lookbookâs quickstart guide is amazing. Weâll follow it closely for these first few steps, then weâll get a little more adventurousâŠ
Letâs first install ViewComponent and Lookbook in our project by adding the following gems to our gemfile:
gem "view_component"
gem "lookbook"
Install those gems by running the following in your command line:
bundle install
Mount the lookbook engine in your routes file:
if Rails.env.development?
mount Lookbook::Engine, at: "/lookbook"
end
ViewComponent comes with a generator that gives us all the starting files we need for a component. In your command line, run:
rails generate component Button --preview
This will give us four files (including a preview file thanks to the âpreview flag):
app/components/button_component.rb
test/components/button_component_test.rb
test/components/previews/button_component_preview.rb
app/components/button_component.html.erb
Visit http://localhost:3000/lookbook, click on âButtonâ in the left-hand nav and you should see the following:
Glorious isnât it? Not really, I agree. Letâs see what we can doâŠ
Weâll keep this pretty simple, but ultimately we want to end up with a button that has any text the creator desires, along with a set of predefined colour schemes. Letâs jump in!
Head to the newly created app/components/button_component.rb file
Also open the newly created app/components/button_component.html.erb template file
I usually like to set these two files up side by side in my IDE like so:
â
Letâs get a basic button element in using a link_to helper, with some Tailwind styles for later. Replace all text inside button_component.html.erb with the following:
Check your preview to see what we have:
You may notice Iâve done a little foundational work in setting up a layout file, giving us that coloured background and some padding.
If youâd like to do this optional step, head back to the Lookbook docs and complete the short Preview Layouts section.
OK, so we have our super basic button. Lets start with allowing the dev using our component to change the text. Using a âslotâ feels appropriate for this task.
ViewComponentâs how-to guide is equally as impressive as Lookbookâs. Head to the slots section.
Seeing as we are not rendering another component, we are going to be using a âpassthrough slotâ, which is achieved simply by omitting a second argument to the renders_one method.
Add our passthrough slot to the button_component.rb file so it looks like this:
Back in the button_component.html.erb file we can replace
with
Meaning your two files should currently look like this:
â
If you visit Lookbook, youâll notice that your preview seems to have disappeared.
This is because weâre asking our component to render text from our text slot, but weâre not currently passing it anything. Letâs do that now.
Head to test/components/previews/button_component_preview.rb, which currently looks like this:
Open up a do block and pass it your desired text, like so:
â
Hey presto! Our first dynamic component. Note the source tab - this allows engineers to quickly reference the workings of a component and how it renders.
Letâs also add this as a dynamic param to our lookbook preview, along with some supporting docs. You can read more about this here, but for now, get your preview file to look like this:
The first three commented lines give us a title and documentation text for the Notes tab:
We then add our âTextâ param. Text is the name of the param, followed by text again which denotes the type in this case, followed by a brief explanation of what it does.
Weâre using this new param to replace the text in the screenshot below:
Finally, letâs add some basic styling for different colour schemes.
First, let's set up our button_component.rb file like so:
So whatâs happening here? Weâre setting up a hash under the constant name âSCHEMEâ.
This hash holds different Tailwind classes for each scheme inside of it.
Below this we see the initialize block - as you probably guessed, this is what the component initialises with. Here we are saying the component needs to be passed a scheme upon initialisation. If a scheme is not passed manually, it will default to :primary.
We raise an argument error in this block if an incorrect scheme argument is passed - for ease of debugging, itâs a good idea to raise this error here, as close to the source as possible.
Finally, we define âschemeâ at the bottom. This is essentially saying âWhatever scheme is passed in, find the corresponding hash key and return that valueâ. We can then place this in the template to yield the resulting Tailwind classes.
In the template, itâs as simple as adding âschemeâ to the class list. Be sure to open up an array, and comma separate the static classes and our new scheme class, like so:
Letâs head back to our preview file and allow for scheme switching:
Whatâs changed here?
Letâs fire Lookbook back up and see what we have:
And there you have it!
Hopefully that was pretty quick and painless. Thanks for reading and happy coding!