Unofficial, open source implementation of Flowbite components for Rails applications, built using ViewComponent.
Flowbite Components provides a comprehensive library of UI components following the Flowbite design system, implemented as Rails ViewComponents with full Tailwind CSS integration and dark mode support.
Preview the components at https://flowbite-components.substancelab.com/lookbook/
- Full Flowbite Design System: Faithful implementation of Flowbite components
- Rails Native: Built specifically for Rails using ViewComponent
- Tailwind CSS Integration: Seamless integration with Tailwind CSS
- Dark Mode Support: Built-in dark theme variants
- Form Helpers: Comprehensive form input components with validation states
- Accessibility First: ARIA attributes and semantic HTML
- Type Safety: Comprehensive test coverage and input validation
Add the gem to your application's Gemfile:
gem "flowbite-components"Then execute:
bundle installTailwind needs to be able to look through your code in order to generate the final CSS file with the class names you actually use. To allow Tailwind to find CSS class names inside flowbite-components you need to use tailwindcss-rails gem:
gem "tailwindcss-rails", ">= 4.3.0"Install Flowbite as an npm dependency:
yarn add flowbiteAdd Flowbite to your Tailwind CSS configuration. In your app/assets/tailwind/application.css:
@plugin "flowbite/plugin";
@import "flowbite/src/themes/default";
@import "../builds/tailwind/flowbite_components";If you want to use one of the other Flowbite themes, change @import "flowbite/src/themes/default"; accordingly.
<% form_with model: @user do |form| %>
<%= render Flowbite::InputField::Text.new(
attribute: :name,
form: form,
label: {content: "Full Name"},
hint: {content: "Enter your full name"}
) %>
<%= render Flowbite::InputField::Email.new(
attribute: :email,
form: form
) %>
<%= render Flowbite::Button.new(
type: :submit,
content: "Save User"
) %>
<% end %>See more examples at https://flowbite-components.substancelab.com/lookbook/inspect/input_field/inputfield.
<!-- Default button -->
<%= render Flowbite::Button.new do %>
Click me
<% end %>
<!-- Outline button with color -->
<%= render Flowbite::Button::Outline.new(style: :success) do %>
Outline Button
<% end %>
<!-- Pill button -->
<%= render Flowbite::Button::Pill.new(style: :danger) do %>
Pill Button
<% end %>See more examples at https://flowbite-components.substancelab.com/lookbook/inspect/button/default.
<%= render Flowbite::InputField::Text.new(
attribute: :username,
form: form,
size: :lg,
input: {
options: {
placeholder: "Enter username",
maxlength: 50,
class: "custom-class"
}
}
) %>See more examples at https://flowbite-components.substancelab.com/lookbook/inspect/input_field/inputfield.
<%= render Flowbite::InputField::Email.new(
attribute: :email,
form: form,
label: {
content: "Email Address",
options: {class: "font-bold"}
}
) %>See more examples at https://flowbite-components.substancelab.com/lookbook/inspect/input_label/inputlabel.
<!-- Disabled field -->
<%= render Flowbite::InputField::Text.new(
attribute: :name,
form: form,
disabled: true
) %>
<!-- Field with hint -->
<%= render Flowbite::InputField::Password.new(
attribute: :password,
form: form,
hint: "Must be at least 8 characters long"
) %>See more examples at https://flowbite-components.substancelab.com/lookbook/inspect/input_field/disabled_state.
A common use case for customizing a component is to add more CSS classes when rendering it, fx to change the size or spacing. flowbite-components is optimized for this case and all you need to do is specify the extra classes:
<%= render(Flowbite::Card.new(class: "w-full my-8")) { "Content" } %>renders
<div class="... all the usual classes... w-full my-8">If you want to fully replace the existing classes, you can pass an entirely new
class attribute via options:
<%= render(Flowbite::Card.new(options: {class: "w-full my-8"})) { "Content" } %>renders
<div class="w-full my-8">Some components, like toasts, have client-side behavior. flowbite-components doesn't require any particular JavaScript library for this.
By default components render Flowbite's own markup and leave the behavior up to you, so you can attach whichever library you prefer, in whatever manner that library requires, fx. using data attributes:
<%= render(Flowbite::Toast.new(
button: {options: {data: {action: "my-toast#close"}}},
message: "Saved!"
)) %>If you use Stimulus, flowbite-components provides controllers that behave like Flowbite's own flowbite.js. They are opt-in, so you only install the ones you need.
-
Copy the controllers for the components you want into
app/javascript/controllers/flowbite/:bin/rails generate flowbite_components:behavior toast
stimulus-rails registers them automatically, ie as
flowbite--toast. If your application registers its controllers inapp/javascript/controllers/index.js, ie when using jsbundling-rails, update it afterwards:bin/rails stimulus:manifest:update
-
Install stimulus-use, which the controllers depend on:
bin/importmap pin stimulus-use # or npm install stimulus-use -
Render the component with
behavior: :stimulus:<%= render(Flowbite::Toast.new(behavior: :stimulus, message: "Saved!")) %>
To use the controllers for all components that support them, set the default
behavior in config/initializers/flowbite_components.rb:
Flowbite::Components.default_behavior = :stimulusComponents that don't support the default behavior render Flowbite's own
markup. Pass behavior: :none to opt a single component out.
Re-run the generator after upgrading flowbite-components to update the controllers.
- Checkbox:
Flowbite::InputField::Checkbox - Date:
Flowbite::InputField::Date - Email:
Flowbite::InputField::Email - File:
Flowbite::InputField::File - Number:
Flowbite::InputField::Number - Password:
Flowbite::InputField::Password - Phone:
Flowbite::InputField::Phone - Radio Button:
Flowbite::InputField::RadioButton - Select:
Flowbite::InputField::Select - Text:
Flowbite::InputField::Text - Textarea:
Flowbite::InputField::Textarea - URL:
Flowbite::InputField::Url
- Checkbox:
Flowbite::Input::Checkbox - Date:
Flowbite::Input::Date - Email:
Flowbite::Input::Email - File:
Flowbite::Input::File - Number:
Flowbite::Input::Number - Password:
Flowbite::Input::Password - Phone:
Flowbite::Input::Phone - Radio Button:
Flowbite::Input::RadioButton - Select:
Flowbite::Input::Select - Textarea:
Flowbite::Input::Textarea - URL:
Flowbite::Input::Url
- Hint:
Flowbite::Input::Hint - Label:
Flowbite::Input::Label - Validation Error:
Flowbite::Input::ValidationError
- Button:
Flowbite::Button(default solid button) - Outline Button:
Flowbite::Button::Outline - Pill Button:
Flowbite::Button::Pill
- Card:
Flowbite::Card(default card with content and title)
- Link:
Flowbite::Link(default link styling)
Passing classes via the class argument to a component adds the classes to the
default ones instead of replacing them.
render(Component.new(class: "these are added"))This makes for easier customization of components, where you don't have to recreate the entire classlist, i.e. in order to increase sizes or add margins or whatever.
If you want to replace the entire class attribute for a component, pass it as part of the options hash, ie
render(Component.new(options: {class: "these replace the classes"}))The Stimulus controllers we provide, which can be used with behavior: :stimulus, must replicate the behavior from Flowbite's flowbite.js.
After checking out the repo, run bin/setup to install dependencies.
To run tests:
bundle exec rake testTo check code style of Ruby, ERB, and JavaScript files:
bundle exec rake lintJavaScript follows JavaScript Standard Style. To fix offenses automatically:
bundle exec rake js:fixTo install this gem onto your local machine:
bundle exec rake installThis library includes a demo application with previews for all components. To view them:
- Run
./demo/runfrom the project root. - Visit
http://localhost:3000/lookbook
Bug reports and pull requests are welcome on GitHub at https://github.com/substancelab/flowbite-components.
- All components should follow Flowbite design system specifications
- Use keyword arguments for component initialization
- Include comprehensive tests for all components
- Follow the existing naming conventions
- Ensure accessibility standards are met
- Make sure
rakepasses. - Make sure latest build on main is green.
- Make sure documentation database is up to date:
rake docs:refresh_registry - Change version number in version.rb to the version you want to release
- Change "Unreleased" in CHANGELOG.md to the version
- Build the gem:
rake build - Update demo site to use the new version
- Commit changes
- Release it:
rake release - Tell the world
If Flowbite isn't your preference, perhaps one of these other component libraries suits you better?
This project is licensed under the MIT License - see the LICENSE file for details.