@link-foundation/ui-themes 0.0.0-stage → 0.0.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 (110) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +121 -2
  3. package/package.json +52 -4
  4. package/src/classic/catalog.js +193 -0
  5. package/src/classic/effect-governor.js +199 -0
  6. package/src/classic/effect-quality.js +76 -0
  7. package/src/classic/frame-color.js +27 -0
  8. package/src/classic/frame-css-controls.js +65 -0
  9. package/src/classic/frame-css-corners.js +138 -0
  10. package/src/classic/frame-css-edge-geometry.js +74 -0
  11. package/src/classic/frame-css-edges.js +99 -0
  12. package/src/classic/frame-css-enums.js +65 -0
  13. package/src/classic/frame-css-geometry.js +133 -0
  14. package/src/classic/frame-css-modes.js +45 -0
  15. package/src/classic/frame-css-states.js +85 -0
  16. package/src/classic/frame-css-values.js +156 -0
  17. package/src/classic/frame-css.js +232 -0
  18. package/src/classic/frame-geometry.js +189 -0
  19. package/src/classic/frame-schema.js +328 -0
  20. package/src/classic/frame-vocabulary.js +10 -0
  21. package/src/classic/frames.d.ts +143 -0
  22. package/src/classic/frames.js +77 -0
  23. package/src/classic/material-visibility.js +94 -0
  24. package/src/classic/materials.d.ts +133 -0
  25. package/src/classic/materials.js +139 -0
  26. package/src/classic/optical-css.js +47 -0
  27. package/src/classic/optics.js +107 -0
  28. package/src/classic/refraction.js +145 -0
  29. package/src/fluent2/brand-aliases.js +157 -0
  30. package/src/fluent2/brand.js +84 -0
  31. package/src/fluent2/names.js +31 -0
  32. package/src/index.d.ts +54 -0
  33. package/src/index.js +79 -0
  34. package/src/mapping.js +135 -0
  35. package/src/material3/mapping.js +18 -0
  36. package/src/material3/mui.js +128 -0
  37. package/src/material3/seed.js +144 -0
  38. package/themes/base/theme.lino +35 -0
  39. package/themes/classic/README.md +90 -0
  40. package/themes/classic/deepcase/2025.lino +156 -0
  41. package/themes/classic/deepcase/classic.lino +86 -0
  42. package/themes/classic/deepcase/dark.lino +87 -0
  43. package/themes/classic/deepcase/deepcase-0.1.lino +63 -0
  44. package/themes/classic/deepcase/handler-windows.lino +22 -0
  45. package/themes/classic/deepcase/landing.lino +60 -0
  46. package/themes/classic/deepcase/perception.lino +88 -0
  47. package/themes/classic/formal-ai/chat-bubbles.lino +40 -0
  48. package/themes/classic/formal-ai/chat-cards.lino +40 -0
  49. package/themes/classic/formal-ai/chat-compact.lino +40 -0
  50. package/themes/classic/formal-ai/composer.lino +132 -0
  51. package/themes/classic/formal-ai/fa-contrast.lino +79 -0
  52. package/themes/classic/formal-ai/fa-flat.lino +79 -0
  53. package/themes/classic/formal-ai/fa-glass.lino +79 -0
  54. package/themes/classic/formal-ai/fa-material.lino +79 -0
  55. package/themes/classic/immortality/arwes.lino +59 -0
  56. package/themes/classic/immortality/palettes.lino +968 -0
  57. package/themes/classic/immortality/poseidon.lino +59 -0
  58. package/themes/classic/immortality/quiet.lino +59 -0
  59. package/themes/classic/konard/futuristic.lino +89 -0
  60. package/themes/classic/konard/glass.lino +89 -0
  61. package/themes/classic/konard/liquid.lino +89 -0
  62. package/themes/classic/meta-sovereign/chat-signal.lino +68 -0
  63. package/themes/classic/meta-sovereign/chat-t3.lino +68 -0
  64. package/themes/classic/meta-sovereign/chat-telegram.lino +74 -0
  65. package/themes/classic/meta-sovereign/chat-whatsapp.lino +68 -0
  66. package/themes/fluent2/LICENSE.txt +15 -0
  67. package/themes/fluent2/brand-aliases.lino +101 -0
  68. package/themes/fluent2/colors-dark.lino +1124 -0
  69. package/themes/fluent2/colors-high-contrast.lino +1109 -0
  70. package/themes/fluent2/colors.lino +1489 -0
  71. package/themes/fluent2/fluent-mapping.lino +120 -0
  72. package/themes/fluent2/motion-dark.lino +62 -0
  73. package/themes/fluent2/motion-high-contrast.lino +62 -0
  74. package/themes/fluent2/motion.lino +70 -0
  75. package/themes/fluent2/shadow-dark.lino +50 -0
  76. package/themes/fluent2/shadow-high-contrast.lino +50 -0
  77. package/themes/fluent2/shadow.lino +49 -0
  78. package/themes/fluent2/shape-dark.lino +47 -0
  79. package/themes/fluent2/shape-high-contrast.lino +47 -0
  80. package/themes/fluent2/shape.lino +46 -0
  81. package/themes/fluent2/spacing-dark.lino +68 -0
  82. package/themes/fluent2/spacing-high-contrast.lino +68 -0
  83. package/themes/fluent2/spacing.lino +67 -0
  84. package/themes/fluent2/styles.lino +511 -0
  85. package/themes/fluent2/templates.lino +60 -0
  86. package/themes/fluent2/theme.lino +31 -0
  87. package/themes/fluent2/type-dark.lino +83 -0
  88. package/themes/fluent2/type-high-contrast.lino +83 -0
  89. package/themes/fluent2/type.lino +82 -0
  90. package/themes/hig/colors.lino +54 -0
  91. package/themes/hig/materials.lino +7 -0
  92. package/themes/hig/motion.lino +13 -0
  93. package/themes/hig/shape.lino +5 -0
  94. package/themes/hig/spacing.lino +11 -0
  95. package/themes/hig/styles.lino +143 -0
  96. package/themes/hig/swiftui-mapping.lino +80 -0
  97. package/themes/hig/templates.lino +36 -0
  98. package/themes/hig/theme.lino +3 -0
  99. package/themes/hig/type.lino +26 -0
  100. package/themes/hig-ios/theme.lino +5 -0
  101. package/themes/hig-macos/theme.lino +5 -0
  102. package/themes/material3/colors.lino +320 -0
  103. package/themes/material3/elevation.lino +110 -0
  104. package/themes/material3/motion.lino +102 -0
  105. package/themes/material3/mui-mapping.lino +216 -0
  106. package/themes/material3/shape.lino +16 -0
  107. package/themes/material3/styles.lino +498 -0
  108. package/themes/material3/templates.lino +55 -0
  109. package/themes/material3/theme.lino +7 -0
  110. package/themes/material3/type.lino +336 -0
