@withone/connect 0.11.0 → 0.12.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.
package/src/button.ts CHANGED
@@ -1,309 +1,469 @@
1
+ import { createConnectFlow, readConnectReturn } from "./flow";
1
2
  import { normalizePlatforms, parsePlatformsAttribute } from "./platforms";
2
- import { useOneConnect } from "./useOneConnect";
3
3
  import type {
4
4
  ConnectButtonHandle,
5
- ConnectButtonOptions,
5
+ ConnectButtonProps,
6
6
  ConnectButtonState,
7
- OneConnectHandle,
7
+ OneConnectFlow,
8
+ OneConnectFlowOptions,
8
9
  } from "./types";
9
10
 
10
11
  /**
11
- * The pre-built trigger for the connect flow. Optional: `useOneConnect`
12
- * with any element remains fully supported. Framework-agnostic: it
13
- * renders real DOM into a container, so React, Vue, Svelte and plain
14
- * HTML all mount it the same way.
12
+ * The pre-built trigger for the connect flow. Optional: any element that
13
+ * calls `createConnectFlow(...).open` works too.
15
14
  *
16
- * The button owns the flow wiring: a click opens the flow, the label
17
- * turns "Connecting" while the tab is away, and lands on "Connected"
18
- * when the return carries a success.
15
+ * It renders into a shadow root, so the host page's CSS cannot reach in
16
+ * and its own CSS cannot leak out, and its styles are a constructed
17
+ * stylesheet (`adoptedStyleSheets`) rather than a <style> tag or style
18
+ * attributes, so it keeps its look under a strict Content Security
19
+ * Policy (`style-src 'self'`). Colours that depend on props travel as
20
+ * custom properties set through the CSSOM, which such a policy allows.
21
+ *
22
+ * Theming hooks for the host page: `--one-connect-font`,
23
+ * `--one-connect-radius`, and `::part(button)` / `::part(label)` on the
24
+ * host (`one-connect-button`, or `.one-connect` for the wrappers).
19
25
  */
20
26
 
21
- const STYLE_ID = "one-connect-button-styles";
22
- const EASE = "cubic-bezier(.2,.9,.25,1)";
23
27
  const MAX_VISIBLE_CHIPS = 3;
24
-
25
- /** One's palette (see the Clockwork design system): lime is the brand
26
- * call-to-action, spring is success, carbon is the ink. */
27
28
  const LIME = "#CCFF00";
28
- const SPRING = "#3FE3A5";
29
29
  const CARBON = "#0A0C0B";
30
-
31
- interface Palette {
32
- buttonBackground: string;
33
- buttonForeground: string;
34
- surface: string;
35
- foreground: string;
36
- muted: string;
37
- line: string;
38
- shadow: string;
39
- }
40
-
41
- const LIGHT: Palette = {
42
- buttonBackground: CARBON,
43
- buttonForeground: "#FFFFFF",
44
- surface: "#FFFFFF",
45
- foreground: CARBON,
46
- muted: "#6B7280",
47
- line: "#D1D5DB",
48
- shadow: "0 1px 2px rgba(10,12,11,.08), 0 4px 14px rgba(10,12,11,.06)",
49
- };
50
-
51
- const DARK: Palette = {
52
- buttonBackground: "#F2F5F3",
53
- buttonForeground: CARBON,
54
- surface: "#101312",
55
- foreground: "#F2F5F3",
56
- muted: "#8A938E",
57
- line: "#2A302D",
58
- shadow: "0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35)",
59
- };
60
-
61
- /** One's lockup — the mark and the wordmark as one asset, every fill set
30
+ const WHITE = "#FFFFFF";
31
+
32
+ const STYLES = `
33
+ :host{display:inline-block;vertical-align:middle;max-width:100%}
34
+ :host([data-full-width]),:host([data-variant="block"]){display:block}
35
+ .b{all:unset;box-sizing:border-box;position:relative;display:inline-flex;align-items:center;gap:var(--gap);
36
+ max-width:100%;cursor:pointer;user-select:none;-webkit-tap-highlight-color:transparent;
37
+ font-family:var(--one-connect-font,inherit);font-size:var(--fs);font-weight:500;line-height:1.2;letter-spacing:-.01em;
38
+ padding:var(--pad);border-radius:var(--one-connect-radius,12px);background:var(--bg);color:var(--fg);
39
+ box-shadow:var(--shadow);transition:box-shadow .2s var(--ease),background-color .2s var(--ease),opacity .2s var(--ease)}
40
+ .b[data-full-width]{display:flex;width:100%;justify-content:center}
41
+ .b:focus-visible{outline:2px solid var(--ring);outline-offset:2px}
42
+ .b:hover{box-shadow:var(--shadow-hover)}
43
+ .b[disabled]{cursor:not-allowed;opacity:.5;box-shadow:none}
44
+ .b[aria-busy="true"]{cursor:progress}
45
+ .b{--ease:cubic-bezier(.2,.9,.25,1)}
46
+ .b[data-size="sm"]{--fs:13px;--pad:8px 12px 8px 10px;--gap:8px;--chip:18px;--logo:12px}
47
+ .b[data-size="md"]{--fs:14.5px;--pad:12px 16px 12px 14px;--gap:12px;--chip:22px;--logo:14px}
48
+ .b[data-size="lg"]{--fs:16px;--pad:14px 20px 14px 16px;--gap:12px;--chip:26px;--logo:16px}
49
+ .b[data-theme="light"],.b[data-theme="auto"]{--ink-bg:${CARBON};--ink-fg:${WHITE};--surface:${WHITE};--surface-fg:${CARBON};
50
+ --muted:#5B6560;--line:#D5DAD7;--ring:${CARBON};--shadow-base:0 1px 2px rgba(10,12,11,.08),0 4px 14px rgba(10,12,11,.06);
51
+ --shadow-strong:0 1px 2px rgba(10,12,11,.12),0 6px 18px rgba(10,12,11,.1)}
52
+ .b[data-theme="dark"]{--ink-bg:#F2F5F3;--ink-fg:${CARBON};--surface:#101312;--surface-fg:#F2F5F3;
53
+ --muted:#9AA39E;--line:#2A302D;--ring:${LIME};--shadow-base:0 1px 2px rgba(0,0,0,.4),0 8px 24px rgba(0,0,0,.35);
54
+ --shadow-strong:0 1px 2px rgba(0,0,0,.5),0 10px 28px rgba(0,0,0,.45)}
55
+ @media (prefers-color-scheme:dark){.b[data-theme="auto"]{--ink-bg:#F2F5F3;--ink-fg:${CARBON};--surface:#101312;--surface-fg:#F2F5F3;
56
+ --muted:#9AA39E;--line:#2A302D;--ring:${LIME};--shadow-base:0 1px 2px rgba(0,0,0,.4),0 8px 24px rgba(0,0,0,.35);
57
+ --shadow-strong:0 1px 2px rgba(0,0,0,.5),0 10px 28px rgba(0,0,0,.45)}}
58
+ .b[data-variant="default"]{--bg:var(--ink-bg);--fg:var(--ink-fg);--shadow:var(--shadow-base);--shadow-hover:var(--shadow-strong)}
59
+ .b[data-variant="accent"]{--bg:var(--one-connect-accent,${LIME});--fg:var(--one-connect-accent-fg,${CARBON});
60
+ --shadow:0 1px 2px rgba(10,12,11,.1);--shadow-hover:0 1px 2px rgba(10,12,11,.12),0 6px 18px rgba(10,12,11,.12)}
61
+ .b[data-variant="block"]{--bg:var(--surface);--fg:var(--surface-fg);--shadow:inset 0 0 0 1px var(--line);
62
+ --shadow-hover:inset 0 0 0 1px var(--muted);flex-direction:column;align-items:stretch;gap:0;
63
+ width:100%;max-width:420px;text-align:left;padding:16px;border-radius:var(--one-connect-radius,14px)}
64
+ .b[data-variant="block"][data-full-width]{max-width:none}
65
+ .stack{display:inline-flex;align-items:center;flex-shrink:0}
66
+ .chip{width:var(--chip);height:var(--chip);border-radius:6px;display:grid;place-items:center;overflow:hidden;
67
+ margin-left:-7px;position:relative;background:${WHITE};color:${CARBON};box-shadow:0 0 0 1.5px var(--bg);
68
+ transition:margin-left .28s var(--ease)}
69
+ .chip:first-child{margin-left:0}
70
+ .b[data-variant="block"] .chip{box-shadow:0 0 0 1.5px var(--bg),0 0 0 2.5px var(--line)}
71
+ .chip img{width:var(--logo);height:var(--logo);display:block;object-fit:contain}
72
+ .chip.more,.chip.letter{font:500 9px/1 ui-monospace,SFMono-Regular,Menlo,monospace;letter-spacing:-.02em}
73
+ .chip.letter{font-size:10px;font-weight:600}
74
+ .b:hover .chip{margin-left:-2px}.b:hover .chip:first-child{margin-left:0}
75
+ .label{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
76
+ .icon{display:inline-flex;flex-shrink:0}
77
+ .arrow{opacity:.55;transition:transform .2s var(--ease),opacity .2s var(--ease)}
78
+ .b:hover .arrow{transform:translateX(2px);opacity:.85}
79
+ .spinner{width:16px;height:16px;border-radius:50%;border:2px solid currentColor;border-top-color:transparent;
80
+ opacity:.7;animation:spin .7s linear infinite;flex-shrink:0}
81
+ @keyframes spin{to{transform:rotate(360deg)}}
82
+ .top{display:flex;align-items:center;justify-content:space-between;gap:12px}
83
+ .title{font-size:15px;font-weight:600;letter-spacing:-.01em}
84
+ .sub{font-size:13px;line-height:1.5;margin-top:6px;font-weight:400;color:var(--muted)}
85
+ .foot{margin-top:14px;padding-top:12px;display:flex;align-items:center;gap:6px;font-size:11px;font-weight:400;
86
+ color:var(--muted);border-top:1px solid var(--line)}
87
+ .lockup{display:inline-flex;color:var(--surface-fg)}
88
+ .sr{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0}
89
+ @media (prefers-reduced-motion:reduce){.b,.chip,.arrow{transition:none}.spinner{animation-duration:1.6s}}
90
+ @media (forced-colors:active){.b{border:1px solid ButtonText}.b:focus-visible{outline-color:Highlight}}
91
+ `;
92
+
93
+ /** One's lockup, the mark and the wordmark as one asset, every fill set
62
94
  * to `currentColor` so a single copy works on either palette. Inlined
63
95
  * rather than fetched: this is the trust mark, and it has to draw even
64
- * behind a strict content policy or offline. Source:
96
+ * behind a strict Content Security Policy or offline. Source:
65
97
  * core-ui/public/logo/logo-full-dark.svg (viewBox 3152x990). */
66
98
  const ONE_LOCKUP_SVG =
67
- '<svg viewBox="0 0 3152 990" width="35" height="11" fill="none" aria-hidden="true" style="flex-shrink:0;display:block"><path d="M1698.02 793.203C1525.75 793.203 1401 666.834 1401 495.102C1401 323.909 1524.67 197 1698.02 197C1870.83 197 1996.12 323.909 1996.12 495.102C1996.12 666.834 1869.75 793.203 1698.02 793.203ZM1698.02 694.916C1801.71 694.916 1884.88 615.53 1884.88 495.102C1884.88 374.133 1801.71 295.827 1698.02 295.827C1594.87 295.827 1511.71 373.053 1511.71 495.102C1511.71 616.61 1595.41 694.916 1698.02 694.916Z" fill="currentColor"/><path d="M2326.71 197.54C2480.62 197.54 2565.95 289.887 2565.95 467.56V779.702H2455.24V468.64C2455.24 361.172 2411.5 292.587 2308.35 292.587C2217.63 292.587 2140.4 356.852 2140.4 484.841V779.702H2030.77V211.041H2140.4V272.065C2185.76 225.082 2248.41 197.54 2326.71 197.54Z" fill="currentColor"/><path d="M3151.98 493.481C3151.98 514.003 3150.36 520.483 3148.74 529.124H2700.51C2715.63 637.672 2794.47 686.815 2922.46 686.815C3031.01 686.815 3099.6 650.633 3116.88 643.072V747.84C3100.68 755.4 3023.45 793.203 2908.96 793.203C2699.97 793.203 2588.18 661.434 2588.18 497.802C2588.18 332.55 2702.67 197 2871.7 197C3031.55 197 3151.98 317.969 3151.98 493.481ZM2876.02 300.688C2784.75 300.688 2720.49 345.511 2702.67 434.617H3050.45C3034.79 344.971 2967.29 300.688 2876.02 300.688Z" fill="currentColor"/><path d="M537.494 782.175C641.011 827.7 767.922 858.83 880.501 839.875C983.155 822.599 1069.64 764.946 1094.39 659.271C1114.47 573.493 1088.54 481.273 1047.27 405.311C983.124 287.246 874.768 183.791 759.847 114.657C644.926 45.5225 507.181 -5.02605 372.102 0.398669C289.698 3.69665 205.938 26.536 146.544 85.6685C112.069 119.989 88.3818 171.231 83.1266 219.823C78.1642 265.64 84.3749 310.317 99.8478 354.007C168.243 547.14 354.009 701.467 537.51 782.191L537.494 782.175ZM751.956 818.053C583.05 796.2 415.7 707.108 292.472 593.004C222.536 528.246 159.366 444.979 125.985 355.471C102.961 293.734 96.3956 223.367 122.41 161.969C137.158 127.14 160.367 98.244 190.743 75.9133C275.581 13.5752 395.819 6.99464 495.977 26.8596C608.848 49.252 713.875 99.4615 806.712 166.515C907.085 239.025 999.537 339.922 1048.42 454.627C1072.45 511.001 1083.99 570.103 1075.64 630.869C1067.38 690.957 1034.57 742.353 983.987 775.379C917.626 818.715 830.861 828.255 751.972 818.053H751.956Z" fill="currentColor"/><path d="M1103.27 661.228C1086.68 755.313 1024.9 818.622 934.021 848.69C821.751 885.846 685.146 860.263 576.251 818.915C452.515 771.942 340.336 699.91 245.157 607.813C209.002 572.83 177.255 534.456 148.837 493.031C100.384 422.402 61.7024 335.19 65.8326 248.071C68.2213 197.769 83.987 159.241 111.403 116.938C45.8289 171.308 31.404 262.511 47.1543 342.002C53.4728 373.841 62.9815 403.23 76.528 432.958C100.338 485.202 130.914 532.992 167.747 577.238C255.452 682.573 367.83 765.13 491.073 824.124C614.317 883.118 769.307 920.691 900.471 889.128C985.788 868.601 1061.47 816.095 1090.83 730.856C1098.18 708.71 1104.61 686.564 1103.25 661.244L1103.27 661.228Z" fill="currentColor"/><path d="M883.297 963.502C923.15 947.074 960.877 926.022 988.309 893.967C954.265 917.839 920.931 934.807 882.403 944.993C751.747 979.53 595.123 942.589 474.731 886.57C422.102 862.082 372.617 834.018 325.181 800.591C205.128 715.984 89.4825 597.996 36.7762 458.756C20.2092 414.973 11.7793 370.404 11.5327 323.739C11.4249 302.641 15.4627 283.747 19.0226 263.004C-2.52219 310.486 -3.66265 380.915 5.21419 431.617C32.0605 584.881 147.151 721.146 266.988 813.983C343.443 873.224 436.403 924.913 528.424 954.672C620.444 984.431 713.851 999.426 808.491 983.598C834.459 979.252 858.362 973.781 883.297 963.502Z" fill="currentColor"/><path d="M1045.09 830.737C1053.55 816.158 1061.75 802.95 1066.99 787.061C1043.75 824.541 1013.3 854.408 974.29 874.982C947.752 888.991 920.228 899.27 890.577 905.219C864.578 910.428 839.673 914.481 812.781 914.851C739.716 915.852 668.717 903.092 599.459 880.854C418.686 822.8 238.422 697.6 127.985 543.165C94.8819 496.87 68.8679 447.847 50.7135 394.031C28.1978 324.45 25.7782 258.69 53.4104 189.078C36.1345 211.825 26.9956 237.793 20.4151 264.916C18.3654 282.732 14.713 299.345 15.0828 317.561C15.9613 360.774 23.5436 402.692 38.477 443.562C73.1522 538.464 134.936 621.869 207.368 691.913C307.34 788.587 426.762 862.268 558.127 907.993C689.491 953.718 871.975 973.768 992.614 885.446C1013.28 870.312 1029.39 851.773 1045.07 830.752L1045.09 830.737Z" fill="currentColor"/><path d="M552.552 745.664C671.079 797.831 835.825 835.526 954.768 769.12C999.137 744.354 1031.99 705.626 1047.99 657.312C1086.75 540.264 1020.28 405.817 947.124 315.631C855.536 202.745 729.688 114.516 592.76 66.0323C506.349 35.4412 418.968 22.0797 328.211 37.46C262.313 48.6177 195.29 81.7054 157.563 138.234C118.912 196.133 115.891 270.785 136.758 335.882C176.519 459.926 276.769 570.763 381.01 646.277C434.995 685.391 491.662 718.864 552.567 745.664H552.552ZM210.162 422.955C199.898 402.381 189.141 382.701 184.733 360.385C182.267 347.918 184.625 333.477 184.564 320.64C184.44 293.223 194.072 269.059 209.992 247.93C219.609 235.17 229.456 224.844 243.018 215.906C341.218 151.179 476.051 175.991 576.763 223.904C661.971 264.435 737.394 321.534 796.11 395.862C836.919 447.52 871.193 513.634 880.502 579.501C888.238 634.272 873.243 695.485 826.763 729.189C807.853 742.905 785.045 751.721 761.728 755.974C721.921 763.217 688.802 758.008 649.565 748.916C594.27 736.094 542.488 715.212 492.279 688.889C441.977 662.521 396.745 631.483 353.008 595.436C314.372 563.597 280.252 537.845 248.458 498.007L248.274 497.837L221.504 461.945L211.379 445.84C194.103 418.408 177.398 391.346 165.238 361.202C151.291 326.604 142.676 291.852 143.678 254.279C146.19 160.564 203.766 96.7467 290.238 67.82C434.394 19.5985 611.561 79.4246 735.298 157.636C825.099 214.411 904.435 286.581 963.321 375.134C989.767 414.894 1010.42 456.998 1023.78 502.646C1049.84 591.66 1036.79 685.884 957.404 742.659C890.257 790.68 785.6 793.146 707.295 776.486C680.403 769.736 654.065 766.007 629.222 754.818C732.107 783.93 882.397 797.723 963.938 715.35C990.599 688.412 1007.55 656.218 1013.75 618.537C1020.87 575.232 1016.12 531.634 1002.62 489.931C985.498 437.071 956.34 384.026 922.405 340.289C876.911 281.665 823.188 230.531 762.468 187.981C681.112 130.975 588.783 88.887 490.799 71.0872C425.595 59.2514 349.463 60.4072 287.557 85.0959C256.441 97.5019 228.501 114.5 205.924 138.85C158.303 190.2 147.176 256.036 161.539 323.383C166.856 348.334 175.456 371.296 185.596 394.737C194.488 415.295 222.676 461.883 236.022 477.649L226.729 456.196L210.162 423.001V422.955ZM832.342 286.936C756.904 211.544 666.44 154.076 564.896 120.249C526.553 107.473 487.948 99.4899 447.648 96.084C401.26 92.1696 356.167 96.7313 312.215 111.341C222.337 141.208 169.399 217.277 175.255 312.333C177.567 324.847 179.555 335.774 180.603 348.503C152.971 264.944 174.454 175.236 251.356 125.581C310.797 87.2072 387.39 78.2071 456.694 86.7603C503.714 92.5549 555.387 106.656 599.078 124.317C706.016 167.546 800.764 234.615 876.741 321.395C912.002 361.664 942.085 405.756 963.876 454.532C996.779 528.166 1011.85 610.046 965.895 681.369C952.379 702.359 935.427 718.818 914.283 731.871C884.432 747.421 852.947 758.671 818.872 761.06C839.523 755.959 859.404 750.981 878.637 740.424C929.186 717.061 964.601 671.567 974.818 616.488C987.671 547.245 964.693 475.429 930.881 415.357C903.834 367.305 871.471 326.049 832.311 286.905L832.342 286.936ZM890.673 420.18C861.623 370.387 825.515 327.621 783.288 287.999C721.659 230.161 650.028 185.345 570.598 156.033C513.623 135.013 454.506 124.394 393.786 128.108C353.316 130.59 315.728 139.867 280.498 159.516C245.268 179.166 216.819 204.918 201.023 240.394L188.617 268.242C204.583 180.445 278.325 132.054 361.931 119.355C412.757 111.634 462.658 116.643 512.421 129.048C596.905 150.116 674.1 189.923 743.45 242.028C816.607 296.999 885.526 373.531 924.578 456.982C949.713 510.675 965.094 571.61 949.837 630.234C939.018 671.768 912.511 706.566 875.308 727.987C847.013 744.292 816.191 754.679 783.242 758.101C825.915 745.926 863.75 731.917 895.327 698.799C913.512 679.72 926.535 657.62 933.054 631.714C951.347 558.989 927.49 483.274 890.642 420.134L890.673 420.18ZM726.19 279.369C625.678 198.799 493.404 141.917 362.655 156.973C343.854 159.131 326.547 164.17 308.639 169.195C301.18 171.29 295.201 177.732 286.493 176.253C378.313 130.081 492.063 149.545 583.143 188.119C684.102 230.87 775.952 302.825 840.664 391.069C871.794 433.527 895.805 479.652 909.506 530.386C918.382 563.273 920.232 596.654 911.941 629.294C905.622 654.153 895.481 677.778 875.786 694.9C893.987 666.327 905.452 637.693 907.086 604.128C908.72 570.562 903.141 536.565 891.567 503.57C861.392 417.53 797.066 336.19 726.205 279.384L726.19 279.369Z" fill="currentColor"/></svg>';
99
+ '<svg viewBox="0 0 3152 990" width="35" height="11" fill="none" aria-hidden="true" focusable="false"><path d="M1698.02 793.203C1525.75 793.203 1401 666.834 1401 495.102C1401 323.909 1524.67 197 1698.02 197C1870.83 197 1996.12 323.909 1996.12 495.102C1996.12 666.834 1869.75 793.203 1698.02 793.203ZM1698.02 694.916C1801.71 694.916 1884.88 615.53 1884.88 495.102C1884.88 374.133 1801.71 295.827 1698.02 295.827C1594.87 295.827 1511.71 373.053 1511.71 495.102C1511.71 616.61 1595.41 694.916 1698.02 694.916Z" fill="currentColor"/><path d="M2326.71 197.54C2480.62 197.54 2565.95 289.887 2565.95 467.56V779.702H2455.24V468.64C2455.24 361.172 2411.5 292.587 2308.35 292.587C2217.63 292.587 2140.4 356.852 2140.4 484.841V779.702H2030.77V211.041H2140.4V272.065C2185.76 225.082 2248.41 197.54 2326.71 197.54Z" fill="currentColor"/><path d="M3151.98 493.481C3151.98 514.003 3150.36 520.483 3148.74 529.124H2700.51C2715.63 637.672 2794.47 686.815 2922.46 686.815C3031.01 686.815 3099.6 650.633 3116.88 643.072V747.84C3100.68 755.4 3023.45 793.203 2908.96 793.203C2699.97 793.203 2588.18 661.434 2588.18 497.802C2588.18 332.55 2702.67 197 2871.7 197C3031.55 197 3151.98 317.969 3151.98 493.481ZM2876.02 300.688C2784.75 300.688 2720.49 345.511 2702.67 434.617H3050.45C3034.79 344.971 2967.29 300.688 2876.02 300.688Z" fill="currentColor"/><path d="M537.494 782.175C641.011 827.7 767.922 858.83 880.501 839.875C983.155 822.599 1069.64 764.946 1094.39 659.271C1114.47 573.493 1088.54 481.273 1047.27 405.311C983.124 287.246 874.768 183.791 759.847 114.657C644.926 45.5225 507.181 -5.02605 372.102 0.398669C289.698 3.69665 205.938 26.536 146.544 85.6685C112.069 119.989 88.3818 171.231 83.1266 219.823C78.1642 265.64 84.3749 310.317 99.8478 354.007C168.243 547.14 354.009 701.467 537.51 782.191L537.494 782.175ZM751.956 818.053C583.05 796.2 415.7 707.108 292.472 593.004C222.536 528.246 159.366 444.979 125.985 355.471C102.961 293.734 96.3956 223.367 122.41 161.969C137.158 127.14 160.367 98.244 190.743 75.9133C275.581 13.5752 395.819 6.99464 495.977 26.8596C608.848 49.252 713.875 99.4615 806.712 166.515C907.085 239.025 999.537 339.922 1048.42 454.627C1072.45 511.001 1083.99 570.103 1075.64 630.869C1067.38 690.957 1034.57 742.353 983.987 775.379C917.626 818.715 830.861 828.255 751.972 818.053H751.956Z" fill="currentColor"/><path d="M1103.27 661.228C1086.68 755.313 1024.9 818.622 934.021 848.69C821.751 885.846 685.146 860.263 576.251 818.915C452.515 771.942 340.336 699.91 245.157 607.813C209.002 572.83 177.255 534.456 148.837 493.031C100.384 422.402 61.7024 335.19 65.8326 248.071C68.2213 197.769 83.987 159.241 111.403 116.938C45.8289 171.308 31.404 262.511 47.1543 342.002C53.4728 373.841 62.9815 403.23 76.528 432.958C100.338 485.202 130.914 532.992 167.747 577.238C255.452 682.573 367.83 765.13 491.073 824.124C614.317 883.118 769.307 920.691 900.471 889.128C985.788 868.601 1061.47 816.095 1090.83 730.856C1098.18 708.71 1104.61 686.564 1103.25 661.244L1103.27 661.228Z" fill="currentColor"/><path d="M883.297 963.502C923.15 947.074 960.877 926.022 988.309 893.967C954.265 917.839 920.931 934.807 882.403 944.993C751.747 979.53 595.123 942.589 474.731 886.57C422.102 862.082 372.617 834.018 325.181 800.591C205.128 715.984 89.4825 597.996 36.7762 458.756C20.2092 414.973 11.7793 370.404 11.5327 323.739C11.4249 302.641 15.4627 283.747 19.0226 263.004C-2.52219 310.486 -3.66265 380.915 5.21419 431.617C32.0605 584.881 147.151 721.146 266.988 813.983C343.443 873.224 436.403 924.913 528.424 954.672C620.444 984.431 713.851 999.426 808.491 983.598C834.459 979.252 858.362 973.781 883.297 963.502Z" fill="currentColor"/><path d="M1045.09 830.737C1053.55 816.158 1061.75 802.95 1066.99 787.061C1043.75 824.541 1013.3 854.408 974.29 874.982C947.752 888.991 920.228 899.27 890.577 905.219C864.578 910.428 839.673 914.481 812.781 914.851C739.716 915.852 668.717 903.092 599.459 880.854C418.686 822.8 238.422 697.6 127.985 543.165C94.8819 496.87 68.8679 447.847 50.7135 394.031C28.1978 324.45 25.7782 258.69 53.4104 189.078C36.1345 211.825 26.9956 237.793 20.4151 264.916C18.3654 282.732 14.713 299.345 15.0828 317.561C15.9613 360.774 23.5436 402.692 38.477 443.562C73.1522 538.464 134.936 621.869 207.368 691.913C307.34 788.587 426.762 862.268 558.127 907.993C689.491 953.718 871.975 973.768 992.614 885.446C1013.28 870.312 1029.39 851.773 1045.07 830.752L1045.09 830.737Z" fill="currentColor"/><path d="M552.552 745.664C671.079 797.831 835.825 835.526 954.768 769.12C999.137 744.354 1031.99 705.626 1047.99 657.312C1086.75 540.264 1020.28 405.817 947.124 315.631C855.536 202.745 729.688 114.516 592.76 66.0323C506.349 35.4412 418.968 22.0797 328.211 37.46C262.313 48.6177 195.29 81.7054 157.563 138.234C118.912 196.133 115.891 270.785 136.758 335.882C176.519 459.926 276.769 570.763 381.01 646.277C434.995 685.391 491.662 718.864 552.567 745.664H552.552ZM210.162 422.955C199.898 402.381 189.141 382.701 184.733 360.385C182.267 347.918 184.625 333.477 184.564 320.64C184.44 293.223 194.072 269.059 209.992 247.93C219.609 235.17 229.456 224.844 243.018 215.906C341.218 151.179 476.051 175.991 576.763 223.904C661.971 264.435 737.394 321.534 796.11 395.862C836.919 447.52 871.193 513.634 880.502 579.501C888.238 634.272 873.243 695.485 826.763 729.189C807.853 742.905 785.045 751.721 761.728 755.974C721.921 763.217 688.802 758.008 649.565 748.916C594.27 736.094 542.488 715.212 492.279 688.889C441.977 662.521 396.745 631.483 353.008 595.436C314.372 563.597 280.252 537.845 248.458 498.007L248.274 497.837L221.504 461.945L211.379 445.84C194.103 418.408 177.398 391.346 165.238 361.202C151.291 326.604 142.676 291.852 143.678 254.279C146.19 160.564 203.766 96.7467 290.238 67.82C434.394 19.5985 611.561 79.4246 735.298 157.636C825.099 214.411 904.435 286.581 963.321 375.134C989.767 414.894 1010.42 456.998 1023.78 502.646C1049.84 591.66 1036.79 685.884 957.404 742.659C890.257 790.68 785.6 793.146 707.295 776.486C680.403 769.736 654.065 766.007 629.222 754.818C732.107 783.93 882.397 797.723 963.938 715.35C990.599 688.412 1007.55 656.218 1013.75 618.537C1020.87 575.232 1016.12 531.634 1002.62 489.931C985.498 437.071 956.34 384.026 922.405 340.289C876.911 281.665 823.188 230.531 762.468 187.981C681.112 130.975 588.783 88.887 490.799 71.0872C425.595 59.2514 349.463 60.4072 287.557 85.0959C256.441 97.5019 228.501 114.5 205.924 138.85C158.303 190.2 147.176 256.036 161.539 323.383C166.856 348.334 175.456 371.296 185.596 394.737C194.488 415.295 222.676 461.883 236.022 477.649L226.729 456.196L210.162 423.001V422.955ZM832.342 286.936C756.904 211.544 666.44 154.076 564.896 120.249C526.553 107.473 487.948 99.4899 447.648 96.084C401.26 92.1696 356.167 96.7313 312.215 111.341C222.337 141.208 169.399 217.277 175.255 312.333C177.567 324.847 179.555 335.774 180.603 348.503C152.971 264.944 174.454 175.236 251.356 125.581C310.797 87.2072 387.39 78.2071 456.694 86.7603C503.714 92.5549 555.387 106.656 599.078 124.317C706.016 167.546 800.764 234.615 876.741 321.395C912.002 361.664 942.085 405.756 963.876 454.532C996.779 528.166 1011.85 610.046 965.895 681.369C952.379 702.359 935.427 718.818 914.283 731.871C884.432 747.421 852.947 758.671 818.872 761.06C839.523 755.959 859.404 750.981 878.637 740.424C929.186 717.061 964.601 671.567 974.818 616.488C987.671 547.245 964.693 475.429 930.881 415.357C903.834 367.305 871.471 326.049 832.311 286.905L832.342 286.936ZM890.673 420.18C861.623 370.387 825.515 327.621 783.288 287.999C721.659 230.161 650.028 185.345 570.598 156.033C513.623 135.013 454.506 124.394 393.786 128.108C353.316 130.59 315.728 139.867 280.498 159.516C245.268 179.166 216.819 204.918 201.023 240.394L188.617 268.242C204.583 180.445 278.325 132.054 361.931 119.355C412.757 111.634 462.658 116.643 512.421 129.048C596.905 150.116 674.1 189.923 743.45 242.028C816.607 296.999 885.526 373.531 924.578 456.982C949.713 510.675 965.094 571.61 949.837 630.234C939.018 671.768 912.511 706.566 875.308 727.987C847.013 744.292 816.191 754.679 783.242 758.101C825.915 745.926 863.75 731.917 895.327 698.799C913.512 679.72 926.535 657.62 933.054 631.714C951.347 558.989 927.49 483.274 890.642 420.134L890.673 420.18ZM726.19 279.369C625.678 198.799 493.404 141.917 362.655 156.973C343.854 159.131 326.547 164.17 308.639 169.195C301.18 171.29 295.201 177.732 286.493 176.253C378.313 130.081 492.063 149.545 583.143 188.119C684.102 230.87 775.952 302.825 840.664 391.069C871.794 433.527 895.805 479.652 909.506 530.386C918.382 563.273 920.232 596.654 911.941 629.294C905.622 654.153 895.481 677.778 875.786 694.9C893.987 666.327 905.452 637.693 907.086 604.128C908.72 570.562 903.141 536.565 891.567 503.57C861.392 417.53 797.066 336.19 726.205 279.384L726.19 279.369Z" fill="currentColor"/></svg>';
68
100
  const ARROW_SVG =
