evaluateTailwindFunctions.test.js 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. import postcss from 'postcss'
  2. import plugin from '../src/lib/evaluateTailwindFunctions'
  3. import tailwind from '../src/index'
  4. import { css } from './util/run'
  5. function run(input, opts = {}) {
  6. return postcss([plugin({ tailwindConfig: opts })]).process(input, { from: undefined })
  7. }
  8. function runFull(input, config) {
  9. return postcss([tailwind(config)]).process(input, { from: undefined })
  10. }
  11. test('it looks up values in the theme using dot notation', () => {
  12. let input = css`
  13. .banana {
  14. color: theme('colors.yellow');
  15. }
  16. `
  17. let output = css`
  18. .banana {
  19. color: #f7cc50;
  20. }
  21. `
  22. return run(input, {
  23. theme: {
  24. colors: {
  25. yellow: '#f7cc50',
  26. },
  27. },
  28. }).then((result) => {
  29. expect(result.css).toEqual(output)
  30. expect(result.warnings().length).toBe(0)
  31. })
  32. })
  33. test('it looks up values in the theme using bracket notation', () => {
  34. let input = css`
  35. .banana {
  36. color: theme('colors[yellow]');
  37. }
  38. `
  39. let output = css`
  40. .banana {
  41. color: #f7cc50;
  42. }
  43. `
  44. return run(input, {
  45. theme: {
  46. colors: {
  47. yellow: '#f7cc50',
  48. },
  49. },
  50. }).then((result) => {
  51. expect(result.css).toEqual(output)
  52. expect(result.warnings().length).toBe(0)
  53. })
  54. })
  55. test('it looks up values in the theme using consecutive bracket notation', () => {
  56. let input = css`
  57. .banana {
  58. color: theme('colors[yellow][100]');
  59. }
  60. `
  61. let output = css`
  62. .banana {
  63. color: #f7cc50;
  64. }
  65. `
  66. return run(input, {
  67. theme: {
  68. colors: {
  69. yellow: {
  70. 100: '#f7cc50',
  71. },
  72. },
  73. },
  74. }).then((result) => {
  75. expect(result.css).toEqual(output)
  76. expect(result.warnings().length).toBe(0)
  77. })
  78. })
  79. test('it looks up values in the theme using bracket notation that have dots in them', () => {
  80. let input = css`
  81. .banana {
  82. padding-top: theme('spacing[1.5]');
  83. }
  84. `
  85. let output = css`
  86. .banana {
  87. padding-top: 0.375rem;
  88. }
  89. `
  90. return run(input, {
  91. theme: {
  92. spacing: {
  93. '1.5': '0.375rem',
  94. },
  95. },
  96. }).then((result) => {
  97. expect(result.css).toEqual(output)
  98. expect(result.warnings().length).toBe(0)
  99. })
  100. })
  101. test('theme with mismatched brackets throws an error ', async () => {
  102. let config = {
  103. theme: {
  104. spacing: {
  105. '1.5': '0.375rem',
  106. },
  107. },
  108. }
  109. let input = (path) => css`
  110. .banana {
  111. padding-top: theme('${path}');
  112. }
  113. `
  114. await expect(run(input('spacing[1.5]]'), config)).rejects.toThrowError(
  115. `Path is invalid. Has unbalanced brackets: spacing[1.5]]`
  116. )
  117. await expect(run(input('spacing[[1.5]'), config)).rejects.toThrowError(
  118. `Path is invalid. Has unbalanced brackets: spacing[[1.5]`
  119. )
  120. await expect(run(input('spacing[a['), config)).rejects.toThrowError(
  121. `Path is invalid. Has unbalanced brackets: spacing[a[`
  122. )
  123. })
  124. test('color can be a function', () => {
  125. let input = css`
  126. .backgroundColor {
  127. color: theme('backgroundColor.fn');
  128. }
  129. .borderColor {
  130. color: theme('borderColor.fn');
  131. }
  132. .caretColor {
  133. color: theme('caretColor.fn');
  134. }
  135. .colors {
  136. color: theme('colors.fn');
  137. }
  138. .divideColor {
  139. color: theme('divideColor.fn');
  140. }
  141. .fill {
  142. color: theme('fill.fn');
  143. }
  144. .gradientColorStops {
  145. color: theme('gradientColorStops.fn');
  146. }
  147. .placeholderColor {
  148. color: theme('placeholderColor.fn');
  149. }
  150. .ringColor {
  151. color: theme('ringColor.fn');
  152. }
  153. .ringOffsetColor {
  154. color: theme('ringOffsetColor.fn');
  155. }
  156. .stroke {
  157. color: theme('stroke.fn');
  158. }
  159. .textColor {
  160. color: theme('textColor.fn');
  161. }
  162. `
  163. let output = css`
  164. .backgroundColor {
  165. color: #f00;
  166. }
  167. .borderColor {
  168. color: #f00;
  169. }
  170. .caretColor {
  171. color: #f00;
  172. }
  173. .colors {
  174. color: #f00;
  175. }
  176. .divideColor {
  177. color: #f00;
  178. }
  179. .fill {
  180. color: #f00;
  181. }
  182. .gradientColorStops {
  183. color: #f00;
  184. }
  185. .placeholderColor {
  186. color: #f00;
  187. }
  188. .ringColor {
  189. color: #f00;
  190. }
  191. .ringOffsetColor {
  192. color: #f00;
  193. }
  194. .stroke {
  195. color: #f00;
  196. }
  197. .textColor {
  198. color: #f00;
  199. }
  200. `
  201. let fn = () => `#f00`
  202. return run(input, {
  203. theme: {
  204. backgroundColor: { fn },
  205. borderColor: { fn },
  206. caretColor: { fn },
  207. colors: { fn },
  208. divideColor: { fn },
  209. fill: { fn },
  210. gradientColorStops: { fn },
  211. placeholderColor: { fn },
  212. ringColor: { fn },
  213. ringOffsetColor: { fn },
  214. stroke: { fn },
  215. textColor: { fn },
  216. },
  217. }).then((result) => {
  218. expect(result.css).toEqual(output)
  219. expect(result.warnings().length).toBe(0)
  220. })
  221. })
  222. test('quotes are optional around the lookup path', () => {
  223. let input = css`
  224. .banana {
  225. color: theme(colors.yellow);
  226. }
  227. `
  228. let output = css`
  229. .banana {
  230. color: #f7cc50;
  231. }
  232. `
  233. return run(input, {
  234. theme: {
  235. colors: {
  236. yellow: '#f7cc50',
  237. },
  238. },
  239. }).then((result) => {
  240. expect(result.css).toEqual(output)
  241. expect(result.warnings().length).toBe(0)
  242. })
  243. })
  244. test('a default value can be provided', () => {
  245. let input = css`
  246. .cookieMonster {
  247. color: theme('colors.blue', #0000ff);
  248. }
  249. `
  250. let output = css`
  251. .cookieMonster {
  252. color: #0000ff;
  253. }
  254. `
  255. return run(input, {
  256. theme: {
  257. colors: {
  258. yellow: '#f7cc50',
  259. },
  260. },
  261. }).then((result) => {
  262. expect(result.css).toEqual(output)
  263. expect(result.warnings().length).toBe(0)
  264. })
  265. })
  266. test('the default value can use the theme function', () => {
  267. let input = css`
  268. .cookieMonster {
  269. color: theme('colors.blue', theme('colors.yellow'));
  270. }
  271. `
  272. let output = css`
  273. .cookieMonster {
  274. color: #f7cc50;
  275. }
  276. `
  277. return run(input, {
  278. theme: {
  279. colors: {
  280. yellow: '#f7cc50',
  281. },
  282. },
  283. }).then((result) => {
  284. expect(result.css).toEqual(output)
  285. expect(result.warnings().length).toBe(0)
  286. })
  287. })
  288. test('quotes are preserved around default values', () => {
  289. let input = css`
  290. .heading {
  291. font-family: theme('fontFamily.sans', 'Helvetica Neue');
  292. }
  293. `
  294. let output = css`
  295. .heading {
  296. font-family: 'Helvetica Neue';
  297. }
  298. `
  299. return run(input, {
  300. theme: {
  301. fontFamily: {
  302. serif: 'Constantia',
  303. },
  304. },
  305. }).then((result) => {
  306. expect(result.css).toEqual(output)
  307. expect(result.warnings().length).toBe(0)
  308. })
  309. })
  310. test('an unquoted list is valid as a default value', () => {
  311. let input = css`
  312. .heading {
  313. font-family: theme('fontFamily.sans', Helvetica, Arial, sans-serif);
  314. }
  315. `
  316. let output = css`
  317. .heading {
  318. font-family: Helvetica, Arial, sans-serif;
  319. }
  320. `
  321. return run(input, {
  322. theme: {
  323. fontFamily: {
  324. serif: 'Constantia',
  325. },
  326. },
  327. }).then((result) => {
  328. expect(result.css).toEqual(output)
  329. expect(result.warnings().length).toBe(0)
  330. })
  331. })
  332. test('a missing root theme value throws', () => {
  333. let input = css`
  334. .heading {
  335. color: theme('colours.gray.100');
  336. }
  337. `
  338. return expect(
  339. run(input, {
  340. theme: {
  341. colors: {
  342. yellow: '#f7cc50',
  343. },
  344. },
  345. })
  346. ).rejects.toThrowError(
  347. `'colours.gray.100' does not exist in your theme config. Your theme has the following top-level keys: 'colors'`
  348. )
  349. })
  350. test('a missing nested theme property throws', () => {
  351. let input = css`
  352. .heading {
  353. color: theme('colors.red');
  354. }
  355. `
  356. return expect(
  357. run(input, {
  358. theme: {
  359. colors: {
  360. blue: 'blue',
  361. yellow: '#f7cc50',
  362. },
  363. },
  364. })
  365. ).rejects.toThrowError(
  366. `'colors.red' does not exist in your theme config. 'colors' has the following valid keys: 'blue', 'yellow'`
  367. )
  368. })
  369. test('a missing nested theme property with a close alternative throws with a suggestion', () => {
  370. let input = css`
  371. .heading {
  372. color: theme('colors.yellw');
  373. }
  374. `
  375. return expect(
  376. run(input, {
  377. theme: {
  378. colors: {
  379. yellow: '#f7cc50',
  380. },
  381. },
  382. })
  383. ).rejects.toThrowError(
  384. `'colors.yellw' does not exist in your theme config. Did you mean 'colors.yellow'?`
  385. )
  386. })
  387. test('a path through a non-object throws', () => {
  388. let input = css`
  389. .heading {
  390. color: theme('colors.yellow.100');
  391. }
  392. `
  393. return expect(
  394. run(input, {
  395. theme: {
  396. colors: {
  397. yellow: '#f7cc50',
  398. },
  399. },
  400. })
  401. ).rejects.toThrowError(
  402. `'colors.yellow.100' does not exist in your theme config. 'colors.yellow' is not an object.`
  403. )
  404. })
  405. test('a path which exists but is not a string throws', () => {
  406. let input = css`
  407. .heading {
  408. color: theme('colors.yellow');
  409. }
  410. `
  411. return expect(
  412. run(input, {
  413. theme: {
  414. colors: {
  415. yellow: Symbol(),
  416. },
  417. },
  418. })
  419. ).rejects.toThrowError(`'colors.yellow' was found but does not resolve to a string.`)
  420. })
  421. test('a path which exists but is invalid throws', () => {
  422. let input = css`
  423. .heading {
  424. color: theme('colors');
  425. }
  426. `
  427. return expect(
  428. run(input, {
  429. theme: {
  430. colors: {},
  431. },
  432. })
  433. ).rejects.toThrowError(`'colors' was found but does not resolve to a string.`)
  434. })
  435. test('a path which is an object throws with a suggested key', () => {
  436. let input = css`
  437. .heading {
  438. color: theme('colors');
  439. }
  440. `
  441. return expect(
  442. run(input, {
  443. theme: {
  444. colors: {
  445. yellow: '#f7cc50',
  446. },
  447. },
  448. })
  449. ).rejects.toThrowError(
  450. `'colors' was found but does not resolve to a string. Did you mean something like 'colors.yellow'?`
  451. )
  452. })
  453. test('array values are joined by default', () => {
  454. let input = css`
  455. .heading {
  456. font-family: theme('fontFamily.sans');
  457. }
  458. `
  459. let output = css`
  460. .heading {
  461. font-family: Inter, Helvetica, sans-serif;
  462. }
  463. `
  464. return run(input, {
  465. theme: {
  466. fontFamily: {
  467. sans: ['Inter', 'Helvetica', 'sans-serif'],
  468. },
  469. },
  470. }).then((result) => {
  471. expect(result.css).toEqual(output)
  472. expect(result.warnings().length).toBe(0)
  473. })
  474. })
  475. test('font sizes are retrieved without default line-heights or letter-spacing', () => {
  476. let input = css`
  477. .heading-1 {
  478. font-size: theme('fontSize.lg');
  479. }
  480. .heading-2 {
  481. font-size: theme('fontSize.xl');
  482. }
  483. `
  484. let output = css`
  485. .heading-1 {
  486. font-size: 20px;
  487. }
  488. .heading-2 {
  489. font-size: 24px;
  490. }
  491. `
  492. return run(input, {
  493. theme: {
  494. fontSize: {
  495. lg: ['20px', '28px'],
  496. xl: ['24px', { lineHeight: '32px', letterSpacing: '-0.01em' }],
  497. },
  498. },
  499. }).then((result) => {
  500. expect(result.css).toMatchCss(output)
  501. expect(result.warnings().length).toBe(0)
  502. })
  503. })
  504. test('outlines are retrieved without default outline-offset', () => {
  505. let input = css`
  506. .element {
  507. outline: theme('outline.black');
  508. }
  509. `
  510. let output = css`
  511. .element {
  512. outline: 2px dotted black;
  513. }
  514. `
  515. return run(input, {
  516. theme: {
  517. outline: {
  518. black: ['2px dotted black', '4px'],
  519. },
  520. },
  521. }).then((result) => {
  522. expect(result.css).toMatchCss(output)
  523. expect(result.warnings().length).toBe(0)
  524. })
  525. })
  526. test('font-family values are joined when an array', () => {
  527. let input = css`
  528. .element {
  529. font-family: theme('fontFamily.sans');
  530. }
  531. `
  532. let output = css`
  533. .element {
  534. font-family: Helvetica, Arial, sans-serif;
  535. }
  536. `
  537. return run(input, {
  538. theme: {
  539. fontFamily: {
  540. sans: ['Helvetica', 'Arial', 'sans-serif'],
  541. },
  542. },
  543. }).then((result) => {
  544. expect(result.css).toMatchCss(output)
  545. expect(result.warnings().length).toBe(0)
  546. })
  547. })
  548. test('font-family values are retrieved without font-feature-settings', () => {
  549. let input = css`
  550. .heading-1 {
  551. font-family: theme('fontFamily.sans');
  552. }
  553. .heading-2 {
  554. font-family: theme('fontFamily.serif');
  555. }
  556. .heading-3 {
  557. font-family: theme('fontFamily.mono');
  558. }
  559. `
  560. let output = css`
  561. .heading-1 {
  562. font-family: Inter;
  563. }
  564. .heading-2 {
  565. font-family: Times, serif;
  566. }
  567. .heading-3 {
  568. font-family: Menlo, monospace;
  569. }
  570. `
  571. return run(input, {
  572. theme: {
  573. fontFamily: {
  574. sans: ['Inter', { fontFeatureSettings: '"cv11"' }],
  575. serif: [['Times', 'serif'], { fontFeatureSettings: '"cv11"' }],
  576. mono: ['Menlo, monospace', { fontFeatureSettings: '"cv11"' }],
  577. },
  578. },
  579. }).then((result) => {
  580. expect(result.css).toMatchCss(output)
  581. expect(result.warnings().length).toBe(0)
  582. })
  583. })
  584. test('box-shadow values are joined when an array', () => {
  585. let input = css`
  586. .element {
  587. box-shadow: theme('boxShadow.wtf');
  588. }
  589. `
  590. let output = css`
  591. .element {
  592. box-shadow: 0 0 2px black, 1px 2px 3px white;
  593. }
  594. `
  595. return run(input, {
  596. theme: {
  597. boxShadow: {
  598. wtf: ['0 0 2px black', '1px 2px 3px white'],
  599. },
  600. },
  601. }).then((result) => {
  602. expect(result.css).toMatchCss(output)
  603. expect(result.warnings().length).toBe(0)
  604. })
  605. })
  606. test('transition-property values are joined when an array', () => {
  607. let input = css`
  608. .element {
  609. transition-property: theme('transitionProperty.colors');
  610. }
  611. `
  612. let output = css`
  613. .element {
  614. transition-property: color, fill;
  615. }
  616. `
  617. return run(input, {
  618. theme: {
  619. transitionProperty: {
  620. colors: ['color', 'fill'],
  621. },
  622. },
  623. }).then((result) => {
  624. expect(result.css).toMatchCss(output)
  625. expect(result.warnings().length).toBe(0)
  626. })
  627. })
  628. test('transition-duration values are joined when an array', () => {
  629. let input = css`
  630. .element {
  631. transition-duration: theme('transitionDuration.lol');
  632. }
  633. `
  634. let output = css`
  635. .element {
  636. transition-duration: 1s, 2s;
  637. }
  638. `
  639. return run(input, {
  640. theme: {
  641. transitionDuration: {
  642. lol: ['1s', '2s'],
  643. },
  644. },
  645. }).then((result) => {
  646. expect(result.css).toMatchCss(output)
  647. expect(result.warnings().length).toBe(0)
  648. })
  649. })
  650. test('basic screen function calls are expanded', () => {
  651. let input = css`
  652. @media screen(sm) {
  653. .foo {
  654. }
  655. }
  656. `
  657. let output = css`
  658. @media (min-width: 600px) {
  659. .foo {
  660. }
  661. }
  662. `
  663. return run(input, {
  664. theme: { screens: { sm: '600px' } },
  665. }).then((result) => {
  666. expect(result.css).toMatchCss(output)
  667. expect(result.warnings().length).toBe(0)
  668. })
  669. })
  670. test('screen function supports max-width screens', () => {
  671. let input = css`
  672. @media screen(sm) {
  673. .foo {
  674. }
  675. }
  676. `
  677. let output = css`
  678. @media (max-width: 600px) {
  679. .foo {
  680. }
  681. }
  682. `
  683. return run(input, {
  684. theme: { screens: { sm: { max: '600px' } } },
  685. }).then((result) => {
  686. expect(result.css).toMatchCss(output)
  687. expect(result.warnings().length).toBe(0)
  688. })
  689. })
  690. test('screen function supports min-width screens', () => {
  691. let input = css`
  692. @media screen(sm) {
  693. .foo {
  694. }
  695. }
  696. `
  697. let output = css`
  698. @media (min-width: 600px) {
  699. .foo {
  700. }
  701. }
  702. `
  703. return run(input, {
  704. theme: { screens: { sm: { min: '600px' } } },
  705. }).then((result) => {
  706. expect(result.css).toMatchCss(output)
  707. expect(result.warnings().length).toBe(0)
  708. })
  709. })
  710. test('screen function supports min-width and max-width screens', () => {
  711. let input = css`
  712. @media screen(sm) {
  713. .foo {
  714. }
  715. }
  716. `
  717. let output = css`
  718. @media (min-width: 600px) and (max-width: 700px) {
  719. .foo {
  720. }
  721. }
  722. `
  723. return run(input, {
  724. theme: { screens: { sm: { min: '600px', max: '700px' } } },
  725. }).then((result) => {
  726. expect(result.css).toMatchCss(output)
  727. expect(result.warnings().length).toBe(0)
  728. })
  729. })
  730. test('screen function supports raw screens', () => {
  731. let input = css`
  732. @media screen(mono) {
  733. .foo {
  734. }
  735. }
  736. `
  737. let output = css`
  738. @media monochrome {
  739. .foo {
  740. }
  741. }
  742. `
  743. return run(input, {
  744. theme: { screens: { mono: { raw: 'monochrome' } } },
  745. }).then((result) => {
  746. expect(result.css).toMatchCss(output)
  747. expect(result.warnings().length).toBe(0)
  748. })
  749. })
  750. test('screen arguments can be quoted', () => {
  751. let input = css`
  752. @media screen('sm') {
  753. .foo {
  754. }
  755. }
  756. `
  757. let output = css`
  758. @media (min-width: 600px) {
  759. .foo {
  760. }
  761. }
  762. `
  763. return run(input, {
  764. theme: { screens: { sm: '600px' } },
  765. }).then((result) => {
  766. expect(result.css).toMatchCss(output)
  767. expect(result.warnings().length).toBe(0)
  768. })
  769. })
  770. test('Theme function can extract alpha values for colors (1)', () => {
  771. let input = css`
  772. .foo {
  773. color: theme(colors.blue.500 / 50%);
  774. }
  775. `
  776. let output = css`
  777. .foo {
  778. color: rgb(59 130 246 / 50%);
  779. }
  780. `
  781. return run(input, {
  782. theme: {
  783. colors: { blue: { 500: '#3b82f6' } },
  784. },
  785. }).then((result) => {
  786. expect(result.css).toMatchCss(output)
  787. expect(result.warnings().length).toBe(0)
  788. })
  789. })
  790. test('Theme function can extract alpha values for colors (2)', () => {
  791. let input = css`
  792. .foo {
  793. color: theme(colors.blue.500 / 0.5);
  794. }
  795. `
  796. let output = css`
  797. .foo {
  798. color: rgb(59 130 246 / 0.5);
  799. }
  800. `
  801. return run(input, {
  802. theme: {
  803. colors: { blue: { 500: '#3b82f6' } },
  804. },
  805. }).then((result) => {
  806. expect(result.css).toMatchCss(output)
  807. expect(result.warnings().length).toBe(0)
  808. })
  809. })
  810. test('Theme function can extract alpha values for colors (3)', () => {
  811. let input = css`
  812. .foo {
  813. color: theme(colors.blue.500 / var(--my-alpha));
  814. }
  815. `
  816. let output = css`
  817. .foo {
  818. color: rgb(59 130 246 / var(--my-alpha));
  819. }
  820. `
  821. return run(input, {
  822. theme: {
  823. colors: { blue: { 500: '#3b82f6' } },
  824. },
  825. }).then((result) => {
  826. expect(result.css).toMatchCss(output)
  827. expect(result.warnings().length).toBe(0)
  828. })
  829. })
  830. test('Theme function can extract alpha values for colors (4)', () => {
  831. let input = css`
  832. .foo {
  833. color: theme(colors.blue.500 / 50%);
  834. }
  835. `
  836. let output = css`
  837. .foo {
  838. color: hsl(217 91% 60% / 50%);
  839. }
  840. `
  841. return run(input, {
  842. theme: {
  843. colors: {
  844. blue: { 500: 'hsl(217, 91%, 60%)' },
  845. },
  846. },
  847. }).then((result) => {
  848. expect(result.css).toMatchCss(output)
  849. expect(result.warnings().length).toBe(0)
  850. })
  851. })
  852. test('Theme function can extract alpha values for colors (5)', () => {
  853. let input = css`
  854. .foo {
  855. color: theme(colors.blue.500 / 0.5);
  856. }
  857. `
  858. let output = css`
  859. .foo {
  860. color: hsl(217 91% 60% / 0.5);
  861. }
  862. `
  863. return run(input, {
  864. theme: {
  865. colors: {
  866. blue: { 500: 'hsl(217, 91%, 60%)' },
  867. },
  868. },
  869. }).then((result) => {
  870. expect(result.css).toMatchCss(output)
  871. expect(result.warnings().length).toBe(0)
  872. })
  873. })
  874. test('Theme function can extract alpha values for colors (6)', () => {
  875. let input = css`
  876. .foo {
  877. color: theme(colors.blue.500 / var(--my-alpha));
  878. }
  879. `
  880. let output = css`
  881. .foo {
  882. color: hsl(217 91% 60% / var(--my-alpha));
  883. }
  884. `
  885. return run(input, {
  886. theme: {
  887. colors: {
  888. blue: { 500: 'hsl(217, 91%, 60%)' },
  889. },
  890. },
  891. }).then((result) => {
  892. expect(result.css).toMatchCss(output)
  893. expect(result.warnings().length).toBe(0)
  894. })
  895. })
  896. test('Theme function can extract alpha values for colors (7)', () => {
  897. let input = css`
  898. .foo {
  899. color: theme(colors.blue.500 / var(--my-alpha));
  900. }
  901. `
  902. let output = css`
  903. .foo {
  904. color: rgb(var(--foo) / var(--my-alpha));
  905. }
  906. `
  907. return runFull(input, {
  908. theme: {
  909. colors: {
  910. blue: {
  911. 500: 'rgb(var(--foo) / <alpha-value>)',
  912. },
  913. },
  914. },
  915. }).then((result) => {
  916. expect(result.css).toMatchCss(output)
  917. expect(result.warnings().length).toBe(0)
  918. })
  919. })
  920. test('Theme function can extract alpha values for colors (8)', () => {
  921. let input = css`
  922. .foo {
  923. color: theme(colors.blue.500 / theme(opacity.myalpha));
  924. }
  925. `
  926. let output = css`
  927. .foo {
  928. color: rgb(var(--foo) / 50%);
  929. }
  930. `
  931. return runFull(input, {
  932. theme: {
  933. colors: {
  934. blue: {
  935. 500: 'rgb(var(--foo) / <alpha-value>)',
  936. },
  937. },
  938. opacity: {
  939. myalpha: '50%',
  940. },
  941. },
  942. }).then((result) => {
  943. expect(result.css).toMatchCss(output)
  944. expect(result.warnings().length).toBe(0)
  945. })
  946. })
  947. test('Theme functions replace the alpha value placeholder even with no alpha provided', () => {
  948. let input = css`
  949. .foo {
  950. background: theme(colors.blue.400);
  951. color: theme(colors.blue.500);
  952. }
  953. `
  954. let output = css`
  955. .foo {
  956. background: rgb(0 0 255 / 1);
  957. color: rgb(var(--foo) / 1);
  958. }
  959. `
  960. return runFull(input, {
  961. theme: {
  962. colors: {
  963. blue: {
  964. 400: 'rgb(0 0 255 / <alpha-value>)',
  965. 500: 'rgb(var(--foo) / <alpha-value>)',
  966. },
  967. },
  968. },
  969. }).then((result) => {
  970. expect(result.css).toMatchCss(output)
  971. expect(result.warnings().length).toBe(0)
  972. })
  973. })
  974. test('Theme functions can reference values with slashes in brackets', () => {
  975. let input = css`
  976. .foo1 {
  977. color: theme(colors[a/b]);
  978. }
  979. .foo2 {
  980. color: theme(colors[a/b]/50%);
  981. }
  982. `
  983. let output = css`
  984. .foo1 {
  985. color: #000000;
  986. }
  987. .foo2 {
  988. color: rgb(0 0 0 / 50%);
  989. }
  990. `
  991. return runFull(input, {
  992. theme: {
  993. colors: {
  994. 'a/b': '#000000',
  995. },
  996. },
  997. }).then((result) => {
  998. expect(result.css).toMatchCss(output)
  999. expect(result.warnings().length).toBe(0)
  1000. })
  1001. })
  1002. test('Theme functions with alpha value inside quotes', () => {
  1003. let input = css`
  1004. .foo {
  1005. color: theme('colors.yellow / 50%');
  1006. }
  1007. `
  1008. let output = css`
  1009. .foo {
  1010. color: rgb(247 204 80 / 50%);
  1011. }
  1012. `
  1013. return runFull(input, {
  1014. theme: {
  1015. colors: {
  1016. yellow: '#f7cc50',
  1017. },
  1018. },
  1019. }).then((result) => {
  1020. expect(result.css).toMatchCss(output)
  1021. expect(result.warnings().length).toBe(0)
  1022. })
  1023. })
  1024. test('Theme functions with alpha with quotes value around color only', () => {
  1025. let input = css`
  1026. .foo {
  1027. color: theme('colors.yellow' / 50%);
  1028. }
  1029. `
  1030. let output = css`
  1031. .foo {
  1032. color: rgb(247 204 80 / 50%);
  1033. }
  1034. `
  1035. return runFull(input, {
  1036. theme: {
  1037. colors: {
  1038. yellow: '#f7cc50',
  1039. },
  1040. },
  1041. }).then((result) => {
  1042. expect(result.css).toMatchCss(output)
  1043. expect(result.warnings().length).toBe(0)
  1044. })
  1045. })
  1046. it('can find values with slashes in the theme key while still allowing for alpha values ', () => {
  1047. let input = css`
  1048. .foo00 {
  1049. color: theme(colors.foo-5);
  1050. }
  1051. .foo01 {
  1052. color: theme(colors.foo-5/10);
  1053. }
  1054. .foo02 {
  1055. color: theme(colors.foo-5/10/25);
  1056. }
  1057. .foo03 {
  1058. color: theme(colors.foo-5 / 10);
  1059. }
  1060. .foo04 {
  1061. color: theme(colors.foo-5/10 / 25);
  1062. }
  1063. `
  1064. return runFull(input, {
  1065. theme: {
  1066. colors: {
  1067. 'foo-5': '#050000',
  1068. 'foo-5/10': '#051000',
  1069. 'foo-5/10/25': '#051025',
  1070. },
  1071. },
  1072. }).then((result) => {
  1073. expect(result.css).toMatchCss(css`
  1074. .foo00 {
  1075. color: #050000;
  1076. }
  1077. .foo01 {
  1078. color: #051000;
  1079. }
  1080. .foo02 {
  1081. color: #051025;
  1082. }
  1083. .foo03 {
  1084. color: rgb(5 0 0 / 10);
  1085. }
  1086. .foo04 {
  1087. color: rgb(5 16 0 / 25);
  1088. }
  1089. `)
  1090. })
  1091. })