Skip to content

Commit

Permalink
feat: add theme
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Nov 2, 2024
1 parent d533534 commit dce5ab5
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
37 changes: 37 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -13217,6 +13217,43 @@ template('<p><%= util["upperCase"](name) %></p>', {
})({ name: 'licia' }); // -> '<p>LICIA</p>'
```

## theme

Theme helper.

<details>
<summary>Type Definition</summary>

```typescript
namespace theme {
interface ITheme extends Emitter {
get(): string;
}
}
const theme: theme.ITheme;
```

</details>

### on

Bind change event.

### off

Unbind change event.

### get

Get current theme(light or dark).

```javascript
theme.on('change', function(theme) {
console.log(theme); // -> 'dark'
});
theme.get(); // -> 'light'
```

## throttle

Return a new throttled version of the passed function.
Expand Down
37 changes: 37 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13208,6 +13208,43 @@ template('<p><%= util["upperCase"](name) %></p>', {
})({ name: 'licia' }); // -> '<p>LICIA</p>'
```

## theme

主题工具库。

<details>
<summary>类型定义</summary>

```typescript
namespace theme {
interface ITheme extends Emitter {
get(): string;
}
}
const theme: theme.ITheme;
```

</details>

### on

绑定 change 事件。

### off

解绑 change 事件。

### get

获取当前主题(light 或 dark)。

```javascript
theme.on('change', function(theme) {
console.log(theme); // -> 'dark'
});
theme.get(); // -> 'light'
```

## throttle

返回函数的节流阀版本。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
"swap",
"table",
"template",
"theme",
"throttle",
"through",
"tildify",
Expand Down
15 changes: 15 additions & 0 deletions i18n/theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## CN

主题工具库。

### on

绑定 change 事件。

### off

解绑 change 事件。

### get

获取当前主题(light 或 dark)。
13 changes: 13 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -6441,6 +6441,19 @@
"browser"
]
},
"theme": {
"dependencies": [
"Emitter",
"MediaQuery"
],
"description": "Theme helper.",
"env": [
"browser"
],
"test": [
"browser"
]
},
"throttle": {
"dependencies": [
"debounce"
Expand Down
49 changes: 49 additions & 0 deletions src/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Theme helper.
*
* ### on
*
* Bind change event.
*
* ### off
*
* Unbind change event.
*
* ### get
*
* Get current theme(light or dark).
*/

/* example
* theme.on('change', function(theme) {
* console.log(theme); // -> 'dark'
* });
* theme.get(); // -> 'light'
*/

/* module
* env: browser
*/

/* typescript
* export declare namespace theme {
* interface ITheme extends Emitter {
* get(): string;
* }
* }
* export declare const theme: theme.ITheme;
*/

_('Emitter MediaQuery');

const m = new MediaQuery('(prefers-color-scheme: dark)');

exports = {
get() {
return m.isMatch() ? 'dark' : 'light';
}
};

Emitter.mixin(exports);

m.on('match', () => exports.emit('change', 'dark'));
m.on('unmatch', () => exports.emit('change', 'light'));
2 changes: 2 additions & 0 deletions test/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const t = util.isDarkMode() ? 'dark' : 'light';
expect(theme.get()).to.equal(t);

0 comments on commit dce5ab5

Please sign in to comment.