69
- '<svg class="owcb-arrow" width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M6 3l5 5-5 5"/></svg>';
101
+ '<svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M6 3l5 5-5 5"/></svg>';
70
102
  const TICK_SVG =
71
- '<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="width:16px;height:16px"><path d="M3 8.5l3.5 3.5L13 5"/></svg>';
72
-
73
- function ensureStyles(): void {
74
- if (typeof document === "undefined" || document.getElementById(STYLE_ID))
75
- return;
103
+ '<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M3 8.5l3.5 3.5L13 5"/></svg>';
104
+
105
+ /* ── Styles: one constructed sheet shared by every button ─────────── */
106
+
107
+ let sharedSheet: CSSStyleSheet | null = null;
108
+
109
+ function adoptStyles(root: ShadowRoot): void {
110
+ const canAdopt =
111
+ typeof CSSStyleSheet !== "undefined" &&
112
+ "replaceSync" in CSSStyleSheet.prototype &&
113
+ "adoptedStyleSheets" in root;
114
+ if (canAdopt) {
115
+ try {
116
+ if (!sharedSheet) {
117
+ sharedSheet = new CSSStyleSheet();
118
+ sharedSheet.replaceSync(STYLES);
119
+ }
120
+ root.adoptedStyleSheets = [sharedSheet];
121
+ return;
122
+ } catch {
123
+ /* a document that refuses constructed sheets: fall through */
124
+ }
125
+ }
76
126
  const style = document.createElement("style");
77
- style.id = STYLE_ID;
78
- style.textContent = [
79
- `.owcb{font:500 14.5px/1.2 inherit;font-family:inherit;letter-spacing:-.01em;cursor:pointer;border:0;display:inline-flex;align-items:center;gap:12px;padding:12px 16px 12px 14px;border-radius:12px;transition:transform .2s ${EASE},box-shadow .2s ${EASE},opacity .2s ${EASE}}`,
80
- ".owcb:hover{transform:translateY(-1px)}",
81
- ".owcb:active{transform:translateY(0)}",
82
- `.owcb:focus-visible{outline:2px solid ${SPRING};outline-offset:3px}`,
83
- ".owcb[disabled]{cursor:default;opacity:.75;transform:none}",
84
- ".owcb-stack{display:inline-flex;align-items:center;flex-shrink:0}",
85
- `.owcb-chip{width:22px;height:22px;border-radius:6px;display:grid;place-items:center;overflow:hidden;margin-left:-7px;transition:margin-left .28s ${EASE};position:relative;background:#FFFFFF}`,
86
- ".owcb-chip:first-child{margin-left:0}",
87
- ".owcb-chip img{width:14px;height:14px;display:block;object-fit:contain}",
88
- `.owcb-chip.owcb-more{font-family:ui-monospace,monospace;font-size:9px;font-weight:500;letter-spacing:-.02em;color:${CARBON}}`,
89
- `.owcb-chip.owcb-letter{font:600 10px ui-monospace,monospace;color:${CARBON}}`,
90
- ".owcb:hover .owcb-chip{margin-left:-2px}",
91
- ".owcb:hover .owcb-chip:first-child{margin-left:0}",
92
- ".owcb-label{white-space:nowrap}",
93
- `.owcb-arrow{opacity:.5;transition:transform .2s ${EASE},opacity .2s ${EASE}}`,
94
- ".owcb:hover .owcb-arrow{transform:translateX(2px);opacity:.8}",
95
- ".owcb-spinner{width:16px;height:16px;border-radius:50%;border:2px solid currentColor;border-top-color:transparent;opacity:.7;animation:owcb-spin .7s linear infinite}",
96
- "@keyframes owcb-spin{to{transform:rotate(360deg)}}",
97
- ".owcb.owcb-block{display:flex;flex-direction:column;align-items:stretch;width:100%;max-width:420px;text-align:left;padding:16px;border-radius:14px;gap:0}",
98
- ".owcb-block-top{display:flex;align-items:center;justify-content:space-between;gap:12px}",
99
- ".owcb-block-title{font-size:15px;font-weight:600;letter-spacing:-.01em}",
100
- ".owcb-block-sub{font-size:13px;line-height:1.5;margin-top:6px;font-weight:400}",
101
- ".owcb-block-foot{margin-top:14px;padding-top:12px;display:flex;align-items:center;gap:6px;font-size:11.5px;font-weight:400}",
102
- "@media (prefers-reduced-motion:reduce){.owcb,.owcb-chip,.owcb-arrow{transition:none}.owcb-spinner{animation-duration:1.4s}}",
103
- ].join("\n");
104
- document.head.appendChild(style);
127
+ style.textContent = STYLES;
128
+ root.appendChild(style);
105
129
  }
106
130
 
107
- function buildStack(
108
- options: ConnectButtonOptions,
109
- chipRing: string,
110
- ): HTMLElement | null {
111
- const platforms = normalizePlatforms(options.platforms);
131
+ /* ── Accent contrast ──────────────────────────────────────────────── */
132
+
133
+ function parseRgb(color: string): [number, number, number] | null {
134
+ const value = color.trim().toLowerCase();
135
+ const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/.exec(value);
136
+ if (hex) {
137
+ const digits = hex[1].length === 3 ? hex[1].replace(/./g, "$&$&") : hex[1];
138
+ return [0, 2, 4].map((i) => parseInt(digits.slice(i, i + 2), 16)) as [
139
+ number,
140
+ number,
141
+ number,
142
+ ];
143
+ }
144
+ const rgb = /^rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)/.exec(value);
145
+ return rgb ? [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])] : null;
146
+ }
147
+
148
+ /** Any CSS colour to sRGB, through a canvas when it is not already hex
149
+ * or rgb() ("navy", "hsl(…)"). Null when the browser cannot say. */
150
+ function toRgb(color: string): [number, number, number] | null {
151
+ const direct = parseRgb(color);
152
+ if (direct) return direct;
153
+ try {
154
+ const context = document.createElement("canvas").getContext("2d");
155
+ if (!context) return null;
156
+ context.fillStyle = "#000";
157
+ context.fillStyle = color;
158
+ return parseRgb(String(context.fillStyle));
159
+ } catch {
160
+ return null;
161
+ }
162
+ }
163
+
164
+ const luminance = ([r, g, b]: [number, number, number]): number => {
165
+ const channel = (c: number) => {
166
+ const s = c / 255;
167
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
168
+ };
169
+ return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
170
+ };
171
+
172
+ /** Black or white, whichever has the higher WCAG contrast on `color`. */
173
+ export function readableTextOn(color: string): string {
174
+ const rgb = toRgb(color);
175
+ if (!rgb) return CARBON;
176
+ const l = luminance(rgb);
177
+ const onWhite = 1.05 / (l + 0.05);
178
+ const onCarbon = (l + 0.05) / (luminance([10, 12, 11]) + 0.05);
179
+ return onWhite > onCarbon ? WHITE : CARBON;
180
+ }
181
+
182
+ /* ── Rendering ────────────────────────────────────────────────────── */
183
+
184
+ const el = <K extends keyof HTMLElementTagNameMap>(
185
+ tag: K,
186
+ className?: string,
187
+ text?: string,
188
+ ): HTMLElementTagNameMap[K] => {
189
+ const node = document.createElement(tag);
190
+ if (className) node.className = className;
191
+ if (text !== undefined) node.textContent = text;
192
+ return node;
193
+ };
194
+
195
+ const icon = (svg: string, className = "icon"): HTMLSpanElement => {
196
+ const node = el("span", className);
197
+ node.innerHTML = svg;
198
+ return node;
199
+ };
200
+
201
+ function buildStack(props: ConnectButtonProps): HTMLElement | null {
202
+ const platforms = normalizePlatforms(props.platforms);
112
203
  if (platforms.length === 0) return null;
113
- const stack = document.createElement("span");
114
- stack.className = "owcb-stack";
204
+ const stack = el("span", "stack");
115
205
  stack.setAttribute("aria-hidden", "true");
116
206
  for (const platform of platforms.slice(0, MAX_VISIBLE_CHIPS)) {
117
- const chip = document.createElement("span");
118
- chip.className = "owcb-chip";
207
+ const chip = el("span", "chip");
119
208
  chip.title = platform.name;
120
- chip.style.boxShadow = `0 0 0 1.5px ${chipRing}`;
121
- const img = document.createElement("img");
209
+ const img = el("img");
122
210
  img.alt = "";
211
+ img.decoding = "async";
123
212
  img.src = platform.imageUrl;
124
213
  img.addEventListener("error", () => {
125
214
  img.remove();
126
- chip.classList.add("owcb-letter");
215
+ chip.classList.add("letter");
127
216
  chip.textContent = platform.name.charAt(0).toUpperCase();
128
217
  });
129
218
  chip.appendChild(img);
130
219
  stack.appendChild(chip);
131
220
  }
132
- // Only ever the apps the caller listed beyond the first three: the chip
133
- // can never claim a number that has nothing to do with them.
134
- const extra = Math.max(0, platforms.length - MAX_VISIBLE_CHIPS);
135
- if (extra > 0) {
136
- const more = document.createElement("span");
137
- more.className = "owcb-chip owcb-more";
138
- more.style.boxShadow = `0 0 0 1.5px ${chipRing}`;
139
- more.textContent = `+${extra}`;
140
- stack.appendChild(more);
141
- }
221
+ // Only ever the apps the caller listed beyond the first three.
222
+ const extra = platforms.length - MAX_VISIBLE_CHIPS;
223
+ if (extra > 0) stack.appendChild(el("span", "chip more", `+${extra}`));
142
224
  return stack;
143
225
  }
