class_variants 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffcbc4e2fd078ac3b22bd391226eec4fc4429bcee10237613f1736bfcff08b6f
4
- data.tar.gz: 4101b943cfd16a259d52e978c3081788451fcd1336ae44644b027aeebb1a4f34
3
+ metadata.gz: b8ecbdedc158b731b446bf166b1dcc6d99b4569cc90f484e95ff6c5466cc3db9
4
+ data.tar.gz: 80de309d4e820f9a3b5667f8c8b4958172906971e923beb3fd169a915cb20811
5
5
  SHA512:
6
- metadata.gz: 3dd5e798a846188944d4fe74342414ccfa180ba6fa319a0e1f2b0ab775f4c32c4d0a5c1129a42c52dce44765cd981db20b5a222b6605989ade32e39a7693fb5f
7
- data.tar.gz: e3eb94faf2f8ad63b6d96fe322b274c77e8a4b2e9fcc2123e267d2b6580398fd06122b73841dbd1a1aea9709667b1afec8ab8c3bce3eecfb4db7e6d409907a76
6
+ metadata.gz: 3fda53be42ac74e51fb85209a24d3bbfed78101052c4bcf016f307f5c0e852adfaf1062102f5e7a80d5563c101c1e418b836eb274557fee1655679084dafe9b8
7
+ data.tar.gz: c20bcba0b71ea4b7c151e2e9a07ca6eac7508f4d938cf8c4b0923baea4dd934651582f4ba7a9eaf0fdacaa606ad71db1faff4ac31705237ca7b5fe5bd31bf26d
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
4
  module ActionView
3
5
  module Helpers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
4
  class Configuration
