radialGradient.js.flow 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // @flow
  2. import constructGradientValue from '../internalHelpers/_constructGradientValue'
  3. import PolishedError from '../internalHelpers/_errors'
  4. import type { RadialGradientConfiguration } from '../types/radialGradientConfiguration'
  5. import type { Styles } from '../types/style'
  6. /**
  7. * CSS for declaring a radial gradient, including a fallback background-color. The fallback is either the first color-stop or an explicitly passed fallback color.
  8. *
  9. * @example
  10. * // Styles as object usage
  11. * const styles = {
  12. * ...radialGradient({
  13. * colorStops: ['#00FFFF 0%', 'rgba(0, 0, 255, 0) 50%', '#0000FF 95%'],
  14. * extent: 'farthest-corner at 45px 45px',
  15. * position: 'center',
  16. * shape: 'ellipse',
  17. * })
  18. * }
  19. *
  20. * // styled-components usage
  21. * const div = styled.div`
  22. * ${radialGradient({
  23. * colorStops: ['#00FFFF 0%', 'rgba(0, 0, 255, 0) 50%', '#0000FF 95%'],
  24. * extent: 'farthest-corner at 45px 45px',
  25. * position: 'center',
  26. * shape: 'ellipse',
  27. * })}
  28. *`
  29. *
  30. * // CSS as JS Output
  31. *
  32. * div: {
  33. * 'backgroundColor': '#00FFFF',
  34. * 'backgroundImage': 'radial-gradient(center ellipse farthest-corner at 45px 45px, #00FFFF 0%, rgba(0, 0, 255, 0) 50%, #0000FF 95%)',
  35. * }
  36. */
  37. export default function radialGradient({
  38. colorStops,
  39. extent = '',
  40. fallback,
  41. position = '',
  42. shape = '',
  43. }: RadialGradientConfiguration): Styles {
  44. if (!colorStops || colorStops.length < 2) {
  45. throw new PolishedError(57)
  46. }
  47. return {
  48. backgroundColor: fallback || colorStops[0].split(' ')[0],
  49. backgroundImage: constructGradientValue`radial-gradient(${position}${shape}${extent}${colorStops.join(
  50. ', ',
  51. )})`,
  52. }
  53. }