Skip to content

Commit 418d609

Browse files
committed
Deprecate usage of positional arguments
1 parent fe9ddbd commit 418d609

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Unreleased
2+
- Deprecate usage of positional arguments ([#12](https://github.com/avo-hq/class_variants/pull/12))
23

34
## 0.0.7 (2023-12-07)
45
- Add support for compound variants ([#8](https://github.com/avo-hq/class_variants/pull/8))

lib/class_variants.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
module ClassVariants
77
class << self
8-
def build(classes, **args)
9-
Instance.new classes, **args
8+
def build(...)
9+
Instance.new(...)
1010
end
1111
end
1212
end

lib/class_variants/instance.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module ClassVariants
22
class Instance
3-
attr_reader :classes, :variants, :compoundVariants, :defaults
3+
attr_reader :base, :variants, :compoundVariants, :defaults
44

55
# rubocop:disable Naming/VariableName
6-
def initialize(classes = "", variants: {}, compoundVariants: [], defaults: {})
7-
@classes = classes
6+
def initialize(classes = nil, base: nil, variants: {}, compoundVariants: [], defaults: {})
7+
warn <<~MSG if classes
8+
(ClassVariants) DEPRECATION WARNING: Use of positional argument for default classes is deprecated
9+
and will be removed in the next version. Use the `base` keyword argument instead.
10+
MSG
11+
12+
@base = base || classes
813
@variants = expand_boolean_variants(variants)
914
@compoundVariants = compoundVariants
1015
@defaults = defaults
@@ -13,7 +18,7 @@ def initialize(classes = "", variants: {}, compoundVariants: [], defaults: {})
1318

1419
def render(**overrides)
1520
# Start with our default classes
16-
result = [@classes]
21+
result = [@base]
1722

1823
# Then merge the passed in overrides on top of the defaults
1924
selected = @defaults.merge(overrides)

readme.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ $ gem install class_variants
3333

3434
We create an object from the class or helper where we define the configuration using four arguments:
3535

36-
1. The default classes that should be applied to each variant
37-
1. The `variants` keyword argument where we declare the variants with their option and classes
38-
1. The `compoundVariants` keyword argument where we declare the compound variants with their conditions and classes
36+
1. The `base` keyword argument with default classes that should be applied to each variant.
37+
1. The `variants` keyword argument where we declare the variants with their option and classes.
38+
1. The `compoundVariants` keyword argument where we declare the compound variants with their conditions and classes.
3939
1. The `defaults` keyword argument (optional) where we declare the default value for each variant.
4040

4141
## Example
@@ -45,7 +45,7 @@ Below we implement the [button component](https://tailwindui.com/components/appl
4545
```ruby
4646
# Define the variants and defaults
4747
button_classes = ClassVariants.build(
48-
"inline-flex items-center rounded border border-transparent font-medium text-white hover:text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2",
48+
base: "inline-flex items-center rounded border border-transparent font-medium text-white hover:text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2",
4949
variants: {
5050
size: {
5151
sm: "px-2.5 py-1.5 text-xs",
@@ -82,7 +82,7 @@ button_classes.render(color: :red, size: :xl, icon: true)
8282

8383
```ruby
8484
button_classes = ClassVariants.build(
85-
"inline-flex items-center rounded",
85+
base: "inline-flex items-center rounded",
8686
variants: {
8787
color: {
8888
red: "bg-red-600",
@@ -105,7 +105,8 @@ button_classes.render(color: :red, border: true) # => "inline-flex items-center
105105
```ruby
106106
# Somewhere in your helpers
107107
def button_classes(classes, **args)
108-
class_variants("inline-flex items-center rounded border border-transparent font-medium text-white hover:text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2",
108+
class_variants(
109+
base: "inline-flex items-center rounded border border-transparent font-medium text-white hover:text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2",
109110
variants: {
110111
size: {
111112
sm: "px-2.5 py-1.5 text-xs",

test/class_variants_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class ClassVariantsTest < Minitest::Test
44
def setup
55
@cv = ClassVariants.build(
6-
"rounded border",
6+
base: "rounded border",
77
variants: {
88
size: {
99
sm: "text-sm",

0 commit comments

Comments
 (0)