Skip to content

Commit

Permalink
refactor: use more descriptive name for v-show original display key
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 25, 2024
1 parent c6defc8 commit 9a365fe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@vue/runtime-dom'
import { type SSRContext, renderToString } from '@vue/server-renderer'
import { PatchFlags } from '@vue/shared'
import { vShowOldKey } from '../../runtime-dom/src/directives/vShow'
import { vShowOriginalDisplay } from '../../runtime-dom/src/directives/vShow'

function mountWithHydration(html: string, render: () => any) {
const container = document.createElement('div')
Expand Down Expand Up @@ -1252,7 +1252,7 @@ describe('SSR hydration', () => {
foo
</div>
`)
expect((container.firstChild as any)[vShowOldKey]).toBe('')
expect((container.firstChild as any)[vShowOriginalDisplay]).toBe('')
expect(vnode.el).toBe(container.firstChild)
expect(`mismatch`).not.toHaveBeenWarned()
})
Expand Down
11 changes: 6 additions & 5 deletions packages/runtime-dom/src/directives/vShow.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { ObjectDirective } from '@vue/runtime-core'

export const vShowOldKey = Symbol('_vod')
export const vShowOriginalDisplay = Symbol('_vod')

interface VShowElement extends HTMLElement {
// _vod = vue original display
[vShowOldKey]: string
[vShowOriginalDisplay]: string
}

export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
beforeMount(el, { value }, { transition }) {
el[vShowOldKey] = el.style.display === 'none' ? '' : el.style.display
el[vShowOriginalDisplay] =
el.style.display === 'none' ? '' : el.style.display
if (transition && value) {
transition.beforeEnter(el)
} else {
Expand All @@ -24,7 +25,7 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
updated(el, { value, oldValue }, { transition }) {
if (
!value === !oldValue &&
(el.style.display === el[vShowOldKey] || !value)
(el.style.display === el[vShowOriginalDisplay] || !value)
)
return
if (transition) {
Expand All @@ -51,7 +52,7 @@ if (__DEV__) {
}

function setDisplay(el: VShowElement, value: unknown): void {
el.style.display = value ? el[vShowOldKey] : 'none'
el.style.display = value ? el[vShowOriginalDisplay] : 'none'
}

// SSR vnode transforms, only used when user includes client-oriented render
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { capitalize, hyphenate, isArray, isString } from '@vue/shared'
import { camelize, warn } from '@vue/runtime-core'
import { vShowOldKey } from '../directives/vShow'
import { vShowOriginalDisplay } from '../directives/vShow'
import { CSS_VAR_TEXT } from '../helpers/useCssVars'

type Style = string | Record<string, string | string[]> | null
Expand Down Expand Up @@ -53,8 +53,8 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
// indicates that the `display` of the element is controlled by `v-show`,
// so we always keep the current `display` value regardless of the `style`
// value, thus handing over control to `v-show`.
if (vShowOldKey in el) {
el[vShowOldKey] = hasControlledDisplay ? style.display : ''
if (vShowOriginalDisplay in el) {
el[vShowOriginalDisplay] = hasControlledDisplay ? style.display : ''
style.display = currentDisplay
}
}
Expand Down

0 comments on commit 9a365fe

Please sign in to comment.