mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2024-12-27 05:00:48 +08:00
refactor: prepare for new theme system
This commit is contained in:
parent
137aff1076
commit
0f060fd125
@ -1,25 +1,9 @@
|
|||||||
import { read, createHoverColor, createActiveColor } from './_utils/color'
|
function mergeStyleSchemes (baseSchemes, schemes) {
|
||||||
|
|
||||||
function extendScheme (scheme) {
|
|
||||||
const extendedScheme = Object.assign({}, scheme)
|
|
||||||
if (extendedScheme.primaryColor) {
|
|
||||||
const primaryColorRgb = read(extendedScheme.primaryColor)
|
|
||||||
if (!extendedScheme.primaryHoverColor) {
|
|
||||||
extendedScheme.primaryHoverColor = createHoverColor(primaryColorRgb)
|
|
||||||
}
|
|
||||||
if (!extendedScheme.primaryActiveColor) {
|
|
||||||
extendedScheme.primaryActiveColor = createActiveColor(primaryColorRgb)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return extendedScheme
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeStyleSchemes (baseSchemes, schemes, extend) {
|
|
||||||
const mergedSchemes = {}
|
const mergedSchemes = {}
|
||||||
Object.keys(baseSchemes).forEach(theme => {
|
Object.keys(baseSchemes).forEach(theme => {
|
||||||
|
const baseScheme = baseSchemes[theme]
|
||||||
const scheme = (schemes || {})[theme]
|
const scheme = (schemes || {})[theme]
|
||||||
const extendedScheme = extend ? extendScheme(scheme) : scheme
|
mergedSchemes[theme] = baseScheme.customize(scheme)
|
||||||
mergedSchemes[theme] = Object.assign({}, baseSchemes[theme], extendedScheme)
|
|
||||||
})
|
})
|
||||||
return mergedSchemes
|
return mergedSchemes
|
||||||
}
|
}
|
||||||
@ -52,9 +36,9 @@ function create ({
|
|||||||
fallbackTheme: fallbackTheme || 'light',
|
fallbackTheme: fallbackTheme || 'light',
|
||||||
setHljs,
|
setHljs,
|
||||||
setHighlightjs: setHljs,
|
setHighlightjs: setHljs,
|
||||||
setStyleSchemes: (schemes, extend = true) => {
|
setStyleSchemes: (schemes) => {
|
||||||
naive.styleSchemes = mergeStyleSchemes(
|
naive.styleSchemes = mergeStyleSchemes(
|
||||||
naive.styleSchemes, schemes, extend
|
naive.styleSchemes, schemes
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
install
|
install
|
||||||
|
9
src/styles/dark/Button.js
Normal file
9
src/styles/dark/Button.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import create from '../utils/create-component-base'
|
||||||
|
|
||||||
|
export default create({
|
||||||
|
getDerivedVariables ({ base, derived }) {
|
||||||
|
return {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
15
src/styles/dark/base.js
Normal file
15
src/styles/dark/base.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import create from '../utils/create-theme-base'
|
||||||
|
|
||||||
|
export default create({
|
||||||
|
base: {
|
||||||
|
baseBackgroundColor: 'rgb(255, 255, 255)'
|
||||||
|
},
|
||||||
|
getDerivedVariables (base) {
|
||||||
|
const derived = {
|
||||||
|
primaryColor: base.primaryDefault,
|
||||||
|
primaryHoverColor: base.primaryHover,
|
||||||
|
primaryActiveColor: base.primaryActive
|
||||||
|
}
|
||||||
|
return derived
|
||||||
|
}
|
||||||
|
})
|
9
src/styles/light/Button.js
Normal file
9
src/styles/light/Button.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import create from '../utils/create-component-base'
|
||||||
|
|
||||||
|
export default create({
|
||||||
|
getDerivedVariables ({ base, derived }) {
|
||||||
|
return {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
15
src/styles/light/base.js
Normal file
15
src/styles/light/base.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import create from '../utils/create-theme-base'
|
||||||
|
|
||||||
|
export default create({
|
||||||
|
base: {
|
||||||
|
baseBackgroundColor: 'rgb(255, 255, 255)'
|
||||||
|
},
|
||||||
|
getDerivedVariables (base) {
|
||||||
|
const derived = {
|
||||||
|
primaryColor: base.primaryDefault,
|
||||||
|
primaryHoverColor: base.primaryHover,
|
||||||
|
primaryActiveColor: base.primaryActive
|
||||||
|
}
|
||||||
|
return derived
|
||||||
|
}
|
||||||
|
})
|
18
src/styles/utils/create-component-base.js
Normal file
18
src/styles/utils/create-component-base.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export default function createBaseComponent (component) {
|
||||||
|
let cachedStyleScheme = null
|
||||||
|
return {
|
||||||
|
getDerivedVariables: component.getDerivedVariables,
|
||||||
|
get styleScheme () {
|
||||||
|
if (!cachedStyleScheme) {
|
||||||
|
cachedStyleScheme = this.createStyleScheme({
|
||||||
|
base: this.theme.base,
|
||||||
|
derived: this.theme.derived
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return cachedStyleScheme
|
||||||
|
},
|
||||||
|
customize (options = {}) {
|
||||||
|
return Object.assign({}, this.styleScheme, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
src/styles/utils/create-theme-base.js
Normal file
30
src/styles/utils/create-theme-base.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
export default function createThemeBase (theme) {
|
||||||
|
function customize (options = {}) {
|
||||||
|
const {
|
||||||
|
base = {},
|
||||||
|
derived = {}
|
||||||
|
} = options
|
||||||
|
const syntheticBase = Object.assign({}, this.base, base)
|
||||||
|
const syntheticDerived = Object.assign(
|
||||||
|
{},
|
||||||
|
this.createDerivedVariables(syntheticBase),
|
||||||
|
derived
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
base: syntheticBase,
|
||||||
|
derived: syntheticDerived
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let cachedDerived = null
|
||||||
|
return {
|
||||||
|
base: theme.base,
|
||||||
|
getDerivedVariables: theme.getDerivedVariables,
|
||||||
|
get derived () {
|
||||||
|
if (!cachedDerived) {
|
||||||
|
cachedDerived = this.getDerivedVariables(this.base)
|
||||||
|
}
|
||||||
|
return cachedDerived
|
||||||
|
},
|
||||||
|
customize
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user