Skip to content

Commit 73d2d6f

Browse files
committed
Added Switch component to the doc
1 parent 48bc8af commit 73d2d6f

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ Here is the list of components that are being built. For reference, see here htt
5757
To contribute to this project, it's recommended to install the gem locally and point to it in your Gemfile:
5858

5959
```ruby
60-
gem "rbui", path: "../rbui"
60+
gem "ruby_ui", path: "../ruby_ui"
6161
```
6262

6363
### Link the JavaScript Package
6464

6565
Similarly, link the rbui-js package locally using yarn:
6666

6767
```bash
68-
yarn add ../rbui
68+
yarn add ../ruby_ui
6969
```
7070

7171
## Working with Components
@@ -74,7 +74,7 @@ yarn add ../rbui
7474

7575
1. Eject the component you want to modify using the generator:
7676
```bash
77-
rails generate rbui:component combobox
77+
rails generate ruby_ui:component combobox
7878
```
7979
2. Make your desired changes to the ejected component
8080
3. Once you're satisfied with the modifications, integrate the component back into the gem in the appropriate location

app/components/ruby_ui/switch.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Switch < Base
5+
def view_template
6+
attrs => { include_hidden:, **input_attrs }
7+
8+
label(
9+
role: "switch",
10+
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"
11+
) do
12+
input(type: "hidden", name: attrs[:name], value: "0") if include_hidden
13+
input(**input_attrs)
14+
15+
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 ")
16+
end
17+
end
18+
19+
private
20+
21+
def default_attrs
22+
{
23+
class: "hidden peer",
24+
type: "checkbox",
25+
include_hidden: true,
26+
value: "1"
27+
}
28+
end
29+
end
30+
end

app/controllers/docs_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def shortcut_key
158158
render Docs::ShortcutKeyView.new
159159
end
160160

161+
def switch
162+
render Docs::SwitchView.new
163+
end
164+
161165
def table
162166
render Docs::TableView.new
163167
end

app/views/components/shared/menu.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def components
9292
{name: "Select", path: helpers.docs_select_path, badge: "New"},
9393
{name: "Sheet", path: helpers.docs_sheet_path},
9494
{name: "Shortcut Key", path: helpers.docs_shortcut_key_path},
95+
{name: "Switch", path: helpers.docs_switch_path},
9596
{name: "Table", path: helpers.docs_table_path},
9697
{name: "Tabs", path: helpers.docs_tabs_path},
9798
{name: "Textarea", path: helpers.docs_textarea_path},

app/views/docs/switch_view.rb

Lines changed: 40 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
get "select", to: "docs#select", as: :docs_select
4646
get "sheet", to: "docs#sheet", as: :docs_sheet
4747
get "shortcut_key", to: "docs#shortcut_key", as: :docs_shortcut_key
48+
get "switch", to: "docs#switch", as: :docs_switch
4849
get "table", to: "docs#table", as: :docs_table
4950
get "tabs", to: "docs#tabs", as: :docs_tabs
5051
get "textarea", to: "docs#textarea", as: :docs_textarea

0 commit comments

Comments
 (0)