Skip to content

Commit 93e6fee

Browse files
authored
Merge pull request #123 from ruby-ui/update-typography
Update to new typography
2 parents 46fca73 + 23b96fa commit 93e6fee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+664
-474
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@
3737

3838
.env*
3939

40-
.tool-versions
40+
.tool-versions
41+
42+
config/credentials/production.key

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575

7676
gem "phlex-rails"
7777

78-
gem "rbui", github: "rbui-labs/rbui", branch: "main"
79-
# gem "rbui", path: "../rbui"
78+
gem "ruby_ui", github: "ruby-ui/ruby_ui", branch: "main"
79+
# gem "ruby_ui", path: "../rbui"
8080

8181
gem "pry"

Gemfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
GIT
2-
remote: https://github.com/rbui-labs/rbui.git
3-
revision: 9428238764f51f5039dffcfa76ba4e90ab77f74b
2+
remote: https://github.com/ruby-ui/ruby_ui.git
3+
revision: 0ae3ed06e7ce56f17c67e2aa530f7fc2c806dc74
44
branch: main
55
specs:
6-
rbui (1.0.0.pre.alpha.3)
6+
ruby_ui (1.0.0.pre.alpha.4)
77
phlex (~> 1.10)
88
rouge (~> 4.2.0)
9-
tailwind_merge (>= 0.12)
9+
tailwind_merge (~> 0.12)
1010

1111
GEM
1212
remote: https://rubygems.org/
@@ -300,7 +300,7 @@ GEM
300300
railties (>= 6.0.0)
301301
stringio (3.1.1)
302302
strscan (3.1.0)
303-
tailwind_merge (0.13.0)
303+
tailwind_merge (0.13.2)
304304
lru_redux (~> 1.1)
305305
thor (1.3.1)
306306
timeout (0.4.1)
@@ -353,7 +353,7 @@ DEPENDENCIES
353353
pry
354354
puma (= 6.4.2)
355355
rails (= 7.2.0)
356-
rbui!
356+
ruby_ui!
357357
selenium-webdriver
358358
sqlite3 (>= 1.4)
359359
standard

app/components/base.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Components::Base < Phlex::HTML
2+
TAILWIND_MERGER = ::TailwindMerge::Merger.new.freeze unless defined?(TAILWIND_MERGER)
3+
4+
attr_reader :attrs
5+
6+
def initialize(**user_attrs)
7+
@attrs = mix(default_attrs, user_attrs)
8+
@attrs[:class] = TAILWIND_MERGER.merge(@attrs[:class]) if @attrs[:class]
9+
end
10+
11+
if defined?(Rails) && Rails.env.development?
12+
def before_template
13+
comment { "Before #{self.class.name}" }
14+
super
15+
end
16+
end
17+
18+
private
19+
20+
def default_attrs
21+
{}
22+
end
23+
end

app/components/heading.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
module Components
4+
class Heading < Base
5+
def initialize(level: nil, as: nil, size: nil, **attrs)
6+
@level = level
7+
@as = as
8+
@size = size
9+
super(**attrs)
10+
end
11+
12+
def view_template(&)
13+
tag = determine_tag
14+
public_send(tag, **attrs, &)
15+
end
16+
17+
private
18+
19+
def determine_tag
20+
return @as if @as
21+
return "h#{@level}" if @level
22+
"h1"
23+
end
24+
25+
def default_attrs
26+
{
27+
class: class_names
28+
}
29+
end
30+
31+
def class_names
32+
base_classes = "scroll-m-20 font-bold tracking-tight"
33+
size_class = size_to_class[@size || level_to_size[@level] || "6"]
34+
tag = determine_tag
35+
"#{base_classes} #{size_class} #{component_specific_classes(tag)}"
36+
end
37+
38+
def component_specific_classes(tag)
39+
component_classes[tag] || ""
40+
end
41+
42+
def component_classes
43+
{
44+
"h1" => "scroll-m-20 text-3xl font-bold leading-normal lg:leading-normal tracking-tight lg:text-4xl",
45+
"h2" => "scroll-m-20 text-2xl font-semibold tracking-tight transition-colors first:mt-0 pb-4 border-b",
46+
"h4" => "scroll-m-20 text-lg font-medium tracking-tight"
47+
}
48+
end
49+
50+
def size_to_class
51+
{
52+
"1" => "text-xs",
53+
"2" => "text-sm",
54+
"3" => "text-base",
55+
"4" => "text-lg",
56+
"5" => "text-xl",
57+
"6" => "text-2xl",
58+
"7" => "text-3xl",
59+
"8" => "text-4xl",
60+
"9" => "text-5xl"
61+
}
62+
end
63+
64+
def level_to_size
65+
{
66+
"1" => "6",
67+
"2" => "5",
68+
"3" => "4",
69+
"4" => "3"
70+
}
71+
end
72+
end
73+
end

app/components/rbui/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { application } from "../../../app/javascript/controllers/application";
44

55
// Register all controllers
66

7-
import RBUI from "rbui-js";
7+
import RBUI from "ruby_ui_js";
88
RBUI.initialize(application);

