| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
class Style_States { |
| 6 |
const HOVER = 'hover'; |
| 7 |
const ACTIVE = 'active'; |
| 8 |
const FOCUS = 'focus'; |
| 9 |
const FOCUS_VISIBLE = 'focus-visible'; |
| 10 |
|
| 11 |
const SELECTED = 'e--selected'; |
| 12 |
|
| 13 |
private static function get_pseudo_states(): array { |
| 14 |
return [ |
| 15 |
self::HOVER, |
| 16 |
self::ACTIVE, |
| 17 |
self::FOCUS, |
| 18 |
self::FOCUS_VISIBLE, |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
private static function get_class_states(): array { |
| 23 |
return [ |
| 24 |
self::SELECTED, |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
private static function get_additional_states_map(): array { |
| 29 |
return [ |
| 30 |
self::HOVER => [ self::FOCUS_VISIBLE ], |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public static function get_selector_with_state( string $base_selector, string $state ): string { |
| 35 |
$additional_states = self::get_additional_states( $state ); |
| 36 |
$all_states = [ $state, ...$additional_states ]; |
| 37 |
|
| 38 |
foreach ( $all_states as $current_state ) { |
| 39 |
$selector_strings[] = $base_selector . self::get_state_selector( $current_state ); |
| 40 |
} |
| 41 |
|
| 42 |
return implode( ',', $selector_strings ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function get_additional_states( string $state ): array { |
| 46 |
return self::get_additional_states_map()[ $state ] ?? []; |
| 47 |
} |
| 48 |
|
| 49 |
public static function get_state_selector( string $state ): string { |
| 50 |
if ( self::is_class_state( $state ) ) { |
| 51 |
return '.' . $state; |
| 52 |
} |
| 53 |
|
| 54 |
if ( self::is_pseudo_state( $state ) ) { |
| 55 |
return ':' . $state; |
| 56 |
} |
| 57 |
|
| 58 |
return $state; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
public static function get_valid_states(): array { |
| 63 |
return [ |
| 64 |
...array_filter( self::get_pseudo_states(), function ( $state ) { |
| 65 |
return ! in_array( $state, self::get_additional_states_map()[ $state ] ?? [], true ); |
| 66 |
} ), |
| 67 |
...self::get_class_states(), |
| 68 |
null, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
public static function is_pseudo_state( string $state ): bool { |
| 73 |
return in_array( $state, self::get_pseudo_states(), true ); |
| 74 |
} |
| 75 |
|
| 76 |
public static function is_class_state( string $state ): bool { |
| 77 |
return in_array( $state, self::get_class_states(), true ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function is_valid_state( $state ): bool { |
| 81 |
if ( null === $state ) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
return is_string( $state ) && in_array( $state, self::get_valid_states(), true ); |
| 86 |
} |
| 87 |
|
| 88 |
public static function get_class_states_map(): array { |
| 89 |
return [ |
| 90 |
'selected' => [ |
| 91 |
'name' => 'selected', |
| 92 |
'value' => self::SELECTED, |
| 93 |
], |
| 94 |
]; |
| 95 |
} |
| 96 |
} |
| 97 |
|