GlassEffect
A piece of Liquid Glass on a fixed frame, as SwiftUI's .glassEffect(_:in:): regular, clear or identity, tinted, interactive under a finger, in a capsule, circle or rounded rectangle. It is a div, so it takes children, handlers and aria props.
import { GlassEffect } from "@adamexu/glass";Variants
regular is the default material. clear is more transparent, for glass over media. A tint colours either one. identity draws no glass at all, only the content, as SwiftUI's .identity does to turn the effect off.
<GlassEffect width={150} height={56}>Regular</GlassEffect>
<GlassEffect glass={{ variant: "clear" }} width={150} height={56}>Clear</GlassEffect>
<GlassEffect glass={{ tint: "#0a84ff" }} width={150} height={56}>Tinted</GlassEffect>
<GlassEffect glass={{ variant: "identity" }} width={150} height={56}>Identity</GlassEffect>Shapes and press
The default shape is a capsule. rect takes a cornerRadius, with continuous corners unless its style is circular. interactive glass swells, leans after the finger and lights up where it is pressed: press these.
<GlassEffect glass={{ interactive: true }} width={200} height={56}>Capsule</GlassEffect>
<GlassEffect glass={{ interactive: true }} shape={{ type: "circle" }} width={64} height={64}>
<Heart size={24} strokeWidth={2} />
</GlassEffect>
<GlassEffect glass={{ interactive: true }} shape={{ type: "rect", cornerRadius: 20 }} width={140} height={96}>
Continuous
</GlassEffect>
<GlassEffect glass={{ interactive: true }} shape={{ type: "rect", cornerRadius: 20, style: "circular" }} width={140} height={96}>
Circular
</GlassEffect>Size
width and height are required, in CSS px: the frame SwiftUI would lay the glass out in. The glass does not measure its content, so size it as you would with .frame(width:height:). Content is centred, in the label colour of the scheme (black in light, white in dark). Regular glass whose shorter side is 80 or more casts a soft shadow.
<GlassEffect shape={{ type: "rect", cornerRadius: 36 }} width={260} height={180}>
Liquid Glass
</GlassEffect>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| width | number | - | Required. The frame's width. |
| height | number | - | Required. The frame's height. |
| glass | { variant?: "regular" | "clear" | "identity"; tint?: string; interactive?: boolean } | {} | SwiftUI's Glass: the variant (regular by default), .tint(_:) with any CSS colour, and .interactive(). |
| shape | { type: "capsule"; style?: CornerStyle } | { type: "circle" } | { type: "rect"; cornerRadius: number; style?: CornerStyle } | { type: "capsule" } | The shape of .glassEffect(_:in:). CornerStyle is "continuous" (the default) or "circular". |
| id | string | - | Inside a GlassEffectContainer: .glassEffectID, matching this glass across changes for morphs. |
| union | string | - | Inside a GlassEffectContainer: .glassEffectUnion, glass of one union id drawn as one shape over their frames. |
| transition | "matchedGeometry" | "materialize" | "identity" | "matchedGeometry" | Inside a GlassEffectContainer: .glassEffectTransition, how it appears and leaves. |
| standalone | boolean | false | Draw as glass of its own, even inside a GlassEffectContainer or a GlassGroup. |
| margin | number | - | How far the filter layer reaches past the shape on every side. By default 40, cut back to the viewport. A fixed value suits glass that moves or scales every frame: its maps are not rebuilt as it does. |
| scales | boolean | false | For glass drawn scaled at times: its maps stay whole images, which scale smoothly. Implied by a fixed margin. |
| children | ReactNode | - | The content, centred on the glass. |
| className | string | - | On the glass element. |
| style | CSSProperties | - | On the glass element, for placement. |
| ...rest | HTMLAttributes<HTMLDivElement> | - | Handlers, role, aria-*, tabIndex. |
Fading and clipping
Glass reads the page behind it. An ancestor with opacity below 1, a filter, mask-image, clip-path, backdrop-filter or a mix-blend-mode cuts it off from that page. To fade the material, set --glass-alpha (0 to 1) on the glass itself: it fades its filter layers without cutting them off. Its content keeps its own opacity.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| .glassEffect() | <GlassEffect width={w} height={h}> |
| .frame(width: w, height: h) | width={w} height={h} |
| .glassEffect(.clear.tint(c).interactive(), in: .circle) | <GlassEffect glass={{ variant: "clear", tint: c, interactive: true }} shape={{ type: "circle" }}> |
| .glassEffect(in: .rect(cornerRadius: 16)) | <GlassEffect shape={{ type: "rect", cornerRadius: 16 }}> |
| .rect(cornerRadius: 16, style: .circular) | shape={{ type: "rect", cornerRadius: 16, style: "circular" }} |
| .glassEffect(.identity) | <GlassEffect glass={{ variant: "identity" }}> |