| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Form\Style; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; // Exit if accessed directly |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class CheckboxRadioStyleGenerator |
| 18 |
* |
| 19 |
* Generates CSS for checkboxes and radio buttons |
| 20 |
* |
| 21 |
* @package IvyForms\Services\Form\StyleHelpers |
| 22 |
*/ |
| 23 |
class CheckboxRadioStyleGenerator implements StyleGeneratorInterface |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Generate checkbox and radio styles |
| 27 |
* |
| 28 |
* @param CssBuilder $css |
| 29 |
* @param StyleContext $context |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function generate(CssBuilder $css, StyleContext $context): void |
| 33 |
{ |
| 34 |
$activeColors = $context->getActiveColors(); |
| 35 |
if (empty($activeColors['checkboxRadioSelection'])) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$borderColor = $activeColors['checkboxRadioBorder'] ?? $activeColors['checkboxRadioSelection']; |
| 40 |
$baseSelector = $context->getBaseSelector(); |
| 41 |
|
| 42 |
$this->addCheckboxStyles($css, $baseSelector, $activeColors['checkboxRadioSelection'], $borderColor); |
| 43 |
$this->addRadioStyles($css, $baseSelector, $activeColors['checkboxRadioSelection'], $borderColor); |
| 44 |
$this->addLabelStyles($css, $baseSelector, $activeColors); |
| 45 |
$this->addRequiredIndicator($css, $baseSelector, $activeColors); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add checkbox styles |
| 50 |
* |
| 51 |
* @param CssBuilder $css |
| 52 |
* @param string $baseSelector |
| 53 |
* @param string $selectionColor |
| 54 |
* @param string $borderColor |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
private function addCheckboxStyles( |
| 58 |
CssBuilder $css, |
| 59 |
string $baseSelector, |
| 60 |
string $selectionColor, |
| 61 |
string $borderColor |
| 62 |
): void { |
| 63 |
// Unchecked state |
| 64 |
$css->addRule( |
| 65 |
[ |
| 66 |
$baseSelector . ' .ivyforms-checkbox .el-checkbox__input .el-checkbox__inner', |
| 67 |
$baseSelector . ' .el-checkbox__input .el-checkbox__inner', |
| 68 |
], |
| 69 |
[ |
| 70 |
' border-color: ' . esc_attr($borderColor) . ' !important;', |
| 71 |
' border-width: 2px !important;', |
| 72 |
' border-radius: 4px !important;', |
| 73 |
' background-color: transparent !important;', |
| 74 |
' box-shadow: none !important;', |
| 75 |
] |
| 76 |
); |
| 77 |
|
| 78 |
// Checked state |
| 79 |
$css->addRule( |
| 80 |
[ |
| 81 |
$baseSelector . ' .ivyforms-checkbox .el-checkbox__input.is-checked .el-checkbox__inner', |
| 82 |
$baseSelector . ' .el-checkbox__input.is-checked .el-checkbox__inner', |
| 83 |
], |
| 84 |
[ |
| 85 |
' background-color: ' . esc_attr($selectionColor) . ' !important;', |
| 86 |
' border-color: ' . esc_attr($selectionColor) . ' !important;', |
| 87 |
' box-shadow: none !important;', |
| 88 |
] |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add radio button styles |
| 94 |
* |
| 95 |
* @param CssBuilder $css |
| 96 |
* @param string $baseSelector |
| 97 |
* @param string $selectionColor |
| 98 |
* @param string $borderColor |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
private function addRadioStyles( |
| 102 |
CssBuilder $css, |
| 103 |
string $baseSelector, |
| 104 |
string $selectionColor, |
| 105 |
string $borderColor |
| 106 |
): void { |
| 107 |
// Unchecked state |
| 108 |
$css->addRule( |
| 109 |
[ |
| 110 |
$baseSelector . ' .ivyforms-radio .el-radio__input .el-radio__inner', |
| 111 |
$baseSelector . ' .el-radio__input .el-radio__inner', |
| 112 |
], |
| 113 |
[ |
| 114 |
' border-color: ' . esc_attr($borderColor) . ' !important;', |
| 115 |
' border-width: 2px !important;', |
| 116 |
' border-radius: 50% !important;', |
| 117 |
' background-color: transparent !important;', |
| 118 |
' box-shadow: none !important;', |
| 119 |
] |
| 120 |
); |
| 121 |
|
| 122 |
// Checked state - outer ring |
| 123 |
$css->addRule( |
| 124 |
[ |
| 125 |
$baseSelector . ' .ivyforms-radio .el-radio__input.is-checked .el-radio__inner', |
| 126 |
$baseSelector . ' .el-radio__input.is-checked .el-radio__inner', |
| 127 |
], |
| 128 |
[ |
| 129 |
' border-color: ' . esc_attr($selectionColor) . ' !important;', |
| 130 |
' background-color: transparent !important;', |
| 131 |
' box-shadow: none !important;', |
| 132 |
] |
| 133 |
); |
| 134 |
|
| 135 |
// Checked state - inner dot |
| 136 |
$css->addRule( |
| 137 |
[ |
| 138 |
$baseSelector . ' .ivyforms-radio .el-radio__input.is-checked .el-radio__inner::after', |
| 139 |
$baseSelector . ' .el-radio__input.is-checked .el-radio__inner::after', |
| 140 |
], |
| 141 |
[ |
| 142 |
' background-color: ' . esc_attr($selectionColor) . ' !important;', |
| 143 |
' width: 8px !important;', |
| 144 |
' height: 8px !important;', |
| 145 |
] |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Add label styles |
| 151 |
* |
| 152 |
* @param CssBuilder $css |
| 153 |
* @param string $baseSelector |
| 154 |
* @param array<string, string> $activeColors |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
private function addLabelStyles( |
| 158 |
CssBuilder $css, |
| 159 |
string $baseSelector, |
| 160 |
array $activeColors |
| 161 |
): void { |
| 162 |
$labelColor = $activeColors['checkboxRadioLabel'] ?? $activeColors['fieldLabels'] ?? null; |
| 163 |
|
| 164 |
if (empty($labelColor)) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$css->addRule( |
| 169 |
[ |
| 170 |
$baseSelector . ' .ivyforms-checkbox .el-checkbox__label', |
| 171 |
$baseSelector . ' .el-checkbox__label', |
| 172 |
], |
| 173 |
[' color: ' . esc_attr($labelColor) . ' !important;'] |
| 174 |
); |
| 175 |
|
| 176 |
$css->addRule( |
| 177 |
[ |
| 178 |
$baseSelector . ' .ivyforms-radio .el-radio__label', |
| 179 |
$baseSelector . ' .el-radio__label', |
| 180 |
], |
| 181 |
[' color: ' . esc_attr($labelColor) . ' !important;'] |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Add required indicator styles |
| 187 |
* |
| 188 |
* @param CssBuilder $css |
| 189 |
* @param string $baseSelector |
| 190 |
* @param array<string, string> $activeColors |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
private function addRequiredIndicator( |
| 194 |
CssBuilder $css, |
| 195 |
string $baseSelector, |
| 196 |
array $activeColors |
| 197 |
): void { |
| 198 |
if (empty($activeColors['requiredIndicator'])) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$css->addRule( |
| 203 |
[ |
| 204 |
$baseSelector . ' .ivyforms-form-item .ivyforms-form-item__asterisk', |
| 205 |
$baseSelector . ' .ivyforms-form-item__asterisk', |
| 206 |
$baseSelector . ' .el-form-item__label--required::before', |
| 207 |
], |
| 208 |
[' color: ' . esc_attr($activeColors['requiredIndicator']) . ' !important;'] |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
|