1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\TagAttribute\Shadow;
8
9class Hover
10{
11    /**
12     * Smooth animation
13     * on hover animation
14     */
15    const COMBO_HOVER_EASING_CLASS = "combo-hover-easing";
16    /**
17     * Supported Animation name of hover
18     * float and grow are not in
19     */
20    const HOVER_ANIMATIONS = ["shrink", "pulse", "pulse-grow", "pulse-shrink", "push", "pop", "bounce-in", "bounce-out", "rotate", "grow-rotate", "sink", "bob", "hang", "skew", "skew-forward", "skew-backward", "wobble-horizontal", "wobble-vertical", "wobble-to-bottom-right", "wobble-to-top-right", "wobble-top", "wobble-bottom", "wobble-skew", "buzz", "buzz-out", "forward", "backward", "fade", "back-pulse", "sweep-to-right", "sweep-to-left", "sweep-to-bottom", "sweep-to-top", "bounce-to-right", "bounce-to-left", "bounce-to-bottom", "bounce-to-top", "radial-out", "radial-in", "rectangle-in", "rectangle-out", "shutter-in-horizontal", "shutter-out-horizontal", "shutter-in-vertical", "shutter-out-vertical", "icon-back", "hollow", "trim", "ripple-out", "ripple-in", "outline-out", "outline-in", "round-corners", "underline-from-left", "underline-from-center", "underline-from-right", "reveal", "underline-reveal", "overline-reveal", "overline-from-left", "overline-from-center", "overline-from-right", "grow-shadow", "float-shadow", "glow", "shadow-radial", "box-shadow-outset", "box-shadow-inset", "bubble-top", "bubble-right", "bubble-bottom", "bubble-left", "bubble-float-top", "bubble-float-right", "bubble-float-bottom", "bubble-float-left", "curl-top-left", "curl-top-right", "curl-bottom-right", "curl-bottom-left"];
21    const ON_HOVER_SNIPPET_ID = "onhover";
22    const ON_HOVER_ATTRIBUTE = "onhover";
23
24    /**
25     * Process hover animation
26     * @param TagAttributes $attributes
27     */
28    public static function processOnHover(TagAttributes &$attributes)
29    {
30        if ($attributes->hasComponentAttribute(self::ON_HOVER_ATTRIBUTE)) {
31            $hover = strtolower($attributes->getValueAndRemove(self::ON_HOVER_ATTRIBUTE));
32            $hoverAnimations = preg_split("/\s/", $hover);
33
34            $comboDataHoverClasses = "";
35            $snippetManager = PluginUtility::getSnippetManager();
36            foreach ($hoverAnimations as $hover) {
37
38                if (in_array($hover, self::HOVER_ANIMATIONS)) {
39
40                    $snippetManager
41                        ->attachRemoteCssStyleSheet(
42                            self::ON_HOVER_SNIPPET_ID,
43                            "https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover-min.css",
44                            "sha512-csw0Ma4oXCAgd/d4nTcpoEoz4nYvvnk21a8VA2h2dzhPAvjbUIK6V3si7/g/HehwdunqqW18RwCJKpD7rL67Xg=="
45                        )
46                        ->setCritical(false);
47
48                    $attributes->addClassName("hvr-$hover");
49
50                } else {
51
52                    /**
53                     * The combo hover effect
54                     */
55                    if (in_array($hover, ["float", "grow"])) {
56                        $hover = "combo-" . $hover;
57                    }
58
59                    /**
60                     * Shadow translation between animation name
61                     * and class
62                     */
63                    switch ($hover) {
64                        case "shadow":
65                            $hover = Shadow::getDefaultClass();
66                            break;
67                        case "shadow-md":
68                            $hover = Shadow::MEDIUM_ELEVATION_CLASS;
69                            break;
70                        case "shadow-lg":
71                            $hover = "shadow";
72                            break;
73                        case "shadow-xl":
74                            $hover = "shadow-lg";
75                            break;
76                    }
77
78                    /**
79                     * Add it to the list of class
80                     */
81                    $comboDataHoverClasses .= " " . $hover;
82
83                }
84
85            }
86            if (!empty($comboDataHoverClasses)) {
87
88                // Grow, float and easing are in the css
89                $snippetManager
90                    ->attachCssInternalStyleSheet(self::ON_HOVER_SNIPPET_ID)
91                    ->setCritical(false);
92
93                // Smooth Transition in and out of hover
94                $attributes->addClassName(self::COMBO_HOVER_EASING_CLASS);
95
96                $attributes->addOutputAttributeValue("data-hover-class", trim($comboDataHoverClasses));
97
98                // The javascript that manage the hover effect by adding the class in the data-hover class
99                $snippetManager->attachJavascriptFromComponentId(self::ON_HOVER_SNIPPET_ID);
100
101            }
102
103        }
104
105    }
106}
107