Skip to content

Commit 4b21a6a

Browse files
authored
split theme components from slot to components (#183)
1 parent f53bdb2 commit 4b21a6a

File tree

7 files changed

+47
-42
lines changed

7 files changed

+47
-42
lines changed

app/components/ruby_ui/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ def initialize(**user_attrs)
1818
def default_attrs
1919
{}
2020
end
21+
22+
if Rails.env.development?
23+
def before_template
24+
comment { "Before #{self.class.name}" }
25+
end
26+
end
2127
end
2228
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class SetDarkMode < ThemeToggle
5+
def view_template(&)
6+
div(**attrs, &)
7+
end
8+
9+
def default_attrs
10+
{
11+
class: "hidden dark:inline-block",
12+
data: {controller: "ruby-ui--theme-toggle", action: "click->ruby-ui--theme-toggle#setLightTheme"}
13+
}
14+
end
15+
end
16+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class SetLightMode < ThemeToggle
5+
def view_template(&)
6+
div(**attrs, &)
7+
end
8+
9+
def default_attrs
10+
{
11+
class: "dark:hidden",
12+
data: {controller: "ruby-ui--theme-toggle", action: "click->ruby-ui--theme-toggle#setDarkTheme"}
13+
}
14+
end
15+
end
16+
end

app/components/ruby_ui/theme_toggle/theme_toggle.rb

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,5 @@ class ThemeToggle < Base
55
def view_template(&)
66
div(**attrs, &)
77
end
8-
9-
def light_mode(**user_attrs, &)
10-
light_attrs = mix(default_light_attrs, user_attrs)
11-
div(**light_attrs, &)
12-
end
13-
14-
def dark_mode(**user_attrs, &)
15-
dark_attrs = mix(default_dark_attrs, user_attrs)
16-
div(**dark_attrs, &)
17-
end
18-
19-
private
20-
21-
def default_attrs
22-
{
23-
data: {controller: "ruby-ui--theme-toggle"}
24-
}
25-
end
26-
27-
def default_light_attrs
28-
{
29-
class: "dark:hidden",
30-
data: {action: "click->ruby-ui--theme-toggle#setDarkTheme"}
31-
}
32-
end
33-
34-
def default_dark_attrs
35-
{
36-
class: "hidden dark:inline-block",
37-
data: {action: "click->ruby-ui--theme-toggle#setLightTheme"}
38-
}
39-
end
408
end
419
end

app/components/shared/navbar.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def view_template
2626
end
2727

2828
def dark_mode_toggle
29-
RubyUI.ThemeToggle do |toggle|
30-
toggle.light_mode do
29+
ThemeToggle do
30+
SetLightMode do
3131
Button(variant: :ghost, icon: true) do
3232
svg(
3333
xmlns: "http://www.w3.org/2000/svg",
@@ -42,8 +42,7 @@ def dark_mode_toggle
4242
end
4343
end
4444
end
45-
46-
toggle.dark_mode do
45+
SetDarkMode do
4746
Button(variant: :ghost, icon: true) do
4847
svg(
4948
xmlns: "http://www.w3.org/2000/svg",

app/views/docs/getting_started/dark_mode.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def view_template
4949
render Docs::VisualCodeExample.new(title: "Toggle component", context: self) do
5050
<<~RUBY
5151
ThemeToggle do |toggle|
52-
toggle.light_mode do
52+
SetLightMode do
5353
Button(variant: :outline, icon: true) do
5454
svg(
5555
xmlns: "http://www.w3.org/2000/svg",
@@ -65,7 +65,7 @@ def view_template
6565
end
6666
end
6767
68-
toggle.dark_mode do
68+
SetDarkMode do
6969
Button(variant: :outline, icon: true) do
7070
svg(
7171
xmlns: "http://www.w3.org/2000/svg",

app/views/docs/theme_toggle.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def view_template
1212
render Docs::VisualCodeExample.new(title: "With icon", context: self) do
1313
<<~RUBY
1414
ThemeToggle do |toggle|
15-
toggle.light_mode do
15+
SetLightMode do
1616
Button(variant: :ghost, icon: true) do
1717
svg(
1818
xmlns: "http://www.w3.org/2000/svg",
@@ -28,7 +28,7 @@ def view_template
2828
end
2929
end
3030
31-
toggle.dark_mode do
31+
SetDarkMode do
3232
Button(variant: :ghost, icon: true) do
3333
svg(
3434
xmlns: "http://www.w3.org/2000/svg",
@@ -52,11 +52,11 @@ def view_template
5252
render Docs::VisualCodeExample.new(title: "With text", context: self) do
5353
<<~RUBY
5454
ThemeToggle do |toggle|
55-
toggle.light_mode do
55+
SetLightMode do
5656
Button(variant: :primary) { "Light" }
5757
end
5858
59-
toggle.dark_mode do
59+
SetDarkMode do
6060
Button(variant: :primary) { "Dark" }
6161
end
6262
end

0 commit comments

Comments
 (0)