<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Full Stack Pancakes - Medium]]></title>
        <description><![CDATA[A collection of TILs and afterthoughts in my day-to-day development work - Medium]]></description>
        <link>https://medium.com/full-stack-pancakes?source=rss----d9317db3357d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Full Stack Pancakes - Medium</title>
            <link>https://medium.com/full-stack-pancakes?source=rss----d9317db3357d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 12:49:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/full-stack-pancakes" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ViewComponent: My attempt to answer what value it brings]]></title>
            <link>https://medium.com/full-stack-pancakes/viewcomponent-my-attempt-to-answer-what-value-it-brings-4ce4bc5c61f2?source=rss----d9317db3357d---4</link>
            <guid isPermaLink="false">https://medium.com/p/4ce4bc5c61f2</guid>
            <category><![CDATA[rails-6]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[viewcomponents]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[kinopyo]]></dc:creator>
            <pubDate>Thu, 17 Dec 2020 06:40:30 GMT</pubDate>
            <atom:updated>2021-01-05T04:29:49.720Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/github/view_component">ViewComponent</a> allows you to write reusable and easy-to-test view components in Rails with pure Ruby.</p><h3>A bit of history</h3><p>ViewComponent, formerly advertised as ActionView::Component, was first introduced in Railsconf 2019. I got the impression from the talk that it was planned to be merged into Rails. Then this month when I heard the word that it is &quot; <a href="https://edgeguides.rubyonrails.org/layouts_and_rendering.html#rendering-objects">supported natively</a> in Rails 6.1,&quot; I thought it&#39;d be available if I upgrade to 6.1. Unfortunately, ViewComponent is still <a href="https://github.com/github/view_component">a separated gem</a> and you need to install it manually.</p><p>What “supported natively” means is that we can use the gem in Rails 6.1 without a monkey patch. To be exact, any 3rd-party solutions (or any Ruby objects) that have implemented a render_in method can be passed to ActionView render method in Rails 6.1. Check out the <a href="https://github.com/rails/rails/pull/36388">original PR</a> for more context.</p><p>Even it’s not included in the default Rails stack, it still has great potential. If you haven’t watched any of the Railsconf videos, one thing you should know is that ViewComponent is initiated by Github developers. They have been using this approach to construct their views. In other words, it’s been dogfooded on a large scale website and with a large amount of developers, so I believe there is a bright future. 🙂</p><h3>Installation</h3><p>If you’re on Rails 6.1:</p><pre>gem &quot;view_component&quot;, require: &quot;view_component/engine&quot;</pre><p>…otherwise, you’ll need to put <a href="https://github.com/github/view_component/blob/master/lib/view_component/render_monkey_patch.rb">this monkey patch</a> in your application.</p><h3>How to use</h3><h4>Folder structure</h4><p>Components are all stored under app/components/ folder. The generator will create corresponding files based on your template engine (ERB, Haml, Slim) and testing framework (TestUnit/RSpec).</p><pre>bin/rails generate component ModalComponent id<br>      invoke  test_unit<br>      create  test/components/modal_component_test.rb<br>      create  app/components/modal_component.rb<br>      create  app/components/modal_component.html.erb</pre><h4>Basic form</h4><p>Say we’re creating a modal component, and it accepts an id parameter:</p><pre># app/components/modal_component.rb<br>class ModalComponent &lt; ViewComponent::Base<br>  with_content_areas :header, :body</pre><pre>  def initialize(id:)<br>    @id = id<br>  end<br>end</pre><pre># app/components/modal_component.html.erb<br>&lt;div class=&quot;modal&quot; id=&quot;&lt;%= @id %&gt;&quot;&gt;<br>  &lt;div class=&quot;header&quot;&gt;&lt;%= header %&gt;&lt;/div&gt;<br>  &lt;div class=&quot;body&quot;&gt;&lt;%= body %&gt;&lt;/div&gt;<br>&lt;/div&gt;</pre><p>Render the component in a parent consumer view, specifying the container id and content:</p><pre>&lt;%= render(ModalComponent.new(id: &quot;login-modal&quot;)) do |component| %&gt;<br>  &lt;% component.with(:header) do %&gt;<br>    Hello Jane<br>  &lt;% end %&gt;<br>  <br>  &lt;% component.with(:body) do %&gt;<br>    &lt;p&gt;Have a great day.&lt;/p&gt;<br>  &lt;% end %&gt;<br>&lt;% end %&gt;</pre><p>Output:</p><pre>&lt;div class=&quot;modal&quot; id=&quot;login-modal&quot;&gt;<br>  &lt;div class=&quot;header&quot;&gt;Hello Jane&lt;/div&gt;<br>  &lt;div class=&quot;body&quot;&gt;&lt;p&gt;Have a great day.&lt;/p&gt;&lt;/div&gt;<br>&lt;/div&gt;</pre><p>The content area (header and body) serves like a callback that the parent caller can pass in what <em>content</em> to render, and the modal component gets to decide the <em>markup</em>. It’s similar to the <a href="https://vuejs.org/v2/guide/components-slots.html#Named-Slots">named slots in Vue</a>.</p><p>Note that there is a <a href="https://viewcomponent.org/#slots-experimental">more advanced Slots API</a>, currently in its second iteration, that is supposed to be the successor to with_content_areas. But it&#39;s still experimental, you need to keep in mind that it&#39;s subject to breaking changes. (It happened to me during the time I wrote this post, the code I wrote a few days ago is now deprecated. 😅)</p><h3>Sidecar assets</h3><p>ViewComponent has a concept of <em>Sidecar assets</em> — views and other assets (CSS, JS) can be placed in the same directory to better encapsulate them.</p><p>There are two ways to do this.</p><ol><li>Keep only the component Ruby file outside of the sidecar directory:</li></ol><pre>app/components<br>├── ...<br>├── example_component.rb<br>├── example_component<br>|   ├── example_component.css<br>|   ├── example_component.html.erb<br>|   └── example_component.js<br>├── ... </pre><p>2. Keep everything in the same directory:</p><pre>app/components<br>├── ...<br>├── example<br>|   ├── component.rb<br>|   ├── component.css<br>|   ├── component.html.erb<br>|   └── component.js<br>├── ...</pre><p>The JavaScript file can also be a Stimulus controller. Take a look at <a href="https://viewcomponent.org/#sidecar-assets-experimental">the doc on Sidecar assets (experimental)</a> to learn how to configure Webpack to compile the sidecar assets.</p><p>There are more advanced usages:</p><ul><li>Conditional rendering</li><li>Using helper methods</li><li>Rendering collections</li><li>before_render hook</li><li>Inline component (or template-less component)</li><li>Previews (like ActionMailer preview)</li><li>Power of composition (render another component inside a component)</li></ul><p>Be sure to check out the “ <a href="https://viewcomponent.org/#building-components">building components</a>” section in the official doc to see more examples.</p><p>ViewComponent is said to be inspired by React, so if you have some experience with React/Vue, you may find it relatively easy to shift the paradigm of how to write your view logic in Ruby.</p><h3>What value it brings</h3><p>The official site already does a great job of selling its greatness — <a href="https://viewcomponent.org/#why-should-i-use-components"><strong>testing, data flow, performance, and standards</strong></a>.</p><p>Here are my takeaways.</p><h4>On testing</h4><p>Keeping up the test coverage for every possible path of the view with Capybara feature specs is expensive and impractical. ViewComponent makes it easy to test your view logic. Because it’s so easy and fast, both the physical and mental hurdle is almost gone, I believe more developers will feel encouraged to spec up their view components.</p><p>I would even say this will drive better design decisions. When I was playing around writing some tests for an existing application, it provided me the opportunity to scrutinize my view logic with fresh eyes, to question myself, why I had added such logic in the first place, would it be possible to simplify it or remove it all. Because adding a test also functions like “locking in” the feature, I’d like to be certain such logic is needed to justify the effort. I can see it would drive such conversations between developers and designers more and come up with better decisions.</p><p>Here is an example from a codebase I worked on, it was a delightful experience of writing the test:</p><pre># test/components/comment_preview_component_test.rb</pre><pre>require &quot;test_helper&quot;<br><br>class CommentPreviewComponentTest &lt; ViewComponent::TestCase<br>  def test_no_comments<br>    answer = create(:answer)<br><br>    render_inline(CommentPreviewComponent.new(answer: answer))<br><br>    assert_link &quot;Leave a comment&quot;<br>    refute_link &quot;0 Comments&quot;<br>  end<br><br>  def test_one_comment<br>    answer = create(:answer)<br>    create(:comment, answer: answer)<br><br>    render_inline(CommentPreviewComponent.new(answer: answer))<br><br>    assert_link &quot;Comment 1&quot;<br>  end<br><br>  def test_more_comments<br>    answer = create(:answer)<br>    create_list(:comment, 2, answer: answer)<br><br>    render_inline(CommentPreviewComponent.new(answer: answer))<br><br>    assert_link &quot;Comments 2&quot;<br>  end<br><br>  def test_duplicated_commenters_avatars<br>    answer = create(:answer)<br>    user = create(:user)<br>    # setup 2 unique commenters for the answer<br>    create_list(:comment, 2, answer: answer, user: user)<br>    create(:comment, answer: answer, user: answer.user)<br><br>    render_inline(CommentPreviewComponent.new(answer: answer))<br><br>    assert_selector &quot;img&quot;, count: 2<br>    assert_selector &quot;img[alt=&#39;#{user.name} avatar&#39;]&quot;<br>    assert_selector &quot;img[alt=&#39;#{answer.user.name} avatar&#39;]&quot;<br>  end<br>end</pre><h4>On packaging and distribution</h4><p>ViewComponent is reusable by design. <a href="https://github.com/primer/view_components/tree/main/app/components/primer">Github’s Primer ViewComponents repo</a> is a good example of how they’ve packaged their design system ( <a href="https://github.com/primer/css">Primer CSS</a>) into a ViewComponent-powered gem.</p><p>As for the sidecar assets feature that allows you to bundle JavaScript and CSS together, it’s still experimental, and I don’t know how we can test the JavaScript behavior without sacrificing the speed, but I see great potential in it.</p><p>Also, if you happen to use <a href="https://tailwindcss.com/">Tailwind CSS</a> and <a href="https://github.com/alpinejs/alpine/">Alpine.js</a>, your HTML markup would work standalone, which makes it much more shareable with ViewComponent. I highly recommend giving these tools a try. 😎</p><h4>More modern, more powerful</h4><p>As React and Vue gain more popularity, more and more developers have built the mental model of thinking in the component way. But what Rails provides by default (partial, helpers, layouts) have their shortcomings. I have more than once talked to myself, oh I wish I could just drop in a React component here for this specific view logic, that’d make the code simpler and more elegant.</p><p>Finally, now we get the tool!</p><p>(There are other libraries solving the view-layer problems such as <a href="https://github.com/trailblazer/cells">trailblazer/cells</a>, <a href="https://github.com/komposable/komponent">komposable/komponent</a>, etc. I don’t have enough experience to make the comparison 😳.)</p><h3>Where to begin</h3><p>If you want to try out ViewComponent in an existing application, here are some starting points:</p><ul><li>Extract UI based components, such as modal, dropdown, etc.</li><li>Extract application based components, such as complex views with many inline ruby variables and conditions</li></ul><p>Try finding some good candidates and write some tests to feel the joy 😃.</p><h3>One more thing…</h3><p>Like the main author <a href="https://github.com/github/view_component/issues/206#issuecomment-587563994">@joelhawksley said in one of the Github Issue</a>, ViewComponent probably is not mature enough to be upstreamed yet. It still needs to iterate with the community. In that sense, living in a separated gem makes the feedback loop much faster.</p><p>If you were to try it out today, you should be mentally prepared that even though it provides many functionalities, you may need to make more coding decisions at the beginning such as how you want to organize the folder structures, namespaces, naming conventions, where to put certain logics (partial, helper, or ViewComponent object), etc, as real-world code is more complex and sophisticated.</p><p>It’d be a good mental exercise, and it’d become easier once we come up with a community best practice. But for now, it’s up to you to make the call. I enjoy this process, but maybe not for everyone. 🙂</p><p>I can see myself using ViewComponent as the default choice, I’ve already installed it in two of my active projects. I’ll share more tips once I get more experience.</p><p>Hope you find this post useful. See you next time. 👋</p><h3>Resources</h3><ul><li><a href="https://www.youtube.com/watch?v=y5Z5a6QdA-M">RailsConf 2019 — Rethinking the View Layer with Components by Joel Hawksley — YouTube</a></li><li><a href="https://www.youtube.com/watch?v=YVYRus_2KZM">RailsConf 2020 CE — Encapsulating Views by Joel Hawksley</a></li><li><a href="https://github.com/rails/rails/pull/36388">Introduce support for 3rd-party component frameworks by joelhawksley · Pull Request #36388 · rails/rails</a></li><li><a href="https://github.com/primer/view_components/tree/main/app/components/primer">Github Primer in ViewComponents · github/primer/view_components</a></li><li><a href="https://github.com/github/view_component/issues/206">Renaming to ViewComponent, Syntax Changes · Issue #206 · github/view_component</a></li></ul><p><em>Originally published at </em><a href="https://bloggie.io/@kinopyo/viewcomponent-my-attempt-to-answer-what-value-it-brings"><em>https://bloggie.io</em></a><em> on December 17, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ce4bc5c61f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/full-stack-pancakes/viewcomponent-my-attempt-to-answer-what-value-it-brings-4ce4bc5c61f2">ViewComponent: My attempt to answer what value it brings</a> was originally published in <a href="https://medium.com/full-stack-pancakes">Full Stack Pancakes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[bootstrap.native: How to Use Toast Component]]></title>
            <link>https://medium.com/full-stack-pancakes/bootstrap-native-how-to-use-toast-component-8df9074c4f00?source=rss----d9317db3357d---4</link>
            <guid isPermaLink="false">https://medium.com/p/8df9074c4f00</guid>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[kinopyo]]></dc:creator>
            <pubDate>Fri, 29 Mar 2019 04:10:41 GMT</pubDate>
            <atom:updated>2019-03-29T04:10:40.822Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://bloggie.io/@kinopyo/bootstrap-native-bootstrap-without-jquery">previous post</a>, I covered how to use <a href="https://github.com/thednp/bootstrap.native">bootstrap.native</a> so you can drop the jQuery dependency right now. In this post, I’m gonna share how to use Toast component with bootstrap.native.</p><h3>Toast UI</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/0*xOJ1jjVGhDwsQsmq.png" /></figure><p>This popular UI component was <a href="https://getbootstrap.com/docs/4.2/components/toasts/">added in Bootstrap 4.2</a>. So first make sure you upgrade to 4.2+ to get the CSS.</p><p>I’m using bootstrap gem in my Rails app. Update the version and run bundle to install.</p><pre>gem &quot;bootstrap&quot;, &quot;~&gt; 4.2.1&quot;</pre><p>Then import the component’s css.</p><pre>// app/assets/stylesheets/_bootstrap-custom.scss</pre><pre>@import &quot;bootstrap/modal&quot;;<br>+@import &quot;bootstrap/toasts&quot;;<br>@import &quot;bootstrap/tooltip&quot;;</pre><p>Add “toast” to bootstrap.native loader:</p><pre>// config/webpack/environment.js</pre><pre>environment.loaders.append(&quot;bootstrap.native&quot;, {<br>  test: /bootstrap\.native/,<br>  use: {<br>    loader: &quot;bootstrap.native-loader&quot;,<br>    options: {<br>-     only: [&quot;alert&quot;, &quot;button&quot;, &quot;dropdown&quot;, &quot;modal&quot;, &quot;tab&quot;, &quot;tooltip&quot;],<br>+     only: [&quot;alert&quot;, &quot;button&quot;, &quot;dropdown&quot;, &quot;modal&quot;, &quot;tab&quot;, &quot;toast&quot;, &quot;tooltip&quot;],<br>      bsVersion: 4<br>    }<br>  }<br>})</pre><p>That’s all the setup.</p><h3>How to use</h3><p>A slim-down version of the markup from the <a href="https://getbootstrap.com/docs/4.3/components/toasts/">official doc</a>:</p><pre>&lt;div class=&quot;toast&quot;&gt;<br>  &lt;div class=&quot;toast-header&quot;&gt;<br>    &lt;strong&gt;Bootstrap&lt;/strong&gt;<br><br>    &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;toast&quot;&gt;<br>      &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;<br>    &lt;/button&gt;<br>  &lt;/div&gt;<br>  <br>  &lt;div class=&quot;toast-body&quot;&gt;<br>    This is a toast message.<br>  &lt;/div&gt;<br>&lt;/div&gt;</pre><p>Initialize it from JavaScript like this:</p><pre>import &quot;bootstrap.native&quot;<br><br>const toast = document.querySelector(&quot;[data-dismiss=toast]&quot;)<br>toast[&quot;Toast&quot;].show()</pre><p>That should display the toast and it’ll disappear in a few seconds.</p><h3>Breakdown</h3><p>As the official doc says, “Toasts are opt-in for performance reasons, <strong>so you must initialize them yourself</strong>.”</p><p>To do so in bootstrap.native, we first get the element by querying [data-dismiss=toast]. Then, we access the instance with toast[&quot;Toast&quot;] which can perform all the methods such as .show(), .hide(), and .dispose().</p><p>That is the syntax of accessing the component instance in bootstrap.native — element[string name of the component]. It&#39;s much easier to take a look at the implementation of Toast and see <a href="https://github.com/thednp/bootstrap.native/commit/1d797a2ba7896758378f92f3d20172183c68f152#diff-466f66ee1461e6021500e0f57d32e37bR1719">how it stores the instance</a>.</p><h3>Integrate with Rails Flash</h3><p><a href="https://api.rubyonrails.org/classes/ActionDispatch/Flash.html">ActionDispatch Flash</a> is a common way of setting notices or alerts after certain actions in the controller.</p><pre>redirect_to post, notice: &quot;Post successfully created!&quot;<br>redirect_to login_path, alert: &quot;You must be logged in&quot;</pre><p>So far I’ve been using Bootstrap <a href="https://getbootstrap.com/docs/4.0/components/alerts/">Alerts component</a> to display the flash message. This time, I want to migrate it to the Toast component.</p><pre># app/views/layouts/_toast_message.html.erb</pre><pre>&lt;% flash.each do |type, message| %&gt;<br>  &lt;% next unless %w[alert notice].include?(type) %&gt;<br><br>  &lt;div class=&quot;toast toast--&lt;%= type %&gt;&quot; role=&quot;alert&quot; aria-live=&quot;assertive&quot; aria-atomic=&quot;true&quot;&gt;<br>    &lt;div class=&quot;toast-body&quot;&gt;<br>      &lt;span class=&quot;mr-auto&quot;&gt;&lt;%= message %&gt;&lt;/span&gt;<br>      <br>      &lt;button type=&quot;button&quot; class=&quot;ml-2 mb-1 close&quot; data-dismiss=&quot;toast&quot; aria-label=&quot;Close&quot; data-delay=&quot;3000&quot;&gt;<br>        &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;<br>      &lt;/button&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;% end %&gt;</pre><p>First, in a partial erb, we filter flash enumerable and find :alert or :notice type of messages.Then we can use the type as part of the css to style them differently. We can also specify data-delay=&quot;3000&quot; so the Toast message automatically disappears in 3 seconds.</p><p>Go back to our JavaScript and add a presence check. There is only one Toast message at a screen for now.</p><pre>import &quot;bootstrap.native&quot;<br><br>document.addEventListener(&quot;turbolinks:load&quot;, () =&gt; {<br>  BSN.initCallback(document.body)<br><br>  const toast = document.querySelector(&quot;[data-dismiss=toast]&quot;)<br>  if (toast) {<br>    toast[&quot;Toast&quot;].show()<br>  }<br>})</pre><p>Finally, render the partial in the main layout file.</p><pre># app/views/layouts/application.html.erb</pre><pre>&lt;body&gt;<br>  &lt;%= render &quot;layouts/toast_message&quot; %&gt;<br>  &lt;%= yield %&gt;<br>&lt;/body&gt;</pre><p>I’ll leave the css up to you 😉.</p><p>That’s it. Now you can use Flash alert and message just like before and they’ll be displayed in the Toast component transparently.</p><p>Hope you find it useful. Thanks for reading.</p><p><em>Originally published at </em><a href="https://bloggie.io/@kinopyo/bootstrap-native-how-to-use-toast-component"><em>bloggie.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8df9074c4f00" width="1" height="1" alt=""><hr><p><a href="https://medium.com/full-stack-pancakes/bootstrap-native-how-to-use-toast-component-8df9074c4f00">bootstrap.native: How to Use Toast Component</a> was originally published in <a href="https://medium.com/full-stack-pancakes">Full Stack Pancakes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[bootstrap.native — Bootstrap without jQuery]]></title>
            <link>https://medium.com/full-stack-pancakes/bootstrap-native-bootstrap-without-jquery-c4cb2639ea35?source=rss----d9317db3357d---4</link>
            <guid isPermaLink="false">https://medium.com/p/c4cb2639ea35</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[bootstrap]]></category>
            <dc:creator><![CDATA[kinopyo]]></dc:creator>
            <pubDate>Wed, 13 Mar 2019 08:30:48 GMT</pubDate>
            <atom:updated>2019-03-22T12:22:30.420Z</atom:updated>
            <content:encoded><![CDATA[<h3>bootstrap.native — Bootstrap without jQuery</h3><p>Bootstrap 5 will <a href="https://github.com/twbs/bootstrap/pull/23586">remove jQuery as a dependency</a>. That’s good news 👏! But if you want to drop the dependency right now, you can accomplish it with bootstrap.native.</p><p><a href="https://github.com/thednp/bootstrap.native">bootstrap.native</a> is a third-party plugin that perfectly mimics Bootstrap with vanilla JavaScript. It’s lightweight, robust, and comes with some sweet enhancements too.</p><h3>Get Started with bootstrap.native</h3><p>Install via yarn:</p><pre>yarn add bootstrap.native bootstrap.native-loader</pre><p>As you’ve guessed, <a href="https://github.com/fleck/bootstrap.native-loader">bootstrap.native-loader</a> is the Webpack loader.</p><p>Back to the code, let’s configure the loader in Webpacker — specify components you want to use and the version. bootstrap.native supports both V3 and V4.</p><pre>// config/webpack/environment.js</pre><pre>const { environment } = require(&quot;@rails/webpacker&quot;)<br>const webpack = require(&quot;webpack&quot;)<br><br>environment.loaders.append(&quot;bootstrap.native&quot;, {<br>  test: /bootstrap\.native/,<br>  use: {<br>    loader: &quot;bootstrap.native-loader&quot;,<br>    options: {<br>      only: [&quot;alert&quot;, &quot;button&quot;, &quot;dropdown&quot;, &quot;modal&quot;, &quot;tooltip&quot;],<br>      bsVersion: 4<br>    }<br>  }<br>})<br><br>module.exports = environment</pre><p>Then, import it in Webpack entry file.</p><pre>import &quot;bootstrap.native&quot;</pre><p>It’s important to note that <a href="https://github.com/thednp/bootstrap.native/wiki/Acknowledgements#all-components-are-initialized-right-away"><strong>all components are initialized right away</strong></a>. Meaning that it’ll scan through the data-* attributes and initialize dropdowns, tooltips, etc. If you&#39;ve had the correct Bootstrap HTML markup, you don&#39;t have to do anything else.</p><p>But, if you’re using Turbolinks…</p><p>You’ll need this:</p><pre>// app/javascript/packs/application.js</pre><pre>import &quot;bootstrap.native&quot;<br>document.addEventListener(&quot;turbolinks:load&quot;, () =&gt; {<br>  BSN.initCallback(document.body)<br>})</pre><p>As explained in <a href="https://github.com/thednp/bootstrap.native/wiki/FAQs#does-it-work-with-later-added-elements">its FAQ</a>, dynamically added elements will not work.</p><blockquote><strong><em>Does it work with later added elements?</em></strong><em><br> …the event listeners used by the components are not bound to the document and delegated to specific elements like jQuery plugins do, rather we took a more performance oriented approach and decided to bind events to elements themselves, for performance reasons.</em></blockquote><p>Since Turbolinks is fetching a new page and replacing the body, the events will not work after clicking a link powered by Turbolink.</p><p>So we’ll need to tell bootstrap.native (BSN global var) to re-initialize events after a turbolinks:load event. You can learn more about this part from the <a href="https://github.com/thednp/bootstrap.native/wiki/Acknowledgements#some-features-are-even-better">official wiki</a> and <a href="https://stackoverflow.com/questions/46857297/bootstrap-native-does-not-work-with-turbo-links/47033647#47033647">this Stackoverflow</a>.</p><h3>The sweet data-persist</h3><p>Compare to the official JavaScript, bootstrap.native comes with some sweet improvements.</p><p>My favorite is data-persist option that keeps the dropdown menu open, a perfect use case for having a form within a dropdown. (Previously I had to add a little hack to make Bootstrap work that way.)</p><p>The markup is straightforward — add data-persist=&quot;true&quot; to the dropdown button.</p><pre>&lt;button class=&quot;dropdown-toggle&quot; data-toggle=&quot;dropdown&quot; data-persist=&quot;true&quot;&gt;</pre><p>Read more: <a href="https://github.com/thednp/bootstrap.native/wiki/Acknowledgements#some-features-are-even-better">Some features are even better</a>.</p><h3>Misc: ESLint, Cleanup</h3><p>In case you’re using ESLint, be sure to add that BSN to the globals:</p><pre>// .eslintrc.json</pre><pre>{<br> &quot;globals&quot;: {<br>    &quot;BSN&quot;: false,<br>    // ...<br>  }<br>}</pre><p>Lastly, happily say goodbye to these old pals 👋 (or even jQuery if Bootstrap is the last dependent of it!).</p><pre>yarn remove bootstrap popper.js</pre><h3>Conclusion</h3><p>Let’s get down to the pros and cons.</p><p><strong>Pros</strong></p><ul><li>works great as it advertised</li><li>lightweight, 5kb</li><li>well maintained</li><li>fast to catch up upstream (e.g. <a href="https://github.com/thednp/bootstrap.native/issues/260">Toast feature</a>)</li><li><em>almost</em> a drop-in solution</li></ul><p>The open-close ratio of Github issues is 1/205! 👏</p><p><strong>Cons</strong></p><p>Being a third-party craft, it unavoidably introduces..</p><ul><li><em>some</em> learning curves and gotchas</li><li>delays of catching up upstream</li></ul><p>At last, I highly recommend you check out its Wiki first to understand the <a href="https://github.com/thednp/bootstrap.native/wiki/About">motivation of the project</a>, <a href="https://github.com/thednp/bootstrap.native/wiki/Acknowledgements#some-features-are-even-better">acknowledgments</a>, and <a href="https://github.com/thednp/bootstrap.native/wiki/FAQs">FAQs</a>.</p><p>I’m pretty happy after switching to bootstrap.native. Overall, less code, smaller bundle size, and one big step towards fully dropping jQuery dependency. 💪</p><p>p.s. If you’re using React, there is a wonderful solution as well: <a href="https://react-bootstrap.github.io/">React Bootstrap</a>.</p><p>p.p.s. If you want to know how to use Toast component with bootstrap.native, check out the <a href="https://bloggie.io/@kinopyo/bootstrap-native-how-to-use-toast-component">followup post</a>.</p><p><em>Originally published at </em><a href="https://bloggie.io/@kinopyo/bootstrap-native-bootstrap-without-jquery"><em>bloggie.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4cb2639ea35" width="1" height="1" alt=""><hr><p><a href="https://medium.com/full-stack-pancakes/bootstrap-native-bootstrap-without-jquery-c4cb2639ea35">bootstrap.native — Bootstrap without jQuery</a> was originally published in <a href="https://medium.com/full-stack-pancakes">Full Stack Pancakes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>