xref: /plugin/combo/ComboStrap/Hover.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4namespace ComboStrap;
5
6
7class Hover
8{
9    /**
10     * Smooth animation
11     * on hover animation
12     */
13    const COMBO_HOVER_EASING_CLASS = "combo-hover-easing";
14    /**
15     * Supported Animation name of hover
16     * float and grow are not in
17     */
18    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"];
19    const ON_HOVER_SNIPPET_ID = "onhover";
20    const ON_HOVER_ATTRIBUTE = "onhover";
21
22    /**
23     * Process hover animation
24     * @param TagAttributes $attributes
25     */
26    public static function processOnHover(&$attributes)
27    {
28        if ($attributes->hasComponentAttribute(self::ON_HOVER_ATTRIBUTE)) {
29            $hover = strtolower($attributes->getValueAndRemove(self::ON_HOVER_ATTRIBUTE));
30            $hoverAnimations = preg_split("/\s/", $hover);
31
32            $comboDataHoverClasses = "";
33            $snippetManager = PluginUtility::getSnippetManager();
34            foreach ($hoverAnimations as $hover) {
35
36                if (in_array($hover, self::HOVER_ANIMATIONS)) {
37
38                    $snippetManager
39                        ->attachCssExternalStyleSheetForSlot(
40                            self::ON_HOVER_SNIPPET_ID,
41                            "https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover-min.css",
42                            "sha512-csw0Ma4oXCAgd/d4nTcpoEoz4nYvvnk21a8VA2h2dzhPAvjbUIK6V3si7/g/HehwdunqqW18RwCJKpD7rL67Xg=="
43                        )
44                        ->setCritical(false);
45
46                    $attributes->addClassName("hvr-$hover");
47
48                } else {
49
50                    /**
51                     * The combo hover effect
52                     */
53                    if (in_array($hover, ["float", "grow"])) {
54                        $hover = "combo-" . $hover;
55                    }
56
57                    /**
58                     * Shadow translation between animation name
59                     * and class
60                     */
61                    switch ($hover) {
62                        case "shadow":
63                            $hover = Shadow::getDefaultClass();
64                            break;
65                        case "shadow-md":
66                            $hover = Shadow::MEDIUM_ELEVATION_CLASS;
67                            break;
68                        case "shadow-lg":
69                            $hover = "shadow";
70                            break;
71                        case "shadow-xl":
72                            $hover = "shadow-lg";
73                            break;
74                    }
75
76                    /**
77                     * Add it to the list of class
78                     */
79                    $comboDataHoverClasses .= " " . $hover;
80
81                }
82
83            }
84            if (!empty($comboDataHoverClasses)) {
85
86                // Grow, float and easing are in the css
87                $snippetManager
88                    ->attachCssInternalStyleSheetForSlot(self::ON_HOVER_SNIPPET_ID)
89                    ->setCritical(false);
90
91                // Smooth Transition in and out of hover
92                $attributes->addClassName(self::COMBO_HOVER_EASING_CLASS);
93
94                $attributes->addOutputAttributeValue("data-hover-class", trim($comboDataHoverClasses));
95
96                // The javascript that manage the hover effect by adding the class in the data-hover class
97                $snippetManager->attachInternalJavascriptForSlot(self::ON_HOVER_SNIPPET_ID);
98
99            }
100
101        }
102
103    }
104}
105