闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

  1. // Like react-motion's Motion, but reduces all animations to cross-fades
  2. // for the benefit of users with motion sickness.
  3. import React from 'react';
  4. import Motion from 'react-motion/lib/Motion';
  5. import PropTypes from 'prop-types';
  6. const stylesToKeep = ['opacity', 'backgroundOpacity'];
  7. const extractValue = (value) => {
  8. // This is either an object with a "val" property or it's a number
  9. return (typeof value === 'object' && value && 'val' in value) ? value.val : value;
  10. };
  11. class ReducedMotion extends React.Component {
  12. static propTypes = {
  13. defaultStyle: PropTypes.object,
  14. style: PropTypes.object,
  15. children: PropTypes.func,
  16. }
  17. render() {
  18. const { style, defaultStyle, children } = this.props;
  19. Object.keys(style).forEach(key => {
  20. if (stylesToKeep.includes(key)) {
  21. return;
  22. }
  23. // If it's setting an x or height or scale or some other value, we need
  24. // to preserve the end-state value without actually animating it
  25. style[key] = defaultStyle[key] = extractValue(style[key]);
  26. });
  27. return (
  28. <Motion style={style} defaultStyle={defaultStyle}>
  29. {children}
  30. </Motion>
  31. );
  32. }
  33. }
  34. export default ReducedMotion;