Class: TailwindMerge::ClassUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/tailwind_merge/class_utils.rb

Constant Summary collapse

CLASS_PART_SEPARATOR =
"-"
ARBITRARY_PROPERTY_REGEX =
/^\[(.+)\]$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ClassUtils

Returns a new instance of ClassUtils.



11
12
13
14
# File 'lib/tailwind_merge/class_utils.rb', line 11

def initialize(config)
  @config = config
  @class_map = create_class_map(config)
end

Instance Attribute Details

#class_mapObject (readonly)

Returns the value of attribute class_map.



5
6
7
# File 'lib/tailwind_merge/class_utils.rb', line 5

def class_map
  @class_map
end

Instance Method Details

#class_group_id(class_name) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/tailwind_merge/class_utils.rb', line 16

def class_group_id(class_name)
  class_parts = class_name.split(CLASS_PART_SEPARATOR)

  # Classes like `-inset-1` produce an empty string as first classPart.
  # Assume that classes for negative values are used correctly and remove it from class_parts.
  class_parts.shift if class_parts.first == "" && class_parts.length != 1

  get_group_recursive(class_parts, @class_map) || get_group_id_for_arbitrary_property(class_name)
end

#get_conflicting_class_group_ids(class_group_id, has_postfix_modifier) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/tailwind_merge/class_utils.rb', line 54

def get_conflicting_class_group_ids(class_group_id, has_postfix_modifier)
  conflicts = @config[:conflicting_class_groups][class_group_id] || []

  if has_postfix_modifier && @config[:conflicting_class_group_modifiers][class_group_id]
    return [...conflicts, ...@config[:conflicting_class_group_modifiers][class_group_id]]
  end

  conflicts
end

#get_group_recursive(class_parts, class_part_object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tailwind_merge/class_utils.rb', line 26

def get_group_recursive(class_parts, class_part_object)
  return class_part_object[:class_group_id] if class_parts.empty?

  current_class_part = class_parts.first

  next_class_part_object = class_part_object[:next_part][current_class_part]

  class_group_from_next_class_part = next_class_part_object ? get_group_recursive(class_parts[1..-1], next_class_part_object) : nil

  return class_group_from_next_class_part if class_group_from_next_class_part

  return if class_part_object[:validators].empty?

  class_rest = class_parts.join(CLASS_PART_SEPARATOR)

  result = class_part_object[:validators].find do |v|
    validator = v[:validator]

    if from_theme?(validator)
      validator.call(@config)
    else
      validator.call(class_rest)
    end
  end

  result.nil? ? result : result[:class_group_id]
end