Skip to content

Commit

Permalink
fix(Stack): rewrite responsive arg logic
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Dutro <camertron@gmail.com>
  • Loading branch information
francinelucca and camertron committed Oct 2, 2024
1 parent 56e457c commit 14b8b88
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 59 deletions.
2 changes: 1 addition & 1 deletion app/components/primer/alpha/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JustifyArg < Primer::ResponsiveArg
OPTIONS = MAPPING.keys.freeze

def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT).map do |value|
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT) do |value|
MAPPING[value]
end
end
Expand Down
29 changes: 20 additions & 9 deletions app/components/primer/responsive_arg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
module Primer
# Base class for responsive Stack and StackItem arguments. Used internally.
class ResponsiveArg
# TODO: rewrite
# The primer/react Stack component defines three breakpoints, but PVC uses five.
# We define this array as an index-based mapping between the two systems. The first
# element is the default and results in eg. { "justify" => "start" }, while the
# other breakpoints result in keys with breakpoint suffixes, eg.
# { "justify-narrow" => "start" }.
BREAKPOINTS = [nil, :narrow, :regular, :wide, :wide]
# { "justify-narrow" => "start" }.
BREAKPOINTS = [:narrow, :regular, :wide].freeze

include FetchOrFallbackHelper

Expand Down Expand Up @@ -37,16 +38,26 @@ def to_data_attributes
private

def data_attributes_for(property, values)
values.take(BREAKPOINTS.size).each_with_object({}).with_index do |(value, memo), i|
next unless value
property_with_breakpoint = [property, BREAKPOINTS[i]].compact.join("-")
memo[property_with_breakpoint] = value
if values.is_a?(Hash)
values.slice(*BREAKPOINTS).each_with_object({}) do |(key, value), memo|
next unless value
property_with_breakpoint = "#{property}-#{key}"
memo[property_with_breakpoint] = value
end
else
{property => values}
end
end

def fetch_or_fallback_all(allowed_values, given_values, default_value)
Array(given_values).map do |given_value|
fetch_or_fallback(allowed_values, given_value, default_value)
def fetch_or_fallback_all(options, values, default)
if values.is_a?(Hash)
values.each_with_object({}) do |(key, value), memo|
value = block_given? ? yield(value) : value
memo[key] = fetch_or_fallback(options, value, default)
end
else
value = block_given? ? yield(values) : values
fetch_or_fallback(options, value, default)
end
end

Expand Down
14 changes: 7 additions & 7 deletions previews/primer/alpha/stack_item_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ def playground(
render_with_template(locals: {
system_arguments: {
tag: tag,
grow: [
grow,
grow_narrow,
grow_regular,
grow_wide,
grow_wide
]
grow: get_control_values(grow, grow_narrow, grow_regular, grow_wide)
}
})
end

private

def get_control_values(normal, narrow, regular, wide)
[narrow, regular, wide].any? ? {narrow:, regular:, wide:} : normal
end
end
end
end
54 changes: 12 additions & 42 deletions previews/primer/alpha/stack_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,56 +69,26 @@ def playground(
system_arguments: {
tag: tag,

justify: [
justify,
justify_narrow,
justify_regular,
justify_wide,
justify_wide
],
justify: get_control_values(justify, justify_narrow, justify_regular, justify_wide),

gap: [
gap,
gap_narrow,
gap_regular,
gap_wide,
gap_wide
],
gap: get_control_values(gap, gap_narrow, gap_regular, gap_wide),

direction: [
direction,
direction_narrow,
direction_regular,
direction_wide,
direction_wide
],
direction: get_control_values(direction, direction_narrow, direction_regular, direction_wide),

wrap: [
wrap,
wrap_narrow,
wrap_regular,
wrap_wide,
wrap_wide
],
wrap: get_control_values(wrap, wrap_narrow, wrap_regular, wrap_wide),

padding: [
padding,
padding_narrow,
padding_regular,
padding_wide,
padding_wide
],
padding: get_control_values(padding, padding_narrow, padding_regular, padding_wide),

align: [
align,
align_narrow,
align_regular,
align_wide,
align_wide
],
align: get_control_values(align, align_narrow, align_regular, align_wide),
}
})
end

private

def get_control_values(normal, narrow, regular, wide)
[narrow, regular, wide].any? ? : {narrow:, regular:, wide:} : normal
end
end
end
end

0 comments on commit 14b8b88

Please sign in to comment.