app/components/text.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
module Components
4+
class Text < Base
5+
def initialize(as: "p", size: "3", weight: "regular", **attrs)
6+
@as = as
7+
@size = size
8+
@weight = weight
9+
super(**attrs)
10+
end
11+
12+
def view_template(&)
13+
public_send(@as, **attrs, &)
14+
end
15+
16+
# private
17+
18+
def default_attrs
19+
{
20+
class: class_names
21+
}
22+
end
23+
24+
def class_names
25+
"#{size_to_class[@size]} #{weight_to_class[@weight]} #{component_specific_classes(@as)}"
26+
end
27+
28+
def component_specific_classes(tag)
29+
component_classes[tag] || ""
30+
end
31+
32+
def component_classes
33+
{
34+
"p" => "leading-7 [&:not(:first-child)]:mt-6"
35+
}
36+
end
37+
38+
def size_to_class
39+
{
40+
"1" => "text-xs", "xs" => "text-xs",
41+
"2" => "text-sm", "sm" => "text-sm",
42+
"3" => "text-base", "base" => "text-base",
43+
"4" => "text-lg", "lg" => "text-lg",
44+
"5" => "text-xl", "xl" => "text-xl",
45+
"6" => "text-2xl", "2xl" => "text-2xl",
46+
"7" => "text-3xl", "3xl" => "text-3xl",
47+
"8" => "text-4xl", "4xl" => "text-4xl",
48+
"9" => "text-5xl", "5xl" => "text-5xl"
49+
}
50+
end
51+
52+
def weight_to_class
53+
{
54+
"muted" => "text-muted-foreground",
55+
"light" => "font-light",
56+
"regular" => "font-normal",
57+
"medium" => "font-medium",
58+
"semibold" => "font-semibold",
59+
"bold" => "font-bold"
60+
}
61+
end
62+
end
63+
end

app/components/typography_list.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
class Components::TypographyList < Components::Base
4+
def initialize(items: [], numbered: false, **attrs)
5+
@items = items
6+
@numbered = numbered
7+
super(**attrs)
8+
end
9+
10+
def view_template(&)
11+
if @items.empty?
12+
list(**attrs, &)
13+
else
14+
list(**attrs) do
15+
@items.each do |item|
16+
TypographyListItem { item }
17+
end
18+
end
19+
end
20+
end
21+
22+
private
23+
24+
def list(**attrs, &)
25+
if numbered?
26+
ol(**attrs, &)
27+
else
28+
ul(**attrs, &)
29+
end
30+
end
31+
32+
def numbered? = @numbered
33+
34+
def not_numbered? = !numbered?
35+
36+
def default_attrs
37+
{
38+
class: tokens(
39+
"my-6 ml-6 [&>li]:mt-2 [&>li]:pl-2",
40+
numbered?: "list-decimal marker:font-medium",
41+
not_numbered?: "list-disc"
42+
)
43+
}
44+
end
45+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Components::TypographyListItem < Components::Base
4+
def view_template(&)
5+
li(**attrs, &)
6+
end
7+
8+
private
9+
10+
def default_attrs
11+
{
12+
class: "leading-7"
13+
}
14+
end
15+
end

app/helpers/application_helper.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
module ApplicationHelper
2+
def component_references(component, code_example = nil, use_component_files = false)
3+
return [] unless code_example
4+
5+
calls = []
6+
Prism.parse(code_example).value.accept(MethodCallFinder.new(calls))
7+
calls_set = Set.new(calls.map(&:to_s))
8+
descendants = Phlex::HTML.descendants.map { |d| d.to_s.gsub(/^RBUI::/, "") }
9+
component_names = descendants.select { |d| calls_set.include?(d) }
10+
11+
# component_names = code_example.scan(/(?<=^|\s)#{component}\w*/).uniq
12+
13+
component_names.map do |name|
14+
Docs::ComponentStruct.new(
15+
name: name,
16+
source: "lib/rbui/#{camel_to_snake(component)}/#{camel_to_snake(name)}.rb",
17+
built_using: :phlex
18+
)
19+
end
20+
21+
# component_names.push(
22+
# Docs::ComponentStruct.new(
23+
# name: "ComboboxController",
24+
# source: "https://github.com/PhlexUI/phlex_ui_stimulus/blob/main/controllers/command_controller.js",
25+
# built_using: :stimulus
26+
# )
27+
# )
28+
end
29+
30+
require "rubygems"
31+
32+
def component_files(component, gem_name = "ruby_ui")
33+
# Find the gem specification
34+
gem_spec = Gem::Specification.find_by_name(gem_name)
35+
return [] unless gem_spec
36+
37+
# Construct the path to the component files within the gem
38+
component_dir = File.join(gem_spec.gem_dir, "lib", "rbui", camel_to_snake(component))
39+
40+
return [] unless Dir.exist?(component_dir)
41+
42+
# Get all Ruby and JavaScript files
43+
rb_files = Dir.glob(File.join(component_dir, "*.rb"))
44+
js_files = Dir.glob(File.join(component_dir, "*_controller.js"))
45+
46+
# Combine and process all files
47+
(rb_files + js_files).map do |file|
48+
ext = File.extname(file)
49+
basename = File.basename(file, ext)
50+
51+
name = basename.camelize
52+
# source = "https://github.com/PhlexUI/phlex_ui/blob/v1/lib/rbui/#{component.to_s.downcase}/#{File.basename(file)}"
53+
source = "lib/rbui/#{camel_to_snake(component)}/#{File.basename(file)}"
54+
built_using = if ext == ".rb"
55+
:phlex
56+
else # ".js"
57+
:stimulus
58+
end
59+
60+
Docs::ComponentStruct.new(
61+
name: name,
62+
source: source,
63+
built_using: built_using
64+
)
65+
end
66+
end
67+
68+
def camel_to_snake(string)
69+
string.gsub("::", "/")
70+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
71+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
72+
.tr("-", "_")
73+
.downcase
74+
end
75+
76+
def snake_to_camel(string)
77+
string.split("_").map(&:capitalize).join
78+
end
279
end

0 commit comments

Comments
 (0)