👀 Lookbook 👀

A native development UI for ViewComponent

Gem version Ruby Style Guide Code style: Prettier
--- **Lookbook gives [ViewComponent](http://viewcomponent.org/)-based projects a _ready-to-go_ development UI for navigating, inspecting and interacting with component previews.** It uses (and extends) the native [ViewComponent preview functionality](https://viewcomponent.org/guide/previews.html), so you don't need to learn a new DSL or create any extra files to get up and running. Lookbook uses [RDoc/Yard-style comment tags](#annotating-preview-files) to extend the capabilities of ViewComponent's previews whilst maintaining compatability with the standard preview class format, so you can add or remove Lookbook at any time without having to rework your code. ![Lookbook UI](.github/assets/lookbook_screenshot.png) ### Features - Tree-style navigation menu with live search/filter - Resizable preview window for responsive testing - Highlighted preview source code and HTML output - Auto-updating UI when component or preview files are updated _(Rails v6.0+ only)_ - Use comment tag annotations for granular customisation of the preview experience - Fully compatible with standard the ViewComponent preview system ## Lookbook demo If you want to have a quick play with Lookbook, the easiest way is to [give the demo app](https://github.com/allmarkedup/lookbook-demo) a spin. It's a basic Rails/ViewComponent app with a few test components included to tinker with. The [demo app repo](https://github.com/allmarkedup/lookbook-demo) contains instructions on how to get it up and running. ## Installing ### 1. Add as a dependency Add Lookbook to your `Gemfile` somewhere **after** the ViewComponent gem. For example: ```ruby gem "view_component", require: "view_component/engine" gem "lookbook" ``` ### 2. Mount the Lookbook engine You then need to mount the Lookbook engine (at a path of your choosing) in your `routes.rb` file: ```ruby Rails.application.routes.draw do if Rails.env.development? mount Lookbook::Engine, at: "/lookbook" end end ``` The `at` property determines the root URL that the Lookbook UI will be served at. > If you would like to expose the Lookbook UI in production as well as in development, just remove the `if Rails.env.development?` condition from around the mount statement. Then you can start your app as normal and navigate to `http://localhost:3000/lookbook` (or whatever mount path you specified) to view your component previews in the Lookbook UI. ## Usage You don't need to do anything special to see your ViewComponent previews and examples in Lookbook - just create them as normal and they'll automatically appear in the Lookbook UI. Preview templates, custom layouts and even bespoke [preview controllers](https://viewcomponent.org/guide/previews.html#configuring-preview-controller) should all work as you would expect. > If you are new to ViewComponent development, checkout the ViewComponent [documentation](https://viewcomponent.org/guide/) on how to get started developing your components and [creating previews](https://viewcomponent.org/guide/previews.html). ## Annotating preview files Lookbook parses [Yard-style comment tags](https://rubydoc.info/gems/yard/file/docs/Tags.md) in your preview classes to customise and extend the standard ViewComponent preview experience: ```ruby # @label Basic Button # @display bg_color "#fff" class ButtonComponentPreview < ViewComponent::Preview # Primary button # --------------- # This is the button style you should use for most things. # # @label Primary def default render ButtonComponent.new do "Click me" end end # Inverted button # --------------- # For light-on-dark screens # # @display bg_color "#000" def secondary render ButtonComponent.new(style: :inverted) do "Click me" end end # Unicorn button # --------------- # This button style is still a **work in progress**. # # @hidden def secondary render ButtonComponent.new do "Click me" end end # @!group More examples def short_text render ButtonComponent.new do "Go" end end def long_text render ButtonComponent.new do "Click here to do this thing because it's the best way to do it" end end def emoji_text render ButtonComponent.new do "👀📗" end end # @!endgroup end ``` **Tags** are just strings identified by their `@` prefix - for example `@hidden`. Tags are always placed in a comment above the relevant preview class or example method. The following Lookbook-specific tags are available for use: * `@label