144
226
 
145
- export const mountConnectButton = (
146
- container: HTMLElement,
147
- options: ConnectButtonOptions,
148
- ): ConnectButtonHandle => {
149
- ensureStyles();
150
-
151
- const palette = options.theme === "dark" ? DARK : LIGHT;
152
- const variant = options.variant ?? "default";
153
- const label = options.label ?? "Connect your apps";
154
- const connectedLabel = options.connectedLabel ?? "Connected";
227
+ const flowOptions = (
228
+ props: ConnectButtonProps,
229
+ handlers: Pick<OneConnectFlowOptions, "onSuccess" | "onError" | "onCancel">,
230
+ ): OneConnectFlowOptions => ({
231
+ authorizeUrl: props.authorizeUrl,
232
+ connectTheme: props.connectTheme ?? props.appTheme,
233
+ ...handlers,
234
+ });
155
235
 
156
- let state: ConnectButtonState = "idle";
236
+ /**
237
+ * Draws the button inside `host`'s shadow root. `host` must not have a
238
+ * shadow root yet; `mountConnectButton` creates one for you.
239
+ */
240
+ export function renderConnectButton(
241
+ host: HTMLElement,
242
+ initial: ConnectButtonProps,
243
+ ): ConnectButtonHandle {
244
+ const root = host.shadowRoot ?? host.attachShadow({ mode: "open" });
245
+ adoptStyles(root);
246
+
247
+ let props = initial;
248
+ let connecting = false;
157
249
  let destroyed = false;
250
+ let lastAnnounced: ConnectButtonState | null = null;
158
251
 
159
- const flow: OneConnectHandle = useOneConnect({
160
- ...options.connect,
161
- onSuccess: () => {
162
- setState("connected");
163
- options.connect.onSuccess?.();
164
- },
165
- onError: (message) => {
166
- setState("idle");
167
- options.connect.onError?.(message);
252
+ const button = el("button", "b");
253
+ button.type = "button";
254
+ button.setAttribute("part", "button");
255
+ const status = el("span", "sr");
256
+ status.setAttribute("role", "status");
257
+ status.setAttribute("aria-live", "polite");
258
+ root.append(button, status);
259
+
260
+ const resting = (): ConnectButtonState => {
261
+ if (props.connected !== undefined)
262
+ return props.connected ? "connected" : "idle";
263
+ return readConnectReturn()?.status === "success" ? "connected" : "idle";
264
+ };
265
+ const state = (): ConnectButtonState =>
266
+ connecting ? "connecting" : resting();
267
+
268
+ // Handlers read `props` when they fire, so an update never needs to
269
+ // replace them.
270
+ const handlers: Pick<
271
+ OneConnectFlowOptions,
272
+ "onSuccess" | "onError" | "onCancel"
273
+ > = {
274
+ onSuccess: () => props.onSuccess?.(),
275
+ onError: (message, code) => props.onError?.(message, code),
276
+ onCancel: () => {
277
+ connecting = false;
278
+ render();
279
+ props.onCancel?.();
168
280
  },
169
- });
281
+ };
282
+ const flow: OneConnectFlow = createConnectFlow(flowOptions(props, handlers));
170
283
 
171
- const button = document.createElement("button");
172
- button.type = "button";
173
- button.className = variant === "block" ? "owcb owcb-block" : "owcb";
174
- button.addEventListener("click", () => {
175
- if (state === "connecting") return;
176
- setState("connecting");
177
- flow.open();
178
- });
284
+ const announce = (next: ConnectButtonState) => {
285
+ if (lastAnnounced === null) {
286
+ lastAnnounced = next; // the first paint is not news
287
+ return;
288
+ }
289
+ if (next === lastAnnounced) return;
290
+ lastAnnounced = next;
291
+ status.textContent =
292
+ next === "connecting"
293
+ ? "Connecting to One…"
294
+ : next === "connected"
295
+ ? (props.connectedLabel ?? "Connected")
296
+ : "";
297
+ };
179
298
 
180
- const titleFor = (): string =>
181
- state === "connecting"
182
- ? "Connecting…"
183
- : state === "connected"
184
- ? connectedLabel
185
- : label;
186
-
187
- const paint = () => {
188
- const accent = variant === "accent";
189
- const block = variant === "block";
190
- const background = block
191
- ? palette.surface
192
- : accent
193
- ? (options.accentColor ?? LIME)
194
- : palette.buttonBackground;
195
- const foreground = block
196
- ? palette.foreground
197
- : accent
198
- ? CARBON
199
- : palette.buttonForeground;
200
- const chipRing = block ? palette.surface : background;
201
-
202
- button.style.background =
203
- state === "connected" && !block ? SPRING : background;
204
- button.style.color = state === "connected" && !block ? CARBON : foreground;
205
- button.style.boxShadow = block
206
- ? `inset 0 0 0 1px ${palette.line}`
207
- : state === "connected"
208
- ? "none"
209
- : accent
210
- ? `0 1px 2px rgba(10,12,11,.1), 0 6px 20px ${options.accentColor ?? LIME}38`
211
- : palette.shadow;
212
- button.disabled = state === "connecting";
213
- button.innerHTML = "";
214
-
215
- const stack = buildStack(options, chipRing);
216
-
217
- if (block) {
218
- const top = document.createElement("span");
219
- top.className = "owcb-block-top";
220
- const title = document.createElement("span");
221
- title.className = "owcb-block-title";
222
- title.textContent = titleFor();
299
+ const render = () => {
300
+ if (destroyed) return;
301
+ const current = state();
302
+ const variant = props.variant ?? "default";
303
+ const text =
304
+ current === "connecting"
305
+ ? "Connecting…"
306
+ : current === "connected"
307
+ ? (props.connectedLabel ?? "Connected")
308
+ : (props.label ?? "Connect your apps");
309
+
310
+ // Colour props travel as custom properties on the host, set through
311
+ // the CSSOM: allowed under a strict CSP, unlike a style attribute.
312
+ if (variant === "accent") {
313
+ const accent = props.accentColor?.trim() || LIME;
314
+ host.style.setProperty("--one-connect-accent", accent);
315
+ host.style.setProperty("--one-connect-accent-fg", readableTextOn(accent));
316
+ } else {
317
+ host.style.removeProperty("--one-connect-accent");
318
+ host.style.removeProperty("--one-connect-accent-fg");
319
+ }
320
+ host.toggleAttribute("data-full-width", Boolean(props.fullWidth));
321
+ host.setAttribute("data-variant", variant);
322
+
323
+ button.dataset.variant = variant;
324
+ button.dataset.size = props.size ?? "md";
325
+ button.dataset.theme = props.theme ?? "light";
326
+ button.dataset.state = current;
327
+ button.toggleAttribute("data-full-width", Boolean(props.fullWidth));
328
+ button.disabled = Boolean(props.disabled);
329
+ // Busy, not disabled, while connecting: a disabled button drops
330
+ // keyboard focus and hides the state from screen readers.
331
+ button.setAttribute("aria-busy", String(current === "connecting"));
332
+ if (current === "connecting") button.setAttribute("aria-disabled", "true");
333
+ else button.removeAttribute("aria-disabled");
334
+
335
+ const stack = current === "idle" ? buildStack(props) : null;
336
+ const leading =
337
+ current === "connecting"
338
+ ? el("span", "spinner")
339
+ : current === "connected"
340
+ ? icon(TICK_SVG)
341
+ : stack;
342
+
343
+ button.replaceChildren();
344
+ button.removeAttribute("aria-labelledby");
345
+ button.removeAttribute("aria-describedby");
346
+
347
+ if (variant === "block") {
348
+ const top = el("span", "top");
349
+ const title = el("span", "title", text);
350
+ title.id = "one-connect-title";
351
+ title.setAttribute("part", "label");
223
352
  top.appendChild(title);
224
- if (state === "connecting") {
225
- const spinner = document.createElement("span");
226
- spinner.className = "owcb-spinner";
227
- top.appendChild(spinner);
228
- } else if (stack) {
229
- top.appendChild(stack);
230
- }
353
+ if (leading) top.appendChild(leading);
231
354
  button.appendChild(top);
232
- if (options.description && state === "idle") {
233
- const sub = document.createElement("span");
234
- sub.className = "owcb-block-sub";
235
- sub.style.color = palette.muted;
236
- sub.textContent = options.description;
355
+ button.setAttribute("aria-labelledby", title.id);
356
+ if (props.description) {
357
+ const sub = el("span", "sub", props.description);
358
+ sub.id = "one-connect-description";
237
359
  button.appendChild(sub);
360
+ button.setAttribute("aria-describedby", sub.id);
238
361
  }
239
- const foot = document.createElement("span");
240
- foot.className = "owcb-block-foot";
241
- foot.style.color = palette.muted;
242
- foot.style.borderTop = `1px solid ${palette.line}`;
243
- foot.innerHTML =
244
- `<span style="font-size:11px;color:${palette.muted}">Secured by</span>` +
245
- `<span style="display:inline-flex;color:${palette.foreground}">${ONE_LOCKUP_SVG}</span>`;
362
+ const foot = el("span", "foot");
363
+ foot.setAttribute("aria-hidden", "true");
364
+ foot.append(el("span", "", "Secured by"), icon(ONE_LOCKUP_SVG, "lockup"));
246
365
  button.appendChild(foot);
247
- return;
248
- }
249
-
250
- if (state === "connecting") {
251
- const spinner = document.createElement("span");
252
- spinner.className = "owcb-spinner";
253
- button.appendChild(spinner);
254
- } else if (state === "connected") {
255
- const tick = document.createElement("span");
256
- tick.innerHTML = TICK_SVG;
257
- tick.style.display = "inline-flex";
258
- button.appendChild(tick);
259
- } else if (stack) {
260
- button.appendChild(stack);
366
+ } else {
367
+ if (leading) button.appendChild(leading);
368
+ const label = el("span", "label", text);
369
+ label.setAttribute("part", "label");
370
+ button.appendChild(label);
371
+ if (current === "idle") button.appendChild(icon(ARROW_SVG, "icon arrow"));
261
372
  }
262
373
 
263
- const text = document.createElement("span");
264
- text.className = "owcb-label";
265
- text.textContent = titleFor();
266
- button.appendChild(text);
267
-
268
- if (state === "idle") {
269
- const arrow = document.createElement("span");
270
- arrow.innerHTML = ARROW_SVG;
271
- arrow.style.display = "inline-flex";
272
- button.appendChild(arrow);
273
- }
374
+ announce(current);
274
375
  };
275
376
 
276
- const setState = (next: ConnectButtonState) => {
277
- if (destroyed) return;
278
- state = next;
279
- paint();
280
- };
377
+ button.addEventListener("click", () => {
378
+ if (destroyed || props.disabled || connecting) return;
379
+ connecting = true;
380
+ render();
381
+ flow.open();
382
+ });
281
383
 
282
- paint();
283
- container.appendChild(button);
384
+ render();
284
385
 
285
386
  return {
286
- setState,
387
+ update: (next) => {
388
+ props = next;
389
+ flow.update(flowOptions(props, handlers));
390
+ render();
391
+ },
287
392
  destroy: () => {
288
393
  destroyed = true;
289
- button.remove();
394
+ flow.destroy();
395
+ root.replaceChildren();
290
396
  },
291
397
  };
292
- };
398
+ }
293
399
 
294
400
  /**
295
- * <one-connect-button>: one tag for any framework. The element wires the
296
- * whole flow from its attributes and manages Connect, Connecting and
297
- * Connected. Registered automatically in the browser on import; defined
298
- * inside a function so importing this module on a server never touches
299
- * HTMLElement.
300
- *
301
- * Attributes: authorize-url (required), app-theme, label, variant,
302
- * theme, platforms ("stripe, notion" or a JSON array),
303
- * description, accent-color, connected-label.
304
- * Events: "success" and "error" (detail: message), plus the matching
305
- * onSuccess and onError function properties that React 19, Vue and
306
- * Svelte set naturally as props.
401
+ * Mounts the button into `container`: React, Vue, Svelte and plain DOM
402
+ * all use this. The button lives in its own host element,
403
+ * `<span class="one-connect">`, whose `::part(button)` the page may style.
404
+ */
405
+ export function mountConnectButton(
406
+ container: HTMLElement,
407
+ props: ConnectButtonProps,
408
+ ): ConnectButtonHandle {
409
+ const host = document.createElement("span");
410
+ host.className = "one-connect";
411
+ container.appendChild(host);
412
+ const handle = renderConnectButton(host, props);
413
+ return {
414
+ update: handle.update,
415
+ destroy: () => {
416
+ handle.destroy();
417
+ host.remove();
418
+ },
419
+ };
420
+ }
421
+
422
+ /* ── <one-connect-button> ─────────────────────────────────────────── */
423
+
424
+ const ATTRIBUTES = [
425
+ "authorize-url",
426
+ "platforms",
427
+ "connected",
428
+ "disabled",
429
+ "variant",
430
+ "size",
431
+ "full-width",
432
+ "theme",
433
+ "connect-theme",
434
+ "app-theme",
435
+ "accent-color",
436
+ "label",
437
+ "connected-label",
438
+ "description",
439
+ ] as const;
440
+
441
+ /** An attribute narrowed to a known set of values; anything else is
442
+ * treated as unset rather than passed through. */
443
+ function attributeAs<T extends string>(
444
+ element: HTMLElement,
445
+ name: string,
446
+ allowed: readonly T[],
447
+ ): T | undefined {
448
+ const value = element.getAttribute(name);
449
+ return value && (allowed as readonly string[]).includes(value)
450
+ ? (value as T)
451
+ : undefined;
452
+ }
453
+
454
+ /** A boolean attribute: present means true, except the literal "false". */
455
+ const flag = (element: HTMLElement, name: string): boolean | undefined =>
456
+ element.hasAttribute(name)
457
+ ? element.getAttribute(name) !== "false"
458
+ : undefined;
459
+
460
+ /**
461
+ * <one-connect-button>: one tag for any framework, registered on import
462
+ * in the browser. Attributes mirror the props in kebab case; `connected`,
463
+ * `disabled` and `full-width` are boolean attributes. Events: `success`,
464
+ * `error` (detail: { message, code }) and `cancel`, plus the matching
465
+ * onSuccess / onError / onCancel properties that React 19, Vue and
466
+ * Svelte set naturally as props. Style it with `::part(button)`.
307
467
  */
308
468
  export function registerConnectButton(): void {
309
469
  if (typeof window === "undefined" || typeof customElements === "undefined")
@@ -311,25 +471,16 @@ export function registerConnectButton(): void {
311
471
  if (customElements.get("one-connect-button")) return;
312
472
 
313
473
  class OneConnectButtonElement extends HTMLElement {
314
- static observedAttributes = [
315
- "authorize-url",
316
- "app-theme",
317
- "label",
318
- "variant",
319
- "theme",
320
- "platforms",
321
- "description",
322
- "accent-color",
323
- "connected-label",
324
- ];
474
+ static observedAttributes = [...ATTRIBUTES];
325
475
 
326
476
  onSuccess: (() => void) | null = null;
327
- onError: ((message: string) => void) | null = null;
477
+ onError: ConnectButtonProps["onError"] | null = null;
478
+ onCancel: (() => void) | null = null;
328
479
 
329
480
  private handle: ConnectButtonHandle | null = null;
330
481
 
331
482
  connectedCallback(): void {
332
- this.mount();
483
+ this.sync();
333
484
  }
334
485
 
335
486
  disconnectedCallback(): void {
@@ -338,53 +489,57 @@ export function registerConnectButton(): void {
338
489
  }
339
490
 
340
491
  attributeChangedCallback(): void {
341
- if (this.isConnected && this.handle) this.mount();
492
+ if (this.isConnected) this.sync();
342
493
  }
343
494
 
344
- private mount(): void {
345
- this.handle?.destroy();
346
- this.handle = null;
347
- const authorizeUrl = this.getAttribute("authorize-url");
348
- if (!authorizeUrl) return;
349
-
350
- this.handle = mountConnectButton(this, {
351
- connect: {
352
- authorizeUrl,
353
- appTheme: attributeAs(this, "app-theme", ["light", "dark"]),
354
- onSuccess: () => {
355
- this.onSuccess?.();
356
- this.dispatchEvent(new CustomEvent("success"));
357
- },
358
- onError: (message) => {
359
- this.onError?.(message);
360
- this.dispatchEvent(new CustomEvent("error", { detail: message }));
361
- },
362
- },
363
- label: this.getAttribute("label") ?? undefined,
364
- variant: attributeAs(this, "variant", ["default", "accent", "block"]),
365
- theme: attributeAs(this, "theme", ["light", "dark"]),
495
+ private props(authorizeUrl: string): ConnectButtonProps {
496
+ return {
497
+ authorizeUrl,
366
498
  platforms: parsePlatformsAttribute(this.getAttribute("platforms")),
367
- description: this.getAttribute("description") ?? undefined,
499
+ connected: flag(this, "connected"),
500
+ disabled: flag(this, "disabled"),
501
+ variant: attributeAs(this, "variant", ["default", "accent", "block"]),
502
+ size: attributeAs(this, "size", ["sm", "md", "lg"]),
503
+ fullWidth: flag(this, "full-width"),
504
+ theme: attributeAs(this, "theme", ["light", "dark", "auto"]),
505
+ connectTheme:
506
+ attributeAs(this, "connect-theme", ["light", "dark"]) ??
507
+ attributeAs(this, "app-theme", ["light", "dark"]),
368
508
  accentColor: this.getAttribute("accent-color") ?? undefined,
509
+ label: this.getAttribute("label") ?? undefined,
369
510
  connectedLabel: this.getAttribute("connected-label") ?? undefined,
370
- });
511
+ description: this.getAttribute("description") ?? undefined,
512
+ onSuccess: () => {
513
+ this.onSuccess?.();
514
+ this.dispatchEvent(new CustomEvent("success"));
515
+ },
516
+ onError: (message, code) => {
517
+ this.onError?.(message, code);
518
+ this.dispatchEvent(
519
+ new CustomEvent("error", { detail: { message, code } }),
520
+ );
521
+ },
522
+ onCancel: () => {
523
+ this.onCancel?.();
524
+ this.dispatchEvent(new CustomEvent("cancel"));
525
+ },
526
+ };
527
+ }
528
+
529
+ private sync(): void {
530
+ const authorizeUrl = this.getAttribute("authorize-url");
531
+ if (!authorizeUrl) {
532
+ this.handle?.destroy();
533
+ this.handle = null;
534
+ return;
535
+ }
536
+ const props = this.props(authorizeUrl);
537
+ if (this.handle) this.handle.update(props);
538
+ else this.handle = renderConnectButton(this, props);
371
539
  }
372
540
  }
373
541
 
374
542
  customElements.define("one-connect-button", OneConnectButtonElement);
375
543
  }
376
544
 
377
- /** An attribute narrowed to a known set of values; anything else is
378
- * treated as unset rather than passed through. */
379
- function attributeAs<T extends string>(
380
- element: HTMLElement,
381
- name: string,
382
- allowed: readonly T[],
383
- ): T | undefined {
384
- const value = element.getAttribute(name);
385
- return value && (allowed as readonly string[]).includes(value)
386
- ? (value as T)
387
- : undefined;
388
- }
389
-
390
545
  registerConnectButton();