Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchangtao committed Feb 1, 2024
1 parent 01af548 commit e559a34
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
11 changes: 7 additions & 4 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,13 @@ export interface ComponentInternalInstance {
* For updating css vars on contained teleports
* @internal
*/
ut?: (
vars?: Record<string, string>,
hydration?: boolean,
) => void | Record<string, string>
ut?: (vars?: Record<string, string>) => void

/**
* For getCssVars on hydration
* @internal
*/
getCssVars?: () => Record<string, string>
}

const emptyAppContext = createAppContext()
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,7 @@ export function createHydrationFunctions(
!optimized ||
patchFlag & (PatchFlags.FULL_PROPS | PatchFlags.NEED_HYDRATION)
) {
const cssVars = parentComponent?.ut?.(undefined, true) as Record<
string,
string
>
const cssVars = parentComponent?.getCssVars?.()
for (const key in props) {
// check hydration mismatch
if (
Expand Down
19 changes: 12 additions & 7 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>) {
return
}

const updateTeleports = (instance.ut = (
vars = getter(instance.proxy),
hydration = false,
) => {
if (hydration) {
return vars
}
const updateTeleports = (instance.ut = (vars = getter(instance.proxy)) => {
Array.from(
document.querySelectorAll(`[data-v-owner="${instance.uid}"]`),
).forEach(node => setVarsOnNode(node, vars))
})

if (__DEV__ && __SSR__) {
instance.getCssVars = () => fommaterVars(getter(instance.proxy))
}

const setVars = () => {
const vars = getter(instance.proxy)
setVarsOnVNode(instance.subTree, vars)
Expand Down Expand Up @@ -94,3 +92,10 @@ function setVarsOnNode(el: Node, vars: Record<string, string>) {
;(style as any)[CSS_VAR_TEXT] = cssText
}
}
function fommaterVars(vars: Record<string, string>) {
const cssVars: Record<string, string> = {}
for (const key in vars) {
cssVars[`--${key}`] = vars[key]
}
return cssVars
}

0 comments on commit e559a34

Please sign in to comment.