|
| 1 | +@namespace MudExtensions |
| 2 | +@using System.Text |
| 3 | +@using MudExtensions.Enums |
| 4 | +@using MudExtensions.Utilities |
| 5 | +@inherits MudComponentBase |
| 6 | + |
| 7 | +@code{ |
| 8 | + Guid _guid = new Guid(); |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Supports id and class selection. Use "#idName" for id selectors (ex. <Component id="idName" />) or ".idName" for class selectors (ex. <Component Class="idName" />). Leave it null or empty to effect all scrollbars. |
| 12 | + /// </summary> |
| 13 | + [Parameter] |
| 14 | + [Category(CategoryTypes.Item.Behavior)] |
| 15 | + public string Selector { get; set; } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The predefined animation type that runs. |
| 19 | + /// </summary> |
| 20 | + [Parameter] |
| 21 | + [Category(CategoryTypes.Item.Appearance)] |
| 22 | + public AnimationType AnimationType { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The primary value depends on animation type. |
| 26 | + /// </summary> |
| 27 | + [Parameter] |
| 28 | + [Category(CategoryTypes.Item.Appearance)] |
| 29 | + public double Value { get; set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The secondary value that sets the start value of animation. |
| 33 | + /// </summary> |
| 34 | + [Parameter] |
| 35 | + [Category(CategoryTypes.Item.Appearance)] |
| 36 | + public double? ValueSecondary { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Sets how long the animation will last. |
| 40 | + /// </summary> |
| 41 | + [Parameter] |
| 42 | + [Category(CategoryTypes.Item.Appearance)] |
| 43 | + public double Duration { get; set; } = 1; |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// The time before animation starts. |
| 47 | + /// </summary> |
| 48 | + [Parameter] |
| 49 | + [Category(CategoryTypes.Item.Appearance)] |
| 50 | + public double Delay { get; set; } = 0; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// If true, amination runs when hover. |
| 54 | + /// </summary> |
| 55 | + [Parameter] |
| 56 | + [Category(CategoryTypes.Item.Appearance)] |
| 57 | + public bool Hover { get; set; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// If true, amination runs contuniously. |
| 61 | + /// </summary> |
| 62 | + [Parameter] |
| 63 | + [Category(CategoryTypes.Item.Appearance)] |
| 64 | + public bool Infinite { get; set; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// If true, animation pauses. Set if false again to continue where it pauses. |
| 68 | + /// </summary> |
| 69 | + [Parameter] |
| 70 | + [Category(CategoryTypes.Item.Appearance)] |
| 71 | + public bool Paused { get; set; } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// How many times the animation repeats in one run. If the value is 0, the animation doesn't run. |
| 75 | + /// </summary> |
| 76 | + [Parameter] |
| 77 | + [Category(CategoryTypes.Item.Appearance)] |
| 78 | + public int IterationCount { get; set; } = 1; |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// The speed curve of the animation. |
| 82 | + /// </summary> |
| 83 | + [Parameter] |
| 84 | + [Category(CategoryTypes.Item.Appearance)] |
| 85 | + public AnimationTiming AnimationTiming { get; set; } = AnimationTiming.EaseInOut; |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// The running line of the animation. |
| 89 | + /// </summary> |
| 90 | + [Parameter] |
| 91 | + [Category(CategoryTypes.Item.Appearance)] |
| 92 | + public AnimationDirection AnimationDirection { get; set; } = AnimationDirection.Normal; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Determines the styles which the element has. Forwards for last keyframe, backwards for first keyframe. |
| 96 | + /// </summary> |
| 97 | + [Parameter] |
| 98 | + [Category(CategoryTypes.Item.Appearance)] |
| 99 | + public AnimationFillMode AnimationFillMode { get; set; } = AnimationFillMode.None; |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Animation keyframe with plain string. For advanced users. |
| 103 | + /// </summary> |
| 104 | + [Parameter] |
| 105 | + [Category(CategoryTypes.Item.Appearance)] |
| 106 | + public string KeyframeAdvanced { get; set; } |
| 107 | + |
| 108 | + |
| 109 | + bool _show = true; |
| 110 | + public async Task Refresh() |
| 111 | + { |
| 112 | + _show = false; |
| 113 | + await Task.Delay(1); |
| 114 | + await InvokeAsync(StateHasChanged); |
| 115 | + _show = true; |
| 116 | + await Task.Delay(1); |
| 117 | + await InvokeAsync(StateHasChanged); |
| 118 | + } |
| 119 | + |
| 120 | + protected string GetKeyframeCode() |
| 121 | + { |
| 122 | + if (!string.IsNullOrEmpty(KeyframeAdvanced)) |
| 123 | + { |
| 124 | + return KeyframeAdvanced; |
| 125 | + } |
| 126 | + |
| 127 | + List<string> valueList = new List<string>() { ValueSecondary?.ToInvariantString() ?? "0", Value.ToInvariantString() }; |
| 128 | + List<Tuple<string, string>> valueListTuple = new List<Tuple<string, string>>() { new Tuple<string, string>(ValueSecondary?.ToInvariantString() ?? "0", "0"), new Tuple<string, string>(Value.ToInvariantString(), "0.65") }; |
| 129 | + |
| 130 | + if (AnimationType == AnimationType.Rotate) |
| 131 | + { |
| 132 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.Rotate); |
| 133 | + } |
| 134 | + else if (AnimationType == AnimationType.Fade) |
| 135 | + { |
| 136 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.Fade); |
| 137 | + } |
| 138 | + else if (AnimationType == AnimationType.Flip) |
| 139 | + { |
| 140 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.Flip); |
| 141 | + } |
| 142 | + else if (AnimationType == AnimationType.Scale) |
| 143 | + { |
| 144 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.Scale); |
| 145 | + } |
| 146 | + else if (AnimationType == AnimationType.SlideX) |
| 147 | + { |
| 148 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.SlideX); |
| 149 | + } |
| 150 | + else if (AnimationType == AnimationType.SlideY) |
| 151 | + { |
| 152 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.SlideY); |
| 153 | + } |
| 154 | + else if (AnimationType == AnimationType.RotateDiagonal) |
| 155 | + { |
| 156 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.RotateDiagonal); |
| 157 | + } |
| 158 | + else if (AnimationType == AnimationType.Blur) |
| 159 | + { |
| 160 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.Blur); |
| 161 | + } |
| 162 | + else if (AnimationType == AnimationType.Shadow) |
| 163 | + { |
| 164 | + return KeyframeBuilder.Build(2, valueListTuple, KeyframePreset.Shadow); |
| 165 | + } |
| 166 | + else if (AnimationType == AnimationType.ShadowInset) |
| 167 | + { |
| 168 | + return KeyframeBuilder.Build(2, valueListTuple, KeyframePreset.ShadowInset); |
| 169 | + } |
| 170 | + else if (AnimationType == AnimationType.TextShadow) |
| 171 | + { |
| 172 | + return KeyframeBuilder.Build(2, valueListTuple, KeyframePreset.TextShadow); |
| 173 | + } |
| 174 | + else if (AnimationType == AnimationType.TextSpacing) |
| 175 | + { |
| 176 | + return KeyframeBuilder.Build(2, valueList, KeyframePreset.TextSpacing); |
| 177 | + } |
| 178 | + |
| 179 | + return $"0% {{ -webkit-transform: rotate({ValueSecondary ?? 0}); transform: rotate({ValueSecondary ?? 0});}} 100% {{ -webkit-transform: rotate({Value}deg); transform: rotate({Value}deg);}}"; |
| 180 | + } |
| 181 | + |
| 182 | + public string AnimationName => $"mud-ani{Selector?.Remove(0, 1)}-{_guid.ToString()}"; |
| 183 | + |
| 184 | + public string KeyframeCode => GetKeyframeCode(); |
| 185 | +} |
| 186 | + |
| 187 | +<style> |
| 188 | + @(Selector)@(Hover ? ":hover" : null) { |
| 189 | + @if (_show == true) |
| 190 | + { |
| 191 | + @($"animation: {AnimationName} {Duration.ToInvariantString()}s {AnimationTiming.ToDescriptionString()} {(Delay != 0 ? Delay.ToInvariantString() + "s" : null)} {(Infinite ? "infinite" : IterationCount.ToString())} {AnimationDirection.ToDescriptionString()} {(Paused ? "paused" : null)} {AnimationFillMode.ToDescriptionString()};"); |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + @("animation: none;"); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @@keyframes @(AnimationName) { |
| 200 | + @GetKeyframeCode() |
| 201 | + } |
| 202 | +</style> |
0 commit comments