Skip to content

Commit

Permalink
Last Rubocop Error
Browse files Browse the repository at this point in the history
  • Loading branch information
jasperfurniss committed Nov 4, 2024
1 parent 6e59a4c commit 2f65ed3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 33 deletions.
38 changes: 37 additions & 1 deletion playbook/app/pb_kits/playbook/pb_card/docs/_card_light.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
<%= pb_rails("card", props: {
}) do %>
html_options: {
class: "pb-card-light"
},
height: "600px"
}) do %>
Card content
<% end %>
<%# Example showing html_options style also works %>
<%= pb_rails("card", props: {
html_options: {
class: "pb-card-light",
style: { height: "422px", background_color: "orange" }
}
}) do %>
Card content with html_options style
<% end %>
<%# Example showing just html_options %>
<%= pb_rails("card", props: {
html_options: {
width: "60px",
}
}) do %>
Card content with just html_options style
<% end %>
<%= pb_rails("card", props: {
height: "100%",
html_options: {
style: { width: "100%", background_color: "blue" },
tabindex: 7
},
min_height: "189px"
}) do %>
HELLO WORLD!!!
<% end %>

65 changes: 33 additions & 32 deletions playbook/lib/playbook/kit_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,35 @@ def object
self
end

def pb_content_tag(name = :div, content_or_options_with_block = {}, options = {}, escape: true, &block)
combined_options = options
.merge(combined_html_options)
.merge(default_options.merge(content_or_options_with_block))
content_tag(name, combined_options, options, escape, &block)
end

def combined_html_options
default_html_options.merge(html_options.deep_merge(data_attributes))
merged = default_html_options.dup

html_options.each do |key, value|
if key == :style && value.is_a?(Hash)
# Convert style hash to CSS string
merged[:style] = value.map { |k, v| "#{k.to_s.gsub('_', '-')}: #{v}" }.join("; ")
else
merged[key] = value
end
end

inline_styles = dynamic_inline_props
if inline_styles.present?
merged[:style] = if merged[:style].present?
"#{merged[:style]}; #{inline_styles}"
else
inline_styles
end
end

merged.deep_merge(data_attributes)
end

def global_inline_props
Expand All @@ -94,34 +121,6 @@ def global_inline_props
}.compact
end

# rubocop:disable Layout/CommentIndentation
# pb_content_tag information (potentially to be abstracted into its own dev doc in the future)
# The pb_content_tag generates HTML content tags for rails kits with flexible options.
# Modify a generated kit.html.erb file accordingly (the default_options listed below no longer need to be explictly outlined in that file, only modifications).
# name - the first argument is for HTML tag. The default is :div.
# content_or_options_with_block - additional content or options for the tag (i.e., the customizations a dev adds to kit.html.erb).
# options - Within combined_options, the empty options hash allows for customizations to
# merge with the default_options and combined_html_options.
# escape - set to true, this allows for HTML-escape.
# block - an optional block for content inside the tag.
# The return is a HTML tag that includes any provided customizations. If nothing is specified in kit.html.erb, the default shape is:
# :div,
# aria: object.aria,
# class: object.classname,
# data: object.data,
# id: object.id,
# **combined_html_options
# rubocop:enable Layout/CommentIndentation

# rubocop:disable Style/OptionalBooleanParameter
def pb_content_tag(name = :div, content_or_options_with_block = {}, options = {}, escape = true, &block)
combined_options = options
.merge(combined_html_options)
.merge(default_options.merge(content_or_options_with_block))
content_tag(name, combined_options, options, escape, &block)
end
# rubocop:enable Style/OptionalBooleanParameter

private

def default_options
Expand All @@ -131,8 +130,10 @@ def default_options
class: classname,
aria: aria,
}

inline_styles = dynamic_inline_props
options[:style] = inline_styles if inline_styles.present?
options[:style] = inline_styles if inline_styles.present? && !html_options.key?(:style)

options
end

Expand All @@ -148,8 +149,8 @@ def data_attributes
end

def dynamic_inline_props
styles = global_inline_props.map { |key, value| "#{key.to_s.gsub('_', '-')}: #{value};" if value.present? }.compact
styles.join(" ").presence
styles = global_inline_props.map { |key, value| "#{key.to_s.gsub('_', '-')}: #{value}" if value.present? }.compact
styles.join("; ").presence
end
end
end

0 comments on commit 2f65ed3

Please sign in to comment.