useCssVars.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import {
  2. type ComponentOptions,
  3. type FunctionalComponent,
  4. Suspense,
  5. Teleport,
  6. createStaticVNode,
  7. defineCustomElement,
  8. h,
  9. nextTick,
  10. onMounted,
  11. reactive,
  12. ref,
  13. render,
  14. useCssVars,
  15. } from '@vue/runtime-dom'
  16. describe('useCssVars', () => {
  17. async function assertCssVars(getApp: (state: any) => ComponentOptions) {
  18. const state = reactive({ color: 'red' })
  19. const App = getApp(state)
  20. const root = document.createElement('div')
  21. render(h(App), root)
  22. await nextTick()
  23. for (const c of [].slice.call(root.children as any)) {
  24. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  25. }
  26. state.color = 'green'
  27. await nextTick()
  28. for (const c of [].slice.call(root.children as any)) {
  29. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
  30. }
  31. }
  32. test('basic', async () => {
  33. await assertCssVars(state => ({
  34. setup() {
  35. // test receiving render context
  36. useCssVars((ctx: any) => ({
  37. color: ctx.color,
  38. }))
  39. return state
  40. },
  41. render() {
  42. return h('div')
  43. },
  44. }))
  45. })
  46. test('on fragment root', async () => {
  47. await assertCssVars(state => ({
  48. setup() {
  49. useCssVars(() => state)
  50. return () => [h('div'), h('div')]
  51. },
  52. }))
  53. })
  54. test('on HOCs', async () => {
  55. const Child = () => [h('div'), h('div')]
  56. await assertCssVars(state => ({
  57. setup() {
  58. useCssVars(() => state)
  59. return () => h(Child)
  60. },
  61. }))
  62. })
  63. test('on suspense root', async () => {
  64. const state = reactive({ color: 'red' })
  65. const root = document.createElement('div')
  66. let resolveAsync: any
  67. let asyncPromise: any
  68. const AsyncComp = {
  69. setup() {
  70. asyncPromise = new Promise(r => {
  71. resolveAsync = () => {
  72. r(() => h('p', 'default'))
  73. }
  74. })
  75. return asyncPromise
  76. },
  77. }
  78. const App = {
  79. setup() {
  80. useCssVars(() => state)
  81. return () =>
  82. h(Suspense, null, {
  83. default: h(AsyncComp),
  84. fallback: h('div', 'fallback'),
  85. })
  86. },
  87. }
  88. render(h(App), root)
  89. await nextTick()
  90. // css vars use with fallback tree
  91. for (const c of [].slice.call(root.children as any)) {
  92. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  93. }
  94. // AsyncComp resolve
  95. resolveAsync()
  96. await asyncPromise.then(() => {})
  97. // Suspense effects flush
  98. await nextTick()
  99. // css vars use with default tree
  100. for (const c of [].slice.call(root.children as any)) {
  101. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  102. }
  103. state.color = 'green'
  104. await nextTick()
  105. for (const c of [].slice.call(root.children as any)) {
  106. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
  107. }
  108. })
  109. test('with v-if & async component & suspense', async () => {
  110. const state = reactive({ color: 'red' })
  111. const root = document.createElement('div')
  112. const show = ref(false)
  113. let resolveAsync: any
  114. let asyncPromise: any
  115. const AsyncComp = {
  116. setup() {
  117. useCssVars(() => state)
  118. asyncPromise = new Promise(r => {
  119. resolveAsync = () => {
  120. r(() => h('p', 'default'))
  121. }
  122. })
  123. return asyncPromise
  124. },
  125. }
  126. const App = {
  127. setup() {
  128. return () =>
  129. h(Suspense, null, {
  130. default: h('div', {}, show.value ? h(AsyncComp) : h('p')),
  131. })
  132. },
  133. }
  134. render(h(App), root)
  135. await nextTick()
  136. // AsyncComp resolve
  137. show.value = true
  138. await nextTick()
  139. resolveAsync()
  140. await asyncPromise.then(() => {})
  141. // Suspense effects flush
  142. await nextTick()
  143. // css vars use with default tree
  144. for (const c of [].slice.call(root.children as any)) {
  145. expect(
  146. ((c as any).children[0] as HTMLElement).style.getPropertyValue(
  147. `--color`,
  148. ),
  149. ).toBe(`red`)
  150. }
  151. state.color = 'green'
  152. await nextTick()
  153. for (const c of [].slice.call(root.children as any)) {
  154. expect(
  155. ((c as any).children[0] as HTMLElement).style.getPropertyValue(
  156. `--color`,
  157. ),
  158. ).toBe('green')
  159. }
  160. })
  161. test('with subTree changed', async () => {
  162. const state = reactive({ color: 'red' })
  163. const value = ref(true)
  164. const root = document.createElement('div')
  165. const App = {
  166. setup() {
  167. useCssVars(() => state)
  168. return () => (value.value ? [h('div')] : [h('div'), h('div')])
  169. },
  170. }
  171. render(h(App), root)
  172. await nextTick()
  173. // css vars use with fallback tree
  174. for (const c of [].slice.call(root.children as any)) {
  175. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  176. }
  177. value.value = false
  178. await nextTick()
  179. for (const c of [].slice.call(root.children as any)) {
  180. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  181. }
  182. })
  183. // #3894
  184. test('with subTree change inside HOC', async () => {
  185. const state = reactive({ color: 'red' })
  186. const value = ref(true)
  187. const root = document.createElement('div')
  188. const Child: FunctionalComponent = (_, { slots }) => slots.default!()
  189. const App = {
  190. setup() {
  191. useCssVars(() => state)
  192. return () =>
  193. h(Child, null, () =>
  194. value.value ? [h('div')] : [h('div'), h('div')],
  195. )
  196. },
  197. }
  198. render(h(App), root)
  199. await nextTick()
  200. // css vars use with fallback tree
  201. for (const c of [].slice.call(root.children as any)) {
  202. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  203. }
  204. value.value = false
  205. await nextTick()
  206. for (const c of [].slice.call(root.children as any)) {
  207. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  208. }
  209. })
  210. test('with createStaticVNode', async () => {
  211. const state = reactive({ color: 'red' })
  212. const root = document.createElement('div')
  213. const App = {
  214. setup() {
  215. useCssVars(() => state)
  216. return () => [
  217. h('div'),
  218. createStaticVNode('<div>1</div><div><span>2</span></div>', 2),
  219. h('div'),
  220. ]
  221. },
  222. }
  223. render(h(App), root)
  224. await nextTick()
  225. for (const c of [].slice.call(root.children as any)) {
  226. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  227. }
  228. })
  229. test('with teleport', async () => {
  230. document.body.innerHTML = ''
  231. const state = reactive({ color: 'red' })
  232. const root = document.createElement('div')
  233. const target = document.body
  234. const App = {
  235. setup() {
  236. useCssVars(() => state)
  237. return () => [h(Teleport, { to: target }, [h('div')])]
  238. },
  239. }
  240. render(h(App), root)
  241. await nextTick()
  242. for (const c of [].slice.call(target.children as any)) {
  243. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  244. }
  245. })
  246. test('with teleport in child slot', async () => {
  247. document.body.innerHTML = ''
  248. const state = reactive({ color: 'red' })
  249. const root = document.createElement('div')
  250. const target = document.body
  251. const Child: FunctionalComponent = (_, { slots }) => {
  252. return h('div', slots.default && slots.default())
  253. }
  254. const App = {
  255. setup() {
  256. useCssVars(() => state)
  257. return () => h(Child, () => [h(Teleport, { to: target }, [h('div')])])
  258. },
  259. }
  260. render(h(App), root)
  261. await nextTick()
  262. for (const c of [].slice.call(target.children as any)) {
  263. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  264. }
  265. })
  266. test('with teleport(change subTree)', async () => {
  267. document.body.innerHTML = ''
  268. const state = reactive({ color: 'red' })
  269. const root = document.createElement('div')
  270. const target = document.body
  271. const toggle = ref(false)
  272. const App = {
  273. setup() {
  274. useCssVars(() => state)
  275. return () => [
  276. h(Teleport, { to: target }, [
  277. h('div'),
  278. toggle.value ? h('div') : null,
  279. ]),
  280. ]
  281. },
  282. }
  283. render(h(App), root)
  284. await nextTick()
  285. expect(target.children.length).toBe(1)
  286. for (const c of [].slice.call(target.children as any)) {
  287. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  288. }
  289. toggle.value = true
  290. await nextTick()
  291. expect(target.children.length).toBe(2)
  292. for (const c of [].slice.call(target.children as any)) {
  293. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  294. }
  295. })
  296. test('with teleport(disabled)', async () => {
  297. document.body.innerHTML = ''
  298. const state = reactive({ color: 'red' })
  299. const root = document.createElement('div')
  300. const target = document.body
  301. const App = {
  302. setup() {
  303. useCssVars(() => state)
  304. return () => [h(Teleport, { to: target, disabled: true }, [h('div')])]
  305. },
  306. }
  307. expect(() => render(h(App), root)).not.toThrow(TypeError)
  308. await nextTick()
  309. expect(target.children.length).toBe(0)
  310. expect(root.children[0].outerHTML.includes('data-v-owner')).toBe(true)
  311. })
  312. test('with string style', async () => {
  313. document.body.innerHTML = ''
  314. const state = reactive({ color: 'red' })
  315. const root = document.createElement('div')
  316. const disabled = ref(false)
  317. const App = {
  318. setup() {
  319. useCssVars(() => state)
  320. return () => [
  321. h(
  322. 'div',
  323. { style: disabled.value ? 'pointer-events: none' : undefined },
  324. 'foo',
  325. ),
  326. ]
  327. },
  328. }
  329. render(h(App), root)
  330. await nextTick()
  331. for (const c of [].slice.call(root.children as any)) {
  332. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  333. }
  334. disabled.value = true
  335. await nextTick()
  336. for (const c of [].slice.call(root.children as any)) {
  337. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  338. }
  339. })
  340. test('with delay mount child', async () => {
  341. const state = reactive({ color: 'red' })
  342. const value = ref(false)
  343. const root = document.createElement('div')
  344. const Child = {
  345. setup() {
  346. onMounted(() => {
  347. const childEl = root.children[0]
  348. expect(getComputedStyle(childEl!).getPropertyValue(`--color`)).toBe(
  349. `red`,
  350. )
  351. })
  352. return () => h('div', { id: 'childId' })
  353. },
  354. }
  355. const App = {
  356. setup() {
  357. useCssVars(() => state)
  358. return () => (value.value ? h(Child) : [h('span')])
  359. },
  360. }
  361. render(h(App), root)
  362. await nextTick()
  363. // css vars use with fallback tree
  364. for (const c of [].slice.call(root.children as any)) {
  365. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  366. }
  367. // mount child
  368. value.value = true
  369. await nextTick()
  370. for (const c of [].slice.call(root.children as any)) {
  371. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  372. }
  373. })
  374. // #8826
  375. test('with custom element', async () => {
  376. const state = reactive({ color: 'red' })
  377. const container = document.createElement('div')
  378. const App = defineCustomElement({
  379. styles: [`div { color: red; }`],
  380. setup() {
  381. useCssVars(() => state)
  382. return () => {
  383. return h('div', 'hello')
  384. }
  385. },
  386. })
  387. customElements.define('css-vars-ce', App)
  388. container.innerHTML = `<css-vars-ce></css-vars-ce>`
  389. document.body.appendChild(container)
  390. await nextTick()
  391. expect(container.innerHTML).toBe(
  392. `<css-vars-ce style="--color: red;"></css-vars-ce>`,
  393. )
  394. })
  395. test('should set vars before child component onMount hook', () => {
  396. const state = reactive({ color: 'red' })
  397. const root = document.createElement('div')
  398. let colorInOnMount
  399. const App = {
  400. setup() {
  401. useCssVars(() => state)
  402. onMounted(() => {
  403. colorInOnMount = (
  404. root.children[0] as HTMLElement
  405. ).style.getPropertyValue(`--color`)
  406. })
  407. return () => h('div')
  408. },
  409. }
  410. render(h(App), root)
  411. expect(colorInOnMount).toBe(`red`)
  412. })
  413. })