Skip to content

Animation Keyframes API Design [feedback welcome!] #431

Open
@rtfeldman

Description

@rtfeldman

Prior discussions: #430 #431 #227 #275

I'd like to add animations to elm-css, which requires some API design work. CSS Tricks has the best intro I've read on the subject of CSS animations, and it also has links to MDN at the end.

Here's a draft of an API to address how keyframes would work. (Let's assume the other properties exist; those don't have any more API challenges than the typical elm-css API, whereas keyframes is trickier.)

keyframes : List Style -> List ( Float, List Style ) -> List Style -> Keyframes

animationName : Keyframes -> Style

The basic ideas are:

  1. You use Css.keyframes to define a Keyframes value. The 0% and 100% (aka from and to) styles are mandatory, but you can optionally specify other percentages as ( Float, List Style ) tuples in between.
  2. You can pass this Keyframes value to animation-related properties like animationName and the shorthand animation.
  3. The animation name is automatically generated as a hash of the contents of the Keyframes value, just like how classnames are automatically generated by the css attribute today. So you'd never need to manually synchronize animation names.

This doesn't offer a way to manually specify an animation name string if you really want to. That said, it would be possible to add a Css.Global.keyframes : String -> Keyframes -> Snippet to support that. (Note that without this, it would be impossible to specify two different animations which have the same transitions and styles, but different names. Is that a problem? I can't think of why it would be, but maybe there's a scenario I'm unaware of.)

Concerns with this API:

  1. Because keyframes receives a List Style, you can use things like hover and important with it, even though those aren't supported in the context of keyframes. It would be nice if using hover or important in these Style values didn't compile, but I can't think of a way to do that without introducing a phantom type variable to Style. That doesn't seem worth it to me. Can anyone think of alternate ways to enforce that?
  2. There's no way to avoid specifying the 0% and 100% transition steps. That seems like a good thing by default, since if you're doing something unusual where you don't want transition steps, you can always set them to be the same thing. However, this is CSS; there might conceivably be situations where specifying the same 0% and 100% is not equivalent to specifying only one of them - which the spec theoretically supports, but which I don't know why anyone would want.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions