Skip to content

Commit

Permalink
feat(Stack): add responsive attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
francinelucca committed Sep 30, 2024
1 parent 0e3ce23 commit adfe229
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions app/components/primer/alpha/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class Stack < Primer::Component

DEFAULT_TAG = :div

BREAKPOINTS = [nil, :narrow, :regular, :wide, :wide]


def self.get_responsive_attributes(property, values, options = [], default = nil, optionsMap = Hash.new { |h, k| k })

if !values.is_a?(Array)
{ property => fetch_or_fallback(options, optionsMap[values], default)}
else
values.take(BREAKPOINTS.size).each_with_object({}).with_index do |(value, memo), i|
property_with_breakpoint = [property, BREAKPOINTS[i]].compact.join("-")
memo[property_with_breakpoint] = fetch_or_fallback(options, optionsMap[value])
end
end
end


def initialize(
tag: DEFAULT_TAG,
justify: DEFAULT_JUSTIFY,
Expand All @@ -71,14 +87,12 @@ def initialize(
@system_arguments[:data] = merge_data(
@system_arguments, {
data: {
justify: JUSTIFY_MAPPING[
fetch_or_fallback(JUSTIFY_OPTIONS, justify, DEFAULT_JUSTIFY)
],
gap: fetch_or_fallback(GAP_OPTIONS, gap, DEFAULT_GAP),
direction: fetch_or_fallback(DIRECTION_OPTIONS, direction, DEFAULT_DIRECTION),
align: fetch_or_fallback(ALIGN_OPTIONS, align, DEFAULT_ALIGN),
wrap: fetch_or_fallback(WRAP_OPTIONS, wrap, DEFAULT_WRAP),
padding: fetch_or_fallback(PADDING_OPTIONS, padding, DEFAULT_PADDING)
**get_responsive_attributes('justify', justify, JUSTIFY_OPTIONS, DEFAULT_JUSTIFY, JUSTIFY_MAPPING),
**get_responsive_attributes('gap', gap, GAP_OPTIONS, DEFAULT_GAP),
**get_responsive_attributes('direction', direction, DIRECTION_OPTIONS, DEFAULT_DIRECTION),
**get_responsive_attributes('align', align, ALIGN_OPTIONS, DEFAULT_ALIGN),
**get_responsive_attributes('wrap', align, WRAP_OPTIONS, DEFAULT_WRAP),
**get_responsive_attributes('padding', padding, PADDING_OPTIONS, DEFAULT_PADDING),
}
}
)
Expand Down

0 comments on commit adfe229

Please sign in to comment.