mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2025-01-30 12:52:43 +08:00
feat(mixin/usecssr)
This commit is contained in:
parent
3e01d76005
commit
394e95a1f0
@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
|
||||||
"name": "naive-ui",
|
"name": "naive-ui",
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"description": "A Vue UI Framework. Caring About Styles, Themed, Batteries Included, Not Rather Slow.",
|
"description": "A Vue UI Framework. Caring About Styles, Themed, Batteries Included, Not Rather Slow.",
|
||||||
|
63
src/_mixins/usecssr.js
Normal file
63
src/_mixins/usecssr.js
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
const styleMountStatus = {}
|
||||||
|
|
||||||
|
function isStyleMounted (componentName, renderedTheme) {
|
||||||
|
const componentStyleMountStatus = styleMountStatus[componentName]
|
||||||
|
if (!componentStyleMountStatus) return false
|
||||||
|
if (!componentStyleMountStatus[renderedTheme]) return false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function markStyleMounted (componentName, renderedTheme) {
|
||||||
|
if (!styleMountStatus[componentName]) {
|
||||||
|
styleMountStatus[componentName] = {
|
||||||
|
[renderedTheme]: true
|
||||||
|
}
|
||||||
|
} else if (!styleMountStatus[componentName][renderedTheme]) {
|
||||||
|
styleMountStatus[componentName][renderedTheme] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getThemeVariables (naive, themeName) {
|
||||||
|
const themes = naive._themes
|
||||||
|
const theme = themes[themeName]
|
||||||
|
return theme.base
|
||||||
|
}
|
||||||
|
|
||||||
|
function prepareTheme (instance, theme, CNode) {
|
||||||
|
const naive = instance.$naive
|
||||||
|
const options = instance.$options
|
||||||
|
const {
|
||||||
|
fallbackTheme,
|
||||||
|
_themes
|
||||||
|
} = naive
|
||||||
|
const renderedTheme = theme || fallbackTheme
|
||||||
|
if (isStyleMounted(options.name, renderedTheme)) return
|
||||||
|
const cssrPropsGetter = _themes[renderedTheme][options.name]
|
||||||
|
if (process.env.NODE_ENV !== 'production' && !cssrPropsGetter) {
|
||||||
|
console.error(`[naive-ui/mixins/usecssr]: ${options.name}'s style not found`)
|
||||||
|
}
|
||||||
|
const themeVariables = getThemeVariables(naive, renderedTheme)
|
||||||
|
const componentCssrProps = Object.assign(
|
||||||
|
{},
|
||||||
|
cssrPropsGetter.cssrProps(themeVariables),
|
||||||
|
{ theme: renderedTheme, fallbackTheme }
|
||||||
|
)
|
||||||
|
CNode.mount({
|
||||||
|
target: options.name,
|
||||||
|
props: componentCssrProps
|
||||||
|
})
|
||||||
|
markStyleMounted(options.name, renderedTheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function (CNode) {
|
||||||
|
return {
|
||||||
|
beforeMount () {
|
||||||
|
prepareTheme(this, this.syntheticTheme || null, CNode)
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
syntheticTheme (value) {
|
||||||
|
prepareTheme(this, value, CNode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,15 +1,12 @@
|
|||||||
export default function createBaseComponent (component) {
|
export default function createBaseComponent (component) {
|
||||||
let cachedStyleScheme = null
|
let cachedCssrProps = null
|
||||||
return {
|
return {
|
||||||
getDerivedVariables: component.getDerivedVariables,
|
getDerivedVariables: component.getDerivedVariables,
|
||||||
get styleScheme () {
|
cssrProps (themeVariables) {
|
||||||
if (!cachedStyleScheme) {
|
if (!cachedCssrProps) {
|
||||||
cachedStyleScheme = this.createStyleScheme({
|
cachedCssrProps = this.getDerivedVariables(themeVariables)
|
||||||
base: this.theme.base,
|
|
||||||
derived: this.theme.derived
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return cachedStyleScheme
|
return cachedCssrProps
|
||||||
},
|
},
|
||||||
customize (options = {}) {
|
customize (options = {}) {
|
||||||
return Object.assign({}, this.styleScheme, options)
|
return Object.assign({}, this.styleScheme, options)
|
||||||
|
Loading…
Reference in New Issue
Block a user