Skip to content

Handle Closed <dialog> and <popover> Animations #536

Open
@mobalti

Description

Keyframe animations are a great way to add smooth transitions between the closed and shown states of <dialog> and <popover>. This approach has several advantages:

  • No need to provide a @starting-style.
  • No need to explicitly enable discrete animations.
  • Unlike transition, animation is supported by all major browsers for shown states; only the closed state animations are currently limited to Chromium-based browsers.

For the shown state, no additional work is needed as animations work out of the box. However, for the closed state, adding the display property to the out animation is required, as shown in this example:

.animate-slide-up-down {
    /* motionOK */
    @media (prefers-reduced-motion: no-preference) {
        animation: slide-out-down 0.5s var(--ease-3);

        &:popover-open {
            /* Built-in animation from Open Props */
            animation: var(--animation-slide-in-up);
        }
    }
}

@keyframes slide-out-down {
    from {
        display: block;
    }
    to {
        transform: translateY(100%);
    }
}

The same applies to fade animations:

.animate-fade-in-out {
    opacity: 0;

    /* motionOK */
    @media (prefers-reduced-motion: no-preference) {
        animation: fade-out 0.5s var(--ease-3) forwards;

        &:popover-open {
            /* Built-in animation from Open Props */
            animation: var(--animation-fade-in) forwards;
        }
    }
}

@keyframes fade-out {
    from {
        display: block;
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

See this example on CodePen and click one of the cards to view the animations live:
https://codepen.io/mobalti/pen/EaYVJgr


Summary

To handle animations when dialogs and popovers are closed, we can either:

  1. Update the current "out" animation props by adding the display property to the keyframes (if there are no side effects).
  2. Create new "out" animation props specifically for handling the closed state.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions