@gracefullight/liquid-glass
Version:
Liquid glass UI components for React
131 lines (129 loc) • 3.8 kB
JavaScript
"use client";
// src/liquid-glass-provider.tsx
import { createContext, useContext } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var defaultConfig = {
radius: "12px",
shadow: "0px 6px 24px rgba(0,0,0,0.2)",
shadowOffset: "0px",
shadowBlur: "20px",
shadowSpread: "-5px",
shadowColor: "rgba(255,255,255,0.7)",
tintColor: "255,255,255",
tintOpacity: 0.04,
frostBlur: "2px",
noiseFrequency: 2e-3,
distortionStrength: 10,
filterSeed: 92,
filterNumOctaves: 2
};
var LiquidGlassContext = createContext(defaultConfig);
var LiquidGlassProvider = ({
value,
children
}) => {
const merged = { ...defaultConfig, ...value };
return /* @__PURE__ */ jsxs(LiquidGlassContext.Provider, { value: merged, children: [
children,
/* @__PURE__ */ jsxs(
"svg",
{
width: "0",
height: "0",
"aria-hidden": "true",
focusable: "false",
style: { position: "absolute" },
children: [
/* @__PURE__ */ jsx("title", { children: "Liquid Glass SVG Filters" }),
/* @__PURE__ */ jsxs(
"filter",
{
id: "liquid-glass-distortion",
x: "0",
y: "0",
width: "100%",
height: "100%",
children: [
/* @__PURE__ */ jsx(
"feTurbulence",
{
type: "fractalNoise",
baseFrequency: `${merged.noiseFrequency} ${merged.noiseFrequency}`,
numOctaves: merged.filterNumOctaves,
seed: merged.filterSeed,
result: "noise"
}
),
/* @__PURE__ */ jsx("feGaussianBlur", { in: "noise", stdDeviation: "2", result: "blurred" }),
/* @__PURE__ */ jsx(
"feDisplacementMap",
{
in2: "blurred",
in: "SourceGraphic",
scale: merged.distortionStrength,
xChannelSelector: "R",
yChannelSelector: "G"
}
)
]
}
)
]
}
)
] });
};
var useLiquidGlass = () => useContext(LiquidGlassContext);
// src/liquid-glass-filters.tsx
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var LiquidGlassFilters = ({
innerShadowZIndex = 0,
backdropFilterZIndex = -1
}) => {
const ctx = useLiquidGlass();
return /* @__PURE__ */ jsxs2(Fragment, { children: [
/* @__PURE__ */ jsx2(
"span",
{
"aria-hidden": "true",
style: {
position: "absolute",
inset: 0,
width: "100%",
height: "100%",
zIndex: innerShadowZIndex,
borderRadius: "inherit",
pointerEvents: "none",
boxShadow: `inset ${ctx.shadowOffset} ${ctx.shadowOffset} ${ctx.shadowBlur} ${ctx.shadowSpread} ${ctx.shadowColor}`,
backgroundColor: `rgba(${ctx.tintColor}, ${ctx.tintOpacity})`
}
}
),
/* @__PURE__ */ jsx2(
"span",
{
"aria-hidden": "true",
style: {
position: "absolute",
inset: 0,
width: "100%",
height: "100%",
zIndex: backdropFilterZIndex,
borderRadius: "inherit",
pointerEvents: "none",
isolation: "isolate",
backdropFilter: `blur(${ctx.frostBlur})`,
WebkitBackdropFilter: `blur(${ctx.frostBlur})`,
filter: "url(#liquid-glass-distortion)",
WebkitFilter: "url(#liquid-glass-distortion)"
}
}
)
] });
};
export {
LiquidGlassFilters,
LiquidGlassProvider,
useLiquidGlass
};
//# sourceMappingURL=index.mjs.map