mirror of https://github.com/vuejs/core.git
parent
6c74fb07a7
commit
364f8902c8
|
@ -1,10 +1,12 @@
|
||||||
import type { ObjectDirective } from '@vue/runtime-core'
|
import type { ObjectDirective } from '@vue/runtime-core'
|
||||||
|
|
||||||
export const vShowOriginalDisplay = Symbol('_vod')
|
export const vShowOriginalDisplay = Symbol('_vod')
|
||||||
|
export const vShowHidden = Symbol('_vsh')
|
||||||
|
|
||||||
interface VShowElement extends HTMLElement {
|
export interface VShowElement extends HTMLElement {
|
||||||
// _vod = vue original display
|
// _vod = vue original display
|
||||||
[vShowOriginalDisplay]: string
|
[vShowOriginalDisplay]: string
|
||||||
|
[vShowHidden]: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
|
export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
|
||||||
|
@ -23,11 +25,7 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updated(el, { value, oldValue }, { transition }) {
|
updated(el, { value, oldValue }, { transition }) {
|
||||||
if (
|
if (!value === !oldValue) return
|
||||||
!value === !oldValue &&
|
|
||||||
(el.style.display === el[vShowOriginalDisplay] || !value)
|
|
||||||
)
|
|
||||||
return
|
|
||||||
if (transition) {
|
if (transition) {
|
||||||
if (value) {
|
if (value) {
|
||||||
transition.beforeEnter(el)
|
transition.beforeEnter(el)
|
||||||
|
@ -53,6 +51,7 @@ if (__DEV__) {
|
||||||
|
|
||||||
function setDisplay(el: VShowElement, value: unknown): void {
|
function setDisplay(el: VShowElement, value: unknown): void {
|
||||||
el.style.display = value ? el[vShowOriginalDisplay] : 'none'
|
el.style.display = value ? el[vShowOriginalDisplay] : 'none'
|
||||||
|
el[vShowHidden] = !value
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSR vnode transforms, only used when user includes client-oriented render
|
// SSR vnode transforms, only used when user includes client-oriented render
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { capitalize, hyphenate, isArray, isString } from '@vue/shared'
|
import { capitalize, hyphenate, isArray, isString } from '@vue/shared'
|
||||||
import { camelize, warn } from '@vue/runtime-core'
|
import { camelize, warn } from '@vue/runtime-core'
|
||||||
import { vShowOriginalDisplay } from '../directives/vShow'
|
import {
|
||||||
|
type VShowElement,
|
||||||
|
vShowHidden,
|
||||||
|
vShowOriginalDisplay,
|
||||||
|
} from '../directives/vShow'
|
||||||
import { CSS_VAR_TEXT } from '../helpers/useCssVars'
|
import { CSS_VAR_TEXT } from '../helpers/useCssVars'
|
||||||
|
|
||||||
type Style = string | Record<string, string | string[]> | null
|
type Style = string | Record<string, string | string[]> | null
|
||||||
|
@ -10,7 +14,6 @@ const displayRE = /(^|;)\s*display\s*:/
|
||||||
export function patchStyle(el: Element, prev: Style, next: Style) {
|
export function patchStyle(el: Element, prev: Style, next: Style) {
|
||||||
const style = (el as HTMLElement).style
|
const style = (el as HTMLElement).style
|
||||||
const isCssString = isString(next)
|
const isCssString = isString(next)
|
||||||
const currentDisplay = style.display
|
|
||||||
let hasControlledDisplay = false
|
let hasControlledDisplay = false
|
||||||
if (next && !isCssString) {
|
if (next && !isCssString) {
|
||||||
if (prev) {
|
if (prev) {
|
||||||
|
@ -50,12 +53,14 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
|
||||||
el.removeAttribute('style')
|
el.removeAttribute('style')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// indicates that the `display` of the element is controlled by `v-show`,
|
// indicates the element also has `v-show`.
|
||||||
// so we always keep the current `display` value regardless of the `style`
|
|
||||||
// value, thus handing over control to `v-show`.
|
|
||||||
if (vShowOriginalDisplay in el) {
|
if (vShowOriginalDisplay in el) {
|
||||||
|
// make v-show respect the current v-bind style display when shown
|
||||||
el[vShowOriginalDisplay] = hasControlledDisplay ? style.display : ''
|
el[vShowOriginalDisplay] = hasControlledDisplay ? style.display : ''
|
||||||
style.display = currentDisplay
|
// if v-show is in hidden state, v-show has higher priority
|
||||||
|
if ((el as VShowElement)[vShowHidden]) {
|
||||||
|
style.display = 'none'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue