@zeroae/countersign-widget 0.1.0 → 0.2.0

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 +3 -1
  2. package/package.json +1 -1
  3. package/src/widget.js +90 -15
package/README.md CHANGED
@@ -8,4 +8,6 @@ npm install @zeroae/countersign-widget
8
8
 
9
9
  Import `@zeroae/countersign-widget` to register the `<countersign-widget>` element. Set its `adapter` property to connect it to your backend. The service worker entry point exports `installPushHandlers` for Web Push notifications.
10
10
 
11
- [Widget and adapter reference](https://github.com/zeroae/countersign/blob/main/docs/reference/widget.md) · [Demo Integrator](https://github.com/zeroae/countersign/tree/main/examples/demo-integrator)
11
+ The element renders a Countersign card with a purple header band and its own buttons. It follows the page's `color-scheme` and works under a strict Content Security Policy. Restyle it with `--countersign-*` custom properties and shadow parts such as `::part(card)`, `::part(button)` and `::part(primary)`. Add the `paired` attribute once the account has a paired Companion (pairing then becomes a secondary "Pair another computer" link; style it with `::part(link)`), and put page-specific text in the `headline` slot.
12
+
13
+ [Widget and adapter reference](https://zeroae.github.io/countersign/reference/widget/) · [Styling hooks](https://zeroae.github.io/countersign/reference/widget/#style-the-web-controls) · [Demo Integrator](https://github.com/zeroae/countersign/tree/main/examples/demo-integrator)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeroae/countersign-widget",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/widget.js",
package/src/widget.js CHANGED
@@ -1,5 +1,62 @@
1
1
  import { approvalLink, isRequestId } from './hints.js';
2
2
 
3
+ // Default look: a Countersign card with a purple band. Every value is a custom
4
+ // property or a shadow part, so an Integrator can restyle it from the page.
5
+ // light-dark() follows the page's color-scheme; a page that declares none
6
+ // keeps the light palette.
7
+ const STYLE = `
8
+ :host {
9
+ --_band: var(--countersign-band, #2b0548);
10
+ --_band-ink: var(--countersign-band-ink, #ffffff);
11
+ --_surface: var(--countersign-surface, light-dark(#faf8fc, #1d1724));
12
+ --_line: var(--countersign-line, light-dark(#d9cfe2, #3d3149));
13
+ --_ink: var(--countersign-ink, light-dark(#251c2b, #ece4f2));
14
+ --_muted: var(--countersign-muted, light-dark(#54485e, #c3b5cf));
15
+ --_primary: var(--countersign-primary, light-dark(#224805, #a1ca7c));
16
+ --_primary-ink: var(--countersign-primary-ink, light-dark(#ffffff, #14240a));
17
+ --_link: var(--countersign-accent, light-dark(#442063, #c49bdf));
18
+ display: block;
19
+ font-family: var(--countersign-font, system-ui, sans-serif);
20
+ color: var(--_ink);
21
+ }
22
+ [hidden] { display: none !important; }
23
+ [part=card] { border: 1px solid var(--_line); border-radius: 10px; overflow: hidden; background: var(--_surface); }
24
+ [part=header] { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--_band); color: var(--_band-ink); }
25
+ [part=mark] { width: 22px; height: 22px; fill: currentColor; flex: none; }
26
+ .divider { width: 1px; height: 20px; background: currentColor; opacity: .7; }
27
+ [part=wordmark] { font-size: 1.125em; font-weight: 300; letter-spacing: .02em; line-height: 1; }
28
+ [part=body] { display: flex; flex-direction: column; gap: 14px; padding: 18px 16px 20px; }
29
+ [part=headline] { margin: 0; font-size: 1.375em; font-weight: 300; line-height: 1.25; }
30
+ [part=actions] { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
31
+ button, a { font: inherit; font-size: .9375em; }
32
+ button { min-height: 44px; padding: 0 16px; border-radius: 6px; border: 1px solid var(--_line);
33
+ background: var(--_surface); color: var(--_ink); cursor: pointer; }
34
+ button[part~=primary] { background: var(--_primary); border-color: var(--_primary); color: var(--_primary-ink); font-weight: 600; }
35
+ button:disabled { cursor: default; opacity: .55; }
36
+ button[part~=link] { order: 1; min-height: 44px; padding: 0 4px; border: 0; background: none; color: var(--_link); text-decoration: underline; }
37
+ a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 4px; color: var(--_link); }
38
+ button:focus-visible, a:focus-visible { outline: 3px solid var(--_link); outline-offset: 2px; }
39
+ [part=status] { margin: 0; font-size: .9375em; line-height: 1.45; color: var(--_muted); }
40
+ [role=status]:empty { display: none; }
41
+ `;
42
+
43
+ // A constructed stylesheet is not an inline <style>, so pages with a strict
44
+ // style-src Content Security Policy still get the default look.
45
+ let sheet;
46
+ function adoptStyles(root) {
47
+ if (typeof CSSStyleSheet === 'function' && 'replaceSync' in CSSStyleSheet.prototype && 'adoptedStyleSheets' in root) {
48
+ if (!sheet) {
49
+ sheet = new CSSStyleSheet();
50
+ sheet.replaceSync(STYLE);
51
+ }
52
+ root.adoptedStyleSheets = [sheet];
53
+ } else {
54
+ const style = root.ownerDocument.createElement('style');
55
+ style.textContent = STYLE;
56
+ root.prepend(style);
57
+ }
58
+ }
59
+
3
60
  // The Integrator supplies a session-bound backend adapter. It owns the user,
4
61
  // Context, CSRF protection and any explanation of a denied request.
5
62
  export class CountersignWidget extends HTMLElement {
@@ -7,31 +64,43 @@ export class CountersignWidget extends HTMLElement {
7
64
  #generation = 0;
8
65
  #busy = false;
9
66
 
67
+ static observedAttributes = ['paired'];
68
+
10
69
  constructor() {
11
70
  super();
12
71
  const root = this.attachShadow({ mode: 'open' });
13
72
  root.innerHTML = `
14
- <style>
15
- :host { display: inline-block; font-family: var(--countersign-font, system-ui, sans-serif); }
16
- [part=actions] { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
17
- button, a { font: inherit; }
18
- a { color: var(--countersign-accent, LinkText); }
19
- [role=status]:empty { display: none; }
20
- </style>
21
- <div part="actions">
22
- <button type="button" data-action="pair">Pair Companion</button>
23
- <button type="button" data-action="connect">Request access</button>
24
- <a part="launch" hidden>Open Companion</a>
25
- <button type="button" data-action="push" hidden>Enable notifications</button>
26
- </div>
27
- <p role="status" part="status"></p>`;
73
+ <div part="card" role="group" aria-label="Countersign">
74
+ <div part="header" aria-hidden="true">
75
+ <svg part="mark" viewBox="0 0 32 32"><rect x="3" y="8" width="18" height="6"/><rect x="11" y="18" width="18" height="6"/></svg>
76
+ <span class="divider"></span>
77
+ <span part="wordmark">countersign</span>
78
+ </div>
79
+ <div part="body">
80
+ <p part="headline"><slot name="headline">Approve with your desktop</slot></p>
81
+ <div part="actions">
82
+ <button type="button" data-action="pair">Pair Companion</button>
83
+ <button type="button" data-action="connect">Request access</button>
84
+ <a part="launch" hidden>Open Companion</a>
85
+ <button type="button" data-action="push" hidden>Enable notifications</button>
86
+ </div>
87
+ <p role="status" part="status"></p>
88
+ </div>
89
+ </div>`;
90
+ adoptStyles(root);
28
91
  root.addEventListener('click', event => {
29
- const action = event.target.dataset?.action;
92
+ const action = event.target.closest?.('[data-action]')?.dataset.action;
30
93
  if (action) void this.#run(action);
31
94
  });
32
95
  this.#renderControls();
33
96
  }
34
97
 
98
+ // `paired` moves the primary emphasis from Pair Companion to Request access
99
+ // and turns pairing into a secondary "Pair another computer" link.
100
+ attributeChangedCallback() {
101
+ this.#renderControls();
102
+ }
103
+
35
104
  set adapter(value) {
36
105
  if (value != null &&
37
106
  (typeof value.pair !== 'function' || typeof value.connect !== 'function')) {
@@ -72,7 +141,13 @@ export class CountersignWidget extends HTMLElement {
72
141
  }
73
142
 
74
143
  #renderControls() {
144
+ const paired = this.hasAttribute('paired');
145
+ const primary = paired ? 'connect' : 'pair';
75
146
  for (const button of this.shadowRoot.querySelectorAll('button')) {
147
+ const action = button.dataset.action;
148
+ const style = action === primary ? ' primary' : action === 'pair' ? ' link' : '';
149
+ button.setAttribute('part', 'button ' + action + style);
150
+ if (action === 'pair') button.textContent = paired ? 'Pair another computer' : 'Pair Companion';
76
151
  button.disabled = !this.#adapter || this.#busy;
77
152
  if (button.dataset.action === 'push') {
78
153
  button.hidden = typeof this.#adapter?.subscribe !== 'function';