-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cssVars): use css vars work with vapor mode
- Loading branch information
1 parent
ef6986f
commit 2895e1a
Showing
6 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
applyCssVars, | ||
currentInstance, | ||
setVarsOnNode, | ||
warn, | ||
} from '@vue/runtime-dom' | ||
import { type VaporComponentInstance, isVaporComponent } from './component' | ||
import { isArray } from '@vue/shared' | ||
import type { Block } from './block' | ||
|
||
export function vaporUseCssVars(getter: () => Record<string, string>): void { | ||
if (!__BROWSER__ && !__TEST__) return | ||
|
||
const instance = currentInstance as VaporComponentInstance | ||
/* v8 ignore start */ | ||
if (!instance) { | ||
__DEV__ && | ||
warn(`useCssVars is called without current active component instance.`) | ||
return | ||
} | ||
/* v8 ignore stop */ | ||
|
||
applyCssVars( | ||
() => resolveParentNode(instance.block), | ||
() => setVarsOnBlock(instance.block, getter()), | ||
) | ||
} | ||
|
||
function resolveParentNode(block: Block): Node { | ||
if (block instanceof Node) { | ||
return block.parentNode! | ||
} else if (isVaporComponent(block)) { | ||
return resolveParentNode(block.block!) | ||
} else if (isArray(block)) { | ||
return resolveParentNode(block[0]) | ||
} else { | ||
return resolveParentNode(block.nodes) | ||
} | ||
} | ||
|
||
function setVarsOnBlock(block: Block, vars: Record<string, string>): void { | ||
if (block instanceof Node) { | ||
setVarsOnNode(block, vars) | ||
} else if (isArray(block)) { | ||
block.forEach(child => setVarsOnBlock(child, vars)) | ||
} else if (isVaporComponent(block)) { | ||
setVarsOnBlock(block.block!, vars) | ||
} else { | ||
setVarsOnBlock(block.nodes, vars) | ||
} | ||
} |