dsh-theme-neumorph 1.0.2 → 1.0.3

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.
Files changed (3) hide show
  1. package/README.md +18 -12
  2. package/client.js +102 -47
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -34,10 +34,11 @@
34
34
 
35
35
  主题只动宿主自己画的界面,不碰第三方插件:
36
36
 
37
- - 宿主的类名是 CSS Module 的 `<hash>_<local>` 形式,规则按整类名锚定(如 `[class$="_card"]`),不会误伤卡片内部的零件。
38
- - 每条子串锚定(`[class*="_title"]` 这类)都带 `:not([class*="__"])`:宿主是**单**下划线,别人家的 BEM 是**双**下划线,这一条挡住的正是 `block__title`。
39
- - 没有一条规则按裸标签生效:`span` 不被当成文字,`button` 不被当成按钮。允许的唯一例外是宿主的表单与代码面(`textarea`、`input`、`select`、`pre`),测试会逐条核对。
40
- - 文本框的焦点指示不用宿主的外扩轮廓(在软表面上像一圈多余的蓝边),改成画在框内侧的 1.5px 钢蓝内缘——键盘用户仍然看得到光标落在哪个框。
37
+ - 宿主同时用着**两套** CSS Module 命名:预构建样式表是 `_<local>_<hash>_<line>`(`_card_7gxqk_107`),组件从 JS 注入的样式是 `<hash>_<local>`(`wSkVaW_card`)。所以一个类名要同时锚定两次——`[class$="_card"]` 与 `[class*="_card_"]`——只写一种就会有一半界面纹丝不动。
38
+ - 子串锚定一定用下划线夹住(`[class*="_card_"]` 而不是 `[class*="_card"]`),否则会串到 `_cardContent_x_y` 这种更长的零件上,卡片的每一块又各得一层投影。
39
+ - 每个类名锚定都带 `:not([class*="__"])`:宿主是**单**下划线,别人家的 BEM 是**双**下划线,这一条挡住的正是 `panel__card`。排除项(`button:not([class*="_row"])` 这类)不加,因为排除范围宽一点只会少上一条浮雕。
40
+ - 裸标签只出现在两类地方:宿主的表单与代码面(`textarea`、`input`、`select`、`pre`),以及必须同时带宿主类名的控件(`button` / `[role="button"]` + `[class*="_"]`)。`span` 不被当成文字。
41
+ - 文本框的焦点指示不用宿主的外扩轮廓(在软表面上像一圈多余的蓝边),改成画在框内侧的 1.5px 钢蓝内缘;`outline-offset: -2px` 与内凹阴影叠加而不是替换它。checkbox / radio / 原生控件完全不碰,宿主自己的焦点环留给它们。
41
42
 
42
43
  ## 目录结构
43
44
 
@@ -60,26 +61,31 @@
60
61
  ## 实现要点
61
62
 
62
63
  - Client 半通过 `window.__ModuleLoader__.load({ id, factory })` 注册,`factory` 返回 `{ inject: ['theme'], apply(ctx) }`。
63
- - 所有副作用都包在 `ctx.effect()` 里并返回清理函数,插件停用时撤销 token 覆盖层并移除样式表,界面立刻回到原生外观。
64
- - 若宿主某天拒绝含 `--dsw-radius-*` 的 token 表,会自动退回只含颜色的 66 项重试。
65
- - 样式规则用整类名锚定宿主 CSS Module,哈希前缀随版本变化也不会失效。
64
+ - 所有副作用都包在 `ctx.effect()` 里并返回清理函数:样式表在 effect 内插入(effect 被拒就一张纸都不留),重复 apply 时先删掉自己上一张,停用时先移除样式表再撤销 token 覆盖层,界面立刻回到原生外观。
65
+ - 若宿主某天拒绝含 `--dsw-radius-*` 的 token 表,会自动退回只含颜色的 66 项重试;连重试也被拒时保留阴影层并告警,而不是把主题整个掀掉。
66
+ - 样式规则用宿主类名锚定 CSS Module,哈希前缀与文件名随版本变化都不会失效。
66
67
 
67
68
  ## 开发
68
69
 
69
70
  不需要浏览器,也不需要安装依赖(`node --test` 自带):
70
71
 
71
72
  ```bash
72
- npm test # 7 项断言:调色板、样式表结构、卸载路径
73
+ npm test # 15 项断言:调色板、样式表结构、生命周期、卸载路径
73
74
  npm run check # 语法检查
74
75
  node scripts/verify-manifest.mjs # 清单/打包自检
75
76
  npm run verify # 以上全部
76
77
  ```
