Show components from a gem #384
-
Hi folks, In our company, we packed all our basic components in a gem. Is there a way to make it happen? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey @sandrods, sorry for the slow reply. This should definitely be possible - like anything though there are a number of ways to achieve it depending on exactly what sort of setup you are looking for. There are a two main possibilities: 1) Bundle a stripped down app running Lookbook in the gem source code repo itself
The Polaris ViewComponents repo is a great example of this option in practice and is one I've implemented a number of times to good effect. 2) Install Lookbook in the main app
Do you have an idea of which approach you are looking to take? Or is there a different type of setup you were looking for? Often it's a case of getting the autoloading of components/previews correctly set up but it should (theoretically!) be possible to get most setups working. |
Beta Was this translation helpful? Give feedback.
-
Thank you @sandrods - I very much appreciate the kind words 😊 So it should definitely be possible to set something up along the lines of option 2. It's a little tricky to say exactly what the steps will be because it is obviously quite setup-dependent, but something along the following lines should work: # config/application.rb
components_gem_dir = Gem::Specification.find_by_name("component_gem_name_here").gem_dir
config.view_component.preview_paths << "#{components_gem_dir}/path/to/previews" That may be enough as it is, but if it isn't working try explicitly autoloading the previews directory: config.autoload_paths << "#{components_gem_dir}/path/to/previews" That sort of setup has worked well for me in the past, but it's always possible that it might not be that simple if your gem implements any autoloading itself as this can sometimes cause conflicts. But I reckon we should be able to figure out - let me know if you have any luck with the above. FWIW, Lookbook will attempt to merge the app previews and the gem previews into one set for display in the UI. Depending on the way your project is structured/namespaced this can sometimes result in a slightly confusing preview navigation tree - hopefully it works out okay for you though :-) |
Beta Was this translation helpful? Give feedback.
Thank you @sandrods - I very much appreciate the kind words 😊
So it should definitely be possible to set something up along the lines of option 2. It's a little tricky to say exactly what the steps will be because it is obviously quite setup-dependent, but something along the following lines should work:
That may be enough as it is, but if it isn't working try explicitly autoloading the previews directory: