@tahoangphuc111/liquid-glass 0.1.1 → 0.3.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 (41) hide show
  1. package/README.md +273 -1
  2. package/dist/chunk-FB6QOH33.js +7532 -0
  3. package/dist/chunk-FB6QOH33.js.map +1 -0
  4. package/dist/chunk-KKMJIY4M.cjs +360 -0
  5. package/dist/chunk-KKMJIY4M.cjs.map +1 -0
  6. package/dist/chunk-RMWCG5MB.js +354 -0
  7. package/dist/chunk-RMWCG5MB.js.map +1 -0
  8. package/dist/chunk-ZECDS4SL.cjs +7578 -0
  9. package/dist/chunk-ZECDS4SL.cjs.map +1 -0
  10. package/dist/index.cjs +281 -146
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +72 -7
  13. package/dist/index.d.ts +72 -7
  14. package/dist/index.js +223 -100
  15. package/dist/index.js.map +1 -1
  16. package/dist/next/index.cjs +206 -0
  17. package/dist/next/index.cjs.map +1 -0
  18. package/dist/next/index.d.cts +26 -0
  19. package/dist/next/index.d.ts +26 -0
  20. package/dist/next/index.js +184 -0
  21. package/dist/next/index.js.map +1 -0
  22. package/dist/react/index.cjs +115 -206
  23. package/dist/react/index.cjs.map +1 -1
  24. package/dist/react/index.d.cts +32 -3
  25. package/dist/react/index.d.ts +32 -3
  26. package/dist/react/index.js +98 -205
  27. package/dist/react/index.js.map +1 -1
  28. package/dist/styles.css +233 -51
  29. package/dist/{types-BYB6mjtX.d.cts → types-BqXPylfU.d.cts} +18 -1
  30. package/dist/{types-BYB6mjtX.d.ts → types-BqXPylfU.d.ts} +18 -1
  31. package/dist/vue/index.cjs +312 -7
  32. package/dist/vue/index.cjs.map +1 -1
  33. package/dist/vue/index.d.cts +314 -2
  34. package/dist/vue/index.d.ts +314 -2
  35. package/dist/vue/index.js +305 -2
  36. package/dist/vue/index.js.map +1 -1
  37. package/package.json +16 -2
  38. package/dist/chunk-HL64SDJN.js +0 -3831
  39. package/dist/chunk-HL64SDJN.js.map +0 -1
  40. package/dist/chunk-I5BLFZPZ.cjs +0 -3873
  41. package/dist/chunk-I5BLFZPZ.cjs.map +0 -1
package/README.md CHANGED
@@ -1 +1,273 @@
1
- liquid glass optical refraction for vanilla js, react, and vue using wasm svg filters on desktop chromium and webgl scene capture on mobile and safari
1
+ # liquid-glass
2
+
3
+ Optical refraction and glass displacement for Vanilla JS, React, Next.js, and Vue 3.
4
+
5
+ Uses WebAssembly (WASM SIMD) displacement maps on Chromium desktop, and falls back to WebGL scene capture or CSS backdrop filters on mobile and Safari.
6
+
7
+ ![liquid-glass interactive playground demo](./assets/demo.gif)
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @tahoangphuc111/liquid-glass
13
+ ```
14
+
15
+ Import base styles once in your root stylesheet or entry file:
16
+
17
+ ```ts
18
+ import "@tahoangphuc111/liquid-glass/styles.css";
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ### Vanilla JavaScript
24
+
25
+ ```ts
26
+ import { createLiquidGlass, glassEffect } from "@tahoangphuc111/liquid-glass";
27
+
28
+ const element = document.querySelector<HTMLElement>(".glass-card")!;
29
+
30
+ const glass = createLiquidGlass(element, {
31
+ surface: "convex_squircle",
32
+ thickness: 48,
33
+ bezel: 14,
34
+ ior: 2.15,
35
+ blur: 2,
36
+ interactive: true,
37
+ draggable: "elastic",
38
+ });
39
+
40
+ // Quick helper on elements, selectors, range sliders, or switch bars
41
+ glassEffect(".glass-button", { pill: true, draggable: true });
42
+
43
+ // Update options at runtime
44
+ glass.update({ thickness: 56 });
45
+
46
+ // Clean up when removing the element
47
+ glass.destroy();
48
+ ```
49
+
50
+ #### Bottom Navigation Switch (`createGlassSwitch`)
51
+
52
+ ```ts
53
+ import { createGlassSwitch } from "@tahoangphuc111/liquid-glass";
54
+
55
+ const nav = document.querySelector<HTMLElement>("#bottom-nav")!;
56
+
57
+ const navSwitch = createGlassSwitch(nav, {
58
+ defaultValue: "home",
59
+ onChange(value, index) {
60
+ console.log("Selected tab:", value, index);
61
+ },
62
+ });
63
+
64
+ // Or activate directly via glassEffect on .lg-switch / [role="tablist"]
65
+ nav.glassEffect({ defaultValue: "home" });
66
+ ```
67
+
68
+ ### React
69
+
70
+ Wrap components with `<LiquidGlass>` or use `<LiquidGlassSwitch>`, `useLiquidGlass`, and `useGlassSwitch`:
71
+
72
+ ```tsx
73
+ import { useState } from "react";
74
+ import { LiquidGlass, LiquidGlassSwitch } from "@tahoangphuc111/liquid-glass/react";
75
+
76
+ export function AppShell() {
77
+ const [tab, setTab] = useState("home");
78
+
79
+ return (
80
+ <>
81
+ <LiquidGlass
82
+ surface="convex_squircle"
83
+ interactive
84
+ draggable="elastic"
85
+ className="p-6 rounded-2xl"
86
+ >
87
+ <h2>Refracted Card</h2>
88
+ <p>Content rendered inside liquid glass container.</p>
89
+ </LiquidGlass>
90
+
91
+ <LiquidGlassSwitch
92
+ value={tab}
93
+ onChange={(nextTab) => setTab(nextTab)}
94
+ items={[
95
+ { value: "home", label: "Home" },
96
+ { value: "search", label: "Search" },
97
+ { value: "library", label: "Library" },
98
+ ]}
99
+ />
100
+ </>
101
+ );
102
+ }
103
+ ```
104
+
105
+ Or using the hooks directly (`useLiquidGlass` / `useGlassSwitch`):
106
+
107
+ ```tsx
108
+ import { useRef } from "react";
109
+ import { useLiquidGlass } from "@tahoangphuc111/liquid-glass/react";
110
+
111
+ export function CustomCard() {
112
+ const ref = useRef<HTMLDivElement>(null);
113
+
114
+ useLiquidGlass(ref, {
115
+ surface: "convex_circle",
116
+ pill: true,
117
+ interactive: true,
118
+ });
119
+
120
+ return <div ref={ref} className="p-4">Pill button</div>;
121
+ }
122
+ ```
123
+
124
+ ### Next.js (App Router & Pages Router)
125
+
126
+ Import from `@tahoangphuc111/liquid-glass/next` (pre-marked with `"use client";` for Next.js App Router) and import base styles in `app/layout.tsx`:
127
+
128
+ ```tsx
129
+ import "@tahoangphuc111/liquid-glass/styles.css";
130
+ ```
131
+
132
+ Inside a Client Component (with `next/link` and `usePathname()`):
133
+
134
+ ```tsx
135
+ "use client";
136
+
137
+ import Link from "next/link";
138
+ import { usePathname, useRouter } from "next/navigation";
139
+ import {
140
+ LiquidGlassProvider,
141
+ LiquidGlassSwitch,
142
+ } from "@tahoangphuc111/liquid-glass/next";
143
+
144
+ export function BottomNavShell({ children }: { children: React.ReactNode }) {
145
+ const pathname = usePathname();
146
+ const router = useRouter();
147
+
148
+ return (
149
+ <LiquidGlassProvider>
150
+ {children}
151
+ <LiquidGlassSwitch
152
+ pathname={pathname}
153
+ linkComponent={Link}
154
+ onChange={(_, __, itemElement) => {
155
+ const href = itemElement.getAttribute("href");
156
+ if (href) router.push(href);
157
+ }}
158
+ items={[
159
+ { value: "home", href: "/", label: "Home", exact: true },
160
+ { value: "search", href: "/search", label: "Search" },
161
+ { value: "library", href: "/library", label: "Library" },
162
+ ]}
163
+ />
164
+ </LiquidGlassProvider>
165
+ );
166
+ }
167
+ ```
168
+
169
+ ### Vue 3
170
+
171
+ Register as a global plugin (`LiquidGlass`, `LiquidGlassSwitch`, `LiquidGlassProvider`) or import directly:
172
+
173
+ ```vue
174
+ <script setup lang="ts">
175
+ import { ref } from "vue";
176
+ import { LiquidGlass, LiquidGlassSwitch } from "@tahoangphuc111/liquid-glass/vue";
177
+
178
+ const activeTab = ref("home");
179
+ </script>
180
+
181
+ <template>
182
+ <LiquidGlass
183
+ surface="convex_squircle"
184
+ :interactive="true"
185
+ draggable="elastic"
186
+ class="glass-panel"
187
+ >
188
+ <span>Vue Liquid Glass Panel</span>
189
+ </LiquidGlass>
190
+
191
+ <LiquidGlassSwitch
192
+ v-model="activeTab"
193
+ :items="[
194
+ { value: 'home', label: 'Home' },
195
+ { value: 'search', label: 'Search' },
196
+ { value: 'library', label: 'Library' },
197
+ ]"
198
+ />
199
+ </template>
200
+ ```
201
+
202
+ Or with the composable:
203
+
204
+ ```vue
205
+ <script setup lang="ts">
206
+ import { ref } from "vue";
207
+ import { useLiquidGlass } from "@tahoangphuc111/liquid-glass/vue";
208
+
209
+ const panelRef = ref<HTMLElement | null>(null);
210
+
211
+ useLiquidGlass(panelRef, {
212
+ surface: "convex_squircle",
213
+ thickness: 35,
214
+ });
215
+ </script>
216
+
217
+ <template>
218
+ <div ref="panelRef" class="glass-box">Content</div>
219
+ </template>
220
+ ```
221
+
222
+ ## How It Works
223
+
224
+ 1. **Chromium Desktop (`wasm-svg`)**: Calculates refraction displacement using a WebAssembly SIMD kernel and outputs dynamic displacement maps directly into SVG `feDisplacementMap` filters.
225
+ 2. **Mobile and Safari (`webgl`)**: When full SVG refraction is unsupported, samples background DOM elements onto a canvas or offscreen buffer and applies real-time GLSL fragment shaders.
226
+ 3. **Low-power or fallback (`css`)**: Automatically steps down to CSS `backdrop-filter` if the GPU drops frames or battery-saving mode is active.
227
+
228
+ ## Configuration Options
229
+
230
+ | Option | Type | Default | Description |
231
+ | :--- | :--- | :--- | :--- |
232
+ | `surface` | `"convex_squircle" \| "convex_circle" \| "linear" \| "lip"` | `"convex_squircle"` | Geometric refraction profile. |
233
+ | `engine` | `"auto" \| "wasm-svg" \| "webgl" \| "css"` | `"auto"` | Execution engine. `auto` picks the best supported engine. |
234
+ | `thickness` | `number` | `48` | Virtual lens thickness in pixels. |
235
+ | `bezel` | `number` | `14` | Width of the refraction boundary edge in pixels. |
236
+ | `scaleRatio` | `number` | `2.35` | Refraction displacement scale factor. |
237
+ | `ior` | `number` | `2.15` | Index of refraction. 1.0 is air, 1.33 is water, 1.5+ is glass. |
238
+ | `blur` | `number` | `2` | Gaussian blur radius applied behind the displacement filter. |
239
+ | `radius` | `number` | `18` | Corner radius override in pixels. Defaults to element border radius. |
240
+ | `pill` | `boolean` | `false` | Locks corners to full pill or circular shape. |
241
+ | `hollow` | `boolean` | `false` | Keeps inner area flat and limits refraction only to bezel edges. |
242
+ | `interactive` | `boolean` | `false` | Enables spring physics on cursor hover and click. |
243
+ | `draggable` | `boolean \| "elastic"` | `false` | Enables micro-drag or droplet elastic drag motion on interactive elements. |
244
+ | `coalesce` | `boolean` | `true` | Enables nearby liquid glass coalescence in WebGL mode. |
245
+ | `chromaticAberration` | `boolean \| number` | `false` | Enables RGB dispersion on WebGL and WASM SVG displacement engines. |
246
+ | `highlight` | `number` | `0.44` | Specular rim highlight intensity. |
247
+ | `tintColor` | `string` | `undefined` | Overlay tint color in hex or rgb format. |
248
+ | `tintOpacity` | `number` | `0.12` (WebGL) | Opacity of the tint layer (0 to 1). |
249
+ | `sceneElement` | `HTMLElement` | `null` | Target DOM element to sample for WebGL refraction mode. |
250
+ | `onEngineChange` | `(engine: GlassEngine) => void` | `undefined` | Callback fired when engine resolves or falls back. |
251
+
252
+ ## Development
253
+
254
+ ```bash
255
+ # Install dependencies
256
+ npm install
257
+
258
+ # Start local interactive playground
259
+ npm run dev
260
+
261
+ # Run unit tests
262
+ npm test
263
+
264
+ # Rebuild the Rust WASM SIMD kernel and update src/core/wasm.ts (requires rustup + wasm32-unknown-unknown)
265
+ npm run build:wasm
266
+
267
+ # Build package
268
+ npm run build
269
+ ```
270
+
271
+ ## License
272
+
273
+ MIT