animation.js.flow 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // @flow
  2. import PolishedError from '../internalHelpers/_errors'
  3. import type { Styles } from '../types/style'
  4. /**
  5. * Shorthand for easily setting the animation property. Allows either multiple arrays with animations
  6. * or a single animation spread over the arguments.
  7. * @example
  8. * // Styles as object usage
  9. * const styles = {
  10. * ...animation(['rotate', '1s', 'ease-in-out'], ['colorchange', '2s'])
  11. * }
  12. *
  13. * // styled-components usage
  14. * const div = styled.div`
  15. * ${animation(['rotate', '1s', 'ease-in-out'], ['colorchange', '2s'])}
  16. * `
  17. *
  18. * // CSS as JS Output
  19. *
  20. * div {
  21. * 'animation': 'rotate 1s ease-in-out, colorchange 2s'
  22. * }
  23. * @example
  24. * // Styles as object usage
  25. * const styles = {
  26. * ...animation('rotate', '1s', 'ease-in-out')
  27. * }
  28. *
  29. * // styled-components usage
  30. * const div = styled.div`
  31. * ${animation('rotate', '1s', 'ease-in-out')}
  32. * `
  33. *
  34. * // CSS as JS Output
  35. *
  36. * div {
  37. * 'animation': 'rotate 1s ease-in-out'
  38. * }
  39. */
  40. export default function animation(
  41. ...args: Array<Array<string | number> | string | number>
  42. ): Styles {
  43. // Allow single or multiple animations passed
  44. const multiMode = Array.isArray(args[0])
  45. if (!multiMode && args.length > 8) {
  46. throw new PolishedError(64)
  47. }
  48. const code = args
  49. .map(arg => {
  50. if ((multiMode && !Array.isArray(arg)) || (!multiMode && Array.isArray(arg))) {
  51. throw new PolishedError(65)
  52. }
  53. if (Array.isArray(arg) && arg.length > 8) {
  54. throw new PolishedError(66)
  55. }
  56. return Array.isArray(arg) ? arg.join(' ') : arg
  57. })
  58. .join(', ')
  59. return {
  60. animation: code,
  61. }
  62. }