10 abstract class Theme {
14 private static $singleton;
19 * @param Theme|null $theme Theme to use throughout the application
21 public static function setSingleton( Theme $theme = null ) {
22 self::$singleton = $theme;
29 public static function singleton() {
30 if ( !self::$singleton ) {
31 throw new Exception( __METHOD__ . ' was called with no singleton theme set.' );
34 return self::$singleton;
38 * Get a list of classes to be applied to a widget.
40 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
41 * otherwise state transitions will not work properly.
43 * @param Element $element Element for which to get classes
44 * @return array Categorized class names with `on` and `off` lists
46 public function getElementClasses( Element $element ) {
47 return [ 'on' => [], 'off' => [] ];
51 * Update CSS classes provided by the theme.
53 * For elements with theme logic hooks, this should be called any time there's a state change.
55 * @param Element $element Element for which to update classes
56 * @return array Categorized class names with `on` and `off` lists
58 public function updateElementClasses( Element $element ) {
59 $classes = $this->getElementClasses( $element );
60 $traits = class_uses( $element );
62 if ( in_array( IconElement::class, $traits ) ) {
63 $element->getIconElement()
64 ->removeClasses( $classes['off'] )
65 ->addClasses( $classes['on'] );
67 if ( in_array( IndicatorElement::class, $traits ) ) {
68 $element->getIndicatorElement()
69 ->removeClasses( $classes['off'] )
70 ->addClasses( $classes['on'] );