@@ -0,0 +1,133 @@
1
+ /// <reference lib="dom" />
2
+ export interface Material {
3
+ color: string;
4
+ opacity: number;
5
+ blur: number;
6
+ noise: number;
7
+ refraction: number;
8
+ saturation?: number;
9
+ brightness?: number;
10
+ contrast?: number;
11
+ tint?: string;
12
+ 'tint-level'?: number;
13
+ highlight?: number;
14
+ depth?: number;
15
+ 'depth-blur'?: number;
16
+ shadow?: number;
17
+ 'shadow-blur'?: number;
18
+ 'shadow-distance'?: number;
19
+ 'shadow-spread'?: number;
20
+ sheen?: number;
21
+ 'sheen-angle'?: number;
22
+ 'light-angle'?: number;
23
+ 'border-width'?: number;
24
+ 'border-light'?: number;
25
+ }
26
+ export interface ViewerPreferences {
27
+ motion?: boolean | 'auto' | 'on' | 'off';
28
+ sound?: boolean;
29
+ volume?: number;
30
+ transparency?: boolean | 'auto' | 'on' | 'off';
31
+ reducedTransparency?: boolean;
32
+ refractionProvider?: RefractionProvider;
33
+ }
34
+ export interface EffectState extends ViewerPreferences {
35
+ visible?: boolean;
36
+ reducedMotion?: boolean;
37
+ }
38
+ export function readMaterial(text: string): Material;
39
+ export function materialStyle(
40
+ material: Material,
41
+ preferences?: ViewerPreferences
42
+ ): Record<string, string>;
43
+ export function applyAppearancePreset<
44
+ T extends { preferences: ViewerPreferences },
45
+ >(current: T, preset: Partial<T>, keys?: (keyof T)[]): T;
46
+ /** Independent quality multipliers, monotonically decreasing until removal. */
47
+ export interface EffectQuality {
48
+ scale: number;
49
+ fps: number;
50
+ }
51
+ export interface EffectOptions {
52
+ /** Target draw rate, 5..60. Omit for every frame at full quality, then a 60 fps base. */
53
+ fps?: number;
54
+ /** Disable resolution degradation for crisp layers. Default true. */
55
+ resolution?: boolean;
56
+ /** Resolution floor (0, 1], default 0.4. */
57
+ minScale?: number;
58
+ /** Resolution multiplier (0, 1), default 0.7. */
59
+ scaleFactor?: number;
60
+ /** Frame-rate share floor (0, 1], default 0.25. */
61
+ minFps?: number;
62
+ /** Frame-rate share multiplier (0, 1), default 0.5. */
63
+ fpsFactor?: number;
64
+ /** Called once per degradation step, before the next draw. */
65
+ onQualityChange?: (quality: EffectQuality) => void;
66
+ }
67
+ export interface EffectGovernorOptions {
68
+ requestFrame?: (callback: (time: number) => void) => number;
69
+ cancelFrame?: (id: number) => void;
70
+ onSound?: (event: unknown, volume: number) => void;
71
+ watchVisibility?: boolean;
72
+ watchReducedMotion?: boolean;
73
+ document?: Pick<
74
+ Document,
75
+ 'hidden' | 'addEventListener' | 'removeEventListener'
76
+ >;
77
+ matchMedia?: (
78
+ query: string
79
+ ) => Pick<
80
+ MediaQueryList,
81
+ 'matches' | 'addEventListener' | 'removeEventListener'
82
+ >;
83
+ }
84
+ export interface EffectGovernor {
85
+ /** Boolean true follows reduced motion; 'on' explicitly overrides it. */
86
+ set(patch: EffectState): void;
87
+ add(
88
+ id: string,
89
+ draw: (
90
+ time: number,
91
+ state: EffectState,
92
+ elapsedSeconds: number,
93
+ quality: EffectQuality
94
+ ) => boolean | void,
95
+ options?: EffectOptions
96
+ ): () => void;
97
+ /** Returns a detached snapshot, or undefined for an unregistered effect. */
98
+ getQuality(id: string): EffectQuality | undefined;
99
+ /** Coalesced repaint with zero elapsed time, even while paused. */
100
+ invalidate(id: string): void;
101
+ sound(event: unknown): void;
102
+ dispose(): void;
103
+ }
104
+ export function createEffectGovernor(
105
+ options?: EffectGovernorOptions
106
+ ): EffectGovernor;
107
+
108
+ export interface RefractionOptions {
109
+ scale?: number;
110
+ detail?: number;
111
+ octaves?: number;
112
+ smoothing?: number;
113
+ seed?: number;
114
+ }
115
+ export interface RefractionProvider {
116
+ supported: boolean;
117
+ reference(strength: number, preferences?: ViewerPreferences): string;
118
+ dispose(): void;
119
+ }
120
+ export function createRefractionProvider(
121
+ document?: Document,
122
+ options?: RefractionOptions
123
+ ): RefractionProvider;
124
+ export function observeMaterialVisibility(
125
+ root: Document | Element,
126
+ selector: string,
127
+ options?: {
128
+ margin?: number | string;
129
+ /** Share with a surface's governor: (_, visible) => governor.set({ visible }). */
130
+ onVisibility?: (element: Element, visible: boolean) => void;
131
+ }
132
+ ): { dispose(): void };
133
+ export function opticalMaterialCss(selector?: string): string;
@@ -0,0 +1,139 @@
1
+ import { opticalRanges, validateMaterial, opticalStyle } from './optics.js';
2
+
3
+ import { parseNested } from '@link-foundation/ui-core';
4
+
5
+ function validateProperty(property, key, value, seen) {
6
+ if (
7
+ !key ||
8
+ !value ||
9
+ key.values.length ||
10
+ value.values.length ||
11
+ property.values.length !== 2 ||
12
+ !(
13
+ Object.hasOwn(opticalRanges, key.id) || ['color', 'tint'].includes(key.id)
14
+ ) ||
15
+ seen.has(key.id)
16
+ ) {
17
+ throw new TypeError('Unknown or duplicate material property');
18
+ }
19
+ }
20
+
21
+ /** Optical material parameters; child content never inherits surface opacity. */
22
+ export function readMaterial(text) {
23
+ const roots = parseNested(text);
24
+ const root = roots[0];
25
+ if (roots.length !== 1 || root.values[0]?.id !== 'material') {
26
+ throw new TypeError('Expected one material root');
27
+ }
28
+ const material = {
29
+ color: 'inherit',
30
+ opacity: 1,
31
+ blur: 0,
32
+ noise: 0,
33
+ refraction: 0,
34
+ };
35
+ const seen = new Set();
36
+ for (const property of root.values.slice(1)) {
37
+ const [key, value] = property.values;
38
+ validateProperty(property, key, value, seen);
39
+ seen.add(key.id);
40
+ if (['color', 'tint'].includes(key.id)) {
41
+ material[key.id] = value.id;
42
+ } else {
43
+ const number = Number(value.id);
44
+ material[key.id] = number;
45
+ }
46
+ }
47
+ validateMaterial(material);
48
+ return material;
49
+ }
50
+
51
+ const transparencyAllowed = (preferences) =>
52
+ ![false, 'off'].includes(preferences.transparency) &&
53
+ !(
54
+ (preferences.transparency ?? 'auto') === 'auto' &&
55
+ preferences.reducedTransparency
56
+ );
57
+
58
+ function materialFilter(material, preferences, transparent) {
59
+ const chain = [];
60
+ if (material.opacity > 0 && transparent) {
61
+ const reference = preferences.refractionProvider?.reference(
62
+ material.refraction,
63
+ preferences
64
+ );
65
+ if (reference) {
66
+ chain.push(reference);
67
+ }
68
+ if (material.blur) {
69
+ chain.push(`blur(${material.blur}px)`);
70
+ }
71
+ for (const [key, fn] of [
72
+ ['saturation', 'saturate'],
73
+ ['brightness', 'brightness'],
74
+ ['contrast', 'contrast'],
75
+ ]) {
76
+ if (material[key] !== undefined && material[key] !== 100) {
77
+ chain.push(`${fn}(${material[key]}%)`);
78
+ }
79
+ }
80
+ }
81
+ return chain.join(' ') || 'none';
82
+ }
83
+
84
+ /** Apply to a dedicated clipped layer, below content and frame strokes. */
85
+ export function materialStyle(material, preferences = {}) {
86
+ validateMaterial(material);
87
+ const transparent = transparencyAllowed(preferences);
88
+ const alpha = transparent ? material.opacity : 1;
89
+ const visible = material.opacity > 0;
90
+ const result = {
91
+ background:
92
+ material.color === 'inherit'
93
+ ? 'transparent'
94
+ : `color-mix(in srgb, ${material.color} ${visible ? alpha * 100 : 0}%, transparent)`,
95
+ 'backdrop-filter': materialFilter(material, preferences, transparent),
96
+ '--material-noise': String(transparent && visible ? material.noise : 0),
97
+ '--material-refraction': String(
98
+ transparent && visible ? material.refraction : 0
99
+ ),
100
+ };
101
+ const extended = Object.keys(material).some(
102
+ (key) => !['color', 'opacity', 'blur', 'noise', 'refraction'].includes(key)
103
+ );
104
+ if (extended || material.noise || !visible) {
105
+ Object.assign(
106
+ result,
107
+ opticalStyle(
108
+ { ...material, noise: transparent ? material.noise : 0 },
109
+ visible ? material.opacity : 0
110
+ )
111
+ );
112
+ }
113
+ return result;
114
+ }
115
+
116
+ /** A preset changes appearance; comfort is owned exclusively by the viewer. */
117
+ export function applyAppearancePreset(current, preset, keys) {
118
+ return {
119
+ ...globalThis.structuredClone(current),
120
+ ...globalThis.structuredClone(
121
+ keys
122
+ ? Object.fromEntries(
123
+ keys
124
+ .filter(
125
+ (key) => key !== 'preferences' && Object.hasOwn(preset, key)
126
+ )
127
+ .map((key) => [key, preset[key]])
128
+ )
129
+ : preset
130
+ ),
131
+ preferences: globalThis.structuredClone(current.preferences),
132
+ };
133
+ }
134
+
135
+ export { createEffectGovernor } from './effect-governor.js';
136
+
137
+ export { createRefractionProvider } from './refraction.js';
138
+ export { observeMaterialVisibility } from './material-visibility.js';
139
+ export { opticalMaterialCss } from './optical-css.js';
@@ -0,0 +1,47 @@
1
+ import { materialNoise } from './optics.js';
2
+
3
+ /** Static surface layer. Put content/frame in siblings, and clip their common host. */
4
+ export function opticalMaterialCss(selector = '[data-material]') {
5
+ const v = (key, fallback = 0) => `var(--material-${key}, ${fallback})`;
6
+ const alpha = v('opacity', 1);
7
+ const light = (key, factor = 1) =>
8
+ `rgb(255 255 255 / calc(${v(key)} * ${alpha} * ${factor}))`;
9
+ const tint = `color-mix(in srgb, ${v('tint', 'var(--color-surface)')} calc(${v('tint-level')} * ${alpha} * 100%), transparent)`;
10
+ const filter = `${v('refraction-filter', '')} blur(${v('blur', '0px')}) saturate(${v('saturation', '100%')}) brightness(${v('brightness', '100%')}) contrast(${v('contrast', '100%')})`;
11
+ return `${selector} {
12
+ --material-noise-image: ${materialNoise(1, 1)};
13
+ isolation: isolate;
14
+ background-color: var(--material-color, var(--color-surface));
15
+ border: ${v('border-width', '0px')} solid ${light('border-light')};
16
+ }
17
+ @media (prefers-reduced-transparency: no-preference) and (forced-colors: none) {
18
+ ${selector}:not([data-transparency="off"], [data-transparency="off"] *) {
19
+ background-color: color-mix(in srgb, var(--material-color, var(--color-surface)) calc(${alpha} * 100%), transparent);
20
+ backdrop-filter: ${filter};
21
+ }
22
+ }
23
+ ${selector}::before, ${selector}::after {
24
+ content: ""; position: absolute; inset: 0; pointer-events: none; border-radius: inherit;
25
+ }
26
+ ${selector}::before {
27
+ z-index: 1;
28
+ background-image: ${v('noise-image', 'none')};
29
+ opacity: calc(${v('noise')} * ${alpha});
30
+ }
31
+ ${selector}[data-material-noise]::before { background-image: var(--material-noise-image); }
32
+ ${selector}::after {
33
+ z-index: 0;
34
+ background-image: linear-gradient(${v('sheen-angle', '0deg')}, transparent, ${light('sheen')}, transparent), linear-gradient(${v('light-angle', '0deg')}, ${light('sheen', 0.35)}, transparent), linear-gradient(${tint}, ${tint});
35
+ box-shadow: inset 0 1px ${light('highlight')}, inset 1px 0 ${light('highlight', 0.35)}, inset -1px 0 ${light('highlight', 0.35)}, inset 0 -1px ${light('highlight', 0.25)}, inset 0 0 ${v('depth-blur', '0px')} ${light('depth')};
36
+ }
37
+ ${selector} { box-shadow: 0 ${v('shadow-distance', '0px')} ${v('shadow-blur', '0px')} ${v('shadow-spread', '0px')} rgb(0 0 0 / calc(${v('shadow')} * ${alpha})); }
38
+ ${selector}[data-material-opacity="0"] { background: transparent; backdrop-filter: none !important; box-shadow: none; }
39
+ ${selector}[data-material-opacity="0"]::before, ${selector}[data-material-opacity="0"]::after { content: none; }
40
+ @keyframes ui-material-sheen { from { background-position: -200% 0; } to { background-position: 200% 0; } }
41
+ @media (prefers-reduced-motion: no-preference) {
42
+ ${selector}[data-material-motion]:not([data-motion="off"], [data-motion="off"] *)::after { animation: ui-material-sheen 6s linear infinite; background-size: 200% 100%; }
43
+ ${selector}[data-material-motion]:hover:not([data-motion="off"], [data-motion="off"] *)::after { animation-duration: 1s; }
44
+ }
45
+ ${selector}[data-material-motion]:is([data-motion="on"], [data-motion="on"] *):not([data-motion="off"], [data-motion="off"] *)::after { animation: ui-material-sheen 6s linear infinite; background-size: 200% 100%; }
46
+ `;
47
+ }
@@ -0,0 +1,107 @@
1
+ import { values } from '@link-foundation/ui-core';
2
+
3
+ export const opticalRanges = Object.freeze({
4
+ opacity: [0, 1, 1],
5
+ blur: [0, 48, 0],
6
+ noise: [0, 1, 0],
7
+ refraction: [0, 1, 0],
8
+ saturation: [100, 300, 100],
9
+ brightness: [70, 150, 100],
10
+ contrast: [50, 200, 100],
11
+ 'tint-level': [0, 1, 0],
12
+ highlight: [0, 1, 0],
13
+ depth: [0, 1, 0],
14
+ 'depth-blur': [0, 80, 0],
15
+ shadow: [0, 1, 0],
16
+ 'shadow-blur': [0, 100, 0],
17
+ 'shadow-distance': [0, 60, 0],
18
+ 'shadow-spread': [-20, 30, 0],
19
+ sheen: [0, 0.8, 0],
20
+ 'sheen-angle': [0, 360, 0],
21
+ 'light-angle': [0, 360, 0],
22
+ 'border-width': [0, 8, 0],
23
+ 'border-light': [0, 1, 0],
24
+ });
25
+ export function validateMaterial(material) {
26
+ for (const [key, [min, max]] of Object.entries(opticalRanges)) {
27
+ const n = material[key];
28
+ if (n !== undefined && (!Number.isFinite(n) || n < min || n > max)) {
29
+ throw new RangeError(`Invalid material ${key}`);
30
+ }
31
+ }
32
+ for (const [key, neutral] of [['tint', 'theme']]) {
33
+ if (
34
+ material[key] !== undefined &&
35
+ material[key] !== neutral &&
36
+ !values.parseColor(material[key])
37
+ ) {
38
+ throw new TypeError(`Invalid material ${key}`);
39
+ }
40
+ }
41
+ }
42
+
43
+ /** Pure, bounded generation: browsers reuse the identical URI until either input changes. */
44
+ export function materialNoise(noise, opacity) {
45
+ if (!noise || !opacity) {
46
+ return 'none';
47
+ }
48
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"><filter id="g" x="0" y="0" width="100%" height="100%"><feTurbulence type="fractalNoise" baseFrequency=".75" numOctaves="3" seed="17" stitchTiles="stitch"/><feColorMatrix type="saturate" values="0"/><feComponentTransfer><feFuncA type="linear" slope="0" intercept="${noise * opacity}"/></feComponentTransfer></filter><path d="M0 0h128v128H0z" filter="url(#g)"/></svg>`;
49
+ return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
50
+ }
51
+
52
+ function opticalShadows(m, alpha) {
53
+ const light = (n) => `rgb(255 255 255 / ${n * alpha})`;
54
+ const shadows = [];
55
+ if (m.highlight) {
56
+ shadows.push(
57
+ `inset 0 1px ${light(m.highlight)}`,
58
+ `inset 1px 0 ${light(m.highlight * 0.35)}`,
59
+ `inset -1px 0 ${light(m.highlight * 0.35)}`,
60
+ `inset 0 -1px ${light(m.highlight * 0.25)}`
61
+ );
62
+ }
63
+ if (m.depth) {
64
+ shadows.push(`inset 0 0 ${m['depth-blur'] ?? 0}px ${light(m.depth)}`);
65
+ }
66
+ if (m.shadow) {
67
+ shadows.push(
68
+ `0 ${m['shadow-distance'] ?? 0}px ${m['shadow-blur'] ?? 0}px ${m['shadow-spread'] ?? 0}px rgb(0 0 0 / ${m.shadow * alpha})`
69
+ );
70
+ }
71
+ return shadows;
72
+ }
73
+
74
+ function opticalImages(m, alpha) {
75
+ const light = (n) => `rgb(255 255 255 / ${n * alpha})`;
76
+ const images = [];
77
+ if (m.noise && alpha) {
78
+ images.push(materialNoise(m.noise, alpha));
79
+ }
80
+ if (m.sheen && alpha) {
81
+ images.push(
82
+ `linear-gradient(${m['sheen-angle'] ?? 0}deg, transparent, ${light(m.sheen)}, transparent)`,
83
+ `linear-gradient(${m['light-angle'] ?? 0}deg, ${light(m.sheen * 0.35)}, transparent)`
84
+ );
85
+ }
86
+ if (m['tint-level'] && alpha) {
87
+ const tint =
88
+ !m.tint || m.tint === 'theme'
89
+ ? 'var(--color-surface, currentColor)'
90
+ : m.tint;
91
+ const color = `color-mix(in srgb, ${tint} ${m['tint-level'] * alpha * 100}%, transparent)`;
92
+ images.push(`linear-gradient(${color}, ${color})`);
93
+ }
94
+ return images;
95
+ }
96
+
97
+ export function opticalStyle(m, alpha) {
98
+ const shadows = opticalShadows(m, alpha);
99
+ const images = opticalImages(m, alpha);
100
+ return {
101
+ 'background-image': images.join(', ') || 'none',
102
+ 'box-shadow': alpha ? shadows.join(', ') || 'none' : 'none',
103
+ 'border-width': `${m['border-width'] ?? 0}px`,
104
+ 'border-style': 'solid',
105
+ 'border-color': `rgb(255 255 255 / ${(m['border-light'] ?? 0) * alpha})`,
106
+ };
107
+ }
@@ -0,0 +1,145 @@
1
+ const NS = 'http://www.w3.org/2000/svg';
2
+ const providerKey = '__linkUiMaterialRefraction';
3
+ const ranges = Object.freeze({
4
+ scale: [0, 150, 40],
5
+ detail: [2, 60, 12],
6
+ octaves: [1, 4, 2],
7
+ smoothing: [0, 10, 2],
8
+ seed: [0, 100, 13],
9
+ });
10
+
11
+ function parameters(options) {
12
+ return Object.fromEntries(
13
+ Object.entries(ranges).map(([key, [min, max, fallback]]) => {
14
+ const value = options[key] ?? fallback;
15
+ if (
16
+ !Number.isFinite(value) ||
17
+ value < min ||
18
+ value > max ||
19
+ (['octaves', 'seed'].includes(key) && !Number.isInteger(value))
20
+ ) {
21
+ throw new RangeError(`Invalid refraction ${key}`);
22
+ }
23
+ return [key, value];
24
+ })
25
+ );
26
+ }
27
+
28
+ function transparencyOff(preferences, media) {
29
+ return (
30
+ [false, 'off'].includes(preferences.transparency) ||
31
+ preferences.reducedTransparency ||
32
+ ((preferences.transparency ?? 'auto') === 'auto' && media?.matches)
33
+ );
34
+ }
35
+
36
+ /** Chromium is deliberately allowlisted: CSS.supports accepts unusable SVG URLs in other engines. */
37
+ export function createRefractionProvider(
38
+ document = globalThis.document,
39
+ options = {}
40
+ ) {
41
+ const params = parameters(options);
42
+ if (document[providerKey]) {
43
+ return document[providerKey];
44
+ }
45
+ const view = document.defaultView;
46
+ const ua = view?.navigator.userAgent ?? '';
47
+ const supported =
48
+ /(?:Chrome|Chromium)\//u.test(ua) &&
49
+ !/Firefox\//u.test(ua) &&
50
+ Boolean(view?.CSS?.supports('backdrop-filter', 'blur(1px)'));
51
+ const svg = document.createElementNS(NS, 'svg');
52
+ svg.setAttribute('aria-hidden', 'true');
53
+ svg.setAttribute('width', '0');
54
+ svg.setAttribute('height', '0');
55
+ svg.style.cssText = 'position:absolute;pointer-events:none;overflow:hidden';
56
+ const make = (name, attrs, parent = svg) => {
57
+ const node = document.createElementNS(NS, name);
58
+ for (const [key, value] of Object.entries(attrs)) {
59
+ node.setAttribute(key, String(value));
60
+ }
61
+ parent.append(node);
62
+ return node;
63
+ };
64
+ // IDs cannot collide with a host's existing artwork.
65
+ let id = 'ui-material-refraction';
66
+ while (document.getElementById(id)) {
67
+ id += '-filter';
68
+ }
69
+ const filter = make('filter', {
70
+ id,
71
+ x: '-50%',
72
+ y: '-50%',
73
+ width: '200%',
74
+ height: '200%',
75
+ 'color-interpolation-filters': 'sRGB',
76
+ });
77
+ make(
78
+ 'feTurbulence',
79
+ {
80
+ type: 'fractalNoise',
81
+ baseFrequency: params.detail / 1000,
82
+ numOctaves: params.octaves,
83
+ seed: params.seed,
84
+ result: 'noise',
85
+ },
86
+ filter
87
+ );
88
+ make(
89
+ 'feGaussianBlur',
90
+ { in: 'noise', stdDeviation: params.smoothing, result: 'smooth' },
91
+ filter
92
+ );
93
+ make(
94
+ 'feDisplacementMap',
95
+ {
96
+ in: 'SourceGraphic',
97
+ in2: 'smooth',
98
+ scale: 0,
99
+ xChannelSelector: 'R',
100
+ yChannelSelector: 'G',
101
+ },
102
+ filter
103
+ );
104
+ (document.body ?? document.documentElement).append(svg);
105
+ const media = view?.matchMedia?.('(prefers-reduced-transparency: reduce)');
106
+ const references = new Map();
107
+ let disposed = false;
108
+ const provider = {
109
+ supported,
110
+ reference(strength, preferences = {}) {
111
+ if (!Number.isFinite(strength) || strength < 0 || strength > 1) {
112
+ throw new RangeError('Invalid refraction strength');
113
+ }
114
+ if (
115
+ disposed ||
116
+ !supported ||
117
+ transparencyOff(preferences, media) ||
118
+ !strength
119
+ ) {
120
+ return '';
121
+ }
122
+ if (!references.has(strength)) {
123
+ const node = references.size ? filter.cloneNode(true) : filter;
124
+ const filterId = references.size ? `${id}-${references.size}` : id;
125
+ node.setAttribute('id', filterId);
126
+ node
127
+ .querySelector('feDisplacementMap')
128
+ .setAttribute('scale', String(params.scale * strength));
129
+ if (node !== filter) {
130
+ svg.append(node);
131
+ }
132
+ references.set(strength, `url("#${filterId}")`);
133
+ }
134
+ return references.get(strength);
135
+ },
136
+ dispose() {
137
+ disposed = true;
138
+ references.clear();
139
+ svg.remove();
140
+ delete document[providerKey];
141
+ },
142
+ };
143
+ document[providerKey] = provider;
144
+ return provider;
145
+ }