@glassware/core 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.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Glassware contributors
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
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Glassware contributors
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
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,13 +1,197 @@
1
- # @glassware/core
2
-
3
- Frosted and watery glass buttons for the web, with text color chosen
4
- automatically from what sits behind them.
5
-
6
- ```js
7
- import '@glassware/core/glassware.css';
8
- import { attach } from '@glassware/core';
9
-
10
- attach(document.querySelector('button'));
11
- ```
12
-
13
- Full documentation is coming with v0.1 on the project page.
1
+ # Glassware
2
+
3
+ Frosted and watery glass buttons for the web, with text color that adapts to the exact part of the background behind each button.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@glassware/core)](https://www.npmjs.com/package/@glassware/core)
6
+ [![CI](https://github.com/Takreem-Jaffery/glassware/actions/workflows/ci.yml/badge.svg)](https://github.com/Takreem-Jaffery/glassware/actions/workflows/ci.yml)
7
+ [![license](https://img.shields.io/npm/l/@glassware/core)](https://github.com/Takreem-Jaffery/glassware/blob/main/packages/core/LICENSE)
8
+
9
+ ![Glass buttons over an animated sunset glow, with a control panel on the right](https://raw.githubusercontent.com/Takreem-Jaffery/glassware/main/docs/images/playground-sunset.jpg)
10
+
11
+ Glass looks great until the page behind it changes: a dark photo, a bright gradient, a video. Glassware
12
+ styles a button as glass and keeps its text readable by measuring what is actually behind it, so the
13
+ text switches between light and dark by itself, even as the button moves or the background animates.
14
+
15
+ - **Two glass types.** Frosted (blur) everywhere, and watery (blur plus a rippling distortion) where the browser supports it, falling back to frosted on its own.
16
+ - **Reads the exact spot under the button.** Not the page average: a button over the dark corner of a photo and the same button over its bright corner get different text colors.
17
+ - **Works with images, gradients, layered and translucent backgrounds,** and with canvas, video or WebGL through a one-function hook.
18
+ - **Small and dependency-free.** Around 9 KB gzipped, TypeScript types included, respects `prefers-reduced-motion`.
19
+
20
+ ![A glass button over a bright cyan blob, with dark text chosen automatically](https://raw.githubusercontent.com/Takreem-Jaffery/glassware/main/docs/images/playground-blobs.jpg)
21
+
22
+ ## Install
23
+
24
+ ```sh
25
+ npm install @glassware/core
26
+ ```
27
+
28
+ ## Quick start
29
+
30
+ ```js
31
+ import '@glassware/core/glassware.css';
32
+ import { attach } from '@glassware/core';
33
+
34
+ attach(document.querySelector('button'));
35
+ ```
36
+
37
+ `attach()` gives the element glass styling and chooses its text color. With a config:
38
+
39
+ ```js
40
+ attach(button, {
41
+ type: 'watery',
42
+ transparency: 0.1,
43
+ blur: 6,
44
+ radius: 24,
45
+ });
46
+ ```
47
+
48
+ The stylesheet also works on its own, with no JavaScript, as a frosted glass button:
49
+
50
+ ```html
51
+ <button class="glass">Frosted</button>
52
+ ```
53
+
54
+ ## Options
55
+
56
+ Every option is optional. Numbers outside their range are clamped; invalid values are ignored and the
57
+ default applies.
58
+
59
+ | Option | Default | Range | Notes |
60
+ | --- | --- | --- | --- |
61
+ | `type` | `'frosted'` | `'frosted'`, `'watery'` | `'watery'` falls back to frosted when the browser can't do it |
62
+ | `transparency` | `0.18` (frosted), `0.08` (watery) | 0.05 to 0.45 | Opacity of the tint fill |
63
+ | `tint` | white | any CSS color | Color of the glass fill |
64
+ | `blur` | `12` (frosted), `4` (watery) | 4 to 24 | Backdrop blur in px |
65
+ | `radius` | `12` | 0 to 32 | Corner radius in px |
66
+ | `edgeReflection` | `true` | `true`, `false` | The thin light edge on the glass |
67
+ | `contrastMode` | `'auto'` | `'auto'`, `'light'`, `'dark'` | `'light'` or `'dark'` declares the **background** behind the button |
68
+ | `distortionScale` | `40` | 10 to 50 | Watery only: strength of the ripple |
69
+ | `distortionSpeed` | `8` | 4 to 16 | Watery only: seconds per ripple cycle |
70
+
71
+ ## Text color
72
+
73
+ By default (`contrastMode: 'auto'`) Glassware measures the part of the background directly under the
74
+ button and picks whichever text color, dark or light, has the higher contrast against it. That means
75
+ dark text on light and on mid-tone backgrounds, and light text only on genuinely dark ones. It re-measures
76
+ when you call `refresh()`, and the color change fades rather than snaps. Near the switching point the text
77
+ holds its current color until the background clearly changes, so it doesn't flicker on a soft gradient.
78
+
79
+ When the background moves under a button, call `refresh()`:
80
+
81
+ ```js
82
+ const glass = attach(button);
83
+
84
+ window.addEventListener('scroll', () => glass.refresh());
85
+ ```
86
+
87
+ You can also state what is behind the button instead of measuring. These describe the **background**,
88
+ so `light` means a light background and the text becomes dark:
89
+
90
+ ```html
91
+ <button class="glass" data-glass-mode="light">Over a light photo</button>
92
+ ```
93
+
94
+ ```js
95
+ attach(button, { contrastMode: 'dark' }); // dark background, light text
96
+ ```
97
+
98
+ The order Glassware uses: `contrastMode` in the config, then `data-glass-mode`, then your
99
+ `sampleBackground` function, then the measured CSS background, then a `data-glass-bg="#112233"` hint,
100
+ then dark text.
101
+
102
+ ### Canvas, video and WebGL backgrounds
103
+
104
+ CSS can't describe what is painted on a canvas, so tell Glassware what is behind the button with
105
+ `sampleBackground`. It gets the button's rectangle (viewport coordinates) and returns the pixels under
106
+ it, a single color, or `null` to fall back to measuring the CSS:
107
+
108
+ ```js
109
+ attach(button, {}, {
110
+ sampleBackground(rect) {
111
+ const { data, width, height } = ctx.getImageData(rect.left, rect.top, rect.width, rect.height);
112
+ return { data, width, height }; // RGBA, 4 values per pixel
113
+ },
114
+ });
115
+ ```
116
+
117
+ The rectangle is in viewport coordinates, so subtract the canvas's own position
118
+ (`canvas.getBoundingClientRect()`) if it isn't at the top-left of the window. Call `glass.refresh()`
119
+ as the canvas changes, for example on a timer or each animation frame.
120
+
121
+ ### What gets measured
122
+
123
+ Glassware reads the backgrounds of the button's ancestors (colors, images, gradients, layered and
124
+ translucent backgrounds, and the page background), including `background-size`, `-position`, `-repeat`
125
+ and `-attachment`. Known limits:
126
+
127
+ - An element that overlaps the button but is not one of its ancestors can't be seen. Use `sampleBackground` for those.
128
+ - Images from another origin need CORS headers to be readable. Otherwise Glassware falls back to a rougher estimate of the whole background, then to `data-glass-bg`.
129
+ - `calc()` in positions, `image-set()`, conic gradients and `background-blend-mode` aren't modelled and fall back the same way.
130
+
131
+ ## The handle
132
+
133
+ `attach()` returns a handle:
134
+
135
+ ```js
136
+ const glass = attach(button, { type: 'frosted' });
137
+
138
+ glass.ready; // Promise: the text color has been decided
139
+ glass.update({ blur: 18 }); // change settings (keys you leave out keep their value)
140
+ glass.refresh(); // measure the background again
141
+ glass.measurement; // { luminance, color, source } from the latest measurement
142
+ glass.config; // the validated config currently applied
143
+ glass.detach(); // remove everything attach() added
144
+ ```
145
+
146
+ Calling `attach()` again on an element that is already attached returns the same handle and only
147
+ updates its settings. The `sampleBackground` option is read on the first call, so to change it,
148
+ `detach()` first.
149
+
150
+ `measurement.source` says where the number came from: `'custom'` (your sampler), `'region'` (the CSS
151
+ background under the button), `'color'`, `'image'` (a whole-image estimate) or `'hint'`.
152
+
153
+ ## Browser support
154
+
155
+ Frosted glass works in current versions of the major browsers. Watery glass needs a browser that
156
+ accepts SVG filters inside `backdrop-filter`, which today means Chromium-based browsers such as Chrome
157
+ and Edge. Elsewhere, buttons set to `type: 'watery'` quietly stay frosted. `supportsWateryGlass()`
158
+ tells you which you got.
159
+
160
+ ## Try it
161
+
162
+ The playground lets you drag a button over different backgrounds, upload your own image, tune every
163
+ option and copy the code. Run it locally:
164
+
165
+ ```sh
166
+ git clone https://github.com/Takreem-Jaffery/glassware.git
167
+ cd glassware
168
+ npm install
169
+ npm run dev
170
+ ```
171
+
172
+ A hosted project page and full documentation are planned.
173
+
174
+ ## Status and roadmap
175
+
176
+ Glassware is at **v0.1**: web only. Planned, roughly in this order: button sound, a "crackable" glass
177
+ effect, glass on any element (not just buttons), then Android and iOS versions that share the same
178
+ configuration and behavior ([SPEC.md](https://github.com/Takreem-Jaffery/glassware/blob/main/SPEC.md)).
179
+
180
+ ## Contributing
181
+
182
+ ```sh
183
+ npm install
184
+ npm test # unit tests
185
+ npm run test:oracle # compares measured brightness to real pixels in Chromium
186
+ npm run test:playground
187
+ npm run build
188
+ ```
189
+
190
+ Both browser tests need Chromium once: `npx playwright install chromium`.
191
+
192
+ If your change affects users, add a note with `npm run changeset` (pick patch, minor or major). Merged
193
+ notes become a "version packages" pull request that releases to npm.
194
+
195
+ ## License
196
+
197
+ [MIT](https://github.com/Takreem-Jaffery/glassware/blob/main/packages/core/LICENSE)
@@ -1,124 +1,124 @@
1
- /*
2
- * Glassware core styles.
3
- * Token names and defaults follow SPEC.md §2-§4.
4
- *
5
- * Works with zero JavaScript as a frosted-glass button. attach() layers on:
6
- * - clamping token values to the ranges in SPEC §2
7
- * - data-glass-type="watery" (only after support is detected and the
8
- * SVG filter has been injected, so the fallback is always frosted)
9
- * - data-glass-edge="off" (when edgeReflection is false)
10
- * - --glass-text-color (set inline from the sampled background)
11
- * - --glass-distortion-filter (points at the per-element SVG filter)
12
- */
13
-
14
- .glass {
15
- /* SPEC §2: visual tokens */
16
- --glass-blur: 12px;
17
- --glass-transparency: 0.18;
18
- --glass-tint: rgba(255, 255, 255, 1);
19
- --glass-radius: 12px;
20
-
21
- /* SPEC §2: watery only. SVG filter attributes can't read CSS variables,
22
- so attach() reads these two and builds the filter with matching values. */
23
- --glass-distortion-scale: 40;
24
- --glass-distortion-speed: 8s;
25
-
26
- /* SPEC §3: edge reflection (layered inset box-shadows) */
27
- --glass-edge-highlight: inset 1px 1px 1px rgba(255, 255, 255, 0.6);
28
- --glass-edge-shadow: inset -1px -1px 1px rgba(0, 0, 0, 0.15);
29
-
30
- /* SPEC §4: text colors, and the one that's currently in use.
31
- With no JS the default is dark text, matching a light page. */
32
- --glass-text-dark: #1a1a1a;
33
- --glass-text-light: #f5f5f5;
34
- --glass-text-color: var(--glass-text-dark);
35
-
36
- /* Filter used by the watery type. attach() overrides this inline when
37
- scale or speed differ from the defaults. */
38
- --glass-distortion-filter: url(#glassware-displacement);
39
-
40
- position: relative;
41
- display: inline-flex;
42
- align-items: center;
43
- justify-content: center;
44
- box-sizing: border-box;
45
- padding: 0.6em 1.2em;
46
- border: 0;
47
- border-radius: var(--glass-radius);
48
- font: inherit;
49
- color: var(--glass-text-color);
50
- cursor: pointer;
51
- -webkit-tap-highlight-color: transparent;
52
-
53
- /* Static fallback first for browsers without color-mix(); the tint is
54
- then mixed at `transparency` alpha (SPEC §2). */
55
- background: rgba(255, 255, 255, 0.18);
56
- background: color-mix(
57
- in srgb,
58
- var(--glass-tint) calc(var(--glass-transparency) * 100%),
59
- transparent
60
- );
61
-
62
- /* SPEC §1: frosted is the universal default */
63
- -webkit-backdrop-filter: blur(var(--glass-blur));
64
- backdrop-filter: blur(var(--glass-blur));
65
-
66
- box-shadow: var(--glass-edge-highlight), var(--glass-edge-shadow);
67
- transition: transform 0.15s ease, color 0.2s ease;
68
- }
69
-
70
- /* SPEC §4 CSS-only fallback: the developer declares what is BEHIND the
71
- element. A light background gets dark text and vice versa. */
72
- .glass[data-glass-mode="light"] {
73
- --glass-text-color: var(--glass-text-dark);
74
- }
75
-
76
- .glass[data-glass-mode="dark"] {
77
- --glass-text-color: var(--glass-text-light);
78
- }
79
-
80
- /* SPEC §3: edge reflection can be switched off */
81
- .glass[data-glass-edge="off"] {
82
- box-shadow: none;
83
- }
84
-
85
- /* SPEC §1: watery = blur + SVG displacement. Gated by @supports as a second
86
- safety net; if the browser can't do it, the rule is ignored and the
87
- element simply stays frosted.
88
-
89
- Watery is meant to be see-through, so it starts from a lower blur and fill
90
- than frosted (SPEC §2). Values set by the developer or by attach() are
91
- inline and still win over these defaults. */
92
- @supports (backdrop-filter: url(#x)) {
93
- .glass[data-glass-type="watery"] {
94
- --glass-blur: 4px;
95
- --glass-transparency: 0.08;
96
-
97
- -webkit-backdrop-filter: blur(var(--glass-blur)) var(--glass-distortion-filter);
98
- backdrop-filter: blur(var(--glass-blur)) var(--glass-distortion-filter);
99
- }
100
- }
101
-
102
- .glass:active {
103
- transform: scale(0.97);
104
- }
105
-
106
- .glass:focus-visible {
107
- outline: 2px solid var(--glass-text-color);
108
- outline-offset: 2px;
109
- }
110
-
111
- .glass:disabled {
112
- cursor: not-allowed;
113
- opacity: 0.55;
114
- }
115
-
116
- @media (prefers-reduced-motion: reduce) {
117
- .glass {
118
- transition: none;
119
- }
120
-
121
- .glass:active {
122
- transform: none;
123
- }
1
+ /*
2
+ * Glassware core styles.
3
+ * Token names and defaults follow SPEC.md §2-§4.
4
+ *
5
+ * Works with zero JavaScript as a frosted-glass button. attach() layers on:
6
+ * - clamping token values to the ranges in SPEC §2
7
+ * - data-glass-type="watery" (only after support is detected and the
8
+ * SVG filter has been injected, so the fallback is always frosted)
9
+ * - data-glass-edge="off" (when edgeReflection is false)
10
+ * - --glass-text-color (set inline from the sampled background)
11
+ * - --glass-distortion-filter (points at the per-element SVG filter)
12
+ */
13
+
14
+ .glass {
15
+ /* SPEC §2: visual tokens */
16
+ --glass-blur: 12px;
17
+ --glass-transparency: 0.18;
18
+ --glass-tint: rgba(255, 255, 255, 1);
19
+ --glass-radius: 12px;
20
+
21
+ /* SPEC §2: watery only. SVG filter attributes can't read CSS variables,
22
+ so attach() reads these two and builds the filter with matching values. */
23
+ --glass-distortion-scale: 40;
24
+ --glass-distortion-speed: 8s;
25
+
26
+ /* SPEC §3: edge reflection (layered inset box-shadows) */
27
+ --glass-edge-highlight: inset 1px 1px 1px rgba(255, 255, 255, 0.6);
28
+ --glass-edge-shadow: inset -1px -1px 1px rgba(0, 0, 0, 0.15);
29
+
30
+ /* SPEC §4: text colors, and the one that's currently in use.
31
+ With no JS the default is dark text, matching a light page. */
32
+ --glass-text-dark: #1a1a1a;
33
+ --glass-text-light: #f5f5f5;
34
+ --glass-text-color: var(--glass-text-dark);
35
+
36
+ /* Filter used by the watery type. attach() overrides this inline when
37
+ scale or speed differ from the defaults. */
38
+ --glass-distortion-filter: url(#glassware-displacement);
39
+
40
+ position: relative;
41
+ display: inline-flex;
42
+ align-items: center;
43
+ justify-content: center;
44
+ box-sizing: border-box;
45
+ padding: 0.6em 1.2em;
46
+ border: 0;
47
+ border-radius: var(--glass-radius);
48
+ font: inherit;
49
+ color: var(--glass-text-color);
50
+ cursor: pointer;
51
+ -webkit-tap-highlight-color: transparent;
52
+
53
+ /* Static fallback first for browsers without color-mix(); the tint is
54
+ then mixed at `transparency` alpha (SPEC §2). */
55
+ background: rgba(255, 255, 255, 0.18);
56
+ background: color-mix(
57
+ in srgb,
58
+ var(--glass-tint) calc(var(--glass-transparency) * 100%),
59
+ transparent
60
+ );
61
+
62
+ /* SPEC §1: frosted is the universal default */
63
+ -webkit-backdrop-filter: blur(var(--glass-blur));
64
+ backdrop-filter: blur(var(--glass-blur));
65
+
66
+ box-shadow: var(--glass-edge-highlight), var(--glass-edge-shadow);
67
+ transition: transform 0.15s ease, color 0.2s ease;
68
+ }
69
+
70
+ /* SPEC §4 CSS-only fallback: the developer declares what is BEHIND the
71
+ element. A light background gets dark text and vice versa. */
72
+ .glass[data-glass-mode="light"] {
73
+ --glass-text-color: var(--glass-text-dark);
74
+ }
75
+
76
+ .glass[data-glass-mode="dark"] {
77
+ --glass-text-color: var(--glass-text-light);
78
+ }
79
+
80
+ /* SPEC §3: edge reflection can be switched off */
81
+ .glass[data-glass-edge="off"] {
82
+ box-shadow: none;
83
+ }
84
+
85
+ /* SPEC §1: watery = blur + SVG displacement. Gated by @supports as a second
86
+ safety net; if the browser can't do it, the rule is ignored and the
87
+ element simply stays frosted.
88
+
89
+ Watery is meant to be see-through, so it starts from a lower blur and fill
90
+ than frosted (SPEC §2). Values set by the developer or by attach() are
91
+ inline and still win over these defaults. */
92
+ @supports (backdrop-filter: url(#x)) {
93
+ .glass[data-glass-type="watery"] {
94
+ --glass-blur: 4px;
95
+ --glass-transparency: 0.08;
96
+
97
+ -webkit-backdrop-filter: blur(var(--glass-blur)) var(--glass-distortion-filter);
98
+ backdrop-filter: blur(var(--glass-blur)) var(--glass-distortion-filter);
99
+ }
100
+ }
101
+
102
+ .glass:active {
103
+ transform: scale(0.97);
104
+ }
105
+
106
+ .glass:focus-visible {
107
+ outline: 2px solid var(--glass-text-color);
108
+ outline-offset: 2px;
109
+ }
110
+
111
+ .glass:disabled {
112
+ cursor: not-allowed;
113
+ opacity: 0.55;
114
+ }
115
+
116
+ @media (prefers-reduced-motion: reduce) {
117
+ .glass {
118
+ transition: none;
119
+ }
120
+
121
+ .glass:active {
122
+ transform: none;
123
+ }
124
124
  }
package/dist/index.cjs CHANGED
@@ -90,10 +90,12 @@ var TEXT_COLORS = {
90
90
  function textModeForTone(tone) {
91
91
  return tone === "light" ? "dark" : "light";
92
92
  }
93
- function pickTextMode(luminance, threshold = 0.5) {
93
+ var DEFAULT_THRESHOLD = 0.19;
94
+ var HYSTERESIS_MARGIN = 0.03;
95
+ function pickTextMode(luminance, threshold = DEFAULT_THRESHOLD) {
94
96
  return luminance > threshold ? "dark" : "light";
95
97
  }
96
- function pickTextModeSticky(luminance, previous, threshold = 0.5, margin = 0.04) {
98
+ function pickTextModeSticky(luminance, previous, threshold = DEFAULT_THRESHOLD, margin = HYSTERESIS_MARGIN) {
97
99
  if (previous === "dark") return luminance > threshold - margin ? "dark" : "light";
98
100
  if (previous === "light") return luminance > threshold + margin ? "dark" : "light";
99
101
  return pickTextMode(luminance, threshold);