📎 Webclip
Using Bootstrap Native with Phoenix LiveView
The article explains how to make Bootstrap and LiveView work together without bringing in jQuery. It uses Bootstrap Native for client-side components, then shows how to configure Webpack, load Bootstrap’s CSS and JavaScript, and reapply Bootstrap hooks after LiveView updates the page.
Reading notes#
- Use Bootstrap Native instead of Bootstrap’s jQuery-based JavaScript because most of the app is already powered by LiveView.
- Install bootstrap, bootstrap.native, and bootstrap.native-loader, then add a Webpack rule to load Bootstrap Native components, with an
onlyoption if you want to limit what gets loaded. - Import Bootstrap’s SCSS in
app.scssandbootstrap.nativeinapp.js. - After LiveView finishes page loading, call
BSN.initCallback(document.body)so newly inserted content gets Bootstrap Native’s JavaScript behavior. - Add
phx-update="ignore"to elements that open and close on the client, such as dropdown roots, so LiveView does not interfere with them during updates. - Use
phx-feedback-foron the wrapper around an input to control when validation feedback appears, and pair it withphx-no-feedbackstyles that hide invalid and valid messages until the field has been used. - The article’s form example relies on a single
inputfunction to generate fields, and points to a previous post about dynamic forms in Phoenix. - For LiveView modals, the article uses a
live-modalclass at the top of the modal, keeps Bootstrap modal classes inside it, and adds CSS based on Phoenix’s modal styling.
