web-appearance-kit 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ko.md +151 -0
- package/README.md +155 -2
- package/dist/appearance-kit.cjs +1337 -0
- package/dist/appearance-kit.cjs.map +1 -0
- package/dist/appearance-kit.min.cjs +2 -0
- package/dist/appearance-kit.min.cjs.map +1 -0
- package/dist/appearance-kit.min.mjs +2 -0
- package/dist/appearance-kit.min.mjs.map +1 -0
- package/dist/appearance-kit.mjs +1333 -0
- package/dist/appearance-kit.mjs.map +1 -0
- package/dist/appearance-kit.umd.js +1343 -0
- package/dist/appearance-kit.umd.js.map +1 -0
- package/dist/appearance-kit.umd.min.js +2 -0
- package/dist/appearance-kit.umd.min.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/package.json +64 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Park Jungyoung
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ko.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
*[English](./README.md) · 한국어*
|
|
6
|
+
|
|
7
|
+
# web-appearance-kit
|
|
8
|
+
|
|
9
|
+
작은 TypeScript 컬러 스킴 감지기 — `prefers-color-scheme`에서 사용자의 **라이트 / 다크**
|
|
10
|
+
외관을 해석하고, 미디어 쿼리는 그대로 둔 채 페이지 색을 바꾸는 **삼성 인터넷의 강제 다크
|
|
11
|
+
모드**까지 감지하며, 바뀔 때마다 구독자에게 알립니다.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install web-appearance-kit
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API 한눈에 보기
|
|
18
|
+
|
|
19
|
+
`AppearanceKit`은 싱글톤입니다.
|
|
20
|
+
|
|
21
|
+
| 멤버 | 타입 | 설명 |
|
|
22
|
+
| --- | --- | --- |
|
|
23
|
+
| `AppearanceKit.version` | `string` | 설치된 패키지 버전 |
|
|
24
|
+
| `AppearanceKit.supported` | `boolean` | 이 환경에서 외관을 판별할 수 있는지 (`value`가 `'unknown'`이 아닌지) |
|
|
25
|
+
| `AppearanceKit.value` | `Appearances` | 현재 외관 — `'light'`, `'dark'`, `'unknown'` |
|
|
26
|
+
| `AppearanceKit.subscribe(callback)` | `AppearanceUnsubscribe` | 현재 외관으로 `callback`을 즉시 호출하고, 이후 바뀔 때마다 호출 |
|
|
27
|
+
|
|
28
|
+
`Appearances`는 문자열 유니온 `'unknown' | 'light' | 'dark'`입니다.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## ESM
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import AppearanceKit from 'web-appearance-kit'
|
|
36
|
+
|
|
37
|
+
console.log(AppearanceKit.value) // "dark"
|
|
38
|
+
console.log(AppearanceKit.supported) // true
|
|
39
|
+
|
|
40
|
+
const unsubscribe = AppearanceKit.subscribe((appearance) => {
|
|
41
|
+
document.documentElement.setAttribute('data-theme', appearance === 'dark' ? 'dark' : 'light')
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## CommonJS
|
|
46
|
+
|
|
47
|
+
번들은 `exports: "named"`로 빌드되므로 싱글톤은 `.default` 아래에 있습니다.
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const { default: AppearanceKit } = require('web-appearance-kit')
|
|
51
|
+
|
|
52
|
+
console.log(AppearanceKit.value) // Node에서는 "unknown" — 물어볼 화면이 없습니다
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## UMD (브라우저 `<script>`)
|
|
56
|
+
|
|
57
|
+
전역 `AppearanceKit`은 네임스페이스 객체이고, 싱글톤은 `AppearanceKit.default`입니다.
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<script src="https://unpkg.com/web-appearance-kit/dist/appearance-kit.umd.min.js"></script>
|
|
61
|
+
<script>
|
|
62
|
+
var appearance = window.AppearanceKit.default
|
|
63
|
+
|
|
64
|
+
appearance.subscribe(function (value) {
|
|
65
|
+
document.documentElement.className = value === 'dark' ? 'theme-dark' : 'theme-light'
|
|
66
|
+
})
|
|
67
|
+
</script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## TypeScript
|
|
71
|
+
|
|
72
|
+
값 타입은 `Appearances`, 인스턴스 형태는 `AppearanceKitInstance`, `subscribe`의 콜백과
|
|
73
|
+
반환 타입은 `AppearanceSubscriber`, `AppearanceUnsubscribe`로 export됩니다.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import AppearanceKit, {
|
|
77
|
+
type Appearances,
|
|
78
|
+
type AppearanceKitInstance,
|
|
79
|
+
type AppearanceSubscriber,
|
|
80
|
+
type AppearanceUnsubscribe,
|
|
81
|
+
} from 'web-appearance-kit'
|
|
82
|
+
|
|
83
|
+
function themeOf(appearance: Appearances): 'light' | 'dark' {
|
|
84
|
+
return appearance === 'dark' ? 'dark' : 'light'
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const onChange: AppearanceSubscriber = (appearance) => render(themeOf(appearance))
|
|
88
|
+
const off: AppearanceUnsubscribe = AppearanceKit.subscribe(onChange)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 변경 감지
|
|
92
|
+
|
|
93
|
+
`subscribe`는 외관을 지켜보다가 바뀔 때마다 콜백을 호출합니다. **unsubscribe** 함수를
|
|
94
|
+
반환하며, 호출하면 구독이 끝납니다.
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import AppearanceKit from 'web-appearance-kit'
|
|
98
|
+
|
|
99
|
+
// 현재 외관으로 즉시 한 번 호출되고, 이후 바뀔 때마다 다시 호출됩니다.
|
|
100
|
+
const unsubscribe = AppearanceKit.subscribe((appearance) => {
|
|
101
|
+
console.log('현재 외관:', appearance)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// 구독 해제
|
|
105
|
+
unsubscribe()
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
CommonJS / UMD에서는 싱글톤의 메서드로 씁니다:
|
|
109
|
+
`require('web-appearance-kit').default.subscribe(...)` /
|
|
110
|
+
`window.AppearanceKit.default.subscribe(...)`.
|
|
111
|
+
|
|
112
|
+
### 동기화 방식
|
|
113
|
+
|
|
114
|
+
- 콜백은 **구독 시 한 번**, 현재 외관으로 동기 호출되고, 이후 바뀔 때마다 호출됩니다.
|
|
115
|
+
같은 값이 반복되면 중복 제거됩니다.
|
|
116
|
+
- 모든 구독은 `(prefers-color-scheme: dark)` 미디어 쿼리의 `change` 리스너 **하나**를
|
|
117
|
+
공유합니다. 첫 구독자와 함께 붙고 마지막 구독자와 함께 떨어집니다. `MediaQueryList`가
|
|
118
|
+
`EventTarget`이 아닌 환경(Safari < 14, IE 10–11)에서는
|
|
119
|
+
[web-event-kit](https://github.com/pjy0509/web-event-kit)이 레거시 `addListener` /
|
|
120
|
+
`removeListener`로 대신 처리합니다.
|
|
121
|
+
- **삼성 인터넷의 강제 다크 모드는 이벤트를 발생시키지 않으므로**, 그곳에서는 구독자가 있는
|
|
122
|
+
동안 엔진을 **2초**마다 폴링합니다.
|
|
123
|
+
- 한 콜백이 던진 예외는 다른 콜백을 막지 않습니다 — 콘솔에 드러나도록 비동기로 다시
|
|
124
|
+
던집니다.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## 참고
|
|
129
|
+
|
|
130
|
+
- **`value`는 접근할 때마다 새로 읽습니다.** 읽기 사이에 캐시하지 않으므로 구독자가 없어도
|
|
131
|
+
항상 최신 값입니다.
|
|
132
|
+
- **선호 없음은 `'light'`로 읽힙니다.** `prefers-color-scheme`을 이해하지만 선호가 설정되지
|
|
133
|
+
않은 브라우저(`no-preference`)는 플랫폼이 실제로 그리는 대로 `'light'`가 됩니다.
|
|
134
|
+
`'unknown'`은 아예 답할 수 없는 환경을 위해 남겨 둡니다.
|
|
135
|
+
- **삼성 인터넷은 질의하지 않고 측정합니다.** 강제 다크 모드는 미디어 쿼리 값과 상관없이
|
|
136
|
+
페이지를 어둡게 하므로, 삼성 인터넷에서는 1×1 흰색 SVG를 캔버스에 그리고 픽셀을 다시
|
|
137
|
+
읽습니다 — 순백이 아니면 엔진이 페이지를 어둡게 하고 있다는 뜻입니다. 이 이미지는
|
|
138
|
+
비동기로 디코딩되므로 처음 사용한 직후 잠깐은 미디어 쿼리가 대신 답하고, 측정이 준비되는
|
|
139
|
+
즉시 구독자에게 알립니다. 캔버스를 읽을 수 없으면 계속 미디어 쿼리가 답합니다.
|
|
140
|
+
- **서버 사이드 렌더링 중 import해도 안전합니다.** 미디어 쿼리와 측정용 이미지는 import
|
|
141
|
+
시점이 아니라 처음 사용할 때 만들어집니다. Node에서 `value`는 `'unknown'`이고,
|
|
142
|
+
`subscribe`는 `'unknown'`으로 한 번 호출한 뒤 다시 호출하지 않습니다.
|
|
143
|
+
- **싱글톤은 동결(freeze)되어 있습니다.** 어떤 멤버에 대입해도 무시됩니다(strict
|
|
144
|
+
모드에서는 예외).
|
|
145
|
+
- **CJS/UMD의 `.default`**는 default export와 named export를 함께 유지한 결과입니다.
|
|
146
|
+
|
|
147
|
+
## 브라우저 지원
|
|
148
|
+
|
|
149
|
+
**IE 9**까지 동작합니다. 외관을 읽으려면 `prefers-color-scheme`(Chrome 76, Safari 12.1,
|
|
150
|
+
Firefox 67, Samsung Internet 12)이 필요하며, 그보다 오래된 엔진과 모든 IE는
|
|
151
|
+
`'unknown'`을 반환합니다.
|
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+

|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
*English · [한국어](./README.ko.md)*
|
|
6
|
+
|
|
7
|
+
# web-appearance-kit
|
|
8
|
+
|
|
9
|
+
A tiny TypeScript color-scheme detector — resolves the user's **light / dark**
|
|
10
|
+
appearance from `prefers-color-scheme`, sees through **Samsung Internet's forced dark
|
|
11
|
+
mode** (which recolours pages without touching the media query), and notifies
|
|
12
|
+
subscribers on every change.
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install web-appearance-kit
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## API at a glance
|
|
19
|
+
|
|
20
|
+
`AppearanceKit` is a singleton.
|
|
21
|
+
|
|
22
|
+
| Member | Type | Description |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| `AppearanceKit.version` | `string` | The installed package version |
|
|
25
|
+
| `AppearanceKit.supported` | `boolean` | Whether the appearance can be determined here (`value` is not `'unknown'`) |
|
|
26
|
+
| `AppearanceKit.value` | `Appearances` | The current appearance — `'light'`, `'dark'`, or `'unknown'` |
|
|
27
|
+
| `AppearanceKit.subscribe(callback)` | `AppearanceUnsubscribe` | Calls `callback` now with the current appearance, then on every change |
|
|
28
|
+
|
|
29
|
+
`Appearances` is the string union `'unknown' | 'light' | 'dark'`.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## ESM
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import AppearanceKit from 'web-appearance-kit'
|
|
37
|
+
|
|
38
|
+
console.log(AppearanceKit.value) // "dark"
|
|
39
|
+
console.log(AppearanceKit.supported) // true
|
|
40
|
+
|
|
41
|
+
const unsubscribe = AppearanceKit.subscribe((appearance) => {
|
|
42
|
+
document.documentElement.setAttribute('data-theme', appearance === 'dark' ? 'dark' : 'light')
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## CommonJS
|
|
47
|
+
|
|
48
|
+
The bundle is built with `exports: "named"`, so the singleton lives under `.default`:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const { default: AppearanceKit } = require('web-appearance-kit')
|
|
52
|
+
|
|
53
|
+
console.log(AppearanceKit.value) // "unknown" under Node — there is no screen to ask
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## UMD (browser `<script>`)
|
|
57
|
+
|
|
58
|
+
The global `AppearanceKit` is a namespace object; the singleton is `AppearanceKit.default`.
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<script src="https://unpkg.com/web-appearance-kit/dist/appearance-kit.umd.min.js"></script>
|
|
62
|
+
<script>
|
|
63
|
+
var appearance = window.AppearanceKit.default
|
|
64
|
+
|
|
65
|
+
appearance.subscribe(function (value) {
|
|
66
|
+
document.documentElement.className = value === 'dark' ? 'theme-dark' : 'theme-light'
|
|
67
|
+
})
|
|
68
|
+
</script>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## TypeScript
|
|
72
|
+
|
|
73
|
+
The value type is exported as `Appearances`, the instance shape as `AppearanceKitInstance`,
|
|
74
|
+
and `subscribe`'s callback and return types as `AppearanceSubscriber` and
|
|
75
|
+
`AppearanceUnsubscribe`.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import AppearanceKit, {
|
|
79
|
+
type Appearances,
|
|
80
|
+
type AppearanceKitInstance,
|
|
81
|
+
type AppearanceSubscriber,
|
|
82
|
+
type AppearanceUnsubscribe,
|
|
83
|
+
} from 'web-appearance-kit'
|
|
84
|
+
|
|
85
|
+
function themeOf(appearance: Appearances): 'light' | 'dark' {
|
|
86
|
+
return appearance === 'dark' ? 'dark' : 'light'
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const onChange: AppearanceSubscriber = (appearance) => render(themeOf(appearance))
|
|
90
|
+
const off: AppearanceUnsubscribe = AppearanceKit.subscribe(onChange)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Observing changes
|
|
94
|
+
|
|
95
|
+
`subscribe` watches the appearance and calls your callback whenever it changes. It
|
|
96
|
+
returns an **unsubscribe** function — call it to stop.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import AppearanceKit from 'web-appearance-kit'
|
|
100
|
+
|
|
101
|
+
// Fires once immediately with the current appearance, then again on every change.
|
|
102
|
+
const unsubscribe = AppearanceKit.subscribe((appearance) => {
|
|
103
|
+
console.log('appearance is now', appearance)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// Stop listening
|
|
107
|
+
unsubscribe()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
In CommonJS / UMD the method lives on the singleton:
|
|
111
|
+
`require('web-appearance-kit').default.subscribe(...)` /
|
|
112
|
+
`window.AppearanceKit.default.subscribe(...)`.
|
|
113
|
+
|
|
114
|
+
### How it stays in sync
|
|
115
|
+
|
|
116
|
+
- The callback is invoked **once on subscribe**, synchronously, with the current
|
|
117
|
+
appearance, then on every change. Repeated identical values are de-duplicated.
|
|
118
|
+
- All subscriptions share **one** `change` listener on the
|
|
119
|
+
`(prefers-color-scheme: dark)` media query, attached with the first subscriber and
|
|
120
|
+
released with the last. Where `MediaQueryList` is not an `EventTarget` (Safari < 14,
|
|
121
|
+
IE 10–11) [web-event-kit](https://github.com/pjy0509/web-event-kit) falls back to the
|
|
122
|
+
legacy `addListener` / `removeListener` pair.
|
|
123
|
+
- **Samsung Internet's forced dark mode fires no event**, so while anyone is subscribed
|
|
124
|
+
there the engine is also polled every **2 seconds**.
|
|
125
|
+
- An exception thrown by one callback does not stop the others — it is re-thrown
|
|
126
|
+
asynchronously so it still surfaces in the console.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Notes
|
|
131
|
+
|
|
132
|
+
- **`value` is read fresh on every access.** Nothing is cached between reads, so it is
|
|
133
|
+
always current even with no subscribers.
|
|
134
|
+
- **No preference reads as `'light'`.** A browser that understands
|
|
135
|
+
`prefers-color-scheme` but has no preference set (`no-preference`) resolves to
|
|
136
|
+
`'light'`, matching how the platform itself renders. `'unknown'` is reserved for
|
|
137
|
+
environments that cannot answer at all.
|
|
138
|
+
- **Samsung Internet is probed, not queried.** Its forced dark mode darkens pages
|
|
139
|
+
regardless of what the media query reports, so on Samsung Internet the kit draws a 1×1
|
|
140
|
+
white SVG to a canvas and reads the pixel back — anything but pure white means the
|
|
141
|
+
engine is darkening the page. The probe image decodes asynchronously, so for a moment
|
|
142
|
+
after first use the media query answers instead; subscribers are notified as soon as the
|
|
143
|
+
probe is ready. If the canvas cannot be read, the media query stays in charge.
|
|
144
|
+
- **Safe to import during server-side rendering.** The media query and the probe are
|
|
145
|
+
created on first use, not at import. Under Node `value` is `'unknown'`, and `subscribe`
|
|
146
|
+
calls back once with `'unknown'` and never again.
|
|
147
|
+
- **The singleton is frozen.** Assigning to any member is a no-op (and throws in strict
|
|
148
|
+
mode).
|
|
149
|
+
- **`.default` in CJS/UMD** is a consequence of keeping both a default and named
|
|
150
|
+
exports.
|
|
151
|
+
|
|
152
|
+
## Browser support
|
|
153
|
+
|
|
154
|
+
Runs down to **IE 9**. Reading the appearance needs `prefers-color-scheme` (Chrome 76,
|
|
155
|
+
Safari 12.1, Firefox 67, Samsung Internet 12); older engines — and every version of IE —
|
|
156
|
+
report `'unknown'`.
|