Skip to content

Add conditional CSS class arrays to all *Class() methods - #143

Open
WarLikeLaux wants to merge 3 commits into
yiisoft:masterfrom
WarLikeLaux:add-conditional-css-classes
Open

Add conditional CSS class arrays to all *Class() methods#143
WarLikeLaux wants to merge 3 commits into
yiisoft:masterfrom
WarLikeLaux:add-conditional-css-classes

Conversation

@WarLikeLaux

Copy link
Copy Markdown
Contributor
Q A
Is bugfix?
New feature?
Docs added?
Tests added? ✔️
Breaks BC?

What does this PR do?

Extends all 29 *Class() methods in Alert, Dropdown, and Menu to accept string|array. The array format supports conditional CSS classes (Laravel Blade pattern).

Menu::widget()
    ->class(['nav' => true, 'active' => $isActive, 'dark' => $isDark])
    ->activeClass(['selected' => true, 'highlight' => $isHighlighted])
    ->items($items)
    ->render();

Keys with true values are included, false are excluded. Numeric keys are always included. This replaces ternary expressions like ->class($active ? 'nav active' : 'nav').

Resolution logic is in Normalizer::cssClasses(). No BC break, existing string calls work unchanged.

@codecov

codecov Bot commented Mar 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a602b53) to head (07c901d).

Additional details and impacted files
@@             Coverage Diff             @@
##              master      #143   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       316       321    +5     
===========================================
  Files              8         8           
  Lines           1005      1015   +10     
===========================================
+ Hits            1005      1015   +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vjik vjik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add conditional CSS class feature to Yii HTML ("class" in attributes array, Html::addCssClass() method) without BC break?

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.

2 participants