react-modal icon indicating copy to clipboard operation
react-modal copied to clipboard

Is it possible to create css3 animation using this library ?.

Open kopax opened this issue 9 years ago • 15 comments

Summary:

I really like theses modal for their Effects: http://xue2han.github.io/react-dynamic-modal/

An effect look like this :

export const ScaleUp = {
      transition : {
         property : 'all',
         duration : 300,
         timingfunction : 'linear'
      },
      begin : {
	      'transform': 'scale(0.7)',
	      'opacity': 0
      },
      end : {
      	'transform': 'scale(1)',
      	'opacity': 1
      }
}

It provides animation, unfortunately, the way it is made doesn't work well with styled-components context.

Is it possible using react-modal to have that kind of result?

kopax avatar Mar 01 '17 06:03 kopax

@kopax [edited] sorry, i didn't pay attention to the styled-components part :). the way it's done in react-modal is by controlling the animation with ReactModal_after-open and ReactModal_before-close. To get the before close correctly, the duration must match with the closeTimeoutMS.

diasbruno avatar Mar 02 '17 03:03 diasbruno

Thanks for the short explanation, it makes sens.

Regarding the styled-components part :

React doesn't allow you to write css in js while styled-components does. You can pass a theme to the context of all your components if you use as a wrapper a <ThemeProvider theme={theme}>

This is how my Virtual DOM looks like :

const render = (messages) => {
  ReactDOM.render(
    <Provider store={store}>
      <LanguageProvider messages={messages}>
        <ThemeProvider theme={theme}>
          <Router
            history={history}
            routes={rootRoute}
            onUpdate={hashLinkScroll}
            render={
              applyRouterMiddleware(useScroll())
            }
          />
        </ThemeProvider>
      </LanguageProvider>
    </Provider>,
    document.getElementById('app')
  );
};

Problem I have with react-dynamic-modal was that it inject it's modal as a brother of <Provider> and not inside <ThemeProvider> which provide the context so it will lost it's context and ability to get customized using the theme provided.

These are my two requirement :

  1. injected under <App> (which is the 1st child of <Router> in my virtual dom, and first component child of <body> in the dom)
  2. being able to apply a modal with and without css3 effect.

@diasbruno, would it be possible to see an example incl css3 effect (2) so I can see if (1) can do it?

kopax avatar Mar 02 '17 04:03 kopax

I am struggling in order to get the same animation as in Bootstrap.

I can't get any animation at all, does anyone has an example of react-modal that involve transitions ?

kopax avatar Mar 06 '17 07:03 kopax

Here is a fade in animation for the wrapper in scss:

.ReactModal__Overlay {
  transition: opacity 500ms ease-in-out;
  opacity: 0;

  &--after-open {
    opacity: 1;
  }

  &--before-close {
    opacity: 0;
  }
}

And here is a 'fade in up' animation for the modal itself.

.ReactModal__Content {
  transition: all 500ms ease-in-out;
  transform: translateY(30px);
  opacity: 0;

  &--after-open {
    opacity: 1;
    transform: translateY(0);
  }

  &--before-close {
    opacity: 0;
    transform: translateY(30px);
  }
}

nckblu avatar Mar 07 '17 18:03 nckblu

Thanks. That would have been helpful when I was still trying but I've now decided to use a different modal. I think this should be an example in the documentation for the others.

kopax avatar Mar 08 '17 05:03 kopax

Good point @kopax I'll make a PR at some point this week.

nckblu avatar Mar 08 '17 09:03 nckblu

It seems like ReactModal__Overlay--after-open class is added directly on opening. Which makes animation impossible (the styles are added straight away as the element is appended to the DOM already with the class on). Is it only me having this issue?

Grsmto avatar Apr 13 '17 16:04 Grsmto

I could get the opacity animation working with styled-components by injecting the style globally and setting the closeTimeoutMS property on the Modal.

However, the scale animation won't work, because I'm already overriding the content with custom styles including transform: 'translate(-50%, -50%)', to centre it. 🤔

kadikraman avatar May 14 '17 15:05 kadikraman

@Grsmto have you tried my example above? The issue you describe can be avoided by using a transition on the class if I understand correctly. If you post your code we can have a look.

nckblu avatar May 15 '17 20:05 nckblu

@kadikraman, you can apply multiple transforms to include the translation and scale: http://stackoverflow.com/questions/10765755/how-to-apply-multiple-transforms-in-css

nckblu avatar May 15 '17 20:05 nckblu

@nckblu True, but because of the transform: 'translate(-50%, -50%)', instead of looking like the modal is "scaling from below", it ends up looking like it's "scaling from the right" if that makes sense

kadikraman avatar May 15 '17 20:05 kadikraman

@kadikraman ahh I see, that's annoying, perhaps you could center using absolute positioning way instead?

nckblu avatar May 15 '17 20:05 nckblu

@Grsmto I also had this issue... Fixed by setting the transition to 'all' like so: transition: all 1000ms ease-in-out

Not sure why that worked, but it did...

nlieb avatar Jun 23 '17 20:06 nlieb

@Grsmto and @nckblu I also gave it a try, this worked for me:

.ReactModal__Overlay {
  background-color: rebeccapurple;
  opacity: 0;
}

.ReactModal__Overlay--after-open {
  opacity: 1;
  transition: opacity 200ms ease-in-out;
}

.ReactModal__Overlay--before-close {
  opacity: 0;
}

Notice that the difference from @nckblu code is that the transition property is set on the --after-open class. Also closeTimeoutMS should be configured to match the transitions ms as mentioned above.

sobelmanus avatar Aug 16 '17 12:08 sobelmanus

Would there be any interesting building this into the API? A new prop named easeTransitionTimeMs (or something) which sets this specific CSS?

I think having a ease transition is common enough to have it be a part of the API. Otherwise, having to override the classes for every modal where we want this transition is kind of a pain... although most often you'd have the override only once and apply it to all modals in the application.

I hacked together a quick branch on my fork to test it out. Seemed easy enough: https://github.com/jseminck/react-modal/commit/b929882ca9d0292f075d24e985048da5b3ca76d2

If there's interest in it I can make a PR out of it (it would still need docs, tests, etc...)

jseminck avatar Aug 24 '17 08:08 jseminck