Skip to content

Commit c4645ce

Browse files
committed
Updated CollapsePanel animation when heigh is Auto CSS cannot calculate transition speeed. So need to animate max-height but it requires grater value then "auto" value...
https://css-tricks.com/using-css-transitions-auto-dimensions/
1 parent c90bad7 commit c4645ce

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

src/Majorsoft.Blazor.Components.Collapse/CollapsePanel.razor

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
</div>
1313
<div class="collapseContent @(Animate ? "animate" : "")"
14-
style="opacity: @(Collapsed ? "0" : "1"); overflow: @(ShowContentOverflow ? "auto" : "hidden"); height: @GetContentHeight()">
14+
style="opacity: @(Collapsed ? "0" : "1"); overflow: @(ShowContentOverflow ? "auto" : "hidden"); max-height: @GetContentHeight()">
1515
@Content
1616
</div>
1717
</div>
@@ -83,10 +83,16 @@
8383
}
8484

8585
/// <summary>
86-
/// Sets the height of Content panel in `px`. 0 is auto.
86+
/// Sets the height (in reality sets max-height because of CSS transition issues) of Content panel in `px`. 0 is auto.
8787
/// </summary>
8888
[Parameter] public int ContentHeight { get; set; } = 200;
8989

90+
/// <summary>
91+
/// Sets the max-height if Content panel <see href="ContentHeight"> is set to 0 (auto).
92+
/// https://css-tricks.com/using-css-transitions-auto-dimensions/
93+
/// </summary>
94+
[Parameter] public int MaxAllowedContentHeight { get; set; } = 200;
95+
9096
/// <summary>
9197
/// Determines whether all the rendered HTML elements should be disabled or not.
9298
/// </summary>
@@ -114,7 +120,8 @@
114120

115121
/// <summary>
116122
/// Determines to apply CSS animation and transition on Collapse state changes or not.
117-
/// Note: in case of `auto` height some animation won't work.
123+
/// Note: in case of Content panel <see href="ContentHeight"> is set to 0 (auto), then use <see href="MaxAllowedContentHeight"> to set max-height CSS property which will be animated.
124+
/// Also important based on max-height value transition speed for expand/collapse might differ!
118125
/// </summary>
119126
[Parameter] public bool Animate { get; set; } = true;
120127

@@ -158,7 +165,7 @@
158165
return "0px;";
159166
}
160167

161-
return ContentHeight <= 0 ? "auto;" : $"{ContentHeight}px;";
168+
return ContentHeight <= 0 ? $"{MaxAllowedContentHeight}px;" : $"{ContentHeight}px;";
162169
}
163170
private string GetStyle()
164171
{

src/Majorsoft.Blazor.Components.Collapse/CollapsePanel.razor.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
}
2323

2424
.collapseContent {
25+
height: auto;
2526
outline: none;
2627
display: block;
2728
}
2829
.collapseContent.animate {
29-
-webkit-transition: opacity 1s, height 0.4s; /* For Safari 3.1 to 6.0 */
30-
transition: opacity 1s, height 0.4s;
30+
-webkit-transition: opacity 1s, max-height 0.8s; /* For Safari 3.1 to 6.0 */
31+
transition: opacity 1s, max-height 0.8s;
3132
}

src/Majorsoft.Blazor.Components.Collapse/CollapsePanel.razor.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Majorsoft.Blazor.Components.Collapse/Majorsoft.Blazor.Components.Collapse.xml

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Majorsoft.Blazor.Components.TestApps.Common/Components/Collapse.razor

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@
3636
</div>
3737
<div class="row pb-2">
3838
<div class="col-12 col-lg-8 col-xl-5">
39-
CollapsePanel content Height (0 is auto): @(_height)px
39+
Collapse Panel Content Height (0 is auto): @(_height)px
4040
<input type="range" class="w-100" min="0" max="400" @bind="_height" @oninput="(e => _height = int.Parse(e.Value?.ToString()))" />
4141
</div>
4242
</div>
43+
<div class="row pb-2">
44+
<div class="col-12 col-lg-8 col-xl-5">
45+
Collapse Panel Max Content Height (only when Content Height is 0 "auto" and collapse Animated Note: animation speed depends on max-height): @(_maxHeight)px
46+
<input type="range" class="w-100" min="0" max="800" disabled="@(_height != 0 || !_isAnimated)" @bind="_maxHeight" @oninput="(e => _maxHeight = int.Parse(e.Value?.ToString()))" />
47+
</div>
48+
</div>
4349

4450
<div class="row pb-2">
4551
<div class="col-12 col-lg-8 col-xl-5">
46-
CollapsePanel Disabled: <input class="mr-3" type="checkbox" @bind="_isCollapseDisabled" />
52+
Collapse Panel Disabled: <input class="mr-3" type="checkbox" @bind="_isCollapseDisabled" />
4753
Collapsed: <input class="mr-3" type="checkbox" @bind="_isCollapsed" />
4854
<br />
4955
Animate Collapse changes: <input class="mr-3" type="checkbox" @bind="_isAnimated" />
@@ -60,6 +66,7 @@
6066
Disabled="@_isCollapseDisabled"
6167
Collapsed="@_isCollapsed"
6268
ContentHeight="@_height"
69+
MaxAllowedContentHeight="@_maxHeight"
6370
Animate="@_isAnimated"
6471
ShowContentOverflow="@_showOverflow"
6572
OnCollapseChanged="OnCollapsed">
@@ -100,8 +107,11 @@
100107
<h3>Multiple CollapsePanel items</h3>
101108
<p>
102109
Example usage of multiple independent <code>CollapsePanel</code>s with default settings.
103-
<br />
104-
<strong>Note</strong>: content <code>height: auto</code> prevents some CSS animation to work.
110+
<br />
111+
<span class="pt-1 text-muted">
112+
<strong class="text-danger">Note</strong>: in case of Content panel <code>ContentHeight</code> is set to 0 (auto), then use <code>MaxAllowedContentHeight</code> to set max-height CSS property which will be animated.
113+
Also important <strong>based on max-height value transition speed for expand/collapse might differ!</strong>
114+
</span>
105115
</p>
106116

107117
<CollapsePanel ContentHeight="0" style="margin-bottom: 10px;">
@@ -335,6 +345,7 @@
335345
private bool _isCollapsed = false;
336346
private bool _showOverflow = false;
337347
private int _height = 200;
348+
private int _maxHeight = 200;
338349

339350
private ElementReference _log;
340351
private string _collapseLog;

0 commit comments

Comments
 (0)