Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add inheritance strategies to style variants #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

omarluq
Copy link

@omarluq omarluq commented Dec 24, 2024

What is the purpose of this pull request?

resolves #48 & #49

Adds 3 inheritance strategies to style variants:

  1. override (current behavior): Completely replaces parent variants.
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants do
      size {
        lg { 'text-larger' }
      }
    end
  end
end

Component only retains the size variant with lg option

  1. merge (deep merge): Preserves all variant keys unless explicitly overwritten.
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants(strategy: :merge) do
      size {
        lg { 'text-larger' }
      }
    end
  end
end

Maintains access to all variants and keys, with lg key overwritten.

  1. extend (shallow merge): Preserves variants unless explicitly overwritten.
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants(strategy: :extend) do
      size {
        lg { 'text-larger' }
      }
    end
  end
end

Maintains all variants but size variant only retains lg key.

Checklist

  • I've added tests for this change
  • I've added a Changelog entry
  • I've updated a documentation

Adds 3 inheritance strategies to style variants:

1. override (current behavior): Completely replaces parent variants.
```ruby
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants do
      size {
        lg { 'text-larger' }
      }
    end
  end
end
```
Component only retains the size variant with lg option

2. merge (deep merge): Preserves all variant keys unless explicitly overwritten.
```ruby
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants(strategy: :merge) do
      size {
        lg { 'text-larger' }
      }
    end
  end
end
```
Maintains access to all variants and keys, with lg key overwritten.

3. extend (shallow merge): Preserves variants unless explicitly overwritten.
```ruby
class Parent::Component < Component
  style do
    variants do
      size {
        md { 'text-md' }
        lg { 'text-lg' }
      }
      disabled {
        yes { "opacity-50" }
      }
    end
  end
end

class Child::Component < Parent::Component
  style do
    variants(strategy: :extend) do
      size {
        lg { 'text-larger' }
      }
    end
  end
end
```
Maintains all variants but size variant only retains lg key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow style blocks to inherit/honor parent components style blocks unless explicitly overriden
1 participant