3
5
  def process_classes_with(&block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
4
  module Helper
3
5
  module ClassMethods
@@ -9,9 +11,12 @@ module ClassVariants
9
11
  def self.included(base)
10
12
  base.extend(ClassMethods)
11
13
  base.singleton_class.instance_variable_set(:@_class_variants_instance, ClassVariants::Instance.new)
12
- base.define_singleton_method(:inherited) do |subclass|
14
+
15
+ def base.inherited(subclass)
16
+ super if defined?(super)
17
+
13
18
  subclass.singleton_class.instance_variable_set(
14
- :@_class_variants_instance, base.singleton_class.instance_variable_get(:@_class_variants_instance).dup
19
+ :@_class_variants_instance, singleton_class.instance_variable_get(:@_class_variants_instance).dup
15
20
  )
16
21
  end
17
22
  end
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
4
  class Instance
3
5
  def initialize(...)
4
6
  @bases = []
5
7
  @variants = []
6
8
  @defaults = {}
9
+ @slots = nil
7
10
 
8
11
  merge(...)
9
12
  end
@@ -17,7 +20,7 @@ module ClassVariants
17
20
  end
18
21
 
19
22
  def merge(**options, &block)
20
- raise ArgumentError, "Use of hash config and code block is not supported" if !options.empty? && block_given?
23
+ raise ArgumentError, "Use of hash config and code block is not supported" if !options.empty? && block
21
24
 
22
25
  (base = options.fetch(:base, nil)) && @bases << {class: base, slot: :default}
23
26
  @variants += [
@@ -26,7 +29,7 @@ module ClassVariants
26
29
  ].inject(:+)
27
30
  @defaults.merge!(options.fetch(:defaults, {}))
28
31
 
29
- instance_eval(&block) if block_given?
32
+ instance_eval(&block) if block
30
33
 
31
34
  self
32
35
  end
@@ -46,9 +49,15 @@ module ClassVariants
46
49
  @variants.each do |candidate|
47
50
  next unless candidate[:slot] == slot
48
51
 
49
- if (candidate.keys - [:class, :slot]).all? { |key| criteria[key] == candidate[key] }
50
- result << candidate[:class]
52
+ match = false
53
+
54
+ candidate.each_key do |key|
55
+ next if key == :class || key == :slot
56
+ match = criteria[key] == candidate[key]
57
+ break unless match
51
58
  end
59
+
60
+ result << candidate[:class] if match
52
61
  end
53
62
 
54
63
  # add the passed in classes to the result
@@ -64,9 +73,9 @@ module ClassVariants
64
73
  private
65
74
 
66
75
  def base(klass = nil, &block)
67
- raise ArgumentError, "Use of positional argument and code block is not supported" if klass && block_given?
76
+ raise ArgumentError, "Use of positional argument and code block is not supported" if klass && block
68
77
 
69
- if block_given?
78
+ if block
70
79
  with_slots(&block).each do |slot|
71
80
  @bases << slot
72
81
  end
@@ -76,9 +85,9 @@ module ClassVariants
76
85
  end
77
86
 
78
87
  def variant(**options, &block)
79
- raise ArgumentError, "Use of class option and code block is not supported" if options.key?(:class) && block_given?
88
+ raise ArgumentError, "Use of class option and code block is not supported" if options.key?(:class) && block
80
89
 
81
- if block_given?
90
+ if block
82
91
  with_slots(&block).each do |slot|
83
92
  @variants << options.merge(slot)
84
93
  end
@@ -98,9 +107,10 @@ module ClassVariants
98
107
  end
99
108
 
100
109
  def with_slots
101
- @slots = []
110
+ new_slots = []
111
+ @slots = new_slots
102
112
  yield
103
- @slots
113
+ new_slots
104
114
  end
105
115
 
106
116
  def expand_variants(variants)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/railtie"
2
4
 
3
5
  module ClassVariants
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
- VERSION = "1.1.0".freeze
4
+ VERSION = "1.1.1"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "class_variants/version"
2
4
  require "class_variants/action_view/helpers"
3
5
  require "class_variants/configuration"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ClassVariants
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
data/readme.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  We ❤️ Tailwind CSS but sometimes it's difficult to manage the state of some elements using conditionals. `class_variants` is a tiny helper that should enable you to create, configure, and apply different variants of elements as classes.
4
4
 
5
+ ![](./logo.png)
6
+
5
7
  Inspired by [variant-classnames](https://github.com/mattvalleycodes/variant-classnames) ✌️
6
8
 
7
9
  ## Quicklinks
@@ -98,7 +100,7 @@ button_classes = ClassVariants.build(
98
100
  )
99
101
 
100
102
  button_classes.render(color: :red) # => "inline-flex items-center rounded bg-red-600"
101
- button_classes.render(color: :red, border: true) # => "inline-flex items-center rounded bg-red-600 border border-red-600"
103
+ button_classes.render(color: :red, border: true) # => "inline-flex items-center rounded bg-red-600 border border-red-800"
102
104
  ```
103
105
 
104
106
  ## Override classes with `render`
@@ -362,8 +364,9 @@ Install the gem using `bundle add tailwind_merge` and use this configuration to
362
364
 
363
365
  ```ruby
364
366
  ClassVariants.configure do |config|
367
+ merger = TailwindMerge::Merger.new
365
368
  config.process_classes_with do |classes|
366
- TailwindMerge::Merger.new.merge(classes)
369
+ merger.merge(classes)
367
370
  end
368
371
  end
369
372
  ```
@@ -371,7 +374,8 @@ end
371
374
  ## Other packages
372
375
 
373
376
  - [`active_storage-blurhash`](https://github.com/avo-hq/active_storage-blurhash) - A plug-n-play [blurhash](https://blurha.sh/) integration for images stored in ActiveStorage
374
- - [`avo`](https://github.com/avo-hq/avo) - Build Content management systems with Ruby on Rails
377
+ - [`avo`](https://github.com/avo-hq/avo) - Build internal tools, admin panels, and dashboards with Ruby on Rails
378
+ - [`marksmith`](https://github.com/avo-hq/marksmith) - GitHub-style markdown editor for Ruby and Rails
375
379
  - [`prop_initializer`](https://github.com/avo-hq/prop_initializer) - A flexible tool for defining properties on Ruby classes.
376
380
  - [`stimulus-confetti`](https://github.com/avo-hq/stimulus-confetti) - The easiest way to add confetti to your StimulusJS app
377
381
 
@@ -379,7 +383,11 @@ end
379
383
 
380
384
  If you enjoyed this gem try out [Avo](https://github.com/avo-hq/avo). It helps developers build Internal Tools, Admin Panels, CMSes, CRMs, and any other type of Business Apps 10x faster on top of Ruby on Rails.
381
385
 
382
- [![](./logo-on-white.png)](https://github.com/avo-hq/avo)
386
+ [![](./avo-logo.png)](https://github.com/avo-hq/avo)
387
+
388
+ ## Articles
389
+
390
+ [TIL: How to use `class_variants` with Phlex](https://henrikbjorn.medium.com/til-how-to-use-class-variants-with-phlex-8042bd4407f1)
383
391
 
384
392
  ## Contributing
385
393
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: class_variants
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Marin
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-01-20 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Easily configure styles and apply them as classes.
14
13
  email: adrian@adrianthedev.com
@@ -36,7 +35,6 @@ metadata:
36
35
  source_code_uri: https://github.com/avo-hq/class_variants
37
36
  bug_tracker_uri: https://github.com/avo-hq/class_variants/issues
38
37
  changelog_uri: https://github.com/avo-hq/class_variants/releases
39
- post_install_message:
40
38
  rdoc_options: []
41
39
  require_paths:
42
40
  - lib
@@ -51,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
49
  - !ruby/object:Gem::Version
52
50
  version: '0'
53
51
  requirements: []
54
- rubygems_version: 3.4.10
55
- signing_key:
52
+ rubygems_version: 3.6.9
56
53
  specification_version: 4
57
54
  summary: Easily configure styles and apply them as classes.
58
55
  test_files: []