In this article we describe how to build an elegantly designed and animated fluent ToggleButton-Popup combination for use in UWP apps as an alternative for the less fluent Button Flyout. The code is not mine, I merely reused code and components from the Continuity framework by Justin Liu. This frameworks contains a lot of helpers for implementing Fluent Design in your apps without needing Windows 10 Fall Creators Update.

The ToggleButton-Popup combo is an excellent widget to display extra information such as a help text.

Here’s how all of this may look like (styling is up to you of course). It starts with a stand-alone round ‘more’ button, with hover animations. When pressed, the button rotates toward the opening flyout, while changing its shape to look like a ‘close’ button that becomes connected to the content that was just revealed. In the mean time, the flyout itself opens with a scale animation as if it grew out of the button:

Closing the button and dismissing the flyout come with reverse animations.

I’ve been looking for ways to animate opening and closing Flyout or ContentDialog instances. I went through many of the XAML animations techniques, but could not find a way. Flyout and ContentDialog don’t come with Visual States, so Visual Transitions are not an option. Although its name seems promising, PopInThemeAnimation is not applicable. And if it were, it doesn’t come with a scale animation (only opacity and translation).

I almost gave up, and tried to to accept that flyouts would always open and close like this:

Frankly, I don’t like the animations. But what’s worse: you can not even see which of the buttons was actually pressed…

Then I came across this sample Kliva design with exactly the user experience I was looking for:

I decided to copy the relevant code into my own solution under Features/Continuity to get some Fluent-As-A-Service:

The Kliva demo is not using a Button with its Flyout, but a ToggleButton with ‘just’ a Grid. My only mission was to replace the Grid with a Popup. After a few iterations, I came up with the following setup, which even allows the Popup to have IsLightDismissEnabled on.:

A toggle button with storyboarded animations is placed in a container

A Popup is placed next to it with its IsOpen bound to IsChecked of the button, and its HorizontalOffset bound to the ActualWidth of that same button.

The content of the Popup (not the Popup itself) is given implicit Show and Hide Composition API animations.

Here’s the corresponding XAML definition:

<!-- Toggle Button --> <continuity:CircularToggleButton x:Name="TheToggle" CheckedBackground="{StaticResource HighlightBrush}" CheckedCornerRadius="6 0 0 6" FontFamily="Segoe UI"> <continuity:CircularToggleButton.CheckedContent> <ContentControl Margin="3" Style="{StaticResource IconCloseStyle}" /> </continuity:CircularToggleButton.CheckedContent> <ContentControl Style="{StaticResource IconMoreStyle}" /> </continuity:CircularToggleButton> <!-- 'Flyout' --> <Popup x:Name="ThePopup" IsOpen="{Binding IsChecked, ElementName=TheToggle, Mode=TwoWay}" IsLightDismissEnabled="False" HorizontalOffset="{Binding ActualWidth, ElementName=TheToggle}" VerticalOffset="-20"> <Grid x:Name="TheGrid" Visibility="{Binding IsOpen, ElementName=ThePopup}"> <!-- Content --> </Grid> </Popup>

That’s it! Let’s dive into some details.

An animated circular toggle button

The CircularToggleButton adds a few dependency properties to ToggleButton, like content, background color and corner radius for the different states:

public sealed class CircularToggleButton : ToggleButton { public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.Register( "CheckedContent", typeof(object), typeof(CircularToggleButton), new PropertyMetadata(null)); public object CheckedContent { get => GetValue(CheckedContentProperty); set => SetValue(CheckedContentProperty, value); } // .. }

It also comes with a circular custom style and some nice visual effects and animations.

Here’s how the PointerOver VisualState pumps up the size of the content using a RenderTransform with a CompositeTransform on ScaleX and ScaleY:

<VisualState x:Name="PointerOver"> <VisualState.Setters> <Setter Target="CheckedContentPresenter.(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Value="1.1" /> <Setter Target="CheckedContentPresenter.(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Value="1.1" /> <Setter Target="UncheckedContentPresenter.(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Value="1.1" /> <Setter Target="UncheckedContentPresenter.(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Value="1.1" /> <Setter Target="BackgroundVisual.Opacity" Value="0.9" /> </VisualState.Setters> </VisualState>

Here’s the VisualTransition with the StoryBoard on CornerRadius that turns the round button into its ‘connected’ shape when checked:

<VisualTransition GeneratedDuration="0:0:0.25" To="Checked"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.CornerRadius)" Storyboard.TargetName="BackgroundVisual"> <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding CheckedCornerRadius, RelativeSource={RelativeSource TemplatedParent}}" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualTransition>

If you want to know more about storyboarded animations, this circular toggle button style definition is a nice tutorial. Storyboarded animations are a way to change a dependency property over time. When there’s a visual impact of this, the animation may or may not require the UI thread. While the SDK’s evolve, more and more of these animations are internally rewritten to run on the composition thread. So there’s no need for you give up readable declarative Storyboards in XAML in favor of dark imperative code against the Composition API. But for the second part of this article, we’ll use the latter…

An animated Popup

The Popup control is a XAML container that allows you to place content on top of other content. You can not animate the Popup itself but fortunately you can animate its content. As promised we’ll use Composition animations for this. These are 60-frames-per-second animations that run independent of the UI thread. They’re a bit harder to write, but there are a lot of helpers available, like these from UWP Community Toolkit.

Here’s the Continuity helper that starts it all:

TheGrid.EnableFluidVisibilityAnimation( centerPoint: new Vector3(0.0f, 40.0f, 0.0f), showFromScale: 0.2f, hideToScale: 0.2f, showDuration: 400, hideDuration: 400);

Here’s part of its implementation.

A time based Composition API animation of the type Vector2KeyFrameAnimation is created with the Scale of the Visual as Target:

if (!showFromScale.Equals(1.0f)) { showeScaleAnimation = compositor.CreateVector2KeyFrameAnimation(); showeScaleAnimation.InsertKeyFrame(0.0f, new Vector2(showFromScale)); showeScaleAnimation.InsertKeyFrame(1.0f, Vector2.One); showeScaleAnimation.Duration = TimeSpan.FromMilliseconds(showDuration); showeScaleAnimation.DelayTime = TimeSpan.FromMilliseconds(showDelay); showeScaleAnimation.Target = "Scale.XY"; }

The different animations (scale, position, opacity, …) are placed together in an AnimationGroup (one for hide and one for show):

var showAnimationGroup = compositor.CreateAnimationGroup(); // ... if (showeScaleAnimation != null) { showAnimationGroup.Add(showeScaleAnimation); }

These animation groups are then implicitly hooked to the Popup’s content, using SetImplicitShowAnimation and SetImplicitHideAnimation:

ElementCompositionPreview.SetImplicitShowAnimation(element, showAnimationGroup);

Under the hood, a lot of different animation techniques were used to create this user experience, but I love the result! And while it’s probably possible to forge this into a single custom control, I’m not sure if it would add much value….

The code

The ‘fluent button flyout’ sample lives here on Github, the inspiring Continuity by Justin Liu is right here.

Enjoy!