Skip to content

Commit 81e90a3

Browse files
Switch Component (#143)
Co-authored-by: Seth Horsley <github.dvxhi@sl.isethi.com>
1 parent 3ee3442 commit 81e90a3

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GIT
1414

1515
GIT
1616
remote: https://github.com/ruby-ui/ruby_ui.git
17-
revision: aa983e83f1ece8a3f62c4e816c07d2a2f17b6b90
17+
revision: 6c79d377e1c550b606f28987c8e04587cb7221ac
1818
branch: main
1919
specs:
2020
ruby_ui (1.0.0.beta1)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ To contribute to this project, it's recommended to install the gem locally and p
1111
```ruby
1212
gem "ruby_ui", path: "../ruby_ui"
1313
```
14+
1415
## Working with Components
1516

1617
### Component Development Workflow

app/components/ruby_ui/switch.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Switch < Base
5+
def initialize(include_hidden: true, checked_value: "1", unchecked_value: "0", **attrs)
6+
@include_hidden = include_hidden
7+
@checked_value = checked_value
8+
@unchecked_value = unchecked_value
9+
super(**attrs)
10+
end
11+
12+
def view_template
13+
label(
14+
role: "switch",
15+
class: "peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50 bg-input has-[:checked]:bg-primary"
16+
) do
17+
input(type: "hidden", name: attrs[:name], value: @unchecked_value) if @include_hidden
18+
input(**attrs.merge(type: "checkbox", class: "hidden peer", value: @checked_value))
19+
20+
span(class: "pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform translate-x-0 peer-checked:translate-x-5 ")
21+
end
22+
end
23+
end
24+
end

app/controllers/docs_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def shortcut_key
162162
render Docs::ShortcutKeyView.new
163163
end
164164

165+
def switch
166+
render Docs::SwitchView.new
167+
end
168+
165169
def table
166170
render Docs::TableView.new
167171
end

app/views/components/shared/menu.rb

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def components
9393
{name: "Select", path: helpers.docs_select_path, badge: "New"},
9494
{name: "Sheet", path: helpers.docs_sheet_path},
9595
{name: "Shortcut Key", path: helpers.docs_shortcut_key_path},
96+
{name: "Switch", path: helpers.docs_switch_path},
9697
{name: "Table", path: helpers.docs_table_path},
9798
{name: "Tabs", path: helpers.docs_tabs_path},
9899
{name: "Textarea", path: helpers.docs_textarea_path},

app/views/docs/switch_view.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
class Docs::SwitchView < ApplicationView
4+
def view_template
5+
component = "Switch"
6+
7+
div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do
8+
render Docs::Header.new(title: "Switch", description: "A control that allows the user to toggle between checked and not checked.")
9+
10+
Heading(level: 2) { "Usage" }
11+
12+
render Docs::VisualCodeExample.new(title: "Default", context: self) do
13+
<<~RUBY
14+
Switch(name: "switch")
15+
RUBY
16+
end
17+
18+
render Docs::VisualCodeExample.new(title: "Checked", context: self) do
19+
<<~RUBY
20+
Switch(name: "switch", checked: true)
21+
RUBY
22+
end
23+
24+
render Docs::VisualCodeExample.new(title: "Disabled", context: self) do
25+
<<~RUBY
26+
Switch(name: "switch", disabled: true)
27+
RUBY
28+
end
29+
30+
render Docs::VisualCodeExample.new(title: "With flag include_hidden false", context: self) do
31+
<<~RUBY
32+
# Supports the creation of a hidden input to be used in forms inspired by the Ruby on Rails implementation of check_box. Default is true.
33+
Switch(name: "switch", include_hidden: false)
34+
RUBY
35+
end
36+
37+
render Docs::ComponentsTable.new(component_files(component))
38+
end
39+
end
40+
end

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
get "select", to: "docs#select", as: :docs_select
4747
get "sheet", to: "docs#sheet", as: :docs_sheet
4848
get "shortcut_key", to: "docs#shortcut_key", as: :docs_shortcut_key
49+
get "switch", to: "docs#switch", as: :docs_switch
4950
get "table", to: "docs#table", as: :docs_table
5051
get "tabs", to: "docs#tabs", as: :docs_tabs
5152
get "textarea", to: "docs#textarea", as: :docs_textarea

0 commit comments

Comments
 (0)