Skip to content

Commit

Permalink
feat: auto theme
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Nov 3, 2024
1 parent b603234 commit 365b555
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/liriliri/luna#readme",
"dependencies": {
"licia": "^1.43.0"
"licia": "^1.44.0"
},
"devDependencies": {
"@babel/core": "^7.10.5",
Expand Down
38 changes: 31 additions & 7 deletions src/share/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import each from 'licia/each'
import extend from 'licia/extend'
import defaults from 'licia/defaults'
import remove from 'licia/remove'
import theme from 'licia/theme'
import startWith from 'licia/startWith'

interface IOptions {
Expand All @@ -23,11 +24,12 @@ export default class Component<
$container: $.$
private subComponents: Component[] = []
private compName: string
private theme = ''
protected options: Required<Options>
constructor(
container: Element,
{ compName }: IOptions,
{ theme = 'light' }: IComponentOptions = {}
{ theme: t = 'light' }: IComponentOptions = {}
) {
super()
this.compName = compName
Expand All @@ -41,19 +43,22 @@ export default class Component<
this.c(`platform-${getPlatform()}`),
])

this.on('optionChange', (name, val, oldVal) => {
const c = this.c
this.on('optionChange', (name, val) => {
if (name === 'theme') {
this.$container
.rmClass(c(`theme-${oldVal}`))
.addClass(c(`theme-${val}`))
let t = val
if (val === 'auto') {
t = theme.get()
}
this.setTheme(t)
each(this.subComponents, (component) =>
component.setOption('theme', val)
)
}
})

this.setOption('theme', theme)
theme.on('change', this.onThemeChange)

this.setOption('theme', t)
}
destroy() {
this.destroySubComponents()
Expand All @@ -67,6 +72,7 @@ export default class Component<
$container.html('')
this.emit('destroy')
this.removeAllListeners()
theme.off('change', this.onThemeChange)
}
setOption(name: string | Partial<Options>, val?: any) {
const options: any = this.options
Expand All @@ -79,6 +85,9 @@ export default class Component<
each(newOptions, (val, name: string) => {
const oldVal = options[name]
options[name] = val
if (val === oldVal) {
return
}
this.emit('optionChange', name, val, oldVal)
})
}
Expand All @@ -103,4 +112,19 @@ export default class Component<
protected find(selector: string) {
return this.$container.find(this.c(selector))
}
private setTheme(theme: string) {
const { c, $container } = this

if (this.theme) {
$container.rmClass(c(`theme-${this.theme}`))
}

$container.addClass(c(`theme-${theme}`))
this.theme = theme
}
private onThemeChange = (t: string) => {
if (this.options.theme === 'auto') {
this.setTheme(t)
}
}
}
1 change: 1 addition & 0 deletions src/share/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function story(
{
Light: 'light',
Dark: 'dark',
Auto: 'auto',
},
themes
),
Expand Down

0 comments on commit 365b555

Please sign in to comment.