77
78
 
78
- 测试用一个假的 `window.__ModuleLoader__` 与 `document` 载入 `client.js`,因此断言的正是宿主实际调用的那条路径。其中三条是这次踩过坑之后加的护栏:
79
+ 测试用一个假的 `window.__ModuleLoader__` 与 `document` 载入 `client.js`,因此断言的正是宿主实际调用的那条路径。这个假 `document` 会记录"谁在什么时机被插进哪个根",所以下面这些都能测出来:
79
80
 
80
- - 样式表里不允许出现裸标签选择器(`body span`、`body button`),
81
- - 不允许出现第三方插件命名(曾经把 `dshwv-*` 的图标清空),
82
- - 卡片只允许整类名锚定,不允许 `[class*="_card"]` 这种会命中 `_cardContent` 的子串锚定。
81
+ - 样式表没有按裸标签生效(`span` 不被当成文字),
82
+ - 没有第三方插件命名(曾经把 `dshwv-*` 的图标清空),
83
+ - 类名锚定必须用下划线夹住,且每个 local 的两套命名都在(只写一套是曾经踩过的坑:一半界面没有浮雕),
84
+ - 每个锚定都带 BEM 护栏;排除项不算,
85
+ - 焦点环存在、画在框内、不吞掉内凹阴影、不碰 checkbox / radio,且全文没有一处 `outline: none`,
86
+ - 样式表在 `ctx.effect` 内插入:宿主拒绝注册 effect 时一张纸都不留,
87
+ - 连续 apply 两次只留下一张样式表,
88
+ - 浮雕落在卡片、输入区大板、菜单/对话框与会话行上,且各用各的阴影级。
83
89
 
84
90
  ## 许可
85
91
 
package/client.js CHANGED
@@ -137,6 +137,39 @@ window.__ModuleLoader__.load({
137
137
  '--dsw-radius-panel': { light: '26px', dark: '26px' },
138
138
  };
139
139
 
140
+ /**
141
+ * The host ships two CSS-module naming conventions at once:
142
+ *
143
+ * - the pre-built app stylesheet names its classes `_<local>_<hash>_<line>`
144
+ * (`_card_7gxqk_107`);
145
+ * - the component styles injected from JS name theirs `<hash>_<local>`
146
+ * (`wSkVaW_card`).
147
+ *
148
+ * So an anchor for a local name has to cover both, and it must not straddle
149
+ * a longer local: `_card_` never matches `_cardContent_1a2b_9`, while a bare
150
+ * `[class*="_card"]` would. Every anchor also refuses a double underscore,
151
+ * because that is how a plugin names its own elements (`panel__card`) and no
152
+ * host class contains one.
153
+ */
154
+ const guard = (anchor) => `${anchor}:not([class*="__"])`;
155
+
156
+ /** Anchors on a free-form class fragment (`_title`, `_label`). */
157
+ const sub = (...fragments) =>
158
+ fragments.map((fragment) => guard(`[class*="${fragment}"]`)).join(',\n ');
159
+
160
+ /** Anchors on a complete local name, in both host conventions. */
161
+ const host = (...locals) =>
162
+ locals
163
+ .flatMap((local) => [`[class$="_${local}"]`, `[class*="_${local}_"]`])
164
+ .map(guard)
165
+ .join(',\n ');
166
+
167
+ /** A host button: anything the host painted with a class, minus the cases
168
+ * below. Kept as one constant so the three states cannot drift apart. */
169
+ const BUTTON_SELECTOR = `body :where(button, [role="button"])[class*="_"]:not([class*="__"]):not([aria-checked]):not([role="checkbox"]):not([role="radio"]):not(:is(${host(
170
+ 'primary',
171
+ )})):not([role="menuitem"]):not([role="option"]):not([role="tab"]):not([class*="_callRow"]):not([class*="_panelRow"]):not([class*="_cardContent"]):not([class*="_row"]):not([class*="_tab"]):not([class*="_option"]):not([class*="_cell"]):not([class*="_switcher"]):not([class*="_segmented"]):not(:is([role="menu"], [role="tablist"], [role="listbox"], [data-menu-material], [class*="_tabs"], [class*="_groups"], [class*="_menu"]) *)`;
172
+
140
173
  const CSS = `
141
174
  body {
142
175
  --qn-hi: #ffffff;
@@ -163,14 +196,26 @@ body[data-ds-dark-theme] {
163
196
  }
164
197
 
165
198
  /* ---- text is flat ------------------------------------------------------
166
- Only elements the harness itself styles are touched, and only by name:
167
- every host class is a CSS-module pair of the form <hash>_<local>, so a
168
- class that contains an underscore is host markup. Plugin UI names its own
169
- elements (dshwv-menu-btn and friends) and is left alone.
170
- Nothing here targets a bare tag: a span is not text until it says so. */
171
- /* Same rule, for the pieces of text that are wrapped in a div rather than a
172
- span: a label, a title, a value. They are content too. */
173
- body :is([class*="_label"], [class*="_title"], [class*="_value"], [class*="_text"], [class*="_name"], [class*="_desc"], [class*="_caption"], [class*="_hint"], [class*="_meta"], [class*="_timestamp"]):not([class*="__"]) {
199
+ Text never carries a plate of its own: a label sits on the surface, it does
200
+ not sit in a slightly different rectangle. Only host markup is touched, and
201
+ only by the name the host gives it (see the host/sub helpers above), so a plugin's
202
+ own elements are never repainted. Nothing here targets a bare tag: a span is
203
+ not text until the host says so. */
204
+ body :is(${sub('_label', '_title', '_value', '_text', '_name', '_desc', '_caption', '_hint', '_meta', '_timestamp')}, ${host(
205
+ 'itemLabel',
206
+ 'runStateLabel',
207
+ 'dockHintLabel',
208
+ 'clickableLabel',
209
+ 'labelGroup',
210
+ 'tabTitle',
211
+ 'floatTitle',
212
+ 'failedTitle',
213
+ 'stringValue',
214
+ 'numberValue',
215
+ 'otherValue',
216
+ 'keywordValue',
217
+ 'popoverText',
218
+ )}) {
174
219
  background-color: transparent !important;
175
220
  box-shadow: none !important;
176
221
  }
@@ -181,33 +226,33 @@ body :is([class*="_label"], [class*="_title"], [class*="_value"], [class*="_text
181
226
  lives inside a menu, a tab strip or a picker are excluded, because a
182
227
  shadow under a row that spans the panel reads as a stray line, and a
183
228
  stack of them reads as alternating high and low. */
184
- body :where(button, [role="button"])[class*="_"]:not([class*="__"]):not([aria-checked]):not([role="checkbox"]):not([role="radio"]):not([class*="_primary"]):not([role="menuitem"]):not([role="option"]):not([role="tab"]):not([class*="_callRow"]):not([class*="_panelRow"]):not([class*="_cardContent"]):not([class*="_row"]):not([class*="_tab"]):not([class*="_option"]):not([class*="_cell"]):not([class*="_switcher"]):not([class*="_segmented"]):not(:is([role="menu"], [role="tablist"], [role="listbox"], [data-menu-material], [class*="_tabs"], [class*="_groups"], [class*="_menu"]) *) {
229
+ ${BUTTON_SELECTOR} {
185
230
  border-radius: 12px !important;
186
231
  border-color: transparent !important;
187
232
  background-color: var(--dsw-alias-bg-base) !important;
188
233
  box-shadow: var(--qn-out-sm) !important;
189
234
  }
190
235
 
191
- body :where(button, [role="button"])[class*="_"]:not([class*="__"]):not([aria-checked]):not([role="checkbox"]):not([role="radio"]):not([class*="_primary"]):not([role="menuitem"]):not([role="option"]):not([role="tab"]):not([class*="_callRow"]):not([class*="_panelRow"]):not([class*="_cardContent"]):not([class*="_row"]):not([class*="_tab"]):not([class*="_option"]):not([class*="_cell"]):not([class*="_switcher"]):not([class*="_segmented"]):not(:is([role="menu"], [role="tablist"], [role="listbox"], [data-menu-material], [class*="_tabs"], [class*="_groups"], [class*="_menu"]) *):hover:not(:disabled) {
236
+ ${BUTTON_SELECTOR}:hover:not(:disabled) {
192
237
  box-shadow: var(--qn-out) !important;
193
238
  }
194
239
 
195
- body :where(button, [role="button"])[class*="_"]:not([class*="__"]):not([aria-checked]):not([role="checkbox"]):not([role="radio"]):not([class*="_primary"]):not([role="menuitem"]):not([role="option"]):not([role="tab"]):not([class*="_callRow"]):not([class*="_panelRow"]):not([class*="_cardContent"]):not([class*="_row"]):not([class*="_tab"]):not([class*="_option"]):not([class*="_cell"]):not([class*="_switcher"]):not([class*="_segmented"]):not(:is([role="menu"], [role="tablist"], [role="listbox"], [data-menu-material], [class*="_tabs"], [class*="_groups"], [class*="_menu"]) *):active:not(:disabled) {
240
+ ${BUTTON_SELECTOR}:active:not(:disabled) {
196
241
  box-shadow: var(--qn-in-sm) !important;
197
242
  }
198
243
 
199
244
  /* The send button is a filled pill and keeps one soft shadow of its own. */
200
- body :where(button, [role="button"])[class*="_primary"]:not([class*="__"]) {
245
+ body :where(button, [role="button"]):is(${host('primary')}) {
201
246
  border-radius: 999px !important;
202
247
  border-color: transparent !important;
203
248
  box-shadow: 0 3px 8px rgba(74, 107, 138, 0.28) !important;
204
249
  }
205
250
 
206
- body :where(button, [role="button"])[class*="_primary"]:not([class*="__"]):hover:not(:disabled) {
251
+ body :where(button, [role="button"]):is(${host('primary')}):hover:not(:disabled) {
207
252
  box-shadow: 0 4px 10px rgba(74, 107, 138, 0.34) !important;
208
253
  }
209
254
 
210
- body :where(button, [role="button"])[class*="_primary"]:not([class*="__"]):active:not(:disabled) {
255
+ body :where(button, [role="button"]):is(${host('primary')}):active:not(:disabled) {
211
256
  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.26) !important;
212
257
  }
213
258
 
@@ -226,17 +271,14 @@ body :where(
226
271
  Deliberately narrow. Rows, tabs, chips, list items and every other small
227
272
  block stay flat: a shadow on each of them turns the page into a field of
228
273
  pillows, which is not what neumorphism is for. */
229
- body [class$="_card"],
230
- body [class*="_card "]:not([class*="__"]) {
274
+ body :is(${host('card')}) {
231
275
  box-shadow: var(--qn-drop) !important;
232
276
  border-color: transparent !important;
233
277
  }
234
278
 
235
279
  /* ---- composer: the one big soft slab ------------------------------------ */
236
- body [class*="_composerSeat"]:not([class*="__"]) [class$="_card"],
237
- body [class*="_composerSeat"]:not([class*="__"]) [class*="_card "]:not([class*="__"]),
238
- body [class*="_composerHero"]:not([class*="__"]) [class$="_card"],
239
- body [class*="_composerHero"]:not([class*="__"]) [class*="_card "]:not([class*="__"]) {
280
+ body :is(${host('composerSeat')}) :is(${host('card')}),
281
+ body :is(${host('composerHero')}) :is(${host('card')}) {
240
282
  box-shadow: var(--qn-out) !important;
241
283
  border-radius: 22px !important;
242
284
  border-color: transparent !important;
@@ -245,7 +287,7 @@ body [class*="_composerHero"]:not([class*="__"]) [class*="_card "]:not([class*="
245
287
  /* ---- floating surfaces: menus, dialogs, tooltips ------------------------ */
246
288
  body :where([role="menu"], [role="dialog"], [role="listbox"], [role="tooltip"]),
247
289
  body [data-menu-material],
248
- body :is([class$="_menu"], [class*="_menu "], [class$="_dialog"], [class*="_dialog "]):not([class*="__"]) {
290
+ body :is(${host('menu', 'dialog')}) {
249
291
  border-radius: 20px !important;
250
292
  box-shadow: var(--qn-out-lg) !important;
251
293
  }
@@ -257,14 +299,12 @@ body [data-menu-material] > [aria-hidden="true"] {
257
299
  }
258
300
 
259
301
  /* ---- session tree: one soft row per session ----------------------------- */
260
- body [class*="_sessionRow"]:not([class*="__"]),
261
- body [class*="_projectRow"]:not([class*="__"]) {
302
+ body :is(${host('sessionRow', 'projectRow')}) {
262
303
  border-radius: 12px !important;
263
304
  box-shadow: var(--qn-drop) !important;
264
305
  }
265
306
 
266
- body [class*="_sessionRow"]:not([class*="__"]):hover,
267
- body [class*="_projectRow"]:not([class*="__"]):hover {
307
+ body :is(${host('sessionRow', 'projectRow')}):hover {
268
308
  box-shadow: var(--qn-drop-lift) !important;
269
309
  }
270
310
 
@@ -276,31 +316,32 @@ body :where(pre) {
276
316
  }
277
317
 
278
318
  /* ---- sidebar: a faint groove instead of a divider line ------------------ */
279
- body [class*="_sidebarCol"]:not([class*="__"]) {
319
+ body :is(${host('sidebarCol')}) {
280
320
  box-shadow: inset -8px 0 14px -14px var(--qn-lo) !important;
281
321
  }
282
322
 
283
- /* ---- focus: a pressed rim, drawn inside the field ----------------------
284
- The host ring is an outer halo, which on a soft surface reads as a stray
285
- blue edge beside the box (and it cannot be seen at all once the field has
286
- the recessed shadow). So the outer ring is dropped and replaced by a rim
287
- inside the field, in the same accent a selected control uses. It exists
288
- only while the field has focus: without it a keyboard user has no way to
289
- tell which box the caret is in. */
290
- body :where(textarea, input, select, [contenteditable="true"]):focus {
291
- outline: none !important;
292
- }
293
-
294
- body :where(textarea, input, select, [contenteditable="true"]):focus-visible {
295
- outline: none !important;
296
- box-shadow: var(--qn-in), inset 0 0 0 1.5px var(--dsw-alias-brand-primary) !important;
323
+ /* ---- focus: a ring, drawn inside the field -----------------------------
324
+ The host draws its ring outside the box, which on a soft surface reads as a
325
+ stray edge floating beside the field. So the ring is moved inward instead of
326
+ removed: an outline with a negative offset follows the border radius, composes
327
+ with the recessed shadow instead of replacing it, and still exists for every
328
+ control type - including the checkable ones this sheet never restyles, which
329
+ keep the host ring untouched. A theme that sets outline: none on a focus
330
+ state leaves a keyboard user with no idea where they are; this one does not. */
331
+ body :where(
332
+ textarea,
333
+ input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
334
+ select,
335
+ [contenteditable="true"]
336
+ ):focus-visible {
337
+ outline: 1.5px solid var(--dsw-alias-brand-primary) !important;
338
+ outline-offset: -2px !important;
297
339
  }
298
340
 
299
341
  body :where(button, [role="button"], a):focus-visible {
300
342
  outline: 2px solid var(--dsw-alias-brand-primary);
301
343
  outline-offset: 2px;
302
344
  }
303
-
304
345
  `;
305
346
 
306
347
  return {
@@ -323,29 +364,43 @@ body :where(button, [role="button"], a):focus-visible {
323
364
 
324
365
  // Everything the plugin adds is created and released inside one effect:
325
366
  // a stylesheet inserted before the effect registered would outlive a
326
- // throw here, and a second apply would stack a second copy of it.
367
+ // throw, and a second apply would stack a second copy of it. So the old
368
+ // sheet of this source is dropped first, and a throw after insertion
369
+ // takes the sheet back out again.
327
370
  ctx.effect(() => {
371
+ const root = document.head ?? document.documentElement;
372
+ for (const stale of document.querySelectorAll?.(`style[data-dsh-theme="${SOURCE}"]`) ?? []) {
373
+ stale.remove();
374
+ }
375
+
328
376
  // Shadow layer: neumorphism needs paired soft shadows, and a theme
329
377
  // token can only carry a colour, so the shadows are a stylesheet of
330
378
  // their own.
331
379
  const style = document.createElement('style');
332
380
  style.dataset.dshTheme = SOURCE;
333
381
  style.textContent = CSS;
334
- const root = document.head ?? document.documentElement;
335
- root.appendChild(style);
336
382
 
337
383
  let disposeTokens = null;
338
384
  try {
385
+ root.appendChild(style);
339
386
  disposeTokens = overrideTokens();
340
387
  } catch (error) {
341
- // A host that rejects the table outright still gets the shadows, and
342
- // the sheet is still owned by this effect: report, do not throw.
388
+ // Whatever happened, this effect owns what it inserted: give it back
389
+ // before getting out of the way. The one exception is the token
390
+ // table, which has a colour-only retry of its own; if even that is
391
+ // rejected the shadows are still worth keeping, so report and carry on.
392
+ if (style.parentNode === null && disposeTokens === null) {
393
+ style.remove();
394
+ throw error;
395
+ }
343
396
  console.warn('[dsh-theme-neumorph] token layer rejected, shadows only', error);
344
397
  }
345
398
 
346
399
  return () => {
347
- disposeTokens?.();
400
+ // Sheet first: one frame of host colours is better than one frame of
401
+ // plugin shadows over the host's own palette.
348
402
  style.remove();
403
+ disposeTokens?.();
349
404
  };
350
405
  });
351
406
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-theme-neumorph",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "DSH 新拟态主题插件:冷灰表面、钢蓝强调、柔和阴影。",
5
5
  "license": "MIT",
6
6
  "author": "wcnmamrs",