github / view_component Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
504 previews with sample source code #667
504 previews with sample source code #667
Conversation
0eb3d7c
to
b0fb20e
Compare
Returns the method body from the preview file located in the host location under test/components/previews. This is being used if the preview file does not use render_template.
b0fb20e
to
3299f47
Compare
3299f47
to
d711993
Compare
The gem adds the method #source which returns a method body as a string
43d14f8
to
fc06afb
Compare
|
A few thoughts:
A more meta level question would be how much do we want to add the Previews? Other solutions, such as Storybook, have many of these features already baked into them and likely offer a superior end user experience with many additional features. I wonder if this is one of the places where we defer to that? |
|
@jonspalmer thanks for your input on this! I had a call with @joelhawksley before getting started on this and we decided on using prism and starting simple by just adding the assets via the assets pipeline. The benefit of using the assets pipeline is that we don't force users to use a specific tool for managing assets which keeps the deployment setup easy - no webpacker or npm required for using view component.
If we want to give users syntax highlighting I guess the most adequate approach would be to rely on a library like prism.js for this which requires us to add assets.
Do you mean alternative ways to include the assets or alternative libraries for syntax highlighting?
Interesting. Yes, it would be nice if we could update the prim assets in an easier matter. Wondering how frequent we need to update the prim version and if it justifies making the asset setup of the gem more complex.
Sure. This is still WIP and I will add tests for this shortly. |
Users can use other templating languages like haml. The template file extention can be used to retrieve the correct language. Fallback to ruby if there is not specific syntax highlighting available for the fiel extention.
…annesengl/view_component into 504-previews-with-sample-source-code
|
@johannesengl Thanks for the feedback. Sounds like you're way ahead of most of my questions. I'm curious about use of the asset pipeline usage. How does that play with apps that are using pure Webpacker and none of the traditional asset pipeline? Have we tested that combination? |
- use content_for for including code example within body - remove stylesheet_link_tag. The stylesheet should be included explicitly via the host application.
I just spend some more time testing out different setups as you suggested and indeed when removing sprockets in the host application the following raises an error: I did some research of how other rails engines include assets in the host application and found the following discussion:
Sidekiq Web UI Active Admin I don't really like the idea of adding an extra step to setup view component and requiring users to run a generator for installation. ResolutionGiven that rails 6 comes by default with webpack for including javascript and sprocket for including other assets I feel like view component should support both webpack and sprocket for including the assets. I am wondering how about we just don't add assets at all for a specific library like prism.js and let this step be up to the users of the gem? @jonspalmer @joelhawksley What is your opinion on the above? |
|
@johannesengl this might not be a great long-term solution, but since this is only meant for diagnostic use, could we start by vendoring the JS and CSS assets directly into the gem and serve them inline on the page? It's not ideal, but could unblock our work here. |
|
@jonspalmer Mind having another look? I updated the PR based on your suggestions. |
Would love seeing this being used at some point :) @joelhawksley or @jonspalmer mind having another look? |
|
Just to reiterate on #667 (comment), I really think the method signature ( |
0f58048
to
fd24109
Compare
fd24109
to
6e9f3ef
Compare
|
Here you go! Thanks again for your contribution @johannesengl |
|
@johannesengl would you be willing to add slim highlighting support to this feature? |
Summary
This PR introduces a source code example snippet on view component previews.
In order to add syntax highlighting we rely on prism.js. To use the library we need to add one js and one css asset file. Those files are served as static assets from the gems
assets/vendordirectory and we therefore don't require the host app to use a specific setup for handling assets like webpacker or the assets pipeline.The prism javascript file includes syntax highlighting for the following languages: ruby, erb, haml.
To enable the source previews users can set
config.view_component.show_previews_source = true. The config option defaults tofalse. A view helper methodpreview_sourceis added to render the preview source.The helper method is conditionally called based on the config option at the end of the
preview.html.erbview file.Users can also use the helper in their views for more flexibility of where to render the preview source.