-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
# TODO: should this "live" here? | ||
module Primer | ||
# Base class for responsive Stack and StackItem arguments. Used internally. | ||
class ResponsiveArg | ||
# 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] | ||
|
||
include FetchOrFallbackHelper | ||
|
||
class << self | ||
def for(values) | ||
cache[[values, arg_name].hash] ||= new(values) | ||
end | ||
|
||
private | ||
|
||
def cache | ||
Thread.current[:pvc_stack_cache] ||= {} | ||
end | ||
end | ||
|
||
def arg_name | ||
raise NotImplementedError, "Subclasses must implement the `#{__method__}' method" | ||
end | ||
|
||
def to_data_attributes | ||
@data_attributes ||= data_attributes_for(self.class.arg_name, values) | ||
end | ||
|
||
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 | ||
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) | ||
end | ||
end | ||
|
||
def values | ||
raise NotImplementedError, "Subclasses must implement the `#{__method__}' method" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="d-flex border color-border-success-emphasis"> | ||
<%= render(Primer::Alpha::StackItem.new(border: true, border_color: :danger_emphasis)) do %> | ||
Hello, world! | ||
<% end %> | ||
</div> |
12 changes: 9 additions & 3 deletions
12
previews/primer/alpha/stack_item_preview/playground.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
<div class="d-flex border color-border-success-emphasis"> | ||
<%= render(Primer::Alpha::StackItem.new(**system_arguments, border: true, border_color: :danger_emphasis)) do %> | ||
Hello, world! | ||
<div class="d-flex"> | ||
<%= render(Primer::Alpha::StackItem.new(**system_arguments, p: 3, m: 1, bg: :success, border_radius: 2)) do %> | ||
Adjust this item | ||
<% end %> | ||
<%= render(Primer::Alpha::StackItem.new(p: 3, m: 1, bg: :accent, border_radius: 2)) do %> | ||
Fixed width | ||
<% end %> | ||
<%= render(Primer::Alpha::StackItem.new(p: 3, m: 1, bg: :attention, border_radius: 2)) do %> | ||
Fixed width | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters