withAlphaVariable.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import withAlphaVariable from '../src/util/withAlphaVariable'
  2. test('it adds the right custom property', () => {
  3. expect(
  4. withAlphaVariable({ color: '#ff0000', property: 'color', variable: '--tw-text-opacity' })
  5. ).toEqual({
  6. '--tw-text-opacity': '1',
  7. color: 'rgb(255 0 0 / var(--tw-text-opacity))',
  8. })
  9. expect(
  10. withAlphaVariable({
  11. color: 'hsl(240 100% 50%)',
  12. property: 'color',
  13. variable: '--tw-text-opacity',
  14. })
  15. ).toEqual({
  16. '--tw-text-opacity': '1',
  17. color: 'hsl(240 100% 50% / var(--tw-text-opacity))',
  18. })
  19. })
  20. test('it ignores colors that cannot be parsed', () => {
  21. expect(
  22. withAlphaVariable({
  23. color: 'currentColor',
  24. property: 'background-color',
  25. variable: '--tw-bg-opacity',
  26. })
  27. ).toEqual({
  28. 'background-color': 'currentColor',
  29. })
  30. expect(
  31. withAlphaVariable({
  32. color: 'rgb(255, 0)',
  33. property: 'background-color',
  34. variable: '--tw-bg-opacity',
  35. })
  36. ).toEqual({
  37. 'background-color': 'rgb(255, 0)',
  38. })
  39. expect(
  40. withAlphaVariable({
  41. color: 'rgb(255)',
  42. property: 'background-color',
  43. variable: '--tw-bg-opacity',
  44. })
  45. ).toEqual({
  46. 'background-color': 'rgb(255)',
  47. })
  48. expect(
  49. withAlphaVariable({
  50. color: 'rgb(255, 0, 0, 255)',
  51. property: 'background-color',
  52. variable: '--tw-bg-opacity',
  53. })
  54. ).toEqual({
  55. 'background-color': 'rgb(255, 0, 0, 255)',
  56. })
  57. expect(
  58. withAlphaVariable({
  59. color: 'rgb(var(--color))',
  60. property: 'background-color',
  61. variable: '--tw-bg-opacity',
  62. })
  63. ).toEqual({
  64. 'background-color': 'rgb(var(--color))',
  65. })
  66. })
  67. test('it ignores colors that already have an alpha channel', () => {
  68. expect(
  69. withAlphaVariable({
  70. color: '#ff0000ff',
  71. property: 'background-color',
  72. variable: '--tw-bg-opacity',
  73. })
  74. ).toEqual({
  75. 'background-color': '#ff0000ff',
  76. })
  77. expect(
  78. withAlphaVariable({
  79. color: '#ff000080',
  80. property: 'background-color',
  81. variable: '--tw-bg-opacity',
  82. })
  83. ).toEqual({
  84. 'background-color': '#ff000080',
  85. })
  86. expect(
  87. withAlphaVariable({
  88. color: '#f00a',
  89. property: 'background-color',
  90. variable: '--tw-bg-opacity',
  91. })
  92. ).toEqual({
  93. 'background-color': '#f00a',
  94. })
  95. expect(
  96. withAlphaVariable({
  97. color: '#f00f',
  98. property: 'background-color',
  99. variable: '--tw-bg-opacity',
  100. })
  101. ).toEqual({
  102. 'background-color': '#f00f',
  103. })
  104. expect(
  105. withAlphaVariable({
  106. color: 'rgba(255, 255, 255, 1)',
  107. property: 'background-color',
  108. variable: '--tw-bg-opacity',
  109. })
  110. ).toEqual({
  111. 'background-color': 'rgba(255, 255, 255, 1)',
  112. })
  113. expect(
  114. withAlphaVariable({
  115. color: 'rgba(255, 255, 255, 0.5)',
  116. property: 'background-color',
  117. variable: '--tw-bg-opacity',
  118. })
  119. ).toEqual({
  120. 'background-color': 'rgba(255, 255, 255, 0.5)',
  121. })
  122. expect(
  123. withAlphaVariable({
  124. color: 'rgba(255 255 255 / 0.5)',
  125. property: 'background-color',
  126. variable: '--tw-bg-opacity',
  127. })
  128. ).toEqual({
  129. 'background-color': 'rgba(255 255 255 / 0.5)',
  130. })
  131. expect(
  132. withAlphaVariable({
  133. color: 'hsla(240, 100%, 50%, 1)',
  134. property: 'background-color',
  135. variable: '--tw-bg-opacity',
  136. })
  137. ).toEqual({
  138. 'background-color': 'hsla(240, 100%, 50%, 1)',
  139. })
  140. expect(
  141. withAlphaVariable({
  142. color: 'hsla(240, 100%, 50%, 0.5)',
  143. property: 'background-color',
  144. variable: '--tw-bg-opacity',
  145. })
  146. ).toEqual({
  147. 'background-color': 'hsla(240, 100%, 50%, 0.5)',
  148. })
  149. expect(
  150. withAlphaVariable({
  151. color: 'hsl(240 100% 50% / 0.5)',
  152. property: 'background-color',
  153. variable: '--tw-bg-opacity',
  154. })
  155. ).toEqual({
  156. 'background-color': 'hsl(240 100% 50% / 0.5)',
  157. })
  158. })
  159. test('it allows a closure to be passed', () => {
  160. expect(
  161. withAlphaVariable({
  162. color: ({ opacityVariable }) => `rgba(0, 0, 0, var(${opacityVariable}))`,
  163. property: 'background-color',
  164. variable: '--tw-bg-opacity',
  165. })
  166. ).toEqual({
  167. '--tw-bg-opacity': '1',
  168. 'background-color': 'rgba(0, 0, 0, var(--tw-bg-opacity))',
  169. })
  170. expect(
  171. withAlphaVariable({
  172. color: ({ opacityValue }) => `rgba(0, 0, 0, ${opacityValue})`,
  173. property: 'background-color',
  174. variable: '--tw-bg-opacity',
  175. })
  176. ).toEqual({
  177. '--tw-bg-opacity': '1',
  178. 'background-color': 'rgba(0, 0, 0, var(--tw-bg-opacity))',
  179. })
  180. })
  181. test('it transforms rgb and hsl to space-separated rgb and hsl', () => {
  182. expect(
  183. withAlphaVariable({
  184. color: 'rgb(50, 50, 50)',
  185. property: 'background-color',
  186. variable: '--tw-bg-opacity',
  187. })
  188. ).toEqual({
  189. '--tw-bg-opacity': '1',
  190. 'background-color': 'rgb(50 50 50 / var(--tw-bg-opacity))',
  191. })
  192. expect(
  193. withAlphaVariable({
  194. color: 'rgb(50 50 50)',
  195. property: 'background-color',
  196. variable: '--tw-bg-opacity',
  197. })
  198. ).toEqual({
  199. '--tw-bg-opacity': '1',
  200. 'background-color': 'rgb(50 50 50 / var(--tw-bg-opacity))',
  201. })
  202. expect(
  203. withAlphaVariable({
  204. color: 'hsl(50, 50%, 50%)',
  205. property: 'background-color',
  206. variable: '--tw-bg-opacity',
  207. })
  208. ).toEqual({
  209. '--tw-bg-opacity': '1',
  210. 'background-color': 'hsl(50 50% 50% / var(--tw-bg-opacity))',
  211. })
  212. expect(
  213. withAlphaVariable({
  214. color: 'hsl(50 50% 50%)',
  215. property: 'background-color',
  216. variable: '--tw-bg-opacity',
  217. })
  218. ).toEqual({
  219. '--tw-bg-opacity': '1',
  220. 'background-color': 'hsl(50 50% 50% / var(--tw-bg-opacity))',
  221. })
  222. })
  223. test('it transforms named colors to rgb', () => {
  224. expect(
  225. withAlphaVariable({
  226. color: 'red',
  227. property: 'background-color',
  228. variable: '--tw-bg-opacity',
  229. })
  230. ).toEqual({
  231. '--tw-bg-opacity': '1',
  232. 'background-color': 'rgb(255 0 0 / var(--tw-bg-opacity))',
  233. })
  234. })