Class: Ariadne::Classify

Inherits:
Object
  • Object
show all
Defined in:
lib/ariadne/classify.rb,
lib/ariadne/classify/utilities.rb,
lib/ariadne/classify/validation.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Utilities, Validation

Constant Summary collapse

FLEX_VALUES =
[1, :auto].freeze
FLEX_WRAP_MAPPINGS =
{
  wrap: "flex-wrap",
  nowrap: "flex-nowrap",
  reverse: "flex-wrap-reverse",
}.freeze
FLEX_ALIGN_SELF_VALUES =
[:auto, :start, :end, :center, :baseline, :stretch].freeze
FLEX_DIRECTION_VALUES =
[:column, :column_reverse, :row, :row_reverse].freeze
FLEX_JUSTIFY_CONTENT_VALUES =
[:flex_start, :flex_end, :center, :space_between, :space_around].freeze
FLEX_ALIGN_ITEMS_VALUES =
[:flex_start, :flex_end, :center, :baseline, :stretch].freeze
LOOKUP =
Ariadne::Classify::Utilities::UTILITIES
BG_PREFIX =

TODO: automate this, ugh. peek at utilities.yml

/^bg-/.freeze
BG_PSEUDO_PREFIX =
/^\S+:bg-/.freeze
BORDER_PREFIX =
/^border-/.freeze
BORDER_PSEUDO_PREFIX =
/^\S+:border-/.freeze
TEXT_ASPECT_PREFIX =
/^text-\S+-/.freeze
TEXT_ASPECT_PSEUDO_PREFIX =
/^\S+:text-\S+-/.freeze
TEXT_PREFIX =
/^text-/.freeze
TEXT_PSEUDO_PREFIX =
/^\S+:text-/.freeze

Class Method Summary collapse

Class Method Details

.call(args) ⇒ Object

Utility for mapping component configuration into Tailwind CSS class names.

args can contain utility keys that mimic the interface used by Ariadne components, as well as the special entries :classes and :style.

Returns a hash containing two entries. The :classes entry is a string of Tailwind CSS class names, including any classes given in the :classes entry in args. The :style entry is the value of the given :style entry given in args.

Example usage: extract_css_attrs({ mt: 4, py: 2 }) => { classes: “mt-4 py-2”, style: nil } extract_css_attrs(classes: “d-flex”, mt: 4, py: 2) => { classes: “d-flex mt-4 py-2”, style: nil } extract_css_attrs(classes: “d-flex”, style: “float: left”, mt: 4, py: 2) => { classes: “d-flex mt-4 py-2”, style: “float: left” }



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ariadne/classify.rb', line 45

def call(args)
  style = nil
  args = [] if args.blank?

  classes = [].tap do |result|
    args.each do |key, val|
      case key
      when :classes
        # insert :classes first to avoid huge doc diffs
        if (class_names = validated_class_names(val))
          result.unshift(class_names)
        end
        next
      when :style
        style = val
        next
      end

      next unless LOOKUP[key]

      if val.is_a?(Array)
        # A while loop is ~3.5x faster than Array#each.
        brk = 0
        while brk < val.size
          item = val[brk]

          if item.nil?
            brk += 1
            next
          end

          # Believe it or not, three calls to Hash#[] and an inline rescue
          # are about 30% faster than Hash#dig. It also ensures validate is
          # only called when necessary, i.e. when the class can't be found
          # in the lookup table.
          # rubocop:disable Style/RescueModifier
          found = (LOOKUP[key][item][brk] rescue nil) || validate(key, item, brk)
          # rubocop:enable Style/RescueModifier
          result << found if found
          brk += 1
        end
      else
        next if val.nil?

        # rubocop:disable Style/RescueModifier
        found = (LOOKUP[key][val][0] rescue nil) || validate(key, val, 0)
        # rubocop:enable Style/RescueModifier
        result << found if found
      end
    end
  end.join(" ")

  result = {}

  result[:class] = classes if classes.present?
  result[:style] = style if style.present?

  result
end