-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5233 from solidusio/elia/admin/ui-button-component
[Admin] add a `ui/button` component
- Loading branch information
Showing
6 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::UI::Button::Component < SolidusAdmin::BaseComponent | ||
SIZES = { | ||
s: %w[ | ||
h-7 px-1.5 py-1 | ||
text-xs font-semibold leading-none | ||
], | ||
m: %w[ | ||
h-9 px-3 py-1.5 | ||
text-sm font-semibold leading-none | ||
], | ||
l: %w[ | ||
h-12 px-4 py-2 | ||
text-base font-semibold leading-none | ||
], | ||
} | ||
|
||
SCHEMES = { | ||
primary: %w[ | ||
text-white bg-black rounded | ||
hover:text-white hover:bg-gray-600 | ||
active:text-white active:bg-gray-800 | ||
focus:text-white focus:bg-gray-700 | ||
disabled:text-gray-400 disabled:bg-gray-100 disabled:cursor-not-allowed | ||
], | ||
secondary: %w[ | ||
text-gray-700 bg-white border border-1 rounded border-gray-200 | ||
hover:bg-gray-50 | ||
active:bg-gray-100 | ||
focus:bg-gray-50 | ||
disabled:text-gray-300 disabled:bg-white border-gray-200 disabled:cursor-not-allowed | ||
], | ||
ghost: %w[ | ||
text-gray-700 bg-transparent rounded | ||
hover:bg-gray-50 | ||
active:bg-gray-100 | ||
focus:bg-gray-50 focus:ring-gray-300 focus:ring-2 | ||
disabled:text-gray-300 disabled:bg-transparent border-gray-300 disabled:cursor-not-allowed | ||
], | ||
} | ||
|
||
def initialize(text: nil, class_name: nil, size: :m, scheme: :primary, **attributes) | ||
@text = text | ||
@attributes = attributes | ||
|
||
@class_name = [ | ||
class_name, | ||
'justify-start items-center gap-1 inline-flex', | ||
SIZES.fetch(size.to_sym), | ||
SCHEMES.fetch(scheme.to_sym), | ||
].join(' ') | ||
end | ||
|
||
def call | ||
content_tag(:button, @text, class: @class_name, **@attributes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
admin/spec/components/previews/solidus_admin/ui/button/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "ui/button" | ||
class SolidusAdmin::UI::Button::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
# @param text text | ||
def overview(text: "Button") | ||
render_with_template locals: { text: text } | ||
end | ||
|
||
# @param size select { choices: [s, m, l] } | ||
# @param scheme select { choices: [primary, secondary, ghost] } | ||
# @param text text | ||
def playground(size: :m, scheme: :primary, text: "Button") | ||
render component("ui/button").new(size: size, scheme: scheme, text: text) | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
admin/spec/components/previews/solidus_admin/ui/button/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<% current_component::SCHEMES.keys.each do |scheme| %> | ||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
<%= scheme.to_s.humanize %> | ||
</h6> | ||
|
||
<table> | ||
<tr> | ||
<td></td> | ||
<% current_component::SIZES.keys.each do |size| %> | ||
<td class="px-3 py-1 text-gray-500 text-center text-[16px]"><%= size.to_s.humanize %></td> | ||
<% end %> | ||
</tr> | ||
<% %i[default disabled].each do |state| %> | ||
<tr> | ||
<td class="font-bold px-3 py-1"><%= state.to_s.humanize %></td> | ||
<% current_component::SIZES.keys.each do |size| %> | ||
<td class="px-3 py-1 text-center"> | ||
<%= render current_component.new(size: size, scheme: scheme, text: text, disabled: state == :disabled) %> | ||
</td> | ||
<% end %> | ||
</tr> | ||
<% end %> | ||
</table> | ||
|
||
</div> | ||
<% end %> |
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/ui/button/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::UI